AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
Issue 701: restore resource XSS protection
Problem and outcome
Issue #701 reports that restore accepts kind=image with mimeType=text/html, while resource responses trust kind to enable inline rendering. Close both the write-time mismatch and the read-time exposure of previously stored resources. Work starts from official main at b8496806 in a separate worktree.
The change affects API resource restore and resource response headers shared by Cloudflare and Docker. Backups explicitly labeling an image as an incompatible type such as HTML are rejected; valid old backups must still restore. The existing nullable MIME contract and unknown/binary fallback remain supported, with unknown types downloading instead of rendering inline. HTML and other arbitrary attachments remain downloadable. There is no migration or deletion of existing data. Rollback reverts the code without rewriting resources; reverting also reintroduces the vulnerability. Native-platform runtime and deployment acceptance are separate from the local checks below.
The backup schema explicitly permits null MIME, the released exporter preserves it, and resumable restore already falls back to application/octet-stream. Rejecting that safe fallback would break legitimate old archives without contributing to XSS protection; response-time MIME checks provide the download boundary.
Compare image MIME case-insensitively without parameters, but retain the stored MIME and its parameters as before. Stripping parameters from the stored value is unnecessary for validation and would discard encoding information such as SVG charset.
A local browser check found a remaining parser mismatch: a value starting with an image/audio type can contain an unquoted comma and a second HTML type. The old first-semicolon check permits inline delivery, while Chromium interprets the response as HTML even with nosniff. Validate and serialize the whole field using the portable [email protected] parser/formatter instead of splitting strings. Check the original HTTP-field characters before trimming/parsing: permit HTAB, visible ASCII and obs-text only, rejecting other controls, DEL and non-byte characters even inside ignored or duplicate parameters. The compatibility risk is rejecting multi-valued or unserializable MIME declarations and changing response-header casing/quoting; valid SVG encoding parameters and the stored backup metadata remain intact. Reverting this correction restores the bypass; rollback requires only code/dependency changes, not data migration.
Allowed changes and acceptance
| ID | Behavior | Acceptance |
|---|---|---|
| S1 | Legacy restore and resumable restore initialization/completion validate the effective MIME of resources marked image against the existing PNG/JPEG/GIF/WebP/AVIF/SVG allowlist, while preserving empty/unspecified and application/octet-stream MIME accepted by the original restore contract. Explicit incompatible types return the existing 415 unsupported_media_type error. Legacy restore includes the existing uploaded-file MIME fallback when metadata MIME is null. Image MIME comparison ignores casing and parameters without rewriting stored MIME metadata or parameters; non-image attachment MIME metadata and size limits keep their existing contracts. |
First add failing isolated API regressions for explicit HTML/XHTML, null metadata with an HTML file type, and multipart restore including an upload session created before the fix. Assert rejection without a new resource/object, preserving existing resources. Verify empty/null/binary image MIME still restores through both APIs and downloads; include null MIME in the released-exporter archive fixture. Preserve SVG encoding parameters and bytes through both restore paths. |
| S2 | Authenticated, public-note and public-notebook blob responses only use the image-specific inline branch when the effective response MIME is allowlisted. Existing malformed resources, including a null database MIME falling back to HTML object metadata, download instead. PDF/audio/video preview rules still use database MIME and filename, not object-metadata fallbacks; nosniff, SVG sandbox CSP, range handling and access checks stay unchanged. |
Seed isolated legacy rows and verify the real response headers through all three routes, including storage-metadata fallback and SVG/PDF/media previews. Untyped binary attachments must not gain inline previews solely from PDF/media object metadata. |
| S3 | Add only regressions and compatibility evidence needed for S1/S2. | Verify rejection and ordinary restore through the actual app/database/blob adapter; restore a valid archive produced by the previous released implementation, preserving note/resource data and bytes. Run affected tests, bun run typecheck, bun run typecheck:mobile and bun run build:web. |
| S4 | Parse nonempty restore MIME for both images and attachments with content-type in comma-stop mode, require full-field consumption, and require successful MIME formatting. Reject type lists, invalid media types or unserializable parameters with 415 unsupported_media_type before legacy object writes, multipart initialization or completion. Preserve existing null/empty/binary fallbacks and raw stored MIME metadata. Both resource response implementations share one Content-Type/disposition policy: serialize a single MIME with safe quoting; malformed/multi-valued database or storage metadata becomes application/octet-stream plus attachment disposition, even when a filename suggests PDF/media. Valid PDF/media preview classification still uses database MIME/filename, not storage-only MIME. SVG sandbox and nosniff apply after serialization. Add only the API dependency [email protected] and its lockfile update. |
Add failing real-route regressions for image/audio/video type lists, old pending uploads, all three blob routes and storage-MIME fallback. Cover legitimate quoted commas/semicolons, escaped quotes and SVG charset without rewriting stored data. Confirm in the local browser that malformed resources download and valid quoted SVG remains SVG; use harmless content without script execution. Reuse unaffected tests and run affected API tests, typechecks, Web build and Worker bundling. |
Unchanged boundaries
- No backup-format, schema, migration, authentication, API-token, permission, upload-size, retry or fallback changes beyond S1/S2/S4.
- Do not forbid HTML attachments, sanitize stored note bodies, delete existing resources, or operate on another user's instance/data.
- Deliver through focused commits and the existing contribution branch/PR #711 linked to #701. Keep the PR in Draft; no automatic ready transition, merge, deployment or release. Keep the original #657 checkout and its uncommitted changes untouched.
Completion check
- S1/S4: all three restore stages reject image/type mismatches, top-level MIME lists, invalid media types and illegal original header characters before object writes or assembly. Hidden/duplicate parameters cannot conceal controls. Null/empty/binary MIME and valid SVG charset/quoted parameters remain supported without rewriting stored metadata.
- S2/S4: both response implementations share one parser/serializer and inline decision; invalid database/storage MIME becomes binary download even with misleading PDF/media filenames. Valid metadata retains the existing PDF/media preview boundary, SVG sandbox,
nosniff, ranges and access checks. - Acceptance: 64 security/compatibility regressions pass. The other 87 affected archive/resource/share tests also pass and remain applicable, for 151 distinct tests. The released-exporter v2.12.0 fixture restores through legacy and resumable APIs with notes, revisions and attachment bytes preserved.
- Chromium acceptance: all three historical image/audio/video type-list samples emit successful binary download events; a quoted UTF-16LE SVG retains its Chinese text, and a quoted PNG decodes at its original width. Content is synthetic and script-free. Other browsers/native engines and live Cloudflare/R2 were not exercised.
- Main/mobile typechecks, Web build, Worker bundling, affected-file oxlint and
git diff --checkpass; frozen-lockfile installation succeeds. The only dependency change is[email protected]in the API, with a minimal lockfile entry. - Scope review: S4 closes the demonstrated browser-parser bypass and rejects raw illegal bytes that the lenient parser can ignore. Removed duplicated Content-Type/disposition decisions from the two handlers. Latest correctness, simplification and design-boundary review has no unresolved substantive findings within this scope.
- PR #711 remains Draft. No deployment or native-platform runtime verification was performed. The original #657 working files were not edited.