Title: NIP-98 payload-hash check hashes re-serialized body, not raw — rejects valid non-minified JSON (401) · Issue #565 · JavaScriptSolidServer/JavaScriptSolidServer · GitHub
Open Graph Title: NIP-98 payload-hash check hashes re-serialized body, not raw — rejects valid non-minified JSON (401) · Issue #565 · JavaScriptSolidServer/JavaScriptSolidServer
X Title: NIP-98 payload-hash check hashes re-serialized body, not raw — rejects valid non-minified JSON (401) · Issue #565 · JavaScriptSolidServer/JavaScriptSolidServer
Description: Summary src/auth/nostr.js verifies the NIP-98 payload tag by hashing JSON.stringify(request.body) — i.e. the parsed-then-re-serialized (compact) body — instead of the raw request bytes the client actually signed. Any NIP-98 client that s...
Open Graph Description: Summary src/auth/nostr.js verifies the NIP-98 payload tag by hashing JSON.stringify(request.body) — i.e. the parsed-then-re-serialized (compact) body — instead of the raw request bytes the client a...
X Description: Summary src/auth/nostr.js verifies the NIP-98 payload tag by hashing JSON.stringify(request.body) — i.e. the parsed-then-re-serialized (compact) body — instead of the raw request bytes the client a...
Opengraph URL: https://github.com/JavaScriptSolidServer/JavaScriptSolidServer/issues/565
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"NIP-98 payload-hash check hashes re-serialized body, not raw — rejects valid non-minified JSON (401)","articleBody":"## Summary\n\n`src/auth/nostr.js` verifies the NIP-98 `payload` tag by hashing **`JSON.stringify(request.body)`** — i.e. the *parsed-then-re-serialized* (compact) body — instead of the **raw request bytes** the client actually signed. Any NIP-98 client that sends non-minified JSON (pretty-printed, or a different key order / string escaping than Node's `JSON.stringify` produces) fails with `Payload hash mismatch` → **401**, even though the signature, URL, method, and timestamp are all valid.\n\n## Where\n\n`src/auth/nostr.js`, the payload-hash block (~L244–262):\n\n```js\nconst payloadTag = getTagValue(event, 'payload');\nif (payloadTag && request.body) {\n let bodyString;\n if (typeof request.body === 'string') {\n bodyString = request.body;\n } else if (Buffer.isBuffer(request.body)) {\n bodyString = request.body.toString();\n } else {\n bodyString = JSON.stringify(request.body); // \u003c-- re-serializes the PARSED object (compact)\n }\n const expectedHash = crypto.createHash('sha256').update(bodyString).digest('hex');\n if (payloadTag.toLowerCase() !== expectedHash.toLowerCase()) {\n return { webId: null, error: 'Payload hash mismatch' };\n }\n}\n```\n\nWhen the JSON body parser turns the request into an object, `request.body` is that object, so the `else` branch runs `JSON.stringify(request.body)` → **compact** JSON. The client (per NIP-98) hashed the **exact body bytes it sent**. If those bytes were pretty-printed (`JSON.stringify(x, null, 2)`) — or just serialized by a different implementation — the two hashes differ.\n\n## Repro\n\n`PUT` a WAC-protected resource with NIP-98 auth and a pretty-printed JSON body:\n\n```\nPUT /…/pool/entries/\u003cpubkey>.json\nAuthorization: Nostr \u003csigned kind-27235 event, payload = sha256(prettyBody)>\nContent-Type: application/json\nBody: JSON.stringify(entry, null, 2)\n```\n\n→ `401`. Server log:\n\n```\n{\"level\":40,\"authError\":\"Payload hash mismatch\",\"method\":\"PUT\",\"urlPath\":\"/…/pool/entries/\u003cpubkey>.json\",\"hasAuth\":true,\"msg\":\"Auth error\"}\n```\n\nSending the **same** entry as compact `JSON.stringify(entry)` succeeds — because it happens to match the server's re-serialization. That's a brittle coincidence, not correctness.\n\n## Impact\n\n- NIP-98 writes only work if the client's body is **byte-identical to Node's `JSON.stringify(parsed)`** — i.e. minified, with Node's exact key order and escaping. Pretty-printed bodies, or bodies from other serializers, always 401.\n- It silently pushes every NIP-98 client toward \"send minified JSON,\" which isn't a real spec requirement and is easy to get wrong. (We hit this from a browser; worked around it by forcing compact `JSON.stringify`.)\n- DPoP/Solid writes are unaffected (no payload-hash check), so the bug only bites Nostr auth — making it look like \"NIP-98 writes are broken\" when the signature is actually fine.\n\n## Proposed fix\n\nHash the **raw request body bytes**, never a re-serialization. NIP-98's `payload` tag is `sha256(request body)` over the bytes on the wire. Options:\n\n- Capture the raw body (e.g. a Fastify content-type parser that stashes the original buffer/string, or `fastify-raw-body`) and hash that.\n- Then `bodyString` is the original bytes regardless of pretty/compact/escaping, and any conformant NIP-98 client verifies.\n\nKeep the existing string/Buffer branches as-is; only the `else { JSON.stringify(...) }` fallback is wrong — there should be no fallback that re-serializes the parsed object.\n\n## Notes\n\n- Found while onboarding a Nostr guest key (xlogin `guestLogin`) writing a pool entry from a browser; the signature/URL/method all verified, only the payload hash failed.","author":{"url":"https://github.com/melvincarvalho","@type":"Person","name":"melvincarvalho"},"datePublished":"2026-06-14T11:09:44Z","url":"https://github.com/JavaScriptSolidServer/JavaScriptSolidServer/issues/565"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f30661f7-1cd7-775b-269e-bee608bac02b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BE34:50BE3:3061B3C:3FF9A80:6B03B1B8 |
| html-safe-nonce | c5528ddea92f059a9df03b14db17d0534df2573c34eb72c36026e14841793698 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRTM0OjUwQkUzOjMwNjFCM0M6M0ZGOUE4MDo2QjAzQjFCOCIsInZpc2l0b3JfaWQiOiIxNjk2MzA0MDk0NjA3ODk2ODgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 109479d15c29864b106d6270d38cac9b6af0360c487aee45831e64e0f9dd5845 |
| hovercard-subject-tag | issue:4658872459 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/JavaScriptSolidServer/JavaScriptSolidServer/565/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4ecf0a18cf13a3616885f8f53af701f545903d41e3c8c84584dd04483d0f7fe2/JavaScriptSolidServer/JavaScriptSolidServer/issues/565 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4ecf0a18cf13a3616885f8f53af701f545903d41e3c8c84584dd04483d0f7fe2/JavaScriptSolidServer/JavaScriptSolidServer/issues/565 |
| og:image:alt | Summary src/auth/nostr.js verifies the NIP-98 payload tag by hashing JSON.stringify(request.body) — i.e. the parsed-then-re-serialized (compact) body — instead of the raw request bytes the client a... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | melvincarvalho |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/JavaScriptSolidServer/JavaScriptSolidServer git https://github.com/JavaScriptSolidServer/JavaScriptSolidServer.git |
| octolytics-dimension-user_id | 205442424 |
| octolytics-dimension-user_login | JavaScriptSolidServer |
| octolytics-dimension-repository_id | 958025407 |
| octolytics-dimension-repository_nwo | JavaScriptSolidServer/JavaScriptSolidServer |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 958025407 |
| octolytics-dimension-repository_network_root_nwo | JavaScriptSolidServer/JavaScriptSolidServer |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width