Skip to content

Fix verifyAction to verify the raw request bytes - #1552

Open
jolinger-godaddy wants to merge 1 commit into
godaddy:mainfrom
jolinger-godaddy:fix-verify-action-raw-body
Open

jolinger-godaddy wants to merge 1 commit into
godaddy:mainfrom
jolinger-godaddy:fix-verify-action-raw-body

Conversation

@jolinger-godaddy

Copy link
Copy Markdown

Summary

verifyAction didn'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), and content-length comes from the body's own length. A parsed object is refused with RAW_BODY_REQUIRED instead of being guessed at. A request with no body (including Express 4's {} placeholder when content-length is 0) still verifies.

The rest of the canonical form now matches the signer byte for byte:

  • The query is read from the raw query string in url, percent-decoded (+ as a space), sorted by key in byte order, with the first value for each key. localeCompare and Express's parsed req.query (arrays, nested keys) could give a different string.
  • A request without signed headers no longer gets an extra blank line.
  • Header values that aren't visible ASCII are left out, as the signer leaves them out.
  • The Express createActionMiddleware signs the full path the app received (originalUrl), so a route under a mounted router verifies. It reads req.rawBody (from an express.json({ verify }) hook) or a Buffer req.body from express.raw().

Keys. The public key can be a PEM -----BEGIN PUBLIC KEY----- block (escaped \n allowed, 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 return INVALID_PUBLIC_KEY, with a reason that never includes the key. A missing key used to come back as INVALID_SIGNATURE. The algorithm header must still be ECDSA-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 and maxTimestampAgeSeconds is 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.log on 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:

  • Express apps using only app.use(express.json()) need to keep the raw body: express.json({ verify: (req, _res, buf) => { req.rawBody = buf; } }), or express.raw({ type: 'application/json' }) on the action route.
  • Next.js and direct callers pass Buffer.from(await request.arrayBuffer()) instead of await request.json().
  • TanStack Start needs no change.

Without that, verification fails with a clear RAW_BODY_REQUIRED instead of failing intermittently with INVALID_SIGNATURE. Re-serializing can't be made correct in general, so there is no fallback. RawBodyRequiredError and InvalidPublicKeyError are new exports, so this is a minor.

Changeset

  • Changeset added (docs), minor for @godaddy/app-connect, with the migration steps

Test Plan

New unit tests in src/utils/verification.test.ts and src/express/action.test.ts:

  • a real signature from the platform's signer verifies over the raw body, with the key as base64 DER, as PEM, and from GODADDY_PUBLIC_KEY
  • the canonical form matches the signer's reference cases (content-length first, GET without a body, sorted query), plus raw-query decoding, byte-order sort, first value wins, and exact body bytes
  • valid requests with a Buffer, string or ArrayBuffer body, a PEM with escaped newlines, an empty POST and a GET
  • refused: wrong key, tampered body, reformatted JSON, wrong path, another store, changed query
  • refused: a parsed body or a missing body (RAW_BODY_REQUIRED), including when there is no content-length
  • refused keys: P-384, secp256k1, RSA, Ed25519 (PEM and DER), a private key, garbage, none (INVALID_PUBLIC_KEY, key never echoed)
  • version 1.0 with a year-2000 timestamp and maxTimestampAgeSeconds: 1 still verifies; version 1.1 and other algorithms are refused
  • nothing logged contains the body, the canonical string or the key
  • Express middleware: req.rawBody, express.raw(), a mounted router, a parsed-only body (401 RAW_BODY_REQUIRED) and a tampered body

pnpm --filter @godaddy/app-connect test (62 passing), typecheck, lint and build pass.

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.
@jolinger-godaddy
jolinger-godaddy requested a review from a team as a code owner December 3, 2026 21:51
@changeset-bot

changeset-bot Bot commented Dec 3, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: cd158ef

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@godaddy/app-connect Minor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant