Commit e31293b
authored
fix(nostr): NIP-98 payload hash verifies raw request bytes, not a re-serialization — closes JavaScriptSolidServer#565 (JavaScriptSolidServer#573)
* fix(nostr): NIP-98 payload hash verifies raw request bytes, not a re-serialization (JavaScriptSolidServer#565)
src/auth/nostr.js hashed JSON.stringify(request.body) — the parsed-
then-recompacted body — to check the NIP-98 `payload` tag. NIP-98
defines that tag as sha256 over the EXACT body bytes on the wire, so
any client sending pretty-printed (or differently-escaped / different
key-order) JSON 401'd with 'Payload hash mismatch' despite a valid
signature, URL, method, and timestamp. It silently forced every
NIP-98 client to send minified JSON in Node's exact serialization —
not a real spec requirement, and the bug only bit Nostr auth (DPoP
has no payload check), making valid signatures look broken.
Root cause: by the time the auth check runs, Fastify's default
application/json parser has already turned the body into an object —
the raw bytes are gone, so the re-serialization was the best the code
could do, and it only matched by coincidence when the client minified.
Fix (two halves):
- server.js overrides the application/json content-type parser to
capture the raw bytes as request.rawBody before parsing. It
otherwise mirrors Fastify 4's defaultJsonParser exactly — empty
body → 400, secure-json-parse (the SAME prototype-pollution
protection JSS gets today; not plain JSON.parse), 400 on malformed
— so no other request path changes. secure-json-parse promoted to
a direct dependency for this.
- nostr.js hashes request.rawBody when present, never a
re-serialization. Other content types are unaffected: they stay
Buffers via the '*' parser and already hashed raw bytes correctly;
only application/json (parsed to an object) was broken.
Tests (test/nip98-payload-hash.test.js, 6):
- parser: pretty JSON accepted, malformed → 400, __proto__ → 400 +
Object.prototype not polluted (proves secure parsing preserved)
- payload hash: a pretty body with rawBody passes AND resolves to
the did:nostr identity; the same body WITHOUT rawBody still
mismatches (control — proves rawBody is the fix); a compact body
still verifies (no regression).
Full suite: 968/968 (the global JSON-parser override regressed nothing).
Closes JavaScriptSolidServer#565.
* review fix (JavaScriptSolidServer#573): hash Buffer bodies directly, not via a lossy toString()
Copilot caught a real correctness bug in the pre-existing Buffer
branch: `request.body.toString()` UTF-8-decodes the bytes, which
mangles binary / non-UTF-8 bodies (an image PUT, etc.) — so the
server's hash wouldn't match the raw-byte sha256 a NIP-98 client
signed. crypto.update() accepts a Buffer directly, so we now pass the
Buffer through unconverted and only ever hash native forms (string →
UTF-8, Buffer → raw bytes).
New test: a deliberately non-UTF-8 Buffer body (with a sanity check
that it's genuinely lossy under a UTF-8 round-trip) verifies against
its raw-byte payload hash — would have failed under the old
.toString() path. 7/7 in the file; full suite green.
* review pass-2 fix (JavaScriptSolidServer#573): hash-check payload for falsy-parsed bodies too
Copilot pass-2 (separate from the Buffer fix): the
`if (payloadTag && request.body)` guard skipped payload validation
when a JSON body parsed to a falsy value (null/false/0/""), so a
client could sign a payload tag, send a falsy body, and the integrity
check silently didn't run. Guard on `request.body !== undefined`
instead — Fastify leaves body undefined only when no body was sent —
so falsy-but-present bodies are still verified while genuinely
bodyless requests stay skipped.
New test: a `false` body with a WRONG payload tag is now rejected
(was skipped), and with the correct tag passes. 8/8 in file; full
suite green.
* review pass-3 fix (JavaScriptSolidServer#573): empty-JSON error carries FST_ERR_CTP_EMPTY_JSON_BODY code
Copilot pass-3: the custom parser's empty-body 400 was a plain Error
with no err.code, but JSS's error handler emits code: err.code, so the
response shape drifted from Fastify's default (which uses
FST_ERR_CTP_EMPTY_JSON_BODY) — undercutting the 'mirrors the default
exactly' claim. Set err.code = 'FST_ERR_CTP_EMPTY_JSON_BODY' with the
same message/status. The malformed-JSON path already matched (Fastify
sets statusCode on the raw parser error without an FST code; so do we)
— documented that in a comment so it doesn't look like an oversight.
New test: an empty application/json body returns 400 with
code === 'FST_ERR_CTP_EMPTY_JSON_BODY'. 9/9 in file; full suite green.1 parent d7617cc commit e31293b
5 files changed
Lines changed: 254 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
243 | 243 | | |
244 | 244 | | |
245 | 245 | | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
250 | 268 | | |
251 | | - | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
252 | 273 | | |
253 | | - | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
254 | 278 | | |
255 | 279 | | |
256 | | - | |
| 280 | + | |
257 | 281 | | |
258 | 282 | | |
259 | 283 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
269 | 270 | | |
270 | 271 | | |
271 | 272 | | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
272 | 307 | | |
| 308 | + | |
| 309 | + | |
273 | 310 | | |
274 | 311 | | |
275 | 312 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
0 commit comments