Skip to content

Commit 95a9380

Browse files
juliusmarmingecodex
andcommitted
Redact cloud CLI endpoint diagnostics
Co-authored-by: codex <[email protected]>
1 parent a60df5f commit 95a9380

2 files changed

Lines changed: 197 additions & 58 deletions

File tree

‎apps/server/src/cloud/CliTokenManager.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,69 @@ function makeTokenManager(secretStore: ServerSecretStore.ServerSecretStore["Serv
4040
}
4141

4242
describe("CloudCliTokenManager", () => {
43+
it("redacts OAuth endpoint credentials while retaining exact causes", () => {
44+
const tokenEndpoint =
45+
"https://user:[email protected]/private/token?client_secret=secret#fragment";
46+
const redirectUri =
47+
"https://callback-user:callback-password@localhost/private/callback?code=secret#fragment";
48+
const cause = new Error("exchange failed");
49+
50+
const refreshError = CliTokenManager.CloudCliCredentialRefreshError.fromStage({
51+
stage: "exchange-token",
52+
tokenEndpoint,
53+
cause,
54+
});
55+
const authorizationError = CliTokenManager.CloudCliAuthorizationError.fromStage({
56+
stage: "exchange-token",
57+
tokenEndpoint,
58+
redirectUri,
59+
cause,
60+
});
61+
const timeoutError = CliTokenManager.CloudCliAuthorizationTimeoutError.fromRedirectUri({
62+
redirectUri,
63+
timeoutMillis: 1000,
64+
cause,
65+
});
66+
67+
expect(refreshError).toMatchObject({
68+
tokenEndpointInputLength: tokenEndpoint.length,
69+
tokenEndpointProtocol: "https:",
70+
tokenEndpointHostname: "auth.example.test",
71+
cause,
72+
});
73+
expect(authorizationError).toMatchObject({
74+
tokenEndpointInputLength: tokenEndpoint.length,
75+
tokenEndpointHostname: "auth.example.test",
76+
redirectUriInputLength: redirectUri.length,
77+
redirectUriHostname: "localhost",
78+
cause,
79+
});
80+
expect(timeoutError).toMatchObject({
81+
redirectUriInputLength: redirectUri.length,
82+
redirectUriHostname: "localhost",
83+
cause,
84+
});
85+
expect(refreshError.cause).toBe(cause);
86+
expect(authorizationError.cause).toBe(cause);
87+
expect(timeoutError.cause).toBe(cause);
88+
for (const error of [refreshError, authorizationError, timeoutError]) {
89+
expect(error).not.toHaveProperty("tokenEndpoint");
90+
expect(error).not.toHaveProperty("redirectUri");
91+
const serialized = JSON.stringify(error);
92+
for (const secret of [
93+
"user:password",
94+
"callback-user:callback-password",
95+
"/private/",
96+
"client_secret=secret",
97+
"code=secret",
98+
"#fragment",
99+
]) {
100+
expect(error.message).not.toContain(secret);
101+
expect(serialized).not.toContain(secret);
102+
}
103+
}
104+
});
105+
43106
it.effect("retains secret context and cause when credential removal fails", () => {
44107
const failure = new ServerSecretStore.SecretStoreRemoveError({
45108
secretName: "cloud-cli-oauth-token",

‎apps/server/src/cloud/CliTokenManager.ts‎

Lines changed: 134 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as HttpClientResponse from "effect/unstable/http/HttpClientResponse";
2020
import * as HttpRouter from "effect/unstable/http/HttpRouter";
2121
import * as HttpServerRequest from "effect/unstable/http/HttpServerRequest";
2222
import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
23+
import { getUrlDiagnostics } from "@t3tools/shared/urlDiagnostics";
2324

2425
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
2526
import { cloudCliOAuthConfig, type CloudCliOAuthConfig } from "./publicConfig.ts";
@@ -63,6 +64,7 @@ const CloudCliCredentialRefreshStage = Schema.Literals([
6364
"encode-credential",
6465
"persist-credential",
6566
]);
67+
type CloudCliCredentialRefreshStage = typeof CloudCliCredentialRefreshStage.Type;
6668

6769
const CloudCliAuthorizationStage = Schema.Literals([
6870
"load-oauth-config",
@@ -72,6 +74,27 @@ const CloudCliAuthorizationStage = Schema.Literals([
7274
"encode-credential",
7375
"persist-credential",
7476
]);
77+
type CloudCliAuthorizationStage = typeof CloudCliAuthorizationStage.Type;
78+
79+
function tokenEndpointDiagnosticFields(tokenEndpoint: string | undefined) {
80+
if (tokenEndpoint === undefined) return {};
81+
const diagnostics = getUrlDiagnostics(tokenEndpoint);
82+
return {
83+
tokenEndpointInputLength: diagnostics.inputLength,
84+
...(diagnostics.protocol === undefined ? {} : { tokenEndpointProtocol: diagnostics.protocol }),
85+
...(diagnostics.hostname === undefined ? {} : { tokenEndpointHostname: diagnostics.hostname }),
86+
};
87+
}
88+
89+
function redirectUriDiagnosticFields(redirectUri: string | undefined) {
90+
if (redirectUri === undefined) return {};
91+
const diagnostics = getUrlDiagnostics(redirectUri);
92+
return {
93+
redirectUriInputLength: diagnostics.inputLength,
94+
...(diagnostics.protocol === undefined ? {} : { redirectUriProtocol: diagnostics.protocol }),
95+
...(diagnostics.hostname === undefined ? {} : { redirectUriHostname: diagnostics.hostname }),
96+
};
97+
}
7598

7699
export class CloudCliCredentialRemovalError extends Schema.TaggedErrorClass<CloudCliCredentialRemovalError>()(
77100
"CloudCliCredentialRemovalError",
@@ -90,10 +113,25 @@ export class CloudCliCredentialRefreshError extends Schema.TaggedErrorClass<Clou
90113
{
91114
stage: CloudCliCredentialRefreshStage,
92115
secretName: Schema.Literal(CLOUD_CLI_OAUTH_TOKEN_SECRET),
93-
tokenEndpoint: Schema.optional(Schema.String),
116+
tokenEndpointInputLength: Schema.optionalKey(Schema.Number),
117+
tokenEndpointProtocol: Schema.optionalKey(Schema.String),
118+
tokenEndpointHostname: Schema.optionalKey(Schema.String),
94119
cause: Schema.Defect(),
95120
},
96121
) {
122+
static fromStage(input: {
123+
readonly stage: CloudCliCredentialRefreshStage;
124+
readonly cause: unknown;
125+
readonly tokenEndpoint?: string;
126+
}): CloudCliCredentialRefreshError {
127+
return new CloudCliCredentialRefreshError({
128+
stage: input.stage,
129+
secretName: CLOUD_CLI_OAUTH_TOKEN_SECRET,
130+
...tokenEndpointDiagnosticFields(input.tokenEndpoint),
131+
cause: input.cause,
132+
});
133+
}
134+
97135
static fromCredentialRead(cause: CredentialReadFailure): CloudCliCredentialRefreshError {
98136
return new CloudCliCredentialRefreshError({
99137
stage: cause._tag === "SecretStoreReadError" ? "read-credential" : "decode-credential",
@@ -111,7 +149,10 @@ export class CloudCliCredentialRefreshError extends Schema.TaggedErrorClass<Clou
111149
}
112150

113151
override get message(): string {
114-
const tokenEndpoint = this.tokenEndpoint ? ` using ${this.tokenEndpoint}` : "";
152+
const tokenEndpoint =
153+
this.tokenEndpointInputLength === undefined
154+
? ""
155+
: ` using the token endpoint${this.tokenEndpointHostname ? ` at ${this.tokenEndpointHostname}` : ""} (input length ${this.tokenEndpointInputLength})`;
115156
return `Could not refresh the T3 Connect CLI credential ${this.secretName} during ${this.stage}${tokenEndpoint}.`;
116157
}
117158
}
@@ -142,13 +183,36 @@ export class CloudCliAuthorizationError extends Schema.TaggedErrorClass<CloudCli
142183
{
143184
stage: CloudCliAuthorizationStage,
144185
secretName: Schema.Literal(CLOUD_CLI_OAUTH_TOKEN_SECRET),
145-
tokenEndpoint: Schema.optional(Schema.String),
146-
redirectUri: Schema.optional(Schema.String),
186+
tokenEndpointInputLength: Schema.optionalKey(Schema.Number),
187+
tokenEndpointProtocol: Schema.optionalKey(Schema.String),
188+
tokenEndpointHostname: Schema.optionalKey(Schema.String),
189+
redirectUriInputLength: Schema.optionalKey(Schema.Number),
190+
redirectUriProtocol: Schema.optionalKey(Schema.String),
191+
redirectUriHostname: Schema.optionalKey(Schema.String),
147192
callbackHost: Schema.optional(Schema.String),
148193
callbackPort: Schema.optional(Schema.Number),
149194
cause: Schema.Defect(),
150195
},
151196
) {
197+
static fromStage(input: {
198+
readonly stage: CloudCliAuthorizationStage;
199+
readonly cause: unknown;
200+
readonly tokenEndpoint?: string;
201+
readonly redirectUri?: string;
202+
readonly callbackHost?: string;
203+
readonly callbackPort?: number;
204+
}): CloudCliAuthorizationError {
205+
return new CloudCliAuthorizationError({
206+
stage: input.stage,
207+
secretName: CLOUD_CLI_OAUTH_TOKEN_SECRET,
208+
...tokenEndpointDiagnosticFields(input.tokenEndpoint),
209+
...redirectUriDiagnosticFields(input.redirectUri),
210+
...(input.callbackHost === undefined ? {} : { callbackHost: input.callbackHost }),
211+
...(input.callbackPort === undefined ? {} : { callbackPort: input.callbackPort }),
212+
cause: input.cause,
213+
});
214+
}
215+
152216
static fromCredentialPersist(cause: CredentialPersistFailure): CloudCliAuthorizationError {
153217
return new CloudCliAuthorizationError({
154218
stage: cause._tag === "SchemaError" ? "encode-credential" : "persist-credential",
@@ -158,8 +222,14 @@ export class CloudCliAuthorizationError extends Schema.TaggedErrorClass<CloudCli
158222
}
159223

160224
override get message(): string {
161-
const tokenEndpoint = this.tokenEndpoint ? ` using ${this.tokenEndpoint}` : "";
162-
const redirectUri = this.redirectUri ? ` with callback ${this.redirectUri}` : "";
225+
const tokenEndpoint =
226+
this.tokenEndpointInputLength === undefined
227+
? ""
228+
: ` using the token endpoint${this.tokenEndpointHostname ? ` at ${this.tokenEndpointHostname}` : ""} (input length ${this.tokenEndpointInputLength})`;
229+
const redirectUri =
230+
this.redirectUriInputLength === undefined
231+
? ""
232+
: ` with a callback URI input of length ${this.redirectUriInputLength}`;
163233
const callbackAddress =
164234
this.callbackHost && this.callbackPort !== undefined
165235
? ` on ${this.callbackHost}:${this.callbackPort}`
@@ -171,13 +241,31 @@ export class CloudCliAuthorizationError extends Schema.TaggedErrorClass<CloudCli
171241
export class CloudCliAuthorizationTimeoutError extends Schema.TaggedErrorClass<CloudCliAuthorizationTimeoutError>()(
172242
"CloudCliAuthorizationTimeoutError",
173243
{
174-
redirectUri: Schema.String,
244+
redirectUriInputLength: Schema.Number,
245+
redirectUriProtocol: Schema.optionalKey(Schema.String),
246+
redirectUriHostname: Schema.optionalKey(Schema.String),
175247
timeoutMillis: Schema.Number,
176248
cause: Schema.Defect(),
177249
},
178250
) {
251+
static fromRedirectUri(input: {
252+
readonly redirectUri: string;
253+
readonly timeoutMillis: number;
254+
readonly cause: unknown;
255+
}): CloudCliAuthorizationTimeoutError {
256+
const diagnostics = getUrlDiagnostics(input.redirectUri);
257+
return new CloudCliAuthorizationTimeoutError({
258+
redirectUriInputLength: diagnostics.inputLength,
259+
...(diagnostics.protocol === undefined ? {} : { redirectUriProtocol: diagnostics.protocol }),
260+
...(diagnostics.hostname === undefined ? {} : { redirectUriHostname: diagnostics.hostname }),
261+
timeoutMillis: input.timeoutMillis,
262+
cause: input.cause,
263+
});
264+
}
265+
179266
override get message(): string {
180-
return `Timed out after ${this.timeoutMillis}ms waiting for T3 Connect authorization at ${this.redirectUri}.`;
267+
const callback = this.redirectUriHostname ? ` for ${this.redirectUriHostname}` : "";
268+
return `Timed out after ${this.timeoutMillis}ms waiting for T3 Connect authorization${callback} (callback URI input length ${this.redirectUriInputLength}).`;
181269
}
182270
}
183271

@@ -262,41 +350,35 @@ export const make = Effect.gen(function* () {
262350

263351
const refresh = Effect.fn("cloud.cli_token.refresh")(function* (token: PersistedToken) {
264352
const metadata = yield* cloudCliOAuthConfig.pipe(
265-
Effect.mapError(
266-
(cause) =>
267-
new CloudCliCredentialRefreshError({
268-
stage: "load-oauth-config",
269-
secretName: CLOUD_CLI_OAUTH_TOKEN_SECRET,
270-
cause,
271-
}),
353+
Effect.mapError((cause) =>
354+
CloudCliCredentialRefreshError.fromStage({
355+
stage: "load-oauth-config",
356+
cause,
357+
}),
272358
),
273359
);
274360
return yield* exchangeToken(metadata, {
275361
grant_type: "refresh_token",
276362
refresh_token: token.refreshToken,
277363
client_id: metadata.clientId,
278364
}).pipe(
279-
Effect.mapError(
280-
(cause) =>
281-
new CloudCliCredentialRefreshError({
282-
stage: "exchange-token",
283-
secretName: CLOUD_CLI_OAUTH_TOKEN_SECRET,
284-
tokenEndpoint: metadata.tokenEndpoint,
285-
cause,
286-
}),
365+
Effect.mapError((cause) =>
366+
CloudCliCredentialRefreshError.fromStage({
367+
stage: "exchange-token",
368+
tokenEndpoint: metadata.tokenEndpoint,
369+
cause,
370+
}),
287371
),
288372
);
289373
});
290374

291375
const login = Effect.fn("cloud.cli_token.login")(function* () {
292376
const metadata = yield* cloudCliOAuthConfig.pipe(
293-
Effect.mapError(
294-
(cause) =>
295-
new CloudCliAuthorizationError({
296-
stage: "load-oauth-config",
297-
secretName: CLOUD_CLI_OAUTH_TOKEN_SECRET,
298-
cause,
299-
}),
377+
Effect.mapError((cause) =>
378+
CloudCliAuthorizationError.fromStage({
379+
stage: "load-oauth-config",
380+
cause,
381+
}),
300382
),
301383
);
302384
const { challenge, state, verifier } = yield* Effect.gen(function* () {
@@ -307,14 +389,12 @@ export const make = Effect.gen(function* () {
307389
const state = yield* crypto.randomUUIDv4;
308390
return { challenge, state, verifier };
309391
}).pipe(
310-
Effect.mapError(
311-
(cause) =>
312-
new CloudCliAuthorizationError({
313-
stage: "prepare-pkce",
314-
secretName: CLOUD_CLI_OAUTH_TOKEN_SECRET,
315-
redirectUri: metadata.redirectUri,
316-
cause,
317-
}),
392+
Effect.mapError((cause) =>
393+
CloudCliAuthorizationError.fromStage({
394+
stage: "prepare-pkce",
395+
redirectUri: metadata.redirectUri,
396+
cause,
397+
}),
318398
),
319399
);
320400
const callback = yield* Deferred.make<string>();
@@ -353,16 +433,14 @@ export const make = Effect.gen(function* () {
353433
}),
354434
),
355435
Layer.build,
356-
Effect.mapError(
357-
(cause) =>
358-
new CloudCliAuthorizationError({
359-
stage: "start-callback-server",
360-
secretName: CLOUD_CLI_OAUTH_TOKEN_SECRET,
361-
redirectUri: metadata.redirectUri,
362-
callbackHost: CLOUD_CLI_OAUTH_CALLBACK_HOST,
363-
callbackPort: CLOUD_CLI_OAUTH_CALLBACK_PORT,
364-
cause,
365-
}),
436+
Effect.mapError((cause) =>
437+
CloudCliAuthorizationError.fromStage({
438+
stage: "start-callback-server",
439+
redirectUri: metadata.redirectUri,
440+
callbackHost: CLOUD_CLI_OAUTH_CALLBACK_HOST,
441+
callbackPort: CLOUD_CLI_OAUTH_CALLBACK_PORT,
442+
cause,
443+
}),
366444
),
367445
);
368446
const authorizationUrl = new URL(metadata.authorizationEndpoint);
@@ -379,7 +457,7 @@ export const make = Effect.gen(function* () {
379457
Effect.catchTags({
380458
TimeoutError: (cause) =>
381459
Effect.fail(
382-
new CloudCliAuthorizationTimeoutError({
460+
CloudCliAuthorizationTimeoutError.fromRedirectUri({
383461
redirectUri: metadata.redirectUri,
384462
timeoutMillis: Duration.toMillis(CLOUD_CLI_OAUTH_CALLBACK_TIMEOUT),
385463
cause,
@@ -394,15 +472,13 @@ export const make = Effect.gen(function* () {
394472
client_id: metadata.clientId,
395473
code_verifier: verifier,
396474
}).pipe(
397-
Effect.mapError(
398-
(cause) =>
399-
new CloudCliAuthorizationError({
400-
stage: "exchange-token",
401-
secretName: CLOUD_CLI_OAUTH_TOKEN_SECRET,
402-
tokenEndpoint: metadata.tokenEndpoint,
403-
redirectUri: metadata.redirectUri,
404-
cause,
405-
}),
475+
Effect.mapError((cause) =>
476+
CloudCliAuthorizationError.fromStage({
477+
stage: "exchange-token",
478+
tokenEndpoint: metadata.tokenEndpoint,
479+
redirectUri: metadata.redirectUri,
480+
cause,
481+
}),
406482
),
407483
);
408484
});

0 commit comments

Comments
 (0)