Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/player/src/shared/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -91,6 +109,10 @@ export async function probeSource(
url: string,
signal?: AbortSignal
): Promise<RemoteSourceDescriptor> {
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(
Expand Down
34 changes: 33 additions & 1 deletion packages/player/tests/unit/source-probe.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
}
})
})
Loading