Skip to content

feat(browser-pay): recognise an Adyen Pay in any body format [SAP-5348] - #1327

Open
ratataque wants to merge 126 commits into
mainfrom
ewan/sap-5348-adyen-any-format
Open

ratataque wants to merge 126 commits into
mainfrom
ewan/sap-5348-adyen-any-format

Conversation

@ratataque

@ratataque ratataque commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What

Browser Pay now recognises an Adyen secured-fields Pay in any body format, not only JSON with a top-level paymentMethod.encryptedCardNumber. It finds the placeholder ciphertexts by hash wherever they sit in the raw body and sends the body to the relay byte for byte.

Format Merchant it came from
top-level JSON paymentMethod.encrypted* Holland & Barrett (Adyen sessions)
flat JSON keys EncryptedCardNumber, … Hawes & Curtis
JSON string inside a data field (JSON or urlencoded transport) AllSaints (SFCC)
urlencoded dwfrm_adyPaydata_adyenStateData (also multipart) Cotton On (SFCC)
urlencoded dwfrm_billing_creditCardFields_adyenStateData Pandora (SFCC)
urlencoded dwfrm_billing_adyenPaymentFields_adyenStateData lululemon (SFCC)

These formats were read from the merchants' page JS during live probes on 2026-11-24. Pay was never clicked on a real merchant, so they are inferred, not captured.

Why

The probes reached six Adyen Web card checkouts. Only one (Holland & Barrett) sends the shape Browser Pay recognised, so the other five would go on unrelayed and be declined. The goal is an Adyen checkout that works on the biggest Adyen merchants.

How to review

All the logic is in detect.ts:

  • BODY_TOKENS splits the raw body into base64url-and-dot runs, with each percent or JSON escape as its own token. A ciphertext's bytes are the same in JSON, in JSON inside a string, in form encoding and in multipart, so each one is a raw token in every format above.
  • compactJweTokens keeps the runs that are five segments whose first segment decodes to a JWE header naming alg and enc. Hostnames, versions and ids are never taken for ciphertexts.
  • scanBody answers one question: can the relay swap every ciphertext in place? Unescaping the body (JSON and percent escapes, layer by layer, up to 32 layers) must leave each raw ciphertext whole and reveal no other ciphertext, including in what is left once the raw copies are masked.
  • adyenPayCandidate: the body carries an observed ciphertext, or a ciphertext next to one of the four secured-field names. A body still encoded after 32 layers counts as a candidate too, so it is refused rather than sent on.

interceptor.ts decide() keeps the gate order: unsupported_encrypted_checkout, adyen_client_key, destination_not_allowed. After those, the body must hold exactly the four observed ciphertexts, raw and swappable, else adyen_placeholder_unobserved.

Tests: test/adyen-bodies.ts builds the synthetic bodies (shaped like stateData, with a checkoutAttemptId, riskData, a userAgent and a five-label returnUrl). interceptor.adyen.test.ts runs the relay and refusal matrix, and detect.test.ts covers the escape cases.

Visual change map, 11 scenes (review-only, not in the tree):

visual change map

Risk & rollout

  • Needs a relay change to pay. The Sapiom relay (checkAdyenRequest, swappedBody) still JSON-parses the body and swaps at the top-level paymentMethod pointers. Until it swaps by hash on the raw body, every format except top-level JSON is refused there with card_fields_invalid. That fails closed: no card data leaves, and the page request fails instead of going on with placeholder ciphertexts. A companion Sapiom branch that does this exists locally (ewan/sap-5180-adyen-any-format-relay). It isn't pushed: this follow-up is SDK-only. Before it ships it needs two more fixes:
    • Its raw card-number scan refuses about 1 Pay in 10, in any format, because Adyen Web's checkoutAttemptId (uuid + epoch ms + hex) sometimes holds a Luhn-valid digit run.
    • It needs the same JWE header check as this PR.
  • Stricter for today's format. A top-level JSON Pay that also carries an unobserved JWE, or a ciphertext that an escape hides or extends, now fails locally instead of reaching the relay.
  • Not in this PR:
    • per-tab Adyen state (Proof F3);
    • logging relay refusals through safeWarn (Proof F2);
    • recovered OOPIF tab ownership (J8GZ);
    • holding a response while its body is read (J8Gz);
    • Adyen Card with hideCVC (I85U; no probed merchant needs it);
    • an Adyen Pay that also carries a plaintext held placeholder PAN, which takes the generic relay as before (found in review; it predates this change).
  • Based on main: feat(browser-pay): pay Adyen secured-fields checkouts [SAP-5180] #1316 and feat(browser-pay): report Adyen's final result after 3-D Secure [SAP-5281] #1319 were merged into main at 08:26 Paris, so this PR targets main directly. Patch changeset for @sapiom/browser-pay.

