The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 24, 22:45 UTC
[codex] Structure relay JWT failures (#3270)
Co-authored-by: codex <codex@users.noreply.github.com>
Julius Marminge committed
Jun 20, 2026 at 11:19 UTC
f98448e8721a491f5945f1857cbd22fa87a955ef
3 files changed
+95
-19
infra/relay/src/auth/RelayTokens.ts
+2
-13
@@ -11,9 +11,9 @@ import {
11
import { encodeOAuthScope, parseAllowedOAuthScope } from "@t3tools/shared/oauthScope";
12
import {
13
normalizeRelayIssuer,
14
+ RelayJwtError,
15
signRelayJwt,
16
verifyRelayJwt,
16
- type RelayJwtError,
17
} from "@t3tools/shared/relayJwt";
18
import * as Context from "effect/Context";
19
import * as Effect from "effect/Effect";
@@ -72,17 +72,6 @@ const allowedScopesByClientId: Record<
72
[RelayWebClientId]: new Set([RelayEnvironmentConnectScope, RelayEnvironmentStatusScope]),
73
};
74
75
-function relayJwtVerificationFailureReason(error: RelayJwtError): string {
76
- const cause = error.cause;
77
- if (typeof cause === "object" && cause !== null && "code" in cause) {
78
- const code = (cause as { readonly code?: unknown }).code;
79
- if (typeof code === "string" && code.length > 0) {
80
- return code;
81
- }
82
- }
83
- return cause instanceof Error && cause.name ? cause.name : "unknown";
84
-}
85
-
75
function resolveDpopAccessTokenScopes(input: {
76
readonly clientId: RelayPublicClientId;
77
readonly scope: string;
@@ -211,7 +200,7 @@ const make = Effect.gen(function* () {
200
Effect.tapError((error) =>
201
Effect.annotateCurrentSpan(
202
"relay.tokens.verification_failure",
214
- relayJwtVerificationFailureReason(error),
203
+ RelayJwtError.diagnosticCode(error),
204
),
205
),
206
Effect.flatMap(decodeDpopAccessTokenClaims),
packages/shared/src/relayJwt.test.ts
new
+58
@@ -0,0 +1,58 @@
1
+import * as NodeCrypto from "node:crypto";
2
+
3
+import { describe, expect, it } from "@effect/vitest";
4
+import * as Effect from "effect/Effect";
5
+
6
+import { RelayJwtError, signRelayJwt, verifyRelayJwt } from "./relayJwt.ts";
7
+
8
+describe("relayJwt", () => {
9
+ it.effect("preserves signing context and the JOSE cause", () =>
10
+ Effect.gen(function* () {
11
+ const error = yield* signRelayJwt({
12
+ privateKey: "not-a-private-key",
13
+ typ: "test-sign+jwt",
14
+ payload: { sub: "subject" },
15
+ }).pipe(Effect.flip);
16
+
17
+ expect(error.operation).toBe("sign");
18
+ expect(error.typ).toBe("test-sign+jwt");
19
+ expect(error.cause).toBeInstanceOf(Error);
20
+ expect(error.message).toBe('Failed to sign relay JWT of type "test-sign+jwt".');
21
+ }),
22
+ );
23
+
24
+ it.effect("preserves verification request context and the JOSE cause", () =>
25
+ Effect.gen(function* () {
26
+ const keyPair = NodeCrypto.generateKeyPairSync("ed25519", {
27
+ publicKeyEncoding: { format: "pem", type: "spki" },
28
+ privateKeyEncoding: { format: "pem", type: "pkcs8" },
29
+ });
30
+ const error = yield* verifyRelayJwt({
31
+ publicKey: keyPair.publicKey,
32
+ token: "not-a-jwt",
33
+ typ: "test-verify+jwt",
34
+ issuer: "https://issuer.example.test",
35
+ audience: "test-audience",
36
+ nowEpochSeconds: 100,
37
+ }).pipe(Effect.flip);
38
+
39
+ expect(error.operation).toBe("verify");
40
+ expect(error.typ).toBe("test-verify+jwt");
41
+ expect(error.issuer).toBe("https://issuer.example.test");
42
+ expect(error.audience).toBe("test-audience");
43
+ expect(error.cause).toBeInstanceOf(Error);
44
+ expect(error.message).toBe('Failed to verify relay JWT of type "test-verify+jwt".');
45
+ }),
46
+ );
47
+
48
+ it("extracts stable diagnostic codes without copying cause text into the error message", () => {
49
+ const error = new RelayJwtError({
50
+ operation: "verify",
51
+ typ: "test+jwt",
52
+ cause: { code: "ERR_JWT_EXPIRED", message: "sensitive library detail" },
53
+ });
54
+
55
+ expect(RelayJwtError.diagnosticCode(error)).toBe("ERR_JWT_EXPIRED");
56
+ expect(error.message).not.toContain("sensitive library detail");
57
+ });
58
+});
packages/shared/src/relayJwt.ts
+35
-6
@@ -1,7 +1,8 @@
1
import { decodeJwt, importPKCS8, importSPKI, jwtVerify, SignJWT, type JWTPayload } from "jose";
2
-import * as Data from "effect/Data";
2
import * as DateTime from "effect/DateTime";
3
import * as Effect from "effect/Effect";
4
+import * as Predicate from "effect/Predicate";
5
+import * as Schema from "effect/Schema";
6
7
export const RELAY_LINK_PROOF_TYP = "t3-env-link+jwt";
8
export const RELAY_MINT_REQUEST_TYP = "t3-cloud-mint+jwt";
@@ -10,9 +11,30 @@ export const RELAY_MINT_RESPONSE_TYP = "t3-env-mint+jwt";
11
export const RELAY_HEALTH_RESPONSE_TYP = "t3-env-health+jwt";
12
export const RELAY_ACTIVITY_PUBLISH_TYP = "t3-env-activity+jwt";
13
13
-export class RelayJwtError extends Data.TaggedError("RelayJwtError")<{
14
- readonly cause: unknown;
15
-}> {}
14
+export class RelayJwtError extends Schema.TaggedErrorClass<RelayJwtError>()("RelayJwtError", {
15
+ operation: Schema.Literals(["sign", "verify"]),
16
+ typ: Schema.String,
17
+ issuer: Schema.optional(Schema.String),
18
+ audience: Schema.optional(Schema.String),
19
+ cause: Schema.Defect(),
20
+}) {
21
+ override get message(): string {
22
+ return `Failed to ${this.operation} relay JWT of type "${this.typ}".`;
23
+ }
24
+
25
+ static diagnosticCode(error: RelayJwtError): string {
26
+ if (
27
+ Predicate.isObject(error.cause) &&
28
+ Predicate.hasProperty(error.cause, "code") &&
29
+ Predicate.isString(error.cause.code) &&
30
+ error.cause.code.length > 0
31
+ ) {
32
+ return error.cause.code;
33
+ }
34
+
35
+ return error.cause instanceof Error && error.cause.name ? error.cause.name : "unknown";
36
+ }
37
+}
38
39
export function normalizeRelayIssuer(value: string): string {
40
return value.trim().replace(/\/+$/gu, "");
@@ -38,7 +60,7 @@ export function signRelayJwt(input: {
60
.setProtectedHeader({ alg: "EdDSA", typ: input.typ })
61
.sign(key);
62
},
41
- catch: (cause) => new RelayJwtError({ cause }),
63
+ catch: (cause) => new RelayJwtError({ operation: "sign", typ: input.typ, cause }),
64
});
65
}
66
@@ -65,6 +87,13 @@ export function verifyRelayJwt(input: {
87
});
88
return verified.payload;
89
},
68
- catch: (cause) => new RelayJwtError({ cause }),
90
+ catch: (cause) =>
91
+ new RelayJwtError({
92
+ operation: "verify",
93
+ typ: input.typ,
94
+ issuer: input.issuer,
95
+ audience: input.audience,
96
+ cause,
97
+ }),
98
});
99
}