From ef24983b5515b10eda28c2d275a5976c7fc9dffe Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Tue, 6 Oct 2026 22:48:00 +0300 Subject: [PATCH] feat: refuse a src no package can be fetched from, naming its scheme A javascript:, file: or ftp: src used to reach fetch, which rejects it with a TypeError that reads like a network failure. The probe now refuses anything but http:, https:, blob: and data: before any request, with a message that names the scheme. Co-Authored-By: Claude Opus 5.5 --- packages/player/src/shared/source.ts | 22 ++++++++++++ .../player/tests/unit/source-probe.test.ts | 34 ++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/packages/player/src/shared/source.ts b/packages/player/src/shared/source.ts index b414b7c8..22a96057 100644 --- a/packages/player/src/shared/source.ts +++ b/packages/player/src/shared/source.ts @@ -62,6 +62,24 @@ function readValidator(response: Response): string | null { * body (which is why the body is cancelled as soon as the headers arrive — a `HEAD` would prove * neither, and is often blocked outright). */ +/** + * The schemes a package can be fetched from. Anything else — `javascript:`, `file:`, `ftp:`, a + * typo — `fetch` rejects with a `TypeError` that reads like a network failure, so the probe + * refuses it first and names the scheme. + */ +const FETCHABLE_SCHEMES = new Set(['http:', 'https:', 'blob:', 'data:']) + +/** The scheme of `url` when no package can be fetched from it, else `null`. */ +export function unfetchableScheme(url: string, pageUrl: string | undefined): string | null { + let target: URL + try { + target = new URL(url, pageUrl) + } catch { + return null + } + return FETCHABLE_SCHEMES.has(target.protocol) ? null : target.protocol +} + /** * The `https:` URL to suggest when `url` is `http:` on an `https:` page, else `null`. Browsers * refuse such a fetch as mixed content — and a page whose CSP names only `https:` refuses it @@ -91,6 +109,10 @@ export async function probeSource( url: string, signal?: AbortSignal ): Promise { + const scheme = unfetchableScheme(url, globalThis.location?.href) + if (scheme) { + throw new PlayerError('network', `${scheme} URLs cannot be played. Use an http:, https:, blob: or data: URL.`) + } const secure = insecureOnSecurePage(url, globalThis.location?.href) if (secure) { throw new PlayerError( diff --git a/packages/player/tests/unit/source-probe.test.ts b/packages/player/tests/unit/source-probe.test.ts index a3296858..5ced0182 100644 --- a/packages/player/tests/unit/source-probe.test.ts +++ b/packages/player/tests/unit/source-probe.test.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it, vi } from 'vitest' -import { insecureOnSecurePage, probeSource } from '../../src/shared/source' +import { insecureOnSecurePage, probeSource, unfetchableScheme } from '../../src/shared/source' /** * What the probe can learn about an archive from the headers a browser lets it read. The fake @@ -119,3 +119,35 @@ describe('an http: package on an https: page', () => { expect(insecureOnSecurePage('http://host.example/a.h5p', undefined)).toBeNull() }) }) + +describe('a URL no package can be fetched from', () => { + afterEach(() => vi.unstubAllGlobals()) + + it('is refused before any request, naming the scheme', async () => { + const fetch = vi.fn() + vi.stubGlobal('fetch', fetch) + vi.stubGlobal('location', new URL('https://player.example/')) + + // `fetch` would reject these with a TypeError that reads like a network failure. + for (const url of ['javascript:alert(1)', 'file:///home/learner/course.h5p', 'ftp://host.example/a.h5p']) { + const probe = probeSource(url) + await expect(probe).rejects.toMatchObject({ code: 'network' }) + await expect(probe).rejects.toThrow(`${new URL(url).protocol} URLs cannot be played`) + } + expect(fetch).not.toHaveBeenCalled() + }) + + it('lets http:, https:, blob:, data: and a relative URL through', () => { + const page = 'https://player.example/demo/' + for (const url of [ + 'http://host.example/a.h5p', + 'https://host.example/a.h5p', + 'blob:https://player.example/8d4a0c1e-0000-4000-8000-000000000000', + 'data:application/zip;base64,UEsFBgAAAAAAAAAAAAAAAAAAAAAAAA==', + '/fixtures/a.h5p', + 'course.h5p' + ]) { + expect(unfetchableScheme(url, page)).toBeNull() + } + }) +})