Verification

  • packages/browser-pay: npx jest --maxWorkers=3, 12 suites, 373/373 passed, including the real-Chromium suites. tsc --noEmit, eslint and prettier --check are clean.
  • Local TEST round trip, run 3 at 937e450: a real Chromium page with Adyen Web 6.6.0 TEST secured fields, the companion relay build, and Adyen TEST. Sessions plus all 7 formats, 8/8 Authorised with a pspReference. In every format:
    • the relay request body has the same sha256 as the page-sent body;
    • the merchant gets the page's bytes, apart from the four ciphertexts, with cookie, CSRF and content-type unchanged.
    • (Relay-side exception: for top-level JSON the relay sets paymentMethod.brand to the vaulted card's brand, by design.)
    • Run 2 at b5443ec lost one case to the relay's checkoutAttemptId false positive above.
  • Replay of the nine top-10 fixtures (synthetic bodies modelled on each merchant's format): all nine are candidates with exactly four raw, swappable ciphertexts in the SDK.
  • Review: Copilot, CodeRabbit and Devin threads are fixed or answered. Two rounds of adversarial review: 6 reproduced findings, 5 fixed here, and the held-PAN path listed above. Final-diff review: in progress, results to follow.

Env vars used for the TEST run: ADYEN_API_KEY, ADYEN_CLIENT_KEY, ADYEN_MERCHANT_ACCOUNT from ~/.config/sapiom-dev/local.env, and Basis Theory TEST via ~/.config/sapiom-dev/run.

SAP-5348

Summary by CodeRabbit

  • New Features
    • Adyen secured-field payments now support JSON, URL-encoded, and multipart request formats, including nested and encoded fields.
    • Supported requests preserve their original body bytes during relay.
  • Bug Fixes
    • Requests are stopped locally when secured-field data is unreadable, unsupported, or cannot be safely matched to values observed during checkout.
  • Documentation
    • Updated Adyen payment guidance to describe supported formats and local failure conditions.

ratataque and others added 30 commits November 17, 2026 01:48
…ption (SAP-5180)

Adyen secured fields encrypt the typed card in Adyen's iframes, so the
placeholder never appears in the payment request and cannot be relayed.
The interceptor now holds a JSON request carrying
paymentMethod.encryptedCardNumber (merchant /payments or Adyen
/sessions/{id}/payments), asks POST /v1/transactions/{id}/payment/adyen/encrypt
for the authorized card's own encrypted fields, swaps the four encrypted*
fields and continues the request. Client key: secured-field iframe URL,
else page config.

Gated by the backend: its adyen_encryption_not_enabled answer, a relay
without adyenEncrypt, or disableAdyenEncryption fail the payment closed
(adyen_not_enabled); the stand-in ciphertext is never sent. fillCard takes
the handle as `adyen` and types nothing on Adyen secured fields unless the
backend returned fields (used once for the payment).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d Adyen stand-in (SAP-5180)

- isAdyenNotEnabled compares case-insensitively: the backend sends
  409 {"code":"ADYEN_ENCRYPTION_NOT_ENABLED"}, which surfaced as
  adyen_encrypt. Contract test feeds that body verbatim through
  httpPaymentRelay -> attachInterceptor -> fillCard.
- fillCard types Adyen's plain consumer test card for the card's brand
  (visa 4111.., mastercard 5555 4444.., amex 3700..) into secured fields
  instead of the placeholder number, whose random BIN can name a variant
  Adyen refuses for the real card (109 Invalid variant). Missing or other
  brand: blocked adyen_card_brand, backend not asked.
- prepareAdyen: a later answer drops earlier prepared fields, an older
  call finishing last cannot restore them, and after detach it reports
  adyen_not_enabled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s budget its siblings have (SAP-5180)

CI test (22.x) on 4f5cbfd hit Jest's 5s default on this Chromium test.
Like the other tests in the suite, it now closes the shop's keep-alive
sockets before close().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…M source names the same one (SAP-5180)

The DOM fallback returned the first key it found, so a stale #clientKey or
script literal could pick the key while a different one was also on the
page. It now collects keys from every secured-field iframe src, #clientKey,
data-client-key and each inline clientKey literal, and returns one only
when they agree. An undecodable iframe key is skipped instead of failing
the whole read. Keys may contain lower-case letters, as Adyen documents.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…yen's frames host (SAP-5180)

A page with idle Adyen secured-field iframes and a visible native or other
provider's card form got the brand's test card in that form, which the
interceptor does not replace. The stand-in now goes only into number
fields inside an Adyen secured-field frame; any other number field gets
the card's own number.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… and in-flight success (SAP-5180)

A not-enabled answer from the backend now clears prepared fields for every
client key and page origin, and bumps a denial count. A prepareAdyen whose
success lands after any denial answers not enabled and stores nothing, so
fillCard types nothing on a superseded prepare.

Regressions: cross-key denial drops cached fields; superseded prepare
answers disabled; fillCard racing a newer denial types nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(SAP-5180)
…twork requests (SAP-5180)

The interceptor records keys from https requests to exactly the
checkoutshopper test host or a live host (live, -us, -au, -apse, -in):
clientKey query, /securedfields/{key}/, /clientKeys/{key}, x-client-key.
It reads them from Network.requestWillBeSent, paused requests and
frame target URLs. test_ only from -test, live_ only from a live host.
Two distinct keys give none. The DOM fallback is deleted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…key before releasing a payment (SAP-5180)

- Key bodies may be any letters and digits again; the env-to-host rule stays.
- After re-encryption, a payment is released only if the attach still holds
  exactly the key it encrypted to; a second key seen meanwhile fails it
  closed with adyen_client_key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…er a newer not-enabled answer (SAP-5180)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ent keys (SAP-5180)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ient key (SAP-5180)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…5180)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…en (SAP-5180)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… into ewan/sap-5180-browser-pay-adyen-result
…whose session detached (SAP-5180)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…(SAP-5180)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…5281)

