diff --git a/scripts/utils.ts b/scripts/utils.ts index 400b0b691e..a355207b35 100644 --- a/scripts/utils.ts +++ b/scripts/utils.ts @@ -59,10 +59,12 @@ function sortQueryParameters(url: string): string { } export function normalizeURL(url: string): string { + // decodeURI, here and inside normalize-url, would turn %25 into a bare "%", so it is swapped + // for a placeholder that is put back once the link is decoded let normalized: string try { normalized = sortQueryParameters( - normalizeUrl(url, { stripWWW: false, sortQueryParameters: false }) + normalizeUrl(url.replace(/%25/gi, '\uE000'), { stripWWW: false, sortQueryParameters: false }) ) } catch { return url.replace(/\s/g, '+') @@ -75,6 +77,7 @@ export function normalizeURL(url: string): string { } catch { decoded = normalized } + decoded = decoded.replace(/\uE000|%EE%80%80/gi, '%25') // "+" means a space only in the query; in the path it is a plus sign const queryStart = decoded.indexOf('?') diff --git a/tests/__data__/input/playlist_normalize/reserved_escapes.m3u b/tests/__data__/input/playlist_normalize/reserved_escapes.m3u index 3e854af934..e6061e35ab 100644 --- a/tests/__data__/input/playlist_normalize/reserved_escapes.m3u +++ b/tests/__data__/input/playlist_normalize/reserved_escapes.m3u @@ -7,3 +7,9 @@ http://example.com/a%2Fb/index.m3u8 http://example.com/index.m3u8?id=1%232 #EXTINF:-1 tvg-id="",Example Query Nested URL (720p) http://example.com/index.m3u8?name=TV%2B&url=http%3A%2F%2Fexample.org%2Fa.m3u8%3Fk%3D1%26exp%3D2 +#EXTINF:-1 tvg-id="",Example Path Percent (720p) +http://example.com/a%2520b/index.m3u8 +#EXTINF:-1 tvg-id="",Example Query Percent (720p) +http://example.com/index.m3u8?token=50%25off +#EXTINF:-1 tvg-id="",Example Malformed Percent (720p) +http://example.com/a%ZZ/index.m3u8?token=50%25off diff --git a/tests/commands/playlist/format.test.ts b/tests/commands/playlist/format.test.ts index b830236f99..a9a46a6530 100644 --- a/tests/commands/playlist/format.test.ts +++ b/tests/commands/playlist/format.test.ts @@ -70,6 +70,9 @@ describe('playlist:format', () => { expect(output).toContain( 'http://example.com/index.m3u8?name=TV%2B&url=http%3A%2F%2Fexample.org%2Fa.m3u8%3Fk%3D1%26exp%3D2' ) + expect(output).toContain('http://example.com/a%2520b/index.m3u8') + expect(output).toContain('http://example.com/index.m3u8?token=50%25off') + expect(output).toContain('http://example.com/a%ZZ/index.m3u8?token=50%25off') }) it('removes a duplicate that was the only stream in its file', () => {