Repository navigation
Fix verifyAction to verify the raw request bytes - #1552
Open
jolinger-godaddy wants to merge 1 commit into
Open
jolinger-godaddy wants to merge 1 commit into
jolinger-godaddy wants to merge 1 commit into
Conversation
verifyAction re-serialized a parsed body with JSON.stringify, read the query from the framework's parsed object, accepted only base64 DER keys, and enforced the age of a timestamp the signature doesn't cover. - Verify over the raw body bytes and the body's own length, building the canonical form exactly as the platform signs it (raw query decoded, byte-order sort, first value per key; no blank line without headers). A parsed body is refused with RAW_BODY_REQUIRED. - Express createActionMiddleware reads req.rawBody or a Buffer req.body and signs the full path the app received. - Accept PEM as well as base64 DER keys; refuse anything but ECDSA P-256 with INVALID_PUBLIC_KEY. - Stop checking the timestamp age for signature version 1.0, which does not sign it; deprecate maxTimestampAgeSeconds and document the replay limitation. - Never log the canonical string, the body or the key.
🦋 Changeset detectedLatest commit: cd158ef The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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.
Summary
verifyActiondidn't check the signature over the bytes the platform signs, and one of its checks gave a false sense of safety. This fixes it in place. It adds no new modules.Raw body, not a re-serialized one. A parsed body was turned back into a string with
JSON.stringify. That only matches the signed bytes when the sender happened to produce the same key order, spacing and escapes. The signature is now checked over the raw body bytes (Buffer, Uint8Array, ArrayBuffer or string), andcontent-lengthcomes from the body's own length. A parsed object is refused withRAW_BODY_REQUIREDinstead of being guessed at. A request with no body (including Express 4's{}placeholder whencontent-lengthis 0) still verifies.The rest of the canonical form now matches the signer byte for byte:
url, percent-decoded (+as a space), sorted by key in byte order, with the first value for each key.localeCompareand Express's parsedreq.query(arrays, nested keys) could give a different string.createActionMiddlewaresigns the full path the app received (originalUrl), so a route under a mounted router verifies. It readsreq.rawBody(from anexpress.json({ verify })hook) or a Bufferreq.bodyfromexpress.raw().Keys. The public key can be a PEM
-----BEGIN PUBLIC KEY-----block (escaped\nallowed, as env vars often hold it) or base64 DER, as before. Only ECDSA P-256 is accepted. RSA, Ed25519, other curves, private keys, unparseable keys and a missing key all returnINVALID_PUBLIC_KEY, with a reason that never includes the key. A missing key used to come back asINVALID_SIGNATURE. The algorithm header must still beECDSA-P256-SHA256.Timestamp. Signature version 1.0 doesn't sign
x-godaddy-signature-timestamp. Checking its age gave no protection, because anyone replaying a request can rewrite the header. The check is removed for 1.0 andmaxTimestampAgeSecondsis deprecated (it has no effect). The header is still required. Version 1.1 or any other version is still refused: how a timestamp would be signed isn't specified yet, so this PR doesn't guess at it. The README now explains the replay limitation and recommends idempotent writes.Logging. The canonical string was printed with
console.logon every request, which included the body. That's removed, along with the debug logs of headers, signature and canonical string. Errors log a reason, never the key or body.Compatibility
This is a behaviour change for apps that pass a parsed body:
app.use(express.json())need to keep the raw body:express.json({ verify: (req, _res, buf) => { req.rawBody = buf; } }), orexpress.raw({ type: 'application/json' })on the action route.Buffer.from(await request.arrayBuffer())instead ofawait request.json().Without that, verification fails with a clear
RAW_BODY_REQUIREDinstead of failing intermittently withINVALID_SIGNATURE. Re-serializing can't be made correct in general, so there is no fallback.RawBodyRequiredErrorandInvalidPublicKeyErrorare new exports, so this is a minor.Changeset
minorfor@godaddy/app-connect, with the migration stepsTest Plan
New unit tests in
src/utils/verification.test.tsandsrc/express/action.test.ts:GODADDY_PUBLIC_KEYRAW_BODY_REQUIRED), including when there is nocontent-lengthINVALID_PUBLIC_KEY, key never echoed)maxTimestampAgeSeconds: 1still verifies; version 1.1 and other algorithms are refusedreq.rawBody,express.raw(), a mounted router, a parsed-only body (401RAW_BODY_REQUIRED) and a tampered bodypnpm --filter @godaddy/app-connect test(62 passing),typecheck,lintandbuildpass.