From e0f6a061b42516cf4856f1debe774a45e7303c4c Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 26 Apr 2026 21:57:54 -0500 Subject: [PATCH] Tighten nostr-security skill (321 -> 152 lines) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removes redundancy without losing substance: - Drops the verbatim CSP meta tag (already in index.html) in favor of a prose summary of what's enforced and the style-src caveat. - Collapses the 'three attack surfaces' table of contents; section headers already serve that purpose. - Merges the §2 duplicate 'addressable events' example (covered by the bullet rules and the spoof example above). - Consolidates the 'safe pattern' + 'mitigation' + 'rule of thumb' triad in §2 into one example and one guideline sentence. - Tightens the NIP-72 walkthrough by removing narrative duplication between the prose and the code comments. - Drops the 'Remember: decode npub' aside (nip19-routing skill's job). The pre-merge checklist, the sanitizeUrl/sanitizeCssString helpers, the spoof-vs-authors example, and the full NIP-72 query sequence are all preserved verbatim. --- .agents/skills/nostr-security/SKILL.md | 293 ++++++------------------- 1 file changed, 62 insertions(+), 231 deletions(-) diff --git a/.agents/skills/nostr-security/SKILL.md b/.agents/skills/nostr-security/SKILL.md index febf889..1b7951b 100644 --- a/.agents/skills/nostr-security/SKILL.md +++ b/.agents/skills/nostr-security/SKILL.md @@ -1,321 +1,152 @@ --- name: nostr-security -description: Threat model and defenses for a web Nostr client — why XSS is catastrophic when nsec keys live in localStorage, how CSP provides defense-in-depth, URL and CSS sanitization for untrusted event data, and author filtering for trust-sensitive queries (admin actions, moderators, addressable events, NIP-72 communities). Load when building trust-boundary features, rendering user-controlled URLs or markup, interpolating event data into CSS, or reviewing the app's security posture. +description: Threat model and defenses for a web Nostr client — why XSS is catastrophic when nsec keys live in localStorage, CSP as defense-in-depth, URL and CSS sanitization for untrusted event data, and author filtering for trust-sensitive queries (admin actions, moderators, addressable events, NIP-72 communities). Load when building trust-boundary features, rendering user-controlled URLs or markup, interpolating event data into CSS, or reviewing the app's security posture. --- # Nostr Security -## The threat model you need to internalize +## Threat model -**Nostr private keys (`nsec`) are stored in plaintext in `localStorage`.** Any JavaScript running on the page origin can read them with a single line: +**Nostr private keys (`nsec`) are stored in plaintext in `localStorage`.** Any JavaScript running on the origin can read them with `localStorage.getItem('nostr-login')`. A successful XSS = instant, silent, irreversible key theft — no rotation, no revocation, permanent impersonation across every Nostr client the user ever touches. External signers (NIP-07 extension, NIP-46 bunker) don't change this: an XSS can still ask the active signer to sign arbitrary events, drain funds via zaps, or scrape DMs as they decrypt. -```js -localStorage.getItem('nostr-login'); -``` +**Treat every piece of untrusted data as a script-injection vector** — event tags, `content`, metadata, URL params, relay responses. -A successful XSS on any page of the app = instant, silent, irreversible key theft. There is no rotation mechanism, no session revocation, no server-side audit trail. Once an attacker has the `nsec`, they can impersonate the user forever across every Nostr client and relay in existence. +## Defense-in-depth -**Every security practice in this skill exists to make XSS harder to achieve.** The stakes for a Nostr client are substantially higher than for a typical web app — treat every bit of untrusted data (event tags, `content`, metadata fields, URL parameters, relay responses) as a potential script injection vector. +**Content Security Policy.** `index.html` ships a restrictive CSP: `default-src 'none'`, `script-src 'self'` (no inline scripts, no `eval`), `base-uri 'self'`, `connect-src 'self' https: wss:`. The one intentional gap is `style-src 'unsafe-inline'` — required by Tailwind/shadcn — which means **CSS injection is not blocked by CSP; sanitization is on you**. When modifying CSP, only narrow it. Never add `'unsafe-eval'`, `'unsafe-inline'` on `script-src`, `http:`, or wildcard sources. -NIP-07 browser extensions and NIP-46 remote signers keep the key material outside the web app's origin, but they don't change this threat model: a successful XSS can still ask an active signer to sign arbitrary events, drain funds via zap requests, publish malicious content under the user's identity, or scrape plaintext DMs as they're decrypted. The mitigation story is the same regardless of login method — make XSS hard, and filter trust-sensitive queries by author. - -## Defense-in-depth layers - -### Layer 1: Content Security Policy - -mkstack ships a CSP meta tag in `index.html`: - -```html - -``` - -What this buys you: - -- **`script-src 'self'`** — no inline `