fix(agent-core): make auth token module browser-import-safe#18
Merged
Conversation
`@tangle-network/agent-core`'s root re-exports the auth token module, so it lands in browser bundles transitively (e.g. via `@tangle-network/sdk-telemetry`). It referenced `Buffer` at module-eval time (top-level `JWT_HEADER` / `JWT_HEADER_EDDSA`) and statically imported `node:crypto`, so any consuming SPA boot-crashed with `ReferenceError: Buffer is not defined`. - base64url encode/decode is now isomorphic (`btoa`/`atob` + `TextEncoder`/ `TextDecoder`), never `Buffer` - the HS256 and EdDSA JWT headers are computed lazily on first use - HMAC/Ed25519 signing, verification, and key generation resolve `node:crypto` on demand via `process.getBuiltinModule` (server-only) Token wire format is unchanged; 418 tests pass incl. a new browser-import-safety suite. Server-only signing/verification stay synchronous.
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.
Problem
@tangle-network/agent-core's root (src/index.ts) re-exports the auth token runtime from./auth/index.js. That module referencedBufferat module-eval time:and statically imported
node:crypto. So any browser bundle that transitively imports agent-core — e.g.platform-web → … → @tangle-network/sdk-telemetry → @tangle-network/agent-core(root) — boot-crashed withReferenceError: Buffer is not defined(React never mounts, blank SPA). This is the agent-core sibling of the@tangle-network/sandboxfix (tangle-network/agent-dev-container#2991).Fix (server signing stays synchronous; only importing is now browser-safe)
btoa/atob+TextEncoder/TextDecoderinstead ofBuffer(works in Node and browsers).node:crypto— HMAC, Ed25519sign/verify,generateKeyPairSync,randomBytes,timingSafeEqualresolve viaprocess.getBuiltinModule("node:crypto")(Node ≥ 22.3; server-only). In a browserglobalThis.processis undefined, so a stray call fails loud with a clear server-only error instead of crashing at import.Token wire format is byte-identical — already-issued tokens still verify.
./auth/browserandtokens-browser.tsare unchanged.Validation
pnpm --filter @tangle-network/agent-core test→ 418 pass (17 files), incl. a newtests/auth/browser-safety.test.tsexercising HMAC read-tokens, Ed25519 sidecar tokens, and API-key hashing withglobalThis.Bufferdeleted.check-typesclean ·tsdownbuild clean · builtdist/auth/index.jshas 0 top-levelBuffer, 0 eagernode:cryptoimports.Bufferpolyfill, rebuilt, and headless-loaded the SPA (dev-browser) → React mounts, sign-in page renders, 0 console errors, no "Buffer is not defined". (Before this fix, the same headless test still crashed on agent-core.)Downstream
Unblocks reverting the platform-web
Bufferpolyfill (agent-dev-container #2987 / #2991): once this releases as@tangle-network/agent-core@0.4.1, the adc override bumps0.4.0 → 0.4.1alongside thetcloud@0.4.14bump, and the polyfill can be removed.