feat(c2pa-wasm): add fromBytes verification entry point for non-browser runtimes - #148
Open
SilyNoMeta wants to merge 1 commit into
Open
feat(c2pa-wasm): add fromBytes verification entry point for non-browser runtimes#148SilyNoMeta wants to merge 1 commit into
SilyNoMeta wants to merge 1 commit into
Conversation
Wrap the asset bytes in an in-memory Cursor and reuse the existing from_stream helper, so verification works without Blob/FileReaderSync on Node.js, Deno, Bun, and Cloudflare Workers (workerd).
🦋 Changeset detectedLatest commit: 11a221e The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Collaborator
|
For node-based workers, why not use https://github.com/contentauth/c2pa-js/tree/main/packages/c2pa-node ? |
Author
|
Good call, i actually dug into that one before going the wasm route! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR: Add
WasmReader.fromBytesfor non-browser C2PA verification - Refs #147Problem
WasmReadertoday exposes onlyfromBlob/fromBlobFragment. Both wrap the asset in aBlobStream(packages/c2pa-wasm/src/stream/blob_stream.rs) which reads bytes throughweb_sys::FileReaderSync— aWorkerGlobalScope-only Web API. As a result, verification is onlyusable inside a browser Web Worker. It cannot run in server/edge JavaScript runtimes that have no
Blob/FileReaderSync/Worker:Workerconstructor, noFileReaderSync.These runtimes already have the asset bytes in memory (an upload, a
fetchresponse, a KV/R2object), so requiring a
Blob+FileReaderSyncround-trip is both a portability blocker and,per #93, a per-read JS↔WASM boundary cost.
Solution
Add a sibling entry point that takes the bytes directly:
It reuses the existing private
from_stream(format, impl Read + Seek + Send, context_json)helper (the same path
fromBlobuses), feeding it astd::io::Cursor<Vec<u8>>instead of aBlobStream. No new dependency, no browser API, and the returnedWasmReaderand itsvalidation results are identical to
fromBlob.Cursor,Read, andSeekare already imported.Runtimes unblocked
Node.js, Deno, Bun, and Cloudflare Workers (workerd). Verified end-to-end on workerd (via
wrangler dev) and Node.js with the pure-Rust crypto build (rust_native_crypto): a validasset validates (signature + hash bindings), an asset with no C2PA errors with
JumbfNotFound, atampered signature yields
claimSignature.mismatch(Invalid), and a tampered asset yieldsassertion.dataHash.mismatch(Invalid) — byte-identical between Node and workerd.getRandomValuesresolves via
globalThis.cryptoon workerd; noFileReaderSync/Blob/Workeris touched.Tests
Adds two
wasm_bindgen_tests inwasm_reader.rsthat run in the default (non-worker) context(unlike the
BlobStreamtests, which requirerun_in_dedicated_worker) — this is itself the proofthat
fromBytesneeds no Web Worker:from_bytes_reads_active_manifest— a signed JPEG (tests/fixtures/C.jpg) yields an active manifest.from_bytes_without_manifest_errors— a buffer with no C2PA data is rejected, not silently accepted.Notes
wasm_reader.rs/blob_stream.rs, so arebase may be needed depending on merge order. The changes are complementary: fix: eliminate repeated JS/WASM boundary crossings in BlobStream #137 optimizes the
Blob path; this adds a Blob-less path. (fix: eliminate repeated JS/WASM boundary crossings in BlobStream #137 already reads the whole Blob into a
Cursorfor≤50 MB assets, so the
bytes → Cursorprimitive is a natural fit.)c2pa-webruns the reader inside a Web Worker;the value of
fromBytesis for consumers that bypass that Worker. Exposing it through thec2pa-webWorker RPC (reader_fromBytes) can be a follow-up.WasmReader::from_streamcurrently uses the deprecatedReader::from_stream_async; this PR reusesthat existing helper rather than introducing new deprecation. Migrating the helper to
Reader::from_context(ctx).with_stream_asynccan be a separate cleanup.Checklist (contributor)
.changeset/add-wasm-reader-frombytes.md, minor bump for@contentauth/c2pa-wasm)nx test c2pa-wasmgreen in CI (headless Chromium)