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
5 changes: 3 additions & 2 deletions packages/verify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ Exit code 0: it plays (possibly with warnings); 1: it does not; 2: the check cou

It checks that the archive indexes and carries the libraries it declares, that the runtime
reaches `ready`, that nothing throws while booting (an error after `ready` is a warning), that the
content drew something, and it lists failed requests, CSP blocks and the build's `revision`. It
does not judge correctness, suitability, accessibility or licensing: look at the screenshot.
content drew something, and it lists failed requests, CSP blocks, a video or audio the browser
could not play, and the build's `revision`. It does not judge correctness, suitability,
accessibility or licensing: look at the screenshot.

## Played untrusted

Expand Down
43 changes: 43 additions & 0 deletions packages/verify/lib/verify.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ const CONSOLE_LINES_KEPT = 20
const REVISION_SHOWN = 23
/** How many of the content check's changes become warnings; `report.json` keeps them all. */
const CHANGES_SHOWN = 20
/** What a media element's error says, by its code (`MediaError`). */
const MEDIA_ERRORS = /** @type {Record<number, string>} */ ({
1: 'its loading was stopped',
2: 'it could not be fetched',
3: 'it could not be decoded',
4: 'it is no format this browser plays'
})
/** How the runtime's boot script begins the notes it writes to the frame's console after the content check. */
const CHECK_NOTES = "<h5p-player>: the content was checked against its libraries' semantics"
/** The header the Service Worker wants before it hands an untrusted package's `content.json` over as written. */
Expand Down Expand Up @@ -237,6 +244,37 @@ export async function verifyPackage(options) {
// the verdict rules read that as nothing drawn.
.catch(() => null)
: null
// Each video and recording in the frame, after the settle: one the browser gave up on does not
// play, though the rest of the content drew (#218). Where this browser plays no H.264, or for a
// recording no AAC, as a Chromium built without them, an mp4 or an m4a it gave up on says
// nothing of the file.
const media = frame
? await frame
.evaluate(() => ({
plays: {
video: document.createElement('video').canPlayType('video/mp4; codecs="avc1.42E01E"') !== '',
audio: document.createElement('audio').canPlayType('audio/mp4; codecs="mp4a.40.2"') !== ''
},
failed: [...document.querySelectorAll('video, audio')].flatMap((element) => {
const played = /** @type {HTMLMediaElement} */ (element)
const src = played.currentSrc || played.src
// A file of the package's by its path there; any other by its address.
const own = URL.canParse(src) && new URL(src).origin === location.origin && src.includes('/content/')
const path = src.replace(/^.*?\/content\//, '')
// As written where an escape in it is not one, `%zz`, rather than losing every warning.
let name = src
if (own) {
try {
name = decodeURIComponent(path)
} catch {
name = path
}
}
return played.error ? [{ kind: played.localName, name, code: played.error.code }] : []
})
}))
.catch(() => ({ plays: { video: true, audio: true }, failed: [] }))
: { plays: { video: true, audio: true }, failed: [] }
/** @type {BlockedLoad[]} */
const csp = frame ? await frame.evaluate(() => /** @type {PageWindow} */ (window).__csp ?? []).catch(() => []) : []
// Untrusted, the package's `content.json` beside what the runtime was handed: the Service
Expand Down Expand Up @@ -321,6 +359,11 @@ export async function verifyPackage(options) {
report.warnings.push(`${library} played at ${to}, older than the ${from} the content was made for, since no library source has ${from}: what ${from} added is left out`)
}
for (const message of dedupe(report.errors.filter((e) => e.afterReady).map((e) => e.message))) report.warnings.push(`runtime error after ready: ${message}`)
for (const { kind, name, code } of media.failed) {
const audio = kind === 'audio'
if (!media.plays[audio ? 'audio' : 'video'] && /\.(?:mp4|m4v|m4a)$/i.test(name)) report.warnings.push(`${kind} not checked: ${name}: this browser plays no ${audio ? 'AAC' : 'H.264'}; Chrome or Edge does`)
else report.warnings.push(`${kind} does not play: ${name}: ${MEDIA_ERRORS[code] ?? `media error ${code}`}`)
}
for (const { url, status, error } of failedRequests) report.warnings.push(`request failed: ${url} (${status ?? error})`)
for (const { directive, blocked } of csp) report.warnings.push(`blocked by the frame's CSP: ${blocked} (${directive})`)
for (const text of report.console) report.warnings.push(`console error: ${text.slice(0, 200)}`)
Expand Down
31 changes: 28 additions & 3 deletions packages/verify/tests/verify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { contentChanges, summarize, verifyPackage } from '../lib/verify.mjs'
const bin = resolve(import.meta.dirname, '../bin/h5p-verify.mjs')
const content = resolve(import.meta.dirname, '../../../apps/demo/demo/content')
const quiz = join(content, 'quiz.h5p')
const video = join(content, 'video.h5p')
/** The workspace's library pack, served by the verifier itself, so the untrusted cases need no network. */
const pack = resolve(import.meta.dirname, '../../libraries/libraries.h5p')
/** The same pack split into its parts, which `libraries=pack` plays from. */
Expand All @@ -30,15 +31,16 @@ afterAll(async () => {
})

/**
* The quiz written again as `name` under `dir`, each file entry through `each`, which writes what
* A package written again as `name` under `dir`, each file entry through `each`, which writes what
* it keeps, as it is or changed; `then` adds what else the archive should carry.
*/
async function rewrittenQuiz(
async function rewritten(
from: string,
name: string,
each: (entry: Entry, bytes: Uint8Array, writer: ZipWriter<Blob>) => Promise<void>,
then: (writer: ZipWriter<Blob>) => Promise<void> = async () => {}
): Promise<string> {
const reader = new ZipReader(new BlobReader(new Blob([await readFile(quiz)])))
const reader = new ZipReader(new BlobReader(new Blob([await readFile(from)])))
const writer = new ZipWriter(new BlobWriter('application/zip'))
for (const entry of await reader.getEntries()) {
if (entry.directory) continue
Expand All @@ -51,6 +53,9 @@ async function rewrittenQuiz(
return path
}

/** The quiz written again, as `rewritten` writes a package. */
const rewrittenQuiz = (name: string, ...rest: Parameters<typeof rewritten> extends [string, string, ...infer R] ? R : never) => rewritten(quiz, name, ...rest)

/** The quiz as h5p.com would export it: `h5p.json` and `content/`, no library folders. */
const strippedQuiz = () =>
rewrittenQuiz('stripped.h5p', async (entry, bytes, writer) => {
Expand Down Expand Up @@ -211,6 +216,26 @@ describe('h5p-verify', () => {
expect(summarize(report)).toContain('--libraries pack')
}, 120_000)

it('warns of a video that does not play, as a web page saved under its name (#218)', async () => {
const page = await rewritten(video, 'page-video.h5p', async (entry, bytes, writer) => {
await writer.add(entry.filename, new Uint8ArrayReader(entry.filename.endsWith('.mp4') ? new TextEncoder().encode('<!DOCTYPE html><title>File:clip.mp4</title>') : bytes))
})
const report = await verifyPackage({ file: page })
// The rest of the content plays: a warning, as it stood unseen beside pass before.
expect(report.verdict).toBe('pass')
expect(report.warnings).toContainEqual(expect.stringMatching(/^video (?:does not play|not checked): videos\/[^/]+\.mp4: /))
// The demo's own video plays, and is not named.
const played = await verifyPackage({ file: video })
expect(played.warnings.filter((warning) => warning.startsWith('video does not play'))).toEqual([])
// A name with an escape that is not one, `%zz`, is named as written, and loses no warning.
const odd = await rewritten(video, 'odd-video.h5p', async (entry, bytes, writer) => {
if (entry.filename.endsWith('.mp4')) await writer.add('content/videos/odd%zz.mp4', new TextReader('<!DOCTYPE html><title>File:clip.mp4</title>'))
else if (entry.filename === 'content/content.json') await writer.add(entry.filename, new TextReader(new TextDecoder().decode(bytes).replace(/videos\/[^"]+\.mp4/, 'videos/odd%zz.mp4')))
else await writer.add(entry.filename, new Uint8ArrayReader(bytes))
})
expect((await verifyPackage({ file: odd })).warnings).toContainEqual(expect.stringMatching(/^video (?:does not play|not checked): videos\/odd%zz\.mp4: /))
}, 120_000)

it('fails a file that is not an archive', async () => {
const path = join(dir, 'not-a-zip.h5p')
await writeFile(path, 'this is not a zip file')
Expand Down
2 changes: 2 additions & 0 deletions skills/h5p-verify/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Exit 2: the check could not run (no browser, no such file).
| `drew nothing` | started, empty frame | usually empty or wrongly nested parameters |
| warning: `runtime error after ready` | often a harmless resize exception; sometimes a broken sub-content | check the screenshot; fix only if something is missing |
| warning: `request failed: content/…` | a media file `content.json` references is not in the archive | add it or fix the path |
| warning: `video does not play: <file>: …` (or `audio`) | the browser gave up on the file: not a video or a recording at all (a web page saved under its name), or one it cannot decode | open the file itself; replace it with the real one, a video as H.264 mp4 |
| warning: `video not checked: … this browser plays no H.264`, or `audio not checked: … no AAC` | the browser the check ran in lacks the codec most mp4 and m4v videos (H.264) or m4a recordings (AAC) use | install Chrome or Edge, or pass `--browser`, and check again |
| warning: `blocked by the frame's CSP` | content loading from an outside origin | put the asset in the package |
| warning: `content check: <field>: markup removed: …` (with `--untrusted`) | the field holds tags, attributes or styles its library does not allow; the site will strip them | write the field in what `semantics.json` allows for it (its `tags` and `font`), or accept the loss if the text still reads right |
| warning: `content check: <field>: removed`, `… became …` | a field the semantics do not name, or a value outside its options or range; or the libraries' upgrade moved it, when the pack is newer than what the content was written for | match the field to `semantics.json`, or build against the pack's versions |
Expand Down
Loading