Skip to content

Commit d25d615

Browse files
juliusmarmingecodex
andcommitted
Redact web cloud endpoint diagnostics
Co-authored-by: codex <[email protected]>
1 parent ca2b9f1 commit d25d615

2 files changed

Lines changed: 70 additions & 11 deletions

File tree

‎apps/web/src/cloud/linkEnvironment.test.ts‎

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ describe("web cloud link environment client", () => {
221221
expect(error).toBeInstanceOf(CloudEnvironmentLinkOperationError);
222222
expect(error).toMatchObject({
223223
action: "list relay-managed environments",
224-
relayUrl: "https://relay.example.test",
224+
relayUrlInputLength: "https://relay.example.test".length,
225+
relayUrlProtocol: "https:",
226+
relayUrlHostname: "relay.example.test",
225227
traceId: "trace-web-cloud-link",
226228
relayError: {
227229
_tag: "RelayAuthInvalidError",
@@ -289,7 +291,9 @@ describe("web cloud link environment client", () => {
289291
expect(error).toMatchObject({
290292
action: "read environment cloud link state",
291293
environmentId: TARGET.environmentId,
292-
httpBaseUrl: TARGET.httpBaseUrl,
294+
httpBaseUrlInputLength: TARGET.httpBaseUrl.length,
295+
httpBaseUrlProtocol: "http:",
296+
httpBaseUrlHostname: "127.0.0.1",
293297
environmentError: {
294298
_tag: "EnvironmentHttpUnauthorizedError",
295299
message: "Environment bearer token is invalid.",
@@ -305,11 +309,13 @@ describe("web cloud link environment client", () => {
305309

306310
it.effect("preserves invalid environment HTTP URL parser causes", () =>
307311
Effect.gen(function* () {
312+
const invalidUrl =
313+
"https://user:password@[invalid-host]/private/path?access_token=secret#fragment";
308314
const error = yield* withServices(
309315
readPrimaryCloudLinkState({
310316
target: {
311317
...TARGET,
312-
httpBaseUrl: "not a URL",
318+
httpBaseUrl: invalidUrl,
313319
},
314320
}),
315321
).pipe(Effect.flip);
@@ -318,12 +324,40 @@ describe("web cloud link environment client", () => {
318324
_tag: "CloudEnvironmentLinkOperationError",
319325
action: "initialize the environment HTTP client",
320326
environmentId: TARGET.environmentId,
321-
httpBaseUrl: "not a URL",
327+
httpBaseUrlInputLength: invalidUrl.length,
322328
});
323329
expect(error.cause).toBeInstanceOf(TypeError);
330+
expect(error).not.toHaveProperty("httpBaseUrl");
331+
expect(error).not.toHaveProperty("httpBaseUrlProtocol");
332+
expect(error).not.toHaveProperty("httpBaseUrlHostname");
333+
expect(error.message).not.toMatch(/user|password|private|path|access_token|secret|fragment/);
324334
}),
325335
);
326336

337+
it("keeps environment endpoint secrets out of mapped error attributes", () => {
338+
const httpBaseUrl =
339+
"https://user:[email protected]/private/path?access_token=secret#fragment";
340+
const cause = new Error("request failed");
341+
const error = CloudEnvironmentLinkOperationError.fromEnvironmentApi({
342+
action: "read environment cloud link state",
343+
environmentId: TARGET.environmentId,
344+
httpBaseUrl,
345+
cause,
346+
});
347+
348+
expect(error).toMatchObject({
349+
httpBaseUrlInputLength: httpBaseUrl.length,
350+
httpBaseUrlProtocol: "https:",
351+
httpBaseUrlHostname: "environment.example.test",
352+
cause,
353+
});
354+
expect(error.cause).toBe(cause);
355+
expect(error).not.toHaveProperty("httpBaseUrl");
356+
const diagnostics = JSON.stringify(error);
357+
expect(diagnostics).not.toMatch(/user|password|private|path|access_token|secret|fragment/);
358+
expect(error.message).not.toMatch(/user|password|private|path|access_token|secret|fragment/);
359+
});
360+
327361
it.effect("uses desktop bearer auth for primary cloud link state", () =>
328362
Effect.gen(function* () {
329363
const fetchMock = vi.fn().mockResolvedValue(

‎apps/web/src/cloud/linkEnvironment.ts‎

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { EnvironmentRegistry } from "@t3tools/client-runtime/connection";
2525
import { request, runStream } from "@t3tools/client-runtime/rpc";
2626
import { makeEnvironmentHttpApiClient } from "@t3tools/client-runtime/rpc";
2727
import { ManagedRelay } from "@t3tools/client-runtime/relay";
28+
import { getUrlDiagnostics } from "@t3tools/shared/urlDiagnostics";
2829

2930
import {
3031
readPrimaryEnvironmentDescriptor,
@@ -61,6 +62,26 @@ const EnvironmentCloudApiError = Schema.Union([
6162
type EnvironmentCloudApiError = typeof EnvironmentCloudApiError.Type;
6263
const isEnvironmentCloudApiError = Schema.is(EnvironmentCloudApiError);
6364

65+
function relayUrlDiagnosticFields(relayUrl: string | undefined) {
66+
if (relayUrl === undefined) return {};
67+
const diagnostics = getUrlDiagnostics(relayUrl);
68+
return {
69+
relayUrlInputLength: diagnostics.inputLength,
70+
...(diagnostics.protocol === undefined ? {} : { relayUrlProtocol: diagnostics.protocol }),
71+
...(diagnostics.hostname === undefined ? {} : { relayUrlHostname: diagnostics.hostname }),
72+
};
73+
}
74+
75+
function httpBaseUrlDiagnosticFields(httpBaseUrl: string | undefined) {
76+
if (httpBaseUrl === undefined) return {};
77+
const diagnostics = getUrlDiagnostics(httpBaseUrl);
78+
return {
79+
httpBaseUrlInputLength: diagnostics.inputLength,
80+
...(diagnostics.protocol === undefined ? {} : { httpBaseUrlProtocol: diagnostics.protocol }),
81+
...(diagnostics.hostname === undefined ? {} : { httpBaseUrlHostname: diagnostics.hostname }),
82+
};
83+
}
84+
6485
export const CloudEnvironmentLinkAction = Schema.Literals([
6586
"check relay client availability",
6687
"confirm relay client installation",
@@ -85,8 +106,12 @@ export class CloudEnvironmentLinkOperationError extends Schema.TaggedErrorClass<
85106
{
86107
action: CloudEnvironmentLinkAction,
87108
environmentId: Schema.optionalKey(Schema.String),
88-
relayUrl: Schema.optionalKey(Schema.String),
89-
httpBaseUrl: Schema.optionalKey(Schema.String),
109+
relayUrlInputLength: Schema.optionalKey(Schema.Number),
110+
relayUrlProtocol: Schema.optionalKey(Schema.String),
111+
relayUrlHostname: Schema.optionalKey(Schema.String),
112+
httpBaseUrlInputLength: Schema.optionalKey(Schema.Number),
113+
httpBaseUrlProtocol: Schema.optionalKey(Schema.String),
114+
httpBaseUrlHostname: Schema.optionalKey(Schema.String),
90115
traceId: Schema.optionalKey(Schema.String),
91116
relayError: Schema.optionalKey(RelayProtectedError),
92117
environmentError: Schema.optionalKey(EnvironmentCloudApiError),
@@ -106,8 +131,8 @@ export class CloudEnvironmentLinkOperationError extends Schema.TaggedErrorClass<
106131
action: input.action,
107132
cause: input.cause,
108133
...(input.environmentId === undefined ? {} : { environmentId: input.environmentId }),
109-
...(input.relayUrl === undefined ? {} : { relayUrl: input.relayUrl }),
110-
...(input.httpBaseUrl === undefined ? {} : { httpBaseUrl: input.httpBaseUrl }),
134+
...relayUrlDiagnosticFields(input.relayUrl),
135+
...httpBaseUrlDiagnosticFields(input.httpBaseUrl),
111136
...(requestFailure?.traceId === undefined ? {} : { traceId: requestFailure.traceId }),
112137
...(requestFailure?.relayError === undefined
113138
? {}
@@ -127,7 +152,7 @@ export class CloudEnvironmentLinkOperationError extends Schema.TaggedErrorClass<
127152
return new CloudEnvironmentLinkOperationError({
128153
action: input.action,
129154
environmentId: input.environmentId,
130-
httpBaseUrl: input.httpBaseUrl,
155+
...httpBaseUrlDiagnosticFields(input.httpBaseUrl),
131156
cause: input.cause,
132157
...(environmentError === undefined ? {} : { environmentError }),
133158
});
@@ -339,7 +364,7 @@ function endpointOrigin(input: { readonly environmentId: string; readonly httpBa
339364
new CloudEnvironmentLinkOperationError({
340365
action: "derive the environment endpoint origin",
341366
environmentId: input.environmentId,
342-
httpBaseUrl: input.httpBaseUrl,
367+
...httpBaseUrlDiagnosticFields(input.httpBaseUrl),
343368
cause,
344369
}),
345370
});
@@ -355,7 +380,7 @@ function makeCloudEnvironmentHttpApiClient(input: {
355380
new CloudEnvironmentLinkOperationError({
356381
action: "initialize the environment HTTP client",
357382
environmentId: input.environmentId,
358-
httpBaseUrl: input.httpBaseUrl,
383+
...httpBaseUrlDiagnosticFields(input.httpBaseUrl),
359384
cause,
360385
}),
361386
}).pipe(Effect.flatten);

0 commit comments

Comments
 (0)