|
| 1 | +// @effect-diagnostics-next-line nodeBuiltinImport:off -- Effect's Crypto has no generateKeyPairSync or sign. |
| 2 | +import * as NodeCrypto from "node:crypto"; |
| 3 | + |
1 | 4 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
2 | 5 | import { authScopeResponse, AuthStandardClientScopes } from "@t3tools/contracts"; |
| 6 | +import { |
| 7 | + computeDpopAccessTokenHash, |
| 8 | + computeDpopJwkThumbprint, |
| 9 | + type DpopPublicJwk, |
| 10 | +} from "@t3tools/shared/dpop"; |
3 | 11 | import { expect, it } from "@effect/vitest"; |
| 12 | +import * as DateTime from "effect/DateTime"; |
4 | 13 | import * as Effect from "effect/Effect"; |
5 | 14 | import * as Layer from "effect/Layer"; |
6 | 15 | import * as Redacted from "effect/Redacted"; |
@@ -68,6 +77,36 @@ const requestMetadata = { |
68 | 77 | ipAddress: "192.168.1.23", |
69 | 78 | }; |
70 | 79 |
|
| 80 | +const makeDpopKey = () => { |
| 81 | + const { privateKey, publicKey } = NodeCrypto.generateKeyPairSync("ec", { namedCurve: "P-256" }); |
| 82 | + const publicJwk = publicKey.export({ format: "jwk" }) as DpopPublicJwk; |
| 83 | + let proofCount = 0; |
| 84 | + return { |
| 85 | + thumbprint: computeDpopJwkThumbprint(publicJwk), |
| 86 | + sign: (input: { |
| 87 | + readonly method: string; |
| 88 | + readonly url: string; |
| 89 | + readonly token: string; |
| 90 | + readonly iat: number; |
| 91 | + }) => { |
| 92 | + const encode = (value: unknown) => Buffer.from(JSON.stringify(value)).toString("base64url"); |
| 93 | + const header = encode({ typ: "dpop+jwt", alg: "ES256", jwk: publicJwk }); |
| 94 | + const payload = encode({ |
| 95 | + htm: input.method, |
| 96 | + htu: input.url, |
| 97 | + jti: `proof-${++proofCount}`, |
| 98 | + iat: input.iat, |
| 99 | + ath: computeDpopAccessTokenHash(input.token), |
| 100 | + }); |
| 101 | + const signature = NodeCrypto.sign("sha256", Buffer.from(`${header}.${payload}`), { |
| 102 | + key: privateKey, |
| 103 | + dsaEncoding: "ieee-p1363", |
| 104 | + }).toString("base64url"); |
| 105 | + return `${header}.${payload}.${signature}`; |
| 106 | + }, |
| 107 | + }; |
| 108 | +}; |
| 109 | + |
71 | 110 | it.layer(NodeServices.layer)("EnvironmentAuth.layer", (it) => { |
72 | 111 | it.effect("uses the reusable dev cookie without overriding a normal scoped cookie", () => |
73 | 112 | Effect.gen(function* () { |
@@ -527,11 +566,14 @@ it.layer(NodeServices.layer)("EnvironmentAuth.layer", (it) => { |
527 | 566 | const pairing = yield* serverAuth.issuePairingCredential(); |
528 | 567 | const exchanged = yield* serverAuth.createBrowserSession(pairing.credential, requestMetadata); |
529 | 568 | const upgrade = (headers: Record<string, string>) => |
530 | | - serverAuth.authenticateWebSocketUpgrade({ |
531 | | - url: "/ws", |
532 | | - cookies: { [sessions.cookieName]: exchanged.sessionToken }, |
533 | | - headers: { host: "192.168.1.50:3773", ...headers }, |
534 | | - } as never); |
| 569 | + serverAuth.authenticateWebSocketUpgrade( |
| 570 | + { |
| 571 | + url: "/ws", |
| 572 | + cookies: { [sessions.cookieName]: exchanged.sessionToken }, |
| 573 | + headers: { host: "192.168.1.50:3773", ...headers }, |
| 574 | + } as never, |
| 575 | + "rpc", |
| 576 | + ); |
535 | 577 |
|
536 | 578 | yield* upgrade({ origin: "http://192.168.1.50:3773" }); |
537 | 579 | yield* upgrade({ |
@@ -588,16 +630,122 @@ it.layer(NodeServices.layer)("EnvironmentAuth.layer", (it) => { |
588 | 630 | Effect.gen(function* () { |
589 | 631 | const serverAuth = yield* EnvironmentAuth.EnvironmentAuth; |
590 | 632 | const bearer = yield* serverAuth.issueSession(); |
591 | | - const verified = yield* serverAuth.authenticateWebSocketUpgrade({ |
592 | | - url: "/ws", |
593 | | - cookies: {}, |
594 | | - headers: { |
595 | | - host: "192.168.1.50:3773", |
596 | | - origin: "http://192.168.1.50:8080", |
597 | | - authorization: `Bearer ${bearer.token}`, |
598 | | - }, |
599 | | - } as never); |
| 633 | + const verified = yield* serverAuth.authenticateWebSocketUpgrade( |
| 634 | + { |
| 635 | + url: "/ws", |
| 636 | + cookies: {}, |
| 637 | + headers: { |
| 638 | + host: "192.168.1.50:3773", |
| 639 | + origin: "http://192.168.1.50:8080", |
| 640 | + authorization: `Bearer ${bearer.token}`, |
| 641 | + }, |
| 642 | + } as never, |
| 643 | + "rpc", |
| 644 | + ); |
600 | 645 | expect(verified.sessionId).toBe(bearer.sessionId); |
601 | 646 | }).pipe(Effect.provide(layerEnvironmentAuth({ mode: "web", host: "192.168.1.50" }))), |
602 | 647 | ); |
| 648 | + |
| 649 | + it.effect("binds a DPoP session's RPC socket ticket to its proof key", () => |
| 650 | + Effect.gen(function* () { |
| 651 | + const serverAuth = yield* EnvironmentAuth.EnvironmentAuth; |
| 652 | + const sessions = yield* SessionStore.SessionStore; |
| 653 | + const key = makeDpopKey(); |
| 654 | + const other = makeDpopKey(); |
| 655 | + const exchanged = yield* serverAuth.exchangeBootstrapCredentialForAccessToken( |
| 656 | + "desktop-bootstrap-token", |
| 657 | + ["orchestration:read"], |
| 658 | + requestMetadata, |
| 659 | + { proofKeyThumbprint: key.thumbprint }, |
| 660 | + ); |
| 661 | + const session = yield* sessions.verify(exchanged.access_token); |
| 662 | + const host = "192.168.1.50:3773"; |
| 663 | + const socketUrl = `http://${host}/ws`; |
| 664 | + const iat = Math.floor((yield* DateTime.now).epochMilliseconds / 1_000); |
| 665 | + const upgrade = (ticket: string, use: "rpc" | "media", proof?: string) => |
| 666 | + serverAuth.authenticateWebSocketUpgrade( |
| 667 | + { |
| 668 | + method: "GET", |
| 669 | + url: `/ws?orchestrationProtocol=2&wsTicket=${ticket}${proof ? `&dpop=${proof}` : ""}`, |
| 670 | + cookies: {}, |
| 671 | + headers: { host }, |
| 672 | + } as never, |
| 673 | + use, |
| 674 | + ); |
| 675 | + |
| 676 | + const rpc = yield* serverAuth.issueWebSocketTicket(session, { use: "rpc" }); |
| 677 | + const bare = yield* Effect.flip(upgrade(rpc.ticket, "rpc")); |
| 678 | + expect(bare._tag).toBe("ServerAuthInvalidCredentialError"); |
| 679 | + const signed = yield* upgrade( |
| 680 | + rpc.ticket, |
| 681 | + "rpc", |
| 682 | + key.sign({ method: "GET", url: socketUrl, token: rpc.ticket, iat }), |
| 683 | + ); |
| 684 | + expect(signed.sessionId).toBe(session.sessionId); |
| 685 | + |
| 686 | + const wrongKey = yield* Effect.flip( |
| 687 | + upgrade( |
| 688 | + rpc.ticket, |
| 689 | + "rpc", |
| 690 | + other.sign({ method: "GET", url: socketUrl, token: rpc.ticket, iat }), |
| 691 | + ), |
| 692 | + ); |
| 693 | + expect(wrongKey._tag).toBe("ServerAuthInvalidCredentialError"); |
| 694 | + const onMedia = yield* Effect.flip( |
| 695 | + upgrade( |
| 696 | + rpc.ticket, |
| 697 | + "media", |
| 698 | + key.sign({ method: "GET", url: socketUrl, token: rpc.ticket, iat }), |
| 699 | + ), |
| 700 | + ); |
| 701 | + expect(onMedia._tag).toBe("ServerAuthInvalidCredentialError"); |
| 702 | + |
| 703 | + const media = yield* serverAuth.issueWebSocketTicket(session, { use: "media" }); |
| 704 | + expect((yield* upgrade(media.ticket, "media")).sessionId).toBe(session.sessionId); |
| 705 | + const mediaOnRpc = yield* Effect.flip(upgrade(media.ticket, "rpc")); |
| 706 | + expect(mediaOnRpc._tag).toBe("ServerAuthInvalidCredentialError"); |
| 707 | + |
| 708 | + // Clients that predate ticket binding still connect with the unbound ticket they mint. |
| 709 | + const legacy = yield* serverAuth.issueWebSocketTicket(session); |
| 710 | + expect((yield* upgrade(legacy.ticket, "rpc")).sessionId).toBe(session.sessionId); |
| 711 | + }).pipe( |
| 712 | + Effect.provide(layerEnvironmentAuth({ desktopBootstrapToken: "desktop-bootstrap-token" })), |
| 713 | + ), |
| 714 | + ); |
| 715 | + |
| 716 | + it.effect("rejects a replayed proof on a bound RPC socket ticket", () => |
| 717 | + Effect.gen(function* () { |
| 718 | + const serverAuth = yield* EnvironmentAuth.EnvironmentAuth; |
| 719 | + const sessions = yield* SessionStore.SessionStore; |
| 720 | + const key = makeDpopKey(); |
| 721 | + const exchanged = yield* serverAuth.exchangeBootstrapCredentialForAccessToken( |
| 722 | + "desktop-bootstrap-token", |
| 723 | + ["orchestration:read"], |
| 724 | + requestMetadata, |
| 725 | + { proofKeyThumbprint: key.thumbprint }, |
| 726 | + ); |
| 727 | + const session = yield* sessions.verify(exchanged.access_token); |
| 728 | + const rpc = yield* serverAuth.issueWebSocketTicket(session, { use: "rpc" }); |
| 729 | + const request = { |
| 730 | + method: "GET", |
| 731 | + url: `/ws?wsTicket=${rpc.ticket}&dpop=${key.sign({ |
| 732 | + method: "GET", |
| 733 | + url: "http://192.168.1.50:3773/ws", |
| 734 | + token: rpc.ticket, |
| 735 | + iat: Math.floor((yield* DateTime.now).epochMilliseconds / 1_000), |
| 736 | + })}`, |
| 737 | + cookies: {}, |
| 738 | + headers: { host: "192.168.1.50:3773" }, |
| 739 | + } as never; |
| 740 | + |
| 741 | + yield* serverAuth.authenticateWebSocketUpgrade(request, "rpc"); |
| 742 | + const replayed = yield* Effect.flip(serverAuth.authenticateWebSocketUpgrade(request, "rpc")); |
| 743 | + expect(replayed._tag).toBe("ServerAuthInvalidCredentialError"); |
| 744 | + if (replayed._tag === "ServerAuthInvalidCredentialError") { |
| 745 | + expect(replayed.dpopFailureReason).toBe("replay"); |
| 746 | + } |
| 747 | + }).pipe( |
| 748 | + Effect.provide(layerEnvironmentAuth({ desktopBootstrapToken: "desktop-bootstrap-token" })), |
| 749 | + ), |
| 750 | + ); |
603 | 751 | }); |
0 commit comments