diff --git a/CHANGELOG.md b/CHANGELOG.md index d4c907d2..a6e0dd8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed + +- Json parsing with invalid chars like `\u0000` preventing some channels from loading + ## [0.44.2] - 2025-11-13 ### Fixed diff --git a/config/bsconfig.base.jsonc b/config/bsconfig.base.jsonc index 449cde76..92824b39 100644 --- a/config/bsconfig.base.jsonc +++ b/config/bsconfig.base.jsonc @@ -58,6 +58,12 @@ "regex": "\\bTr\\s*\\(\\s*\"[^\\\"]*\"\\s*\\)", "regexFlags": "ig", "message": "Tr(\"String\") is not allowed. Use Tr(Locale.Key) instead." + }, + { + "code": "NO_PARSE_JSON", + "regex": "\\s*ParseJson\\(", + "regexFlags": "ig", + "message": "ParseJson is not allowed. Use StringUtils.ParseJsonSafe() instead." } ], "files": [ diff --git a/playlet-app/src/components/BootstrapScene/LoadingMessages.bs b/playlet-app/src/components/BootstrapScene/LoadingMessages.bs index 18388ac4..588e4b5f 100644 --- a/playlet-app/src/components/BootstrapScene/LoadingMessages.bs +++ b/playlet-app/src/components/BootstrapScene/LoadingMessages.bs @@ -37,6 +37,7 @@ function GetLoadingMessagesFromCache() as object if messagesFile = "" return invalid end if + ' bs:disable-next-line NO_PARSE_JSON return ParseJson(messagesFile) end function diff --git a/playlet-app/src/source/Registry.bs b/playlet-app/src/source/Registry.bs index 6d622bf2..a3c4a886 100644 --- a/playlet-app/src/source/Registry.bs +++ b/playlet-app/src/source/Registry.bs @@ -28,6 +28,7 @@ namespace Registry if urlsJson = invalid return invalid end if + ' bs:disable-next-line NO_PARSE_JSON urls = ParseJson(urlsJson) return urls end function diff --git a/playlet-lib/src/components/Dialog/DialogUtils.bs b/playlet-lib/src/components/Dialog/DialogUtils.bs index ed9af62a..a7f5373b 100644 --- a/playlet-lib/src/components/Dialog/DialogUtils.bs +++ b/playlet-lib/src/components/Dialog/DialogUtils.bs @@ -125,7 +125,7 @@ namespace DialogUtils if hasMessageId messages = RegistryUtils.Read(RegistryUtils.MESSAGES) if not StringUtils.IsNullOrEmpty(messages) - messages = ParseJson(messages) + messages = StringUtils.ParseJsonSafe(messages) if IsAssociativeArray(messages) savedMessageId = ValidString(messages["messageId"]) if savedMessageId = messageId diff --git a/playlet-lib/src/components/Screens/HomeScreen/HomeScreenUtils.bs b/playlet-lib/src/components/Screens/HomeScreen/HomeScreenUtils.bs index 11160540..8ad62d2c 100644 --- a/playlet-lib/src/components/Screens/HomeScreen/HomeScreenUtils.bs +++ b/playlet-lib/src/components/Screens/HomeScreen/HomeScreenUtils.bs @@ -1,9 +1,10 @@ +import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/Types.bs" namespace HomeScreenUtils function GetFeed(feedFileName as string, preferences as object) as object - feed = ParseJson(ReadAsciiFile(feedFileName)) + feed = StringUtils.ParseJsonSafe(ReadAsciiFile(feedFileName)) for each item in feed item.title = Tr(item.title) diff --git a/playlet-lib/src/components/Screens/SettingsScreen/HomeScreenEditor/HomeScreenEditor.bs b/playlet-lib/src/components/Screens/SettingsScreen/HomeScreenEditor/HomeScreenEditor.bs index d6f7d079..6616e8fd 100644 --- a/playlet-lib/src/components/Screens/SettingsScreen/HomeScreenEditor/HomeScreenEditor.bs +++ b/playlet-lib/src/components/Screens/SettingsScreen/HomeScreenEditor/HomeScreenEditor.bs @@ -3,11 +3,12 @@ import "pkg:/components/parts/AutoBind/OnNodeReadyNoOp.bs" import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/MathUtils.bs" import "pkg:/source/utils/RemoteKeys.bs" +import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/Types.bs" function Init() m.homeLayout = {} - homeLayout = ParseJson(ReadAsciiFile("libpkg:/config/default_home_layout.yaml")) + homeLayout = StringUtils.ParseJsonSafe(ReadAsciiFile("libpkg:/config/default_home_layout.yaml")) for each item in homeLayout m.homeLayout[item.id] = item end for diff --git a/playlet-lib/src/components/Services/BookmarksService/BookmarksService.bs b/playlet-lib/src/components/Services/BookmarksService/BookmarksService.bs index 38930d9c..9367d764 100644 --- a/playlet-lib/src/components/Services/BookmarksService/BookmarksService.bs +++ b/playlet-lib/src/components/Services/BookmarksService/BookmarksService.bs @@ -20,7 +20,7 @@ function Load() as void end if m.bookmarksString = bookmarksString - bookmarks = ParseJson(bookmarksString) + bookmarks = StringUtils.ParseJsonSafe(bookmarksString) if bookmarks = invalid LogWarn("Failed to parse bookmarks json") return diff --git a/playlet-lib/src/components/Services/Innertube/PlayerEndpoint.bs b/playlet-lib/src/components/Services/Innertube/PlayerEndpoint.bs index d511ef3c..b2c6c3cf 100644 --- a/playlet-lib/src/components/Services/Innertube/PlayerEndpoint.bs +++ b/playlet-lib/src/components/Services/Innertube/PlayerEndpoint.bs @@ -454,7 +454,7 @@ namespace Innertube LogDebug(`ReadAsciiFile("tmp:/sts_cache.json")`) cache = ReadAsciiFile("tmp:/sts_cache.json") if not StringUtils.IsNullOrEmpty(cache) - json = ParseJson(cache) + json = StringUtils.ParseJsonSafe(cache) return json["sts"] end if diff --git a/playlet-lib/src/components/Services/Innertube/SearchEndpoint.bs b/playlet-lib/src/components/Services/Innertube/SearchEndpoint.bs index 2abc27c9..d5d091cd 100644 --- a/playlet-lib/src/components/Services/Innertube/SearchEndpoint.bs +++ b/playlet-lib/src/components/Services/Innertube/SearchEndpoint.bs @@ -87,7 +87,7 @@ namespace Innertube text = response.Text() text = text.Replace(`)]}'`, "") - parsed = ParseJson(text) + parsed = StringUtils.ParseJsonSafe(text) if not IsArray(parsed) or parsed.Count() < 2 return { error: "Failed to parse search suggestions" diff --git a/playlet-lib/src/components/Services/Innertube/SessionData.bs b/playlet-lib/src/components/Services/Innertube/SessionData.bs index 9eef6390..c6dceec8 100644 --- a/playlet-lib/src/components/Services/Innertube/SessionData.bs +++ b/playlet-lib/src/components/Services/Innertube/SessionData.bs @@ -46,7 +46,7 @@ namespace Innertube text = response.Text() text = text.Replace(`)]}'`, "") - json = ParseJson(text) + json = StringUtils.ParseJsonSafe(text) if json = invalid return { "error": "Failed to parse session data: " + text @@ -78,7 +78,7 @@ namespace Innertube return invalid end if - sessionData = ParseJson(sessionData) + sessionData = StringUtils.ParseJsonSafe(sessionData) if sessionData = invalid RegistryUtils.Delete(RegistryUtils.INNERTUBE_SESSION_DATA) return invalid diff --git a/playlet-lib/src/components/Services/Invidious/Invidious.bs b/playlet-lib/src/components/Services/Invidious/Invidious.bs index 9c45e1b2..a161be40 100644 --- a/playlet-lib/src/components/Services/Invidious/Invidious.bs +++ b/playlet-lib/src/components/Services/Invidious/Invidious.bs @@ -1,7 +1,7 @@ import "InvidiousService.bs" function Init() - m.top.apiDefinitions = ParseJson(ReadAsciiFile("libpkg:/config/invidious_video_api.yaml")) + m.top.apiDefinitions = StringUtils.ParseJsonSafe(ReadAsciiFile("libpkg:/config/invidious_video_api.yaml")) m.service = new Invidious.InvidiousService(m.top) end function diff --git a/playlet-lib/src/components/Services/Invidious/InvidiousService.bs b/playlet-lib/src/components/Services/Invidious/InvidiousService.bs index 0c451d61..c1c6e096 100644 --- a/playlet-lib/src/components/Services/Invidious/InvidiousService.bs +++ b/playlet-lib/src/components/Services/Invidious/InvidiousService.bs @@ -698,7 +698,7 @@ namespace Invidious end function function VerifyTokenScope(token as string) as boolean - tokenObject = ParseJson(token) + tokenObject = StringUtils.ParseJsonSafe(token) if tokenObject = invalid LogWarn("Failed to json parse token:", token) return false diff --git a/playlet-lib/src/components/Services/LoungeService/LoungeApi.bs b/playlet-lib/src/components/Services/LoungeService/LoungeApi.bs index 91c73de6..55c557e6 100644 --- a/playlet-lib/src/components/Services/LoungeService/LoungeApi.bs +++ b/playlet-lib/src/components/Services/LoungeService/LoungeApi.bs @@ -212,7 +212,7 @@ class LoungeApi text = text.Mid(text.instr(`\n`) + 1) messagesToSend = [] - commands = ParseJson(text) + commands = StringUtils.ParseJsonSafe(text) m.ProcessCommands(commands, messagesToSend) m.SendMessages(messagesToSend) return true @@ -268,7 +268,7 @@ class LoungeApi payloadSize = chunk.Left(newLineIndex).trim() payloadSize = payloadSize.ToInt() payload = chunk.Mid(newLineIndex + 1, payloadSize) - commands = ParseJson(payload) + commands = StringUtils.ParseJsonSafe(payload) m.ProcessCommands(commands, messagesToSend) chunk = chunk.Mid(newLineIndex + payloadSize + 1) newLineIndex = chunk.InStr(`\n`) @@ -376,7 +376,7 @@ class LoungeApi if not IsString(devices) return end if - devices = ParseJson(devices) + devices = StringUtils.ParseJsonSafe(devices) if devices = invalid return end if @@ -618,7 +618,7 @@ class LoungeApi RegistryUtils.Delete(RegistryUtils.LAST_LOUNGE_DATA) - loungeData = ParseJson(loungeData) + loungeData = StringUtils.ParseJsonSafe(loungeData) if not IsAssociativeArray(loungeData) LogWarn("Can't disconnect from lounge. Invalid data:", loungeData) return diff --git a/playlet-lib/src/components/Services/LoungeService/LoungeVideoQueue.bs b/playlet-lib/src/components/Services/LoungeService/LoungeVideoQueue.bs index 06e9538d..3b232b62 100644 --- a/playlet-lib/src/components/Services/LoungeService/LoungeVideoQueue.bs +++ b/playlet-lib/src/components/Services/LoungeService/LoungeVideoQueue.bs @@ -86,7 +86,7 @@ namespace LoungeVideoQueue ' - eventDetails is of the form {"eventType":"VIDEO_REMOVED","videoId":""} eventDetails = playlistData[FIELD_EVENT_DETAILS] if IsString(eventDetails) - eventDetails = ParseJson(eventDetails) + eventDetails = StringUtils.ParseJsonSafe(eventDetails) end if if eventDetails <> invalid and eventDetails["eventType"] = "VIDEO_ADDED" eventVideoId = eventDetails[FIELD_VIDEO_ID] diff --git a/playlet-lib/src/components/Services/Preferences/Preferences.bs b/playlet-lib/src/components/Services/Preferences/Preferences.bs index 7126d434..5214ca50 100644 --- a/playlet-lib/src/components/Services/Preferences/Preferences.bs +++ b/playlet-lib/src/components/Services/Preferences/Preferences.bs @@ -21,7 +21,7 @@ function Init() end function function LoadPreferencesFile() - m.top.prefsModel = ParseJson(ReadAsciiFile("libpkg:/config/preferences.json5")) + m.top.prefsModel = StringUtils.ParseJsonSafe(ReadAsciiFile("libpkg:/config/preferences.json5")) end function function AddNodeFields() @@ -60,7 +60,7 @@ function LoadUserPreferencesFromRegistry() if userPrefs = invalid userPrefs = {} else - userPrefs = ParseJson(userPrefs) + userPrefs = StringUtils.ParseJsonSafe(userPrefs) end if ' Step 2: Migrate existing preferences to new format (if needed) @@ -101,7 +101,7 @@ function MigrateExistingPreferences(userPrefs as object) as boolean if not StringUtils.IsNullOrEmpty(instances) currentInstance = userPrefs["invidious.instance"] if StringUtils.IsNullOrEmpty(currentInstance) - instances = ParseJson(instances) + instances = StringUtils.ParseJsonSafe(instances) if IsArray(instances) and instances.Count() > 0 instance = instances[0] if not StringUtils.IsNullOrEmpty(instance) diff --git a/playlet-lib/src/components/Services/ProfilesService/ProfilesService.bs b/playlet-lib/src/components/Services/ProfilesService/ProfilesService.bs index ba084ad9..5e9a3238 100644 --- a/playlet-lib/src/components/Services/ProfilesService/ProfilesService.bs +++ b/playlet-lib/src/components/Services/ProfilesService/ProfilesService.bs @@ -31,7 +31,7 @@ function LoadProfilesFromRegistry() currentProfile: invalid } else - userProfiles = ParseJson(userProfiles) + userProfiles = StringUtils.ParseJsonSafe(userProfiles) end if ' Step 2: Migrate existing profiles to new format (if needed) @@ -98,7 +98,7 @@ function MigrateExistingProfiles(userProfiles as object) as boolean return false end if - authToken = ParseJson(tokenPayload) + authToken = StringUtils.ParseJsonSafe(tokenPayload) RegistryUtils.Delete(RegistryUtils.INVIDIOUS_TOKEN) if authToken = invalid or authToken.instance = invalid or authToken.token = invalid or authToken.username = invalid diff --git a/playlet-lib/src/components/Services/SearchHistory/SearchHistoryUtils.bs b/playlet-lib/src/components/Services/SearchHistory/SearchHistoryUtils.bs index ed15b9a8..f4698594 100644 --- a/playlet-lib/src/components/Services/SearchHistory/SearchHistoryUtils.bs +++ b/playlet-lib/src/components/Services/SearchHistory/SearchHistoryUtils.bs @@ -55,7 +55,7 @@ namespace SearchHistoryUtils return [] end if - history = ParseJson(history) + history = StringUtils.ParseJsonSafe(history) if not IsArray(history) return [] end if diff --git a/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockService.bs b/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockService.bs index 0b9bb3b0..07297aca 100644 --- a/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockService.bs +++ b/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockService.bs @@ -9,7 +9,7 @@ namespace SponsorBlock class SponsorBlockService function new() ' TODO:P2 this is being read and parsed on every skipped segment. It should be cached. - m.sponsorBlockConfig = ParseJson(ReadAsciiFile("libpkg:/config/sponsorblock_config.json5")) + m.sponsorBlockConfig = StringUtils.ParseJsonSafe(ReadAsciiFile("libpkg:/config/sponsorblock_config.json5")) end function function GetCategories() as object diff --git a/playlet-lib/src/components/VideoPlayer/VideoContentJob.bs b/playlet-lib/src/components/VideoPlayer/VideoContentJob.bs index 7d60c56c..dcf8b0bd 100644 --- a/playlet-lib/src/components/VideoPlayer/VideoContentJob.bs +++ b/playlet-lib/src/components/VideoPlayer/VideoContentJob.bs @@ -341,7 +341,7 @@ function SetCaptions(metadata as object, service as object, contentNode as objec return end if - isoLanguageList = ParseJson(ReadAsciiFile("libpkg:/config/ISO-639-1-to-ISO-639-2T.json5")) + isoLanguageList = StringUtils.ParseJsonSafe(ReadAsciiFile("libpkg:/config/ISO-639-1-to-ISO-639-2T.json5")) instance = service.GetInstance() subtitleTracks = [] diff --git a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/InvidiousRouter.bs b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/InvidiousRouter.bs index a06aa12a..470b68d0 100644 --- a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/InvidiousRouter.bs +++ b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/InvidiousRouter.bs @@ -66,7 +66,7 @@ namespace Http return true end if - feedSource = ParseJson(feedSource) + feedSource = StringUtils.ParseJsonSafe(feedSource) if feedSource = invalid response.Default(400, "Expected valid JSON for feed-source") return true diff --git a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletInvidiousBackendRouter.bs b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletInvidiousBackendRouter.bs index 2ae96459..eb02fa9e 100644 --- a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletInvidiousBackendRouter.bs +++ b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletInvidiousBackendRouter.bs @@ -804,7 +804,7 @@ namespace Http ' (such as "newest", "oldest", etc.), we'll ignore it, as it is not supported. sortBy = request.query.sort_by if not StringUtils.IsNullOrEmpty(sortBy) and sortBy.StartsWith("{") - sortBy = ParseJson(sortBy) + sortBy = StringUtils.ParseJsonSafe(sortBy) if IsAssociativeArray(sortBy) if sortBy.DoesExist("params") options.params = sortBy.params diff --git a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletLibUrlsRouter.bs b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletLibUrlsRouter.bs index 2605475c..565ca0ec 100644 --- a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletLibUrlsRouter.bs +++ b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/PlayletLibUrlsRouter.bs @@ -19,7 +19,7 @@ namespace Http saved = RegistryUtils.Read(RegistryUtils.PLAYLET_LIB_URLS) if saved <> invalid - saved = ParseJson(saved) + saved = StringUtils.ParseJsonSafe(saved) end if response.Json({ diff --git a/playlet-lib/src/components/Web/WebServer/Http/HttpRequest.bs b/playlet-lib/src/components/Web/WebServer/Http/HttpRequest.bs index fc25167d..69955a1a 100644 --- a/playlet-lib/src/components/Web/WebServer/Http/HttpRequest.bs +++ b/playlet-lib/src/components/Web/WebServer/Http/HttpRequest.bs @@ -272,7 +272,7 @@ namespace Http return invalid end if - m.body_json = ParseJson(text) + m.body_json = StringUtils.ParseJsonSafe(text) return m.body_json end function end class diff --git a/playlet-lib/src/components/Web/WebServer/Http/HttpUtils.bs b/playlet-lib/src/components/Web/WebServer/Http/HttpUtils.bs index b6bdc7ac..a0531e71 100644 --- a/playlet-lib/src/components/Web/WebServer/Http/HttpUtils.bs +++ b/playlet-lib/src/components/Web/WebServer/Http/HttpUtils.bs @@ -11,7 +11,7 @@ namespace Http if StringUtils.IsNullOrEmpty(json) return "application/octet-stream" end if - map = ParseJson(json) + map = StringUtils.ParseJsonSafe(json) if not IsAssociativeArray(map) return "application/octet-stream" end if @@ -41,7 +41,7 @@ namespace Http if StringUtils.IsNullOrEmpty(json) return "" end if - map = ParseJson(json) + map = StringUtils.ParseJsonSafe(json) if not IsAssociativeArray(map) return "" end if diff --git a/playlet-lib/src/source/services/HttpClient.bs b/playlet-lib/src/source/services/HttpClient.bs index 5787b2d0..05e4874c 100644 --- a/playlet-lib/src/source/services/HttpClient.bs +++ b/playlet-lib/src/source/services/HttpClient.bs @@ -703,7 +703,7 @@ namespace HttpClient if text = invalid return invalid end if - m._json = ParseJson(text) + m._json = StringUtils.ParseJsonSafe(text) return m._json end function @@ -813,7 +813,7 @@ namespace HttpClientCache end if metadataText = cacheText.Left(firstLineIndex) - metadata = ParseJson(metadataText) + metadata = StringUtils.ParseJsonSafe(metadataText) if metadata = invalid LogWarn("Failed to parse metadata from cache.") Delete(request) @@ -821,7 +821,7 @@ namespace HttpClientCache end if headersText = cacheText.Mid(firstLineIndex + 1, metadata.headers) - headers = ParseJson(headersText) + headers = StringUtils.ParseJsonSafe(headersText) if headers = invalid LogWarn("Failed to parse headers from cache.") Delete(request) diff --git a/playlet-lib/src/source/utils/StringUtils.bs b/playlet-lib/src/source/utils/StringUtils.bs index 95f6acba..08da1f78 100644 --- a/playlet-lib/src/source/utils/StringUtils.bs +++ b/playlet-lib/src/source/utils/StringUtils.bs @@ -29,5 +29,18 @@ namespace StringUtils function RandomId() as string return StrI(rnd(2147483647), 36) end function + + function ParseJsonSafe(jsonString as dynamic) as dynamic + if IsNullOrEmpty(jsonString) + return invalid + end if + ' bs:disable-next-line NO_PARSE_JSON + json = ParseJson(jsonString) + if json <> invalid + return json + end if + ' bs:disable-next-line NO_PARSE_JSON + return ParseJson(jsonString.Replace("\u0000", "")) + end function end namespace