After RedirectShopper, IdentifyShopper or ChallengeShopper, watch the
same origin's /payments/details (or /sessions/{id}/paymentDetails) answer
and report its resultCode through the existing result endpoint. The page's
request goes on unchanged; the SDK never drives the challenge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…event (SAP-5180)

The two result-report warnings called the caller's logger unguarded, so a
throwing or rejecting logger skipped the adyen_result event and left an
unhandled rejection. Route them through the same guard as logStepFailure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
detach() can close CDP while Fetch.getResponseBody is pending; the failed
read then became an `unknown` report sent after detach() had returned.
Skip the report once the interceptor is closed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…completion (SAP-5180)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…result' into ewan/sap-5281-adyen-3ds

# Conflicts:
#	packages/browser-pay/README.md
…answer (SAP-5281)

An Adyen error or non-JSON details answer is reported as `unknown`, which
the backend refuses; dropping the origin then left a retried, final details
call unreported. Only a final code ends the watch now. README: the final
code is posted once per payment, an action code may come first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…-5180)

An answer already paused at the response stage was dropped when detach()
started while its body was read or its continue acknowledgement was in
transit, although the page received it. detach() now waits for those
answers to reach the page and be reported before it disables Fetch, so
the read is never cut short and the result is never lost.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…281-adyen-3ds

# Conflicts:
#	packages/browser-pay/README.md
#	packages/browser-pay/src/interceptor.ts
…nswer (SAP-5281)

