security: whitelist message types in content script relay#19
Conversation
Add an ALLOWED_TYPES whitelist to validate the type field from page events before forwarding to the background script. Previously, the type field came directly from the page with no validation, and the spread operator forwarded arbitrary fields, allowing a malicious page to invoke internal extension message handlers or inject unexpected data. Now only known NIP message types are forwarded, and only safe fields per message type are included in the forwarded message. Co-Authored-By: claude-flow <ruv@ruv.net>
There was a problem hiding this comment.
Pull request overview
This PR tightens the page→content-script→background relay in src/injected.js by introducing a message-type allowlist and by forwarding only per-type “safe” fields, to prevent arbitrary message type/field injection into extension internals.
Changes:
- Add an
ALLOWED_TYPESwhitelist to gate whichpodkey-requestmessagetypes are forwarded. - Introduce per-type field filtering (
safeData) instead of forwarding arbitrary fields via...data. - Return an immediate error to the page for non-allowlisted request types.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 'NIP04_DECRYPT', | ||
| 'NIP44_ENCRYPT', | ||
| 'NIP44_DECRYPT' |
| } else if ((type === 'NIP04_ENCRYPT' || type === 'NIP04_DECRYPT' || | ||
| type === 'NIP44_ENCRYPT' || type === 'NIP44_DECRYPT')) { |
| if (data.peer) safeData.peer = String(data.peer); | ||
| if (data.plaintext !== undefined) safeData.plaintext = String(data.plaintext || ''); | ||
| if (data.ciphertext !== undefined) safeData.ciphertext = String(data.ciphertext); |
|
Folded into #22 — a single CI-green, adversarially-reviewed PR that consolidates this branch with the other security fixes plus NIP-44 and residual gap-fixes (exact-host trust, NIP-98 nonce, signEvent self-verify, honest provider surface, auto-sign default OFF). Suggest reviewing/merging #22; this can be closed in its favour. |
|
DreamLab-AI Mega-Sprint seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
Consolidated hardening of the Podkey key-holding signer: brings seven in-flight security/feature branches plus residual gap-fills, a polished CSP-clean UI, a CI build/test/lint gate, and a 133-test suite into one reviewable PR. The fix set was independently re-derived from a full audit of the extension and every item below was verified fixed *in the final tree* by an adversarial review pass (not just claimed). **Supersedes** (folded in here; safe to close in favour of this PR): #12, #14, #17, #18, #19, #20, #21. ## Security posture — before → after | Area | Before | After | |---|---|---| | Consent | Auto-approve everything (`showPermissionPrompt` hard-returned `true`) | **Explicit per-origin consent** popup that blocks on the user's decision; closing or 60s timeout denies | | Key at rest | `storage.local` (persisted plaintext) | **`storage.session` only** (in-memory, cleared on browser close); raw key never crosses to the page | | Crypto | A fake `crypto-browser.js` in the tree (`pubkey = sha256(privkey)`) | Deleted; **real `@noble` crypto** only, with `signEvent` schnorr self-verify before return | | NIP-98 | Signed-event cache → byte-identical id → replayable | **Fresh per-token 16-byte nonce**, redirect-aware `u` tag, page-side body-hash binding | | Solid-host trust | Substring match (`inrupt.net.evil.com` accepted) | **Exact-host match**, case-insensitive; lookalikes rejected | | Interception | Double fetch/XHR interception + double 401-retry | **Single interception path**, one retry | | Message channel | Page could inject arbitrary fields / origin / `privateKey` | Content script **whitelists 4 message types, forwards only safe fields, enforces the real origin** | | Provider surface | Advertised `nip04`/`getRelays` that throw / return `{}` | **Honest**: `getPublicKey`, `signEvent`, `nip44.{encrypt,decrypt}` only | | UI | Inline scripts + `innerHTML` XSS sinks | **CSP `script-src 'self'`**; all DOM via `textContent`/`createElement` | | Auto-sign | Defaulted **ON** (first-run silent trust + NIP-98 mint) | Defaults **OFF** — silent trusted-origin Solid signing and NIP-98 auto-trust are strictly opt-in | ## Notes for review - **Consent timeout:** `approve.js` actively sends `approved:false` + closes at the 60s mark (one-shot `decisionSent` guard) so the visible countdown is truthful; the background `chrome.windows` 60s timer remains a backstop. If you'd prefer the popup stay passive and let the background own the timeout, that is the single behavioural line to reconsider — everything else on the UI is restyling. - The page-context `nip98-interceptor.js` still duplicates a couple of `auth-header-utils.js` helpers because page context can't ESM-import the module; intentional, tracked separately. ## Verification - `npm run build` ✓ · `npm test` → **133 pass / 0 fail** ✓ · `npm run lint` → **0 errors** (now `no-unused-vars: error`) ✓ - New `.github/workflows/ci.yml` runs build → test → lint on every PR and push to `main`. 🤖 Generated by Claude Code
Summary
ALLOWED_TYPESwhitelist (Set) to validate thetypefield from page events before forwarding to the background script insrc/injected.jstypefield directly from page events with no validation, and the spread operator (...data) forwarded arbitrary fields, allowing a malicious page to invoke internal extension message handlers (e.g.GENERATE_KEYPAIR,IMPORT_KEYPAIR) or inject unexpected data fieldsGET_PUBLIC_KEY,SIGN_EVENT,GET_RELAYS,NIP04_ENCRYPT,NIP04_DECRYPT,NIP44_ENCRYPT,NIP44_DECRYPTTest plan
window.nostr.getPublicKey()still works from a trusted pagewindow.nostr.signEvent()still works with valid event datatype: 'GENERATE_KEYPAIR'via podkey-request is rejectedtype: 'IMPORT_KEYPAIR'via podkey-request is rejectedCo-Authored-By: claude-flow ruv@ruv.net