Open History to see the last commit
1// @effect-diagnostics-next-line nodeBuiltinImport:off -- Effect's Crypto has no KeyObject, generateKeyPairSync, or sign.
2import * as NodeCrypto from "node:crypto";
3
4import { assert, describe, it } from "@effect/vitest";
5import { vi } from "vite-plus/test";
6
7import {
8 computeDpopAccessTokenHash,
9 computeDpopJwkThumbprint,
10 normalizeDpopHtu,
11 type DpopPublicJwk,
12 verifyDpopProof,
13} from "./dpop.ts";
14
15function signDpopProof(input: {
16 readonly method: string;
17 readonly url: string;
18 readonly iat: number;
19 readonly privateKey: NodeCrypto.KeyObject;
20 readonly publicJwk: DpopPublicJwk | (DpopPublicJwk & { readonly d: string });
21 readonly accessToken?: string;
22}) {
23 const header = Buffer.from(
24 JSON.stringify({
25 typ: "dpop+jwt",
26 alg: "ES256",
27 jwk: input.publicJwk,
28 }),
29 ).toString("base64url");
30 const payload = Buffer.from(
31 JSON.stringify({
32 htm: input.method,
33 htu: input.url,
34 jti: "proof-1",
35 iat: input.iat,
36 ...(input.accessToken ? { ath: computeDpopAccessTokenHash(input.accessToken) } : {}),
37 }),
38 ).toString("base64url");
39 const signature = NodeCrypto.sign("sha256", Buffer.from(`${header}.${payload}`), {
40 key: input.privateKey,
41 dsaEncoding: "ieee-p1363",
42 }).toString("base64url");
43 return `${header}.${payload}.${signature}`;
44}
45
46describe("verifyDpopProof", () => {
47 const { privateKey, publicKey } = NodeCrypto.generateKeyPairSync("ec", {
48 namedCurve: "P-256",
49 });
50 const publicJwk = publicKey.export({ format: "jwk" }) as DpopPublicJwk;
51 const proof = signDpopProof({
52 method: "POST",
53 url: "https://example.com/oauth/token",
54 iat: 100,
55 privateKey,
56 publicJwk,
57 });
58
59 it("computes the RFC 7638 thumbprint whatever the host locale", () => {
60 // Latvian collation sorts "y" before "kty", which broke every remote client.
61 const latvian = new Intl.Collator("lv");
62 const localeCompare = vi.spyOn(String.prototype, "localeCompare").mockImplementation(function (
63 this: string,
64 that: string,
65 ) {
66 return latvian.compare(this, that);
67 });
68 try {
69 const rfc7638Input = JSON.stringify({
70 crv: publicJwk.crv,
71 kty: publicJwk.kty,
72 x: publicJwk.x,
73 y: publicJwk.y,
74 });
75 assert.equal(
76 computeDpopJwkThumbprint(publicJwk),
77 NodeCrypto.createHash("sha256").update(rfc7638Input).digest("base64url"),
78 );
79 } finally {
80 localeCompare.mockRestore();
81 }
82 });
83
84 it("verifies an ES256 DPoP proof and returns the RFC 7638 thumbprint", () => {
85 const thumbprint = computeDpopJwkThumbprint(publicJwk);
86 const result = verifyDpopProof({
87 proof,
88 method: "POST",
89 url: "https://example.com/oauth/token",
90 nowEpochSeconds: 101,
91 expectedThumbprint: thumbprint,
92 });
93
94 if (!result.ok) {
95 assert.fail(result.reason);
96 }
97 assert.equal(result.thumbprint, thumbprint);
98 assert.equal(result.jti, "proof-1");
99 });
100
101 it("rejects malformed DPoP header and payload JSON", () => {
102 const [header, payload, signature] = proof.split(".");
103 if (!header || !payload || !signature) {
104 assert.fail("Expected the test DPoP proof to use compact JWT format.");
105 }
106 const malformedJson = Buffer.from("{").toString("base64url");
107
108 const malformedHeader = verifyDpopProof({
109 proof: `${malformedJson}.${payload}.${signature}`,
110 method: "POST",
111 url: "https://example.com/oauth/token",
112 nowEpochSeconds: 101,
113 });
114 if (malformedHeader.ok) {
115 assert.fail("Expected malformed DPoP header JSON to fail.");
116 }
117 assert.equal(malformedHeader.reason, "Invalid DPoP JWT header.");
118
119 const malformedPayload = verifyDpopProof({
120 proof: `${header}.${malformedJson}.${signature}`,
121 method: "POST",
122 url: "https://example.com/oauth/token",
123 nowEpochSeconds: 101,
124 });
125 if (malformedPayload.ok) {
126 assert.fail("Expected malformed DPoP payload JSON to fail.");
127 }
128 assert.equal(malformedPayload.reason, "Invalid DPoP JWT payload.");
129 });
130
131 it("rejects method, URL, thumbprint, and time-window mismatches", () => {
132 const thumbprint = computeDpopJwkThumbprint(publicJwk);
133 assert.equal(
134 verifyDpopProof({
135 proof,
136 method: "GET",
137 url: "https://example.com/oauth/token",
138 nowEpochSeconds: 101,
139 expectedThumbprint: thumbprint,
140 }).ok,
141 false,
142 );
143 assert.equal(
144 verifyDpopProof({
145 proof,
146 method: "POST",
147 url: "https://example.com/other",
148 nowEpochSeconds: 101,
149 expectedThumbprint: thumbprint,
150 }).ok,
151 false,
152 );
153 assert.equal(
154 verifyDpopProof({
155 proof,
156 method: "POST",
157 url: "https://example.com/oauth/token",
158 nowEpochSeconds: 101,
159 expectedThumbprint: "other-thumbprint",
160 }).ok,
161 false,
162 );
163 assert.equal(
164 verifyDpopProof({
165 proof,
166 method: "POST",
167 url: "https://example.com/oauth/token",
168 nowEpochSeconds: 1_000,
169 expectedThumbprint: thumbprint,
170 }).ok,
171 false,
172 );
173 });
174
175 it("reports a time-window failure only after the proof signature is valid", () => {
176 const thumbprint = computeDpopJwkThumbprint(publicJwk);
177 const outsideWindow = verifyDpopProof({
178 proof,
179 method: "POST",
180 url: "https://example.com/oauth/token",
181 nowEpochSeconds: 1_000,
182 expectedThumbprint: thumbprint,
183 });
184 if (outsideWindow.ok) {
185 assert.fail("Expected an old DPoP proof to fail.");
186 }
187 assert.equal(outsideWindow.code, "time_window");
188
189 const { privateKey: otherPrivateKey } = NodeCrypto.generateKeyPairSync("ec", {
190 namedCurve: "P-256",
191 });
192 const invalidSignatureProof = signDpopProof({
193 method: "POST",
194 url: "https://example.com/oauth/token",
195 iat: 100,
196 privateKey: otherPrivateKey,
197 publicJwk,
198 });
199 const invalidSignature = verifyDpopProof({
200 proof: invalidSignatureProof,
201 method: "POST",
202 url: "https://example.com/oauth/token",
203 nowEpochSeconds: 1_000,
204 expectedThumbprint: thumbprint,
205 });
206 if (invalidSignature.ok) {
207 assert.fail("Expected a proof signed by a different key to fail.");
208 }
209 assert.equal(invalidSignature.code, "invalid_signature");
210 });
211
212 it("requires the RFC 9449 access token hash when an access token is expected", () => {
213 const thumbprint = computeDpopJwkThumbprint(publicJwk);
214 const accessTokenProof = signDpopProof({
215 method: "POST",
216 url: "https://example.com/v1/environments/env/connect",
217 iat: 100,
218 privateKey,
219 publicJwk,
220 accessToken: "clerk-access-token",
221 });
222
223 assert.equal(
224 verifyDpopProof({
225 proof: accessTokenProof,
226 method: "POST",
227 url: "https://example.com/v1/environments/env/connect",
228 nowEpochSeconds: 101,
229 expectedThumbprint: thumbprint,
230 expectedAccessToken: "clerk-access-token",
231 }).ok,
232 true,
233 );
234
235 const missingHash = verifyDpopProof({
236 proof,
237 method: "POST",
238 url: "https://example.com/oauth/token",
239 nowEpochSeconds: 101,
240 expectedThumbprint: thumbprint,
241 expectedAccessToken: "clerk-access-token",
242 });
243 if (missingHash.ok) {
244 assert.fail("Expected DPoP proof without an access token hash to fail.");
245 }
246 assert.equal(missingHash.reason, "DPoP access token hash mismatch.");
247
248 const mismatchedHash = verifyDpopProof({
249 proof: accessTokenProof,
250 method: "POST",
251 url: "https://example.com/v1/environments/env/connect",
252 nowEpochSeconds: 101,
253 expectedThumbprint: thumbprint,
254 expectedAccessToken: "other-access-token",
255 });
256 if (mismatchedHash.ok) {
257 assert.fail("Expected DPoP proof with a mismatched access token hash to fail.");
258 }
259 assert.equal(mismatchedHash.reason, "DPoP access token hash mismatch.");
260 });
261
262 it("normalizes htu by excluding query and fragment components per RFC 9449", () => {
263 assert.equal(
264 normalizeDpopHtu("https://example.com/v1/environments/env/connect?foo=bar#frag"),
265 "https://example.com/v1/environments/env/connect",
266 );
267
268 const thumbprint = computeDpopJwkThumbprint(publicJwk);
269 const queryProof = signDpopProof({
270 method: "POST",
271 url: "https://example.com/v1/environments/env/connect",
272 iat: 100,
273 privateKey,
274 publicJwk,
275 });
276
277 assert.equal(
278 verifyDpopProof({
279 proof: queryProof,
280 method: "POST",
281 url: "https://example.com/v1/environments/env/connect?foo=bar#frag",
282 nowEpochSeconds: 101,
283 expectedThumbprint: thumbprint,
284 }).ok,
285 true,
286 );
287 });
288
289 it("rejects DPoP public JWK headers that expose private key material", () => {
290 const thumbprint = computeDpopJwkThumbprint(publicJwk);
291 const privateJwk = privateKey.export({ format: "jwk" }) as DpopPublicJwk & {
292 readonly d: string;
293 };
294 const proofWithPrivateJwk = signDpopProof({
295 method: "POST",
296 url: "https://example.com/oauth/token",
297 iat: 100,
298 privateKey,
299 publicJwk: privateJwk,
300 });
301
302 const result = verifyDpopProof({
303 proof: proofWithPrivateJwk,
304 method: "POST",
305 url: "https://example.com/oauth/token",
306 nowEpochSeconds: 101,
307 expectedThumbprint: thumbprint,
308 });
309
310 if (result.ok) {
311 assert.fail("Expected DPoP proof with private JWK material to fail.");
312 }
313 assert.equal(result.reason, "Invalid DPoP JWT header.");
314 });
315});