The watch for the page's details call was set only after the answer's
Fetch.continueRequest returned. A details call that paused before that
acknowledgement was continued without interceptResponse and its final
result was never reported. Read the code and arm the watch first, then
hand the answer to the page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t (SAP-5281)

The 3-D Secure watch was keyed by origin alone, so a details call from
another tab on that origin, or from another Adyen session on Adyen's
shared host, was reported for the held payment and could clear the watch
before the real result came. Key the watch by the tab's page session and
the checkout: the origin, plus the session id for /sessions/{id} calls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…(SAP-5180)

A second detach() returned at once while the first still waited on the
Adyen result report, so a second Ctrl+C could close the browser first.
Every call now shares one shutdown. A tab attached during that shutdown
is resumed and released instead of waiting behind the report.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

Starting Devin Review.

Devin Review

@ratataque
ratataque requested a balanced review from Copilot November 24, 2026 08:16
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

devin-ai-integration[bot]

This comment was marked as resolved.

This comment was marked as resolved.

…the ciphertexts (SAP-5348)

One scan answers whether the relay can swap every ciphertext byte for byte:
unescaping the body must leave each raw ciphertext whole and reveal no other.
That replaces the hidden-hash set and closes the cases review found:

- an escape after a ciphertext that extends it (\u0041, %41, %2E);
- JSON escapes before a ciphertext (\n, \u0020) whose letters joined the run,
  now tokens of their own, so the ciphertext stays raw;
- nesting deeper than eight layers: inspection runs to 32 and a body still
  encoded after that is refused, never sent on;
- raw ciphertexts are masked in one pass, not one body scan per token.

A body is an Adyen Pay candidate when it carries an observed ciphertext, or a
ciphertext beside any of the four secured-field names. A field name alone,
such as a validation message, no longer is.

Synthetic stateData now carries a checkoutAttemptId, riskData and a dotted
userAgent. The changeset names the relay prerequisite.
@ratataque
ratataque requested a balanced review from Copilot November 24, 2026 08:31
@ratataque

Copy link
Copy Markdown
Contributor Author

/devin review

@ratataque

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@devin-ai-integration

Copy link
Copy Markdown
Contributor

Starting Devin Review.

Devin Review

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

devin-ai-integration[bot]

This comment was marked as resolved.

This comment was marked as resolved.

…y encoded form values (SAP-5348)

A Pay candidate is a body with an observed ciphertext, or one that gives any
of the four secured fields a value as a JSON member or form field. A mention
in text, such as a validation message, still continues; a non-JWE value is
caught again, as the README promises.

A percent escape of a percent escape (%2522) is one token, so a ciphertext
after a doubly encoded quote stays raw and swappable.
…nd a JWE with an empty key (SAP-5348)

Interrogate round 4: a form value with + for spaces, a bracketed form field and a multipart part
named for a secured field were not Pay candidates, so a Pay with unobserved ciphertexts went on to
the merchant. A direct-encryption JWE (empty encrypted key) was not a ciphertext, so an extra one
rode along with the four observed.
…es the text changed (SAP-5348)

Interrogate round 5. SECURED_FIELD now reads a secured name only as a key: a JSON member (bracketed
or dotted too), a form field, or a multipart Content-Disposition name. A validation message that
names a field, or holds name=, is not a Pay, and the scan is linear. A relayed body whose bytes
were not UTF-8 is refused (unreadable_body) rather than relayed with replacement characters.
…ltipart header starts a line (SAP-5348)

Interrogate round 4: a quoted name in a message is not a key; a bare form key counts as an empty field; the multipart header scan is linear.
@ratataque
ratataque marked this pull request as ready for review November 26, 2026 23:41
devin-ai-integration[bot]

This comment was marked as resolved.

coderabbitai[bot]

This comment was marked as resolved.

…escan blank lines for a secured field (SAP-5348)

A replacement character the page really sent no longer reads as lost bytes: the entries' bytes come before Chrome's decoded postData, and only a decoding that changed them is refused. A secured-field key at a line start skips spaces on that line only, so a run of blank lines is scanned once.
devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

This branch has not been deployed

No deployments
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.

2 participants