Skip to content

feat: support DPoP (RFC 9449) - #1870

Open
stomita wants to merge 22 commits into
mainfrom
feat/dpop
Open

stomita wants to merge 22 commits into
mainfrom
feat/dpop

Conversation

@stomita

@stomita stomita commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1867

Starting 2027-03-30, Salesforce will block installing connected apps and external client apps into other orgs if they do not enable and lock PKCE, refresh token rotation, and at least one proof-of-possession (PoP) control (a refresh token IP allowlist or DPoP); see Security Change: PKCE, Refresh Token Rotation, and Proof-of-Possession. Existing connections keep working. So that apps choosing DPoP can keep using Salesforce APIs through jsforce, this PR adds support for DPoP as defined in RFC 9449.

Goals

  • Let apps that require DPoP authorize and call Salesforce APIs through jsforce, the same way they do today.
  • Keep current usage unchanged. DPoP is used only when a signer is provided.

Scope

  • DPoP with ES256 as the only signing algorithm.
  • DPoP applies to obtaining, refreshing and revoking tokens, and to API requests:
    • REST-family APIs such as REST, Composite, Tooling, Apex REST, Bulk 2.0 and identity.
    • SOAP-family APIs: SOAP, Metadata and Apex SOAP.
  • Bulk 1 and Streaming are out of scope. In a scratch org with DPoP enabled, at the time of this post:
    • Bulk 1 rejected every way of sending a DPoP token (the DPoP or Bearer scheme in Authorization, or X-SFDC-Session, each with a proof) with InvalidSessionId, on API versions 31.0, 45.0, 62.0 and 68.0. REST and Bulk 2.0 worked with the same token at the same moment.
    • The Streaming API (CometD) handshake was denied with a DPoP token. This also covers Change Data Capture and platform events received through it.

Public API

  • A dpop option on the OAuth2 config takes { signer }. The OAuth2 instance holds a DPoP instance made from it in oauth2.dpop, and a Connection using that OAuth2 sends its requests with DPoP.
  • Signing is delegated to the signer, so users can choose how to hold their keys and never have to take a private key out of a KMS or HSM. Its type DPoPSigner is exported.
export type DPoPSigner = {
  readonly algorithm: 'ES256';
  readonly format: 'jose' | 'der';
  readonly publicJwk: JsonWebKey;
  sign(signingInput: Uint8Array): Promise<Uint8Array>;
};
  • The DPoP class is exported:
    • DPoP.createSigner(key) creates a signer from a CryptoKeyPair or a PKCS#8 PEM P-256 private key. It works in both Node and the browser with no extra dependency.
    • createProof({ method, url, accessToken, nonce }) creates a proof with the signer.
  • Signatures are accepted in either JOSE format (r and s concatenated, 64 bytes) or DER, as declared by format, so KMS keys can be used too.
  • requestToken(), refreshToken() and authenticate() return the token response as before, with extra.proofNonce added: the nonce received while obtaining the token. extra is added by jsforce and is not part of the response from Salesforce.
  • Connection#proofNonce holds the nonce that API requests need (see "Nonce" below). It can be passed in the Connection config, next to accessToken.

Usage

Once the signer is passed, authorization and API calls are written the same way as today.

In Node, with a key stored as PEM:

import fs from 'node:fs';
import jsforce from 'jsforce';

const pem = fs.readFileSync('dpop-key.pem', 'utf8'); // storing the key is up to the user
const signer = await jsforce.DPoP.createSigner(pem);

const oauth2 = new jsforce.OAuth2({ clientId, clientSecret, redirectUri, dpop: { signer } });
const conn = new jsforce.Connection({ oauth2 });

await conn.authorize(code);
const result = await conn.query('SELECT Id, Name FROM Account');

In the browser, with a WebCrypto key. The private key can stay non-extractable:

// storing the key (e.g. in IndexedDB) is up to the user
const keyPair = await crypto.subtle.generateKey(
  { name: 'ECDSA', namedCurve: 'P-256' },
  false,
  ['sign', 'verify'],
);
const signer = await jsforce.DPoP.createSigner(keyPair);

const oauth2 = new jsforce.OAuth2({ clientId, redirectUri, dpop: { signer } });
location.href = oauth2.getAuthorizationUrl(); // includes dpop_jkt

With a KMS key, implement DPoPSigner yourself and pass it in:

const signer: DPoPSigner = {
  algorithm: 'ES256',
  format: 'der',
  publicJwk, // the KMS public key as a JWK
  async sign(signingInput) {
    // sign the SHA-256 of signingInput with KMS and return the DER signature as is
    return kmsSign(await sha256(signingInput));
  },
};

const oauth2 = new jsforce.OAuth2({ clientId, clientSecret, redirectUri, dpop: { signer } });

To create a connection later from saved tokens without a refresh, save proofNonce with them:

// storing these values is up to the user
const saved = {
  accessToken: conn.accessToken,
  refreshToken: conn.refreshToken,
  instanceUrl: conn.instanceUrl,
  proofNonce: conn.proofNonce,
};

// later, possibly in another process
const conn2 = new jsforce.Connection({
  oauth2,
  accessToken: saved.accessToken,
  refreshToken: saved.refreshToken,
  instanceUrl: saved.instanceUrl,
  proofNonce: saved.proofNonce,
});
await conn2.query('SELECT Id FROM Account');

A refresh changes the access token and proofNonce, and also the refresh token when Salesforce rotates it. The connection already has the new values when the refresh event fires, so save them again then.

Nonce

Salesforce requires a nonce in DPoP proofs. It gives the nonce only at the token endpoint (use_dpop_nonce), not at API endpoints, and the nonce belongs to the session of the access token. It is valid for 24 hours.

jsforce keeps the nonce received with a token in proofNonce and uses it in API requests. A connection created from a saved access token without proofNonce needs a refresh before its first API call succeeds.

Behavior

  • To bind tokens to the key, a DPoP proof is attached to token requests (authorization code, refresh, JWT bearer, client credentials) and to revokeToken(), and dpop_jkt is added to getAuthorizationUrl(). Salesforce does not revoke a DPoP token when the revoke request has no proof.
  • To avoid using a Bearer token by mistake, it fails if the returned token type is not DPoP.
  • A new proof is attached to each request, and if the access token has expired, it is refreshed and the request is retried once.
  • For SOAP-family APIs, the proof goes in the DpopProofHeader SOAP header next to SessionHeader. With DPoP, Salesforce reports an expired or revoked token as an INVALID_AUTH_HEADER fault, so that fault also triggers the refresh.
  • When a request is redirected, it is sent to the redirect URL with a new proof for that URL. Proofs are made only for HTTPS URLs. jsforce does not check the scheme of proxyUrl; use HTTPS outside local development.
  • Bulk 1 and Streaming requests fail before they are sent, to avoid calling them with a DPoP token by mistake.

Left to users

jsforce does not store keys or tokens. The following are left to users:

  • Implementing a DPoPSigner when using a KMS or HSM key.
  • Storing the private key so that the same key can be used again after a restart. Tokens, including refresh tokens, are bound to the key, so if the key is lost, the user has to authorize again.
  • Storing proofNonce with the access token, to use the access token in another process without a refresh.

Implementation notes

  • The DPoP code lives in src/oauth2.ts, in the DPoP class. OAuth2 uses it for token and revoke requests. HttpApi and SOAP make the proof in beforeSend(), which may now return a promise, with the access token they put in the request; SOAP puts the proof in the envelope.
  • Redirects are still followed in the transport. The followRedirect function option may now return a promise, and HttpApi and OAuth2 pass a function that builds the redirected request with a new proof.
  • XdProxyTransport used to drop the request options. It now passes them through, so options such as timeout and followRedirect: false also take effect through the proxy, with or without a signer. In the browser, a failed fetch (such as a timeout or a CORS error) now ends the request with the error, instead of leaving it pending.
  • Browsers cannot read redirect responses, so with a followRedirect function, XdProxyTransport asks jsforce-ajax-proxy to report redirects in headers instead. This applies to a followRedirect function passed without a signer too, so such requests through a cross-origin proxy need a proxy that supports it. Using DPoP in the browser through the proxy needs jsforce-ajax-proxy 2.1.0 or later.

Tests

The DPoP tests in test/oauth2.test.ts check what jsforce sends, in both Node and the browser:

  • Proofs made with a signer from DPoP.createSigner(), or with a custom signer returning DER, verify against the signer's public key.
  • The proof header and claims follow RFC 9449, Section 4.2, with a new jti for each request.
  • dpop_jkt in the authorization URL is the RFC 7638 thumbprint of the key.
  • SOAP requests carry the proof in DpopProofHeader, with the same access token in SessionHeader.

Whether Salesforce accepts them was checked against a scratch org with DPoP enabled on an external client app: authorization, REST, Bulk 2.0, SOAP, Metadata, refresh and revoke, from Node (directly and through jsforce-ajax-proxy) and from the browser (with a non-extractable WebCrypto key, through jsforce-ajax-proxy).

stomita and others added 5 commits November 23, 2026 02:09
So that a redirected request can be built asynchronously, for example
when it needs a newly signed header.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
XdProxyTransport dropped the options given to httpRequest. It now passes
them through, honors followRedirect: false, and wraps the redirected
request built by a given followRedirect function for the proxy.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Salesforce will require PKCE, refresh token rotation and a
proof-of-possession control such as DPoP for installing connected apps
and external client apps into other orgs. Add a dpop option to the
OAuth2 config that takes a signer, and a DPoP class that creates the
proofs, so that tokens are bound to the key and API requests are sent
with DPoP proofs. SOAP APIs (SOAP, Metadata, Apex SOAP) get the proof in
the DpopProofHeader SOAP header. Bulk 1 and Streaming requests fail
before they are sent, since they do not accept DPoP tokens.

Salesforce gives the nonce only at the token endpoint, and the nonce
belongs to the session of the access token. The token methods return it
as extra.proofNonce, and a connection keeps it as proofNonce, which can
be saved and passed with the access token, so that a connection created
later can call APIs without a refresh.

Refs #1867

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Browsers do not let scripts read a redirect response, so a request
through the AJAX proxy cannot follow a redirect in the browser. When the
caller builds the redirected request itself with a followRedirect
function, as DPoP requests do, ask the proxy to report redirects in
headers (SalesforceProxy-Redirect: report), and turn a reported redirect
back into the redirect response. In the browser, a failed fetch,
including that of a redirected request, now ends the request with the
error instead of leaving it pending.

Refs jsforce/jsforce-ajax-proxy#18

Co-Authored-By: Claude Opus 5.5 <[email protected]>
jsforce-ajax-proxy 2.1.0 allows the DPoP request header and reports
redirects in headers, which DPoP requests from the browser through the
proxy need.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
stomita and others added 5 commits November 23, 2026 02:55
Bulk 1 cannot be called with a DPoP token. Bulk._request() now fails
before creating the request, as Streaming does in _createClient(), so
HttpApi does not need to know about APIs that do not support DPoP.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… the report

A 200 response with SalesforceProxy-Redirect-* headers was turned into a
redirect for any request, also without the proxy. It is now restored only
when the request was sent with SalesforceProxy-Redirect: report.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
SOAP.beforeSend() now builds the envelope and computes its length for
every attempt, so HttpApi no longer removes content-length of SOAP
requests after a refresh.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The access token and its nonce always arrive together, so proofNonce is
set when a nonce is received, without comparing access tokens.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@stomita
stomita marked this pull request as draft November 22, 2026 18:15
stomita and others added 6 commits November 23, 2026 03:44
beforeSend() used the access token from the connection, or with DPoP the one in a DPoP-only argument. HttpApi now reads the access token once for each request, makes the proof with it when DPoP is used, and passes both to beforeSend(), so HttpApi, SOAP and Bulk 1 put the same token in the request. The SOAP session expiry check no longer builds its regular expression from a string.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Also drop the comments on TokenResponse.extra, which the other fields of the type do not have.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The SalesforceProxy-Redirect headers are part of the protocol between XdProxyTransport and jsforce-ajax-proxy, but the request functions for Node and the browser handled them. XdProxyTransport now follows a redirect that the proxy reported in headers, and leaves other redirects to the request functions as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
beforeSend() puts the credentials in a request, so it now also makes the DPoP proof, reading the access token and the nonce from the connection like the Bearer token before. HttpApi no longer reads them itself or passes them around, and beforeSend() may return a promise.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The followRedirect function passed for DPoP now only puts a new proof on the redirected request that the default or the given followRedirect makes, instead of rebuilding its headers in a separate method.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Response header names are already lower-cased, the DPoP header is set under one name, and DPoP.createProof() already rejects a URL other than HTTPS, so getHeader(), withoutHeader() and isHttpsUrl() are not needed. Also build the request options before sending them to the transport.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@stomita
stomita marked this pull request as ready for review November 22, 2026 19:40
stomita and others added 6 commits November 23, 2026 05:06
The browser tests run without the Node build, so test/oauth2.test.ts failed to load '../lib/request-helper' there. The mock transports build their responses themselves, and the test of performRedirectRequest is dropped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The app has the certificate of SF_OAUTH2_JWT_KEY, so that the tests can get tokens for the scratch org by the JWT bearer flow. External client apps need a newer API version, so package.xml is raised to 68.0, and the permission set gets FreezeUsers and MonitorLoginHistory, which InstallPackaging depends on in that version.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A mock transport answers whatever is sent, so it cannot tell whether Salesforce accepts the requests. The DPoP tests now check only proofs made by the public API without sending requests, and check against the scratch org that a token from the JWT bearer flow is bound to the key, is refreshed once when invalid, is used by another connection with a saved nonce without refreshing, and is revoked by logout. The JWT bearer flow is also tested by itself.

The assertion is signed with jwtKey.txt, which scripts/org-setup.mjs writes. In the browser, a karma middleware signs it, so that the key stays out of the browser.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The tests after the first one use the token, so the connection is authorized in beforeAll instead of in the first test. The first test also checks that the DPoP connection can call the API with the token, in addition to that a connection without DPoP cannot.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
In CI, lint runs before the build, so the type of Query is not known there and @typescript-eslint/require-await reports the async callbacks of assert.rejects() that only return the query.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…arer flow

A consumer key of an external client app is unique across orgs, so a fixed
key in the metadata works only in the first org it is deployed to. Read the
key the org generates on deploy in scripts/org-setup.mjs, and pass it to the
tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

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.

Add support for Salesforce OAuth 2.0 DPoP

1 participant