Skip to content

feat: browser BIDS validation on the upload page - #473

Merged
neuromechanist merged 20 commits into
stagingfrom
feature/browser-bids-validation
Oct 21, 2026
Merged

neuromechanist merged 20 commits into
stagingfrom
feature/browser-bids-validation

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The upload page now validates the dropped folder with the real BIDS validator (@bids/validator 3.0.2, the release nemar-cli pins and the repository check runs) in the browser, before any file is sent.
The instant structure check is unchanged and runs first; the full check follows it, with a progress state and a panel of findings grouped by code, errors first, with file paths.
An error disables the upload button and a warning never does, which is the repository check's rule, including its exemption for git-annex pointer files.

The design and the alternatives are in ADR 0028.
A follow-up, #474, builds the validator from npm.jsr.io with Bun, so that it need not be committed.
This PR targets staging, so the issue is closed by hand after the merge.

Where this differs from the issue text

  • The validator runs in a module Web Worker, not on the page.
    The issue proposes importing it on the page.
    Measured in Chrome 155, that fails: the validator evaluates code with the Function constructor (the schema's expressions, and Ajv), and /upload's Content-Security-Policy has no 'unsafe-eval', so the run throws EvalError.
    A worker takes its policy from its own response, so the worker script gets a header rule in public/_headers (default-src 'none'; script-src 'self' 'unsafe-eval'; connect-src https://raw.githubusercontent.com, with Referrer-Policy: no-referrer) and the page's policy is untouched.
    The alternative, 'unsafe-eval' on /upload in the pattern of ADR 0009, is one line in src/middleware.ts; the rest of this PR does not depend on the choice.
  • @bids/validator/web does not exist in 3.0.2.
    The worker imports @bids/validator/files/browser (fileListToTree) and @bids/validator/validate (validate).
  • The pin is 3.0.2, not 2.4.1 (nemar-cli dev).
  • The validator is vendored, built with Deno, and committed.
    Bun cannot install a jsr: specifier, and Cloudflare's build has no Deno.
    JSR's npm registry (npm.jsr.io) does serve the package, and Bun can install it with a scope entry; bun build of the entry points gave a bundle that reported the same findings.
    That route was not built through Astro and Vite; ADR 0028 records it as the alternative a later change could take, with the protocol, gate, panel and tests unchanged, and Build the BIDS validator worker from npm.jsr.io with Bun #474 tracks it.
  • /upload's initial payload is not unchanged: its own script grows by 11.2 KB (4.1 KB gzipped).
    The issue asked to keep it unchanged.
    The 10 MB validator is still fetched only when a folder is validated, but the code that drives it (the client, gate, report, panel and wording modules) is part of the page's script, which goes from 13,783 to 25,031 bytes (4,841 to 8,927 gzipped), and the site's one stylesheet grows by 4,077 bytes (891 gzipped).

What is in it

  • validator-version.json (copy of nemar-cli's pin), scripts/vendor-bids-validator.ts (Deno; --relock after a pin move), scripts/bids-validator-worker.ts (the module it bundles), scripts/bids-validator-worker.lock.
  • public/vendor/bids-validator/3.0.2/: the bundle, SHA256SUMS, provenance.json, THIRD_PARTY_LICENSES.txt (the 37 packages and the Node.js shims the bundler adds).
    The build is reproducible: the same bytes from a cold Deno cache and from another directory, given network access and the same Deno (2.9.7).
    .gitattributes marks public/vendor/** as generated, so GitHub folds it and git diff does not print the bundle; provenance.json and SHA256SUMS stay diffable.
  • src/lib/bids-validation/: protocol.ts (shared with the worker, imports nothing), gate.ts, report.ts, wording.ts, client.ts, panel.ts; src/components/BidsValidationPanel.astro; the wiring in src/pages/upload.astro.
  • .bidsignore now travels with a web upload (src/lib/upload-client.ts, src/lib/bids-precheck.ts): the dropzone skipped every dotfile, so a dataset that lists its non-BIDS files there could pass neither check from the browser.
    Other dotfiles are still skipped.
  • Tests: vendored-asset integrity and the worker policy, the pin comparison with nemar-cli, the committed bytes run on real datasets, the panel and its markup, and unit tests of the helpers.
    Fixtures are real files; test/fixtures/bids-validation/README.md names the origins, including a pointer written by git-annex itself.
  • ADR 0028, a CHANGELOG entry, AGENTS.md notes; ci.yml sparse-checks out nemar-cli's pin.

Gating rule

Finding Upload
warning allowed, listed collapsed
error blocked
error whose location is a git-annex pointer file not counted, noted on the panel; the headline says no errors need fixing
HED_ERROR with SCHEMA_LOAD_FAILED, where the browser's request for the schema got no answer at all not counted, listed with the warnings; the verdict is amber and says "The HED annotations were not checked."
any other SCHEMA_LOAD_FAILED: HED columns and no HEDVersion, an invalid or unmergeable specification, a version GitHub answers 404 for, any other answer from GitHub (a 503 included) blocked, as the repository check blocks it
the validator could not run (no worker, failed load, crash, a result that is not a report, a throw before it has an answer or while drawing one with no errors in it, no answer in 15 minutes) allowed, in a state of its own that is never worded as a pass
a throw while the panel draws a verdict that has errors in it blocked: the verdict is kept, and the line beside the button says so

The pointer rule is the repository check's (run-bids-validation.yml): a file of fewer than 1024 bytes with a line that starts /annex/objects/, compared with the error's location as a whole string.
A browser is never told about symlinks, so the check's other half, symlinks, has no counterpart here.
The options are the check's (maxRows 1000), except that warnings are kept, because the page lists them.

The HED row is the one place the page is more lenient than the repository check, on purpose, and it is narrow.
SCHEMA_LOAD_FAILED is one sub-code for seven conditions of the HED validator; only a request that got no answer (offline, a failed lookup, a blocker) is the browser's failure and not the dataset's, and an answer from GitHub, of any status, is the answer the check would get.
The gate tells them apart by the validator's own wording, anchored at the start of the message because the validator quotes a dataset's HEDVersion into some of its other messages, and a wording it does not know stops the upload.
The cost is that a page that cannot reach GitHub lets through a HED dataset naming a version that does not exist, which only the repository check catches.

Privacy

The panel says the check runs in the browser and nothing from the files is sent anywhere, with one stated exception: a HED schema version the validator does not ship is downloaded from raw.githubusercontent.com, in a request that carries only its name and version and no Referer.
Checked in Chrome with every request logged: no request at all for a dataset whose HED schema ships (and for datasets with none), one GET for one whose does not.
The worker's own policy allows that one host and nothing else; a same-origin fetch, another host, a WebSocket and importScripts were all refused, in Chrome 155 and in Firefox 158, under wrangler pages dev, which applies _headers as Pages does.

Verification

  • bun install: no changes (package.json and bun.lock untouched, no version bump).
  • bun run test: 153 files, 4121 tests pass (the base had 144 files, 3917 tests), run as CI runs it: the five NEMAR_CLI_* variables against a snapshot of nemar-cli dev.
    A sibling ../nemar-cli that is behind origin/dev fails the new pin test (a stale one pins 2.4.1); update it, or set NEMAR_CLI_VALIDATOR_PIN.
  • bun run typecheck: 0 errors, 0 warnings (the 13 hints are the base's).
  • bun run lint: clean.
  • bun run build: succeeds, with the same five warnings as the base; dist/_routes.json excludes /vendor/*, so the files are static assets, and dist/_headers carries the worker rule.
  • CI on this PR: lint, typecheck, test (153 files, with the strict drift tests against nemar-cli dev), build and the Cloudflare Pages deploy all pass.
  • Each of the 20 commits passes the suite, typecheck and lint on its own.
  • The bundle is byte for byte what was first committed: sha256sum -c SHA256SUMS passes, and its ETag on the preview deployment did not change between the first head and this one.
  • The HED cases, run on the committed bytes: the validator's wording for no HEDVersion, an invalid one, unmergeable library schemas and a 404 was captured in Chrome 155 (the worker served by a preview deployment) and Node 26 (the library schemas in Node only), and for a request with no answer in Chrome (aborted) and in Node 26 and Deno 2.9.7 (HTTPS_PROXY at a closed port).
    The wording is copied into gate.test.ts; the integration tests produce the ones that need no network again (a real local server that answers 404 or 503, a closed port for no answer), so a validator release that rewords one fails there.
    Against the old broad rule 18 of the new tests fail.
  • Mutation checks: an edited worker source, a hand-edited bundle, and a pin moved without a rebuild each fail the vendor test; ten mutants of the gate, protocol, report and wording code, nine are caught (the tenth changes nothing observable); and after review mutants of the HED rule (never exempt, no start anchor), of the dropzone rule for .bidsignore, of panel.ts using innerHTML, of the deadline parameter and of terminating the worker are all caught.
  • Real Chrome 155 against astro dev with the dev sign-in: valid, broken-sidecar, git-annex-pointer, HED (bundled; no HEDVersion; a version GitHub answers 404 for; a request that gets no answer), structure-error and larger datasets (1,204 files and 10,404 findings in 0.8 s from the click; 20,003 files and 135,003 findings in 24 s); a .bidsignore upload by the file input and by a real folder dropped through CDP, which runs the drag-and-drop walk (9 files kept with .bidsignore, .DS_Store left out); Cancel, a second drop while running, "Drop something else", Enter in a field while blocked; five ways for the worker to fail (offline, 404, a script that throws, the wrong content type, an answer of failure) and three more (a result that is not a report, a throw while the panel draws the result, with and without errors in it, no panel on the page); the 15-minute limit with a faked clock; Tab to a findings list and ArrowDown scrolling it; focus and the live-region announcements; light and dark; 390 px wide with no horizontal overflow.
    Firefox 158 ran the policy probe and the real validator through wrangler pages dev with the same results.
  • Smoke test with bun run dev: /upload without a session answers 302 to the sign-in page, and with the dev session 200 text/html with no reference to the vendor file, no preload and no 'unsafe-eval' in its policy; the asset URL answers 200 text/javascript, 10,267,556 bytes.
  • On the Cloudflare Pages preview deployments of this PR: the worker asset answers 200 with the content-security-policy and referrer-policy of the _headers rule and cache-control: public, max-age=0, must-revalidate, /upload's own policy is unchanged, and in Chrome 155 the module worker, started from that origin, validated the bids-minimal fixture (0 errors, 30 warnings, schema 1.2.7, about half a second with the download) with no request other than the asset.
  • /upload's own load: its script grows from 13,783 to 25,031 bytes (4,841 to 8,927 gzipped); the site-wide stylesheet grows by 4,077 bytes (891 gzipped).
    Nothing imports the bundle (the page script holds the worker's URL as a string), the HTML names no vendor file, the worker file is requested only when a folder is validated, and the server bundle lists the files as static assets and does not carry them.

Asset: 10,267,556 bytes (1,456,230 with gzip -9, 872,874 with brotli -q 9).

Known limits

  • No browser run happens in CI: the worker, the policy and the page were exercised with scripted Chrome and Firefox runs that this PR does not add (there is no end-to-end harness here, website#435).
    A few of the tests read the page's and the panel's source as text (the guard around the bind, the fallback line, the single try, the textContent rule, the list's tab stop), because there is no DOM to run them in; they are named for what they are.
    Safari was not run.
  • The drag-and-drop walk (walkEntry) needs a browser's FileSystemEntry, so its plumbing has no automated test.
    Its per-file decision, which .bidsignore depends on, is one function, toDroppedFile, shared with the file input and tested with real files, and the walk itself was run in Chrome on a real folder.
  • The policy is enforced where _headers is served.
    It was checked on this PR's preview deployments (above), not on test.nemar.org or production.
    After the merge to staging, curl -sI https://test.nemar.org/vendor/bids-validator/3.0.2/bids-validator-worker.js should show the same content-security-policy and referrer-policy; without them the worker still runs, but the privacy sentence no longer holds.
  • A re-vendor at the same version reuses the worker's URL and relies on revalidation by ETag (Pages sends max-age=0, must-revalidate); the path must never be given a longer lifetime.
  • Symlinks are invisible to a browser (above).
  • A page that cannot reach GitHub lets through a HED dataset naming a version that does not exist; the repository check catches it.
  • "The same validator" holds for a release, not for what it resolves: the repository check runs the release without a lock, so a patch release of one of its dependencies can change a finding on one side until the next re-vendor.
  • The bundle is one 10 MB file because the HED validator is inside it, and splitting it makes the bytes depend on the Deno cache path.
  • Every move of nemar-cli's pin (its workflow runs monthly) turns this repository's CI red until the file is copied and deno run -A scripts/vendor-bids-validator.ts --relock is run; that needs Deno on a maintainer's machine, not in CI.
  • docs.nemar.org/web/uploading describes BIDS validation as server-side only, so it needs a matching update in the documentation repository, which this PR does not touch.

Refs #161

validator-version.json is a copy of nemar-cli's pin (3.0.2).
scripts/vendor-bids-validator.ts builds that release with deno bundle
into one module worker, from scripts/bids-validator-worker.ts and the
protocol the page shares with it, against a committed lock. The build
is reproducible from a cold cache and from any directory.
--relock resolves into a temporary lock and replaces the committed one
only after every check has passed.

Both Deno programs are excluded from tsconfig; typecheck and lint pass.
Output of: deno run -A scripts/vendor-bids-validator.ts
public/vendor/bids-validator/3.0.2/ holds the bundle (10,267,556 bytes),
SHA256SUMS, provenance.json and the license of each package built in
(37 packages, and the Node.js shims the bundler adds).
public/_headers gives the worker its own Content-Security-Policy:
unsafe-eval (the validator uses the Function constructor) and a single
connect-src host (HED schema downloads), and no Referer. The page
policy is unchanged.

Rebuilt from a cold cache in another directory: identical bytes.
bids-minimal is a byte-for-byte copy of nemar-cli's fixture of that name.
annex-pointer is the participants.tsv pointer git-annex wrote for it.
hed overlays two small HED files on it. The README names the origins.
report groups findings by code, errors first. gate decides what stops
an upload the way the repository check does: errors block, warnings do
not, errors on git-annex pointer files are not counted. A HED schema
that could not be downloaded is listed with the warnings and not
counted either, because that is a failure of the browser's request and
not a fault in the dataset. wording holds the panel's sentences,
privacy statement included. client runs the worker and never rejects;
a run that has not answered in 15 minutes becomes "could not run".

84 unit tests on real-shape inputs and the real fixtures; no mocks.
Integrity: the bundle matches SHA256SUMS, carries the pinned release,
and was built from the sources and lock in the tree. Drift: the pin
equals nemar-cli's, strictly in CI (ci.yml now sparse-checks it out and
sets NEMAR_CLI_VALIDATOR_PIN). Policy: the worker's header is the only
static rule that allows eval and sends no Referer; the upload page's
policy has no eval. Integration: the committed bytes validate the real
fixtures, gate them, and make no request for a dataset whose HED schema
ships.

Full suite passes at this commit: 152 files, 4036 tests, with the pin
variable pointing at nemar-cli dev. Lint clean, typecheck 0 errors.
The dropzone skipped every file whose name starts with a dot.
.bidsignore is the one dotfile BIDS defines and the validator reads, so
a dataset that lists its non-BIDS files there could pass neither the
check on this page nor the repository check from the browser, with no
way to fix it. It now travels with the upload, and the instant
structure check no longer calls it a stray top-level file. Other
dotfiles are still skipped.

Two unit tests; suite passes (152 files, 4038 tests), lint clean.
After the instant structure check passes, the folder is validated in a
module Web Worker, started on first use: /upload loads no new request
until a folder is validated, and its own script grows from 13,783 to
22,672 bytes (stylesheet +3,952 bytes). The panel groups findings by
code with file paths. Errors disable the upload button, warnings are
advisory, and a HED schema that could not be downloaded is listed as a
warning. A validator that cannot run (no worker, failed load, crash, no
answer in 15 minutes) is its own amber state and lets the upload
through. Cancel, a new drop and a reset drop a stale answer.

The live region is always rendered, outside the panel that is hidden at
rest, so a result set while the panel appears is heard; focus moves to
the panel heading when a run starts.

Run in Chrome against astro dev: valid, broken, git-annex pointer, HED
(bundled and not), structure-error and 1,204-file datasets; cancel,
re-drop, reset, Enter-submit while blocked; five ways for the worker to
fail. Suite passes (153 files, 4052 tests), lint clean, typecheck 0
errors.
Records why the BIDS validator is built with Deno, committed, and run in
a Web Worker under its own Content-Security-Policy: it needs unsafe-eval,
which the page must not get. Also records the pin lockstep with nemar-cli,
the gating rule (including the one place it is more lenient than the
repository check), what the privacy sentence rests on, the .bidsignore
change, and the npm.jsr.io route (installable with Bun, not taken here).
Index updated.
CHANGELOG.md: the validator on /upload, the .bidsignore change, and the
CI pin comparison.
AGENTS.md: the architecture map entries, the Deno commands, and the
lockstep note (re-vendor when nemar-cli moves the pin; never hand-edit
public/vendor/; the worker's header is where its unsafe-eval lives).
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

The page exempted every HED_ERROR with sub-code SCHEMA_LOAD_FAILED, but
that is seven conditions. A dataset with HED columns and no HEDVersion
makes no request, passed the upload gate and failed the repository
check, and the panel said "No BIDS errors found".

Only a request that got no answer is exempt now. No specification, an
invalid one, unmergeable library schemas, and any answer from GitHub,
404 for a version that does not exist included, stop the upload. The
gate reads the validator's own wording, anchored at the start because
the validator quotes the dataset's HEDVersion into other messages; an
unknown wording stops the upload. When something is exempt the verdict
says the HED annotations were not checked, in an amber tone.

The wording of each case is copied from real runs (Chrome 155, Node 26
and Deno 2.9.7 with an unreachable proxy, a live 404) into the unit
tests, and the real bundle is run offline for the rest: a real server
that answers 404 or 503, and a closed port for no answer. The old
broad rule fails 18 of the new tests.

Suite 153 files, 4075 tests; lint clean; typecheck 0 errors.
Everything after the worker answered (the pointer lookup, the gate,
drawing the panel) ran outside any try. A throw there left the state at
"running" with the upload button disabled and Cancel doing nothing; in
Chrome, with the panel's groups container made to throw, the page said
"No BIDS errors found" and the button stayed disabled.

startFullValidation now runs inside one try, and a throw becomes "could
not run" like every other way the check can fail. validationCannotRun
tells the person through the panel and, when there is none or it
cannot draw, through the status line beside the button, so it is never
only a console message. The worker's result is checked for the shape
the page reads (isValidationReport) before anything uses it, and a
failing progress callback no longer ends a run. The pointer lookup and
the gate are one function, judge, which the page and the tests that run
the real validator both call; every real report in those tests is held
to the shape check after a structured clone.

The test whose name claimed to check that the page says the check could
not run only read the source; the tests that do are renamed for what
they are, and cover the whole chain. Checked in Chrome: a result that is
not a report, a throw while drawing, and no panel on the page.

Suite 153 files, 4098 tests; lint clean; typecheck 0 errors.
The .bidsignore change was only tested at the unit level, with a
FileList of stand-in names. Now the real bundle runs on real files:
a stray foo.xyz is NOT_INCLUDED, an error on that file that stops the
upload; with .bidsignore listing it the error is gone; and the same
holds when the drop is what the dropzone keeps (hidden files and OS
litter left out, .bidsignore kept), and fails again without it.

The per-file decision that the drag-and-drop walk and the file input
share is now one exported function, toDroppedFile, so real File
objects test it for both. The walk's own plumbing (a browser's
FileSystemEntry) cannot be built in Node and has no automated test;
it was run in Chrome with a real folder dropped through CDP
(Input.dispatchDragEvent): 9 files kept with .bidsignore, .DS_Store
left out, and the check passed.

Suite 153 files, 4110 tests; lint clean; typecheck 0 errors.
panel.ts sets text with textContent and nothing that parses HTML,
because file names come from the person's disk and messages from the
validator. That was a rule a reviewer had to remember. A source test
now fails on innerHTML, outerHTML, insertAdjacentHTML, document.write
or createContextualFragment in panel.ts, ignores comments that name
them, and checks that the file does set text with textContent. Checked
by putting innerHTML in the panel (fails) and in a comment (passes).

Suite 153 files, 4116 tests; lint clean; typecheck 0 errors.
runValidation takes a deadlineMs option (the page still uses 15
minutes), so the give-up path can be run without waiting a quarter of
an hour or faking the clock. Node has no Worker, so the test gives the
client the browser's Worker surface over a real thread from
node:worker_threads that never answers, with real timers: the run is
settled as "could not run" at the deadline, the thread is really
terminated (its exit event follows), the state lets the upload through,
and Cancel ends the thread too with no later deadline. Checked by
ignoring the parameter and by not terminating (both fail by timeout).

Suite 153 files, 4119 tests; lint clean; typecheck 0 errors.
public/vendor/** is generated by scripts/vendor-bids-validator.ts, so
it is marked linguist-generated (GitHub folds it in a pull request) and
-diff (git does not print the 10 MB bundle or the licence text as a
diff). The two small files a re-vendor is reviewed by, provenance.json
and SHA256SUMS, stay diffable. Checked with git check-attr.

Suite 153 files, 4119 tests; lint clean; typecheck 0 errors.
The vendor script's header now says identical bytes need network access
(the packages come from jsr.io and npm, the shim licence from
unpkg.com) and exactly the Deno that built the committed bundle, 2.9.7,
and that the script prints a note when it is not that Deno.

A re-vendor at the same version reuses the worker's URL. The client's
comment on workerUrl and ADR 0028 now say so: the browser is corrected
by revalidation, not by the name, because Pages answers a static file
with an ETag and max-age=0, must-revalidate, and nothing may give the
path a longer lifetime. The ADR's npm.jsr.io alternative links the
follow-up issue (#474).

Suite 153 files, 4119 tests; lint clean; typecheck 0 errors.
The list of findings in a group is capped at 18rem and scrolls, and a
scrolling region that a keyboard cannot focus cannot be scrolled by
one. It is now a tab stop (tabindex 0) with an accessible name,
"Findings for <code>", and a visible focus ring. Checked in Chrome: Tab
from a group's summary lands on the list, six ArrowDown presses scroll
it 240 px, and the accessibility tree names it. A source test holds
the pieces together.

Suite 153 files, 4120 tests; lint clean; typecheck 0 errors.
The review fixes added code to the page's script (the shape check, the
single try, judge, the deadline option, the HED copy), so the figures
in the ADR and the changelog were stale. Measured on the build of this
head against the unmodified base: the script goes from 13,783 to 25,031
bytes (4,841 to 8,927 gzipped, so +11.2 KB, +4.1 KB gzipped) and the
site's one stylesheet grows by 4,077 bytes (891 gzipped). The ADR also
says what the issue asked for and this is not: an unchanged initial
payload for /upload, which holds for the validator and not for the
code that drives it.
The state is set to "finished" before the panel draws the result. If
showFinished or setStatus then threw, the catch called
validationCannotRun, which set "unavailable" and let the upload through
although the validator had found errors.

The error count is now remembered when the gate answers. A throw after
that keeps the verdict: the state stays "finished" with its errors, the
upload button stays disabled, the line beside it says "Fix the BIDS
errors above to enable the upload.", and the console has the error. A
throw with no errors, or before the gate has answered, is still "could
not run" and the upload is possible. A source test pins the order; in
Chrome, with the panel's groups container made to throw, a verdict with
6 errors leaves the button disabled and one with none says the check
could not run.

Suite 153 files, 4121 tests; lint clean; typecheck 0 errors.
The cancel test said the deadline does not fire afterwards, and nothing
asserted it. It now waits 250 ms, past the 200 ms deadline, and checks
that the worker was terminated once (a deadline that fired would end it
a second time) and that nothing was written to the console. Checked by
removing clearTimeout from settle: the test fails with 2 terminations.

Suite 153 files, 4121 tests; lint clean; typecheck 0 errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant