Skip to content

Commit 62192e0

Browse files
juliusmarmingecodex
andcommitted
Redact managed relay request diagnostics
Co-authored-by: codex <[email protected]>
1 parent b7c9164 commit 62192e0

9 files changed

Lines changed: 306 additions & 104 deletions

File tree

‎apps/desktop/src/ipc/methods/sshEnvironment.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
bootstrapRemoteBearerSession,
33
fetchRemoteSessionState,
44
issueRemoteWebSocketTicket,
5-
RemoteEnvironmentAuthUndeclaredStatusError,
5+
isRemoteEnvironmentAuthUndeclaredStatusError,
66
type RemoteEnvironmentAuthError,
77
} from "@t3tools/client-runtime/authorization";
88
import { fetchRemoteEnvironmentDescriptor } from "@t3tools/client-runtime/environment";
@@ -52,10 +52,7 @@ const isEnvironmentRequestInvalidError = Schema.is(EnvironmentRequestInvalidErro
5252
const isEnvironmentScopeRequiredError = Schema.is(EnvironmentScopeRequiredError);
5353

5454
function readSshHttpStatus(cause: DesktopSshEnvironmentRequestCause): number | null {
55-
if (
56-
cause instanceof RemoteEnvironmentAuthUndeclaredStatusError ||
57-
cause instanceof SshHttpBridgeError
58-
) {
55+
if (isRemoteEnvironmentAuthUndeclaredStatusError(cause) || cause instanceof SshHttpBridgeError) {
5956
return cause.status ?? null;
6057
}
6158
if (isEnvironmentRequestInvalidError(cause)) {

‎apps/mobile/src/features/cloud/managedRelayLayer.ts‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,24 @@ const relayDpopSignerLayer = Layer.effect(
2828
),
2929
createProof: Effect.fn("mobile.managedRelayDpopSigner.createProof")(function* (input) {
3030
const proofKey = yield* loadProofKey.pipe(
31-
Effect.mapError(
32-
(error) =>
33-
new ManagedRelay.ManagedRelayDpopKeyLoadError({
34-
keyStore: "expo-secure-store",
35-
method: input.method,
36-
url: input.url,
37-
cause: error,
38-
}),
31+
Effect.mapError((error) =>
32+
ManagedRelay.ManagedRelayDpopKeyLoadError.fromTarget({
33+
keyStore: "expo-secure-store",
34+
method: input.method,
35+
url: input.url,
36+
cause: error,
37+
}),
3938
),
4039
);
4140
return yield* createDpopProof({ ...input, proofKey }).pipe(
4241
Effect.provideService(Crypto.Crypto, crypto),
4342
Effect.map((proof) => proof.proof),
44-
Effect.mapError(
45-
(error) =>
46-
new ManagedRelay.ManagedRelayDpopProofCreationError({
47-
method: input.method,
48-
url: input.url,
49-
cause: error,
50-
}),
43+
Effect.mapError((error) =>
44+
ManagedRelay.ManagedRelayDpopProofCreationError.fromTarget({
45+
method: input.method,
46+
url: input.url,
47+
cause: error,
48+
}),
5149
),
5250
);
5351
}),

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,24 @@ export const relayDpopSignerLayer = Layer.effect(
5050
),
5151
createProof: Effect.fn("web.managedRelayDpopSigner.createProof")(function* (input) {
5252
const proofKey = yield* loadOrCreateBrowserDpopKey.pipe(
53-
Effect.mapError(
54-
(error) =>
55-
new ManagedRelay.ManagedRelayDpopKeyLoadError({
56-
keyStore: "indexed-db",
57-
method: input.method,
58-
url: input.url,
59-
cause: error,
60-
}),
53+
Effect.mapError((error) =>
54+
ManagedRelay.ManagedRelayDpopKeyLoadError.fromTarget({
55+
keyStore: "indexed-db",
56+
method: input.method,
57+
url: input.url,
58+
cause: error,
59+
}),
6160
),
6261
);
6362
return yield* createBrowserDpopProof({ ...input, proofKey }).pipe(
6463
Effect.provideService(Crypto.Crypto, crypto),
6564
Effect.map((proof) => proof.proof),
66-
Effect.mapError(
67-
(error) =>
68-
new ManagedRelay.ManagedRelayDpopProofCreationError({
69-
method: input.method,
70-
url: input.url,
71-
cause: error,
72-
}),
65+
Effect.mapError((error) =>
66+
ManagedRelay.ManagedRelayDpopProofCreationError.fromTarget({
67+
method: input.method,
68+
url: input.url,
69+
cause: error,
70+
}),
7371
),
7472
);
7573
}),

‎packages/client-runtime/src/authorization/remote.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,9 @@ describe("remote environment authorization", () => {
391391

392392
expect(error).toBeInstanceOf(RemoteEnvironmentAuthTimeoutError);
393393
expect(error.message).toBe(
394-
"Remote environment endpoint http://remote.example.com/.well-known/t3/environment timed out after 25ms.",
394+
`Remote environment endpoint at host remote.example.com (${
395+
"http://remote.example.com/.well-known/t3/environment".length
396+
} URL characters) timed out after 25ms.`,
395397
);
396398
}).pipe(Effect.provide(TestClock.layer())),
397399
);
@@ -446,7 +448,9 @@ describe("remote environment authorization", () => {
446448

447449
expect(error).toBeInstanceOf(RemoteEnvironmentAuthInvalidJsonError);
448450
expect(error.message).toBe(
449-
"Remote environment endpoint returned an invalid response from https://remote.example.com/oauth/token.",
451+
`Remote environment endpoint at host remote.example.com (${
452+
"https://remote.example.com/oauth/token".length
453+
} URL characters) returned an invalid response.`,
450454
);
451455
}),
452456
);

‎packages/client-runtime/src/authorization/remote.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from "../rpc/http.ts";
1616

1717
export {
18+
isRemoteEnvironmentAuthUndeclaredStatusError,
1819
RemoteEnvironmentAuthFetchError,
1920
RemoteEnvironmentAuthInvalidJsonError,
2021
RemoteEnvironmentAuthTimeoutError,

‎packages/client-runtime/src/connection/errors.test.ts‎

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { describe, expect, it } from "@effect/vitest";
44

55
import { mapManagedRelayError, mapRemoteEnvironmentError } from "./errors.ts";
66
import * as ManagedRelay from "../relay/managedRelay.ts";
7-
import { RemoteEnvironmentAuthFetchError } from "../rpc/http.ts";
7+
import {
8+
RemoteEnvironmentAuthFetchError,
9+
RemoteEnvironmentAuthUndeclaredStatusError,
10+
} from "../rpc/http.ts";
811

912
describe("connection error mapping", () => {
1013
it("retains the managed relay request as the cause when classifying a protected error", () => {
@@ -65,15 +68,14 @@ describe("connection error mapping", () => {
6568

6669
it("retains local transport failures without deriving their message from the cause", () => {
6770
const transportCause = new Error("sensitive transport implementation detail");
68-
const source = new RemoteEnvironmentAuthFetchError({
69-
requestUrl: "https://environment.example.test/api/auth/session",
70-
cause: transportCause,
71-
});
71+
const requestUrl =
72+
"https://environment-user:[email protected]/private/session?access_token=environment-secret#environment-fragment";
73+
const source = RemoteEnvironmentAuthFetchError.fromRequestUrl(requestUrl, transportCause);
7274

7375
const error = mapRemoteEnvironmentError(source);
7476

7577
expect(source.message).toBe(
76-
"Failed to fetch remote environment endpoint https://environment.example.test/api/auth/session.",
78+
`Failed to fetch remote environment endpoint at host environment.example.test (${requestUrl.length} URL characters).`,
7779
);
7880
expect(source.message).not.toContain(transportCause.message);
7981
expect(error).toMatchObject({
@@ -82,5 +84,37 @@ describe("connection error mapping", () => {
8284
});
8385
expect(error.cause).toBe(source);
8486
expect(source.cause).toBe(transportCause);
87+
expect(source).toMatchObject({
88+
requestUrlInputLength: requestUrl.length,
89+
requestUrlProtocol: "https:",
90+
requestUrlHostname: "environment.example.test",
91+
});
92+
const diagnostics = JSON.stringify(source);
93+
for (const secret of [
94+
"environment-user",
95+
"environment-password",
96+
"/private/session",
97+
"environment-secret",
98+
"environment-fragment",
99+
]) {
100+
expect(diagnostics).not.toContain(secret);
101+
expect(source.message).not.toContain(secret);
102+
}
103+
});
104+
105+
it("retains the HTTP client cause for undeclared statuses", () => {
106+
const cause = new Error("upstream response metadata");
107+
const requestUrl =
108+
"https://environment-user:[email protected]/private/session?access_token=environment-secret#environment-fragment";
109+
const error = RemoteEnvironmentAuthUndeclaredStatusError.fromRequestUrl(requestUrl, 502, cause);
110+
111+
expect(error).toMatchObject({
112+
status: 502,
113+
requestUrlInputLength: requestUrl.length,
114+
requestUrlProtocol: "https:",
115+
requestUrlHostname: "environment.example.test",
116+
});
117+
expect(error.cause).toBe(cause);
118+
expect(error).not.toHaveProperty("requestUrl");
85119
});
86120
});

‎packages/client-runtime/src/relay/managedRelay.test.ts‎

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,65 @@ function clerkToken(subject: string, nonce: string): string {
3939
}
4040

4141
describe("ManagedRelayClient", () => {
42+
it("redacts relay URLs and DPoP targets while preserving causes", () => {
43+
const relayUrl =
44+
"http://relay-user:[email protected]/private/workspace?access_token=relay-secret#relay-fragment";
45+
const dpopTarget =
46+
"https://dpop-user:[email protected]/v1/client/dpop-token?access_token=dpop-secret#dpop-fragment";
47+
const cause = new Error("proof failed");
48+
const invalidUrlError = ManagedRelay.ManagedRelayUrlInvalidError.fromInput(relayUrl);
49+
const proofErrors = [
50+
ManagedRelay.ManagedRelayDpopKeyLoadError.fromTarget({
51+
keyStore: "indexed-db",
52+
method: "POST",
53+
url: dpopTarget,
54+
cause,
55+
}),
56+
ManagedRelay.ManagedRelayDpopProofCreationError.fromTarget({
57+
method: "POST",
58+
url: dpopTarget,
59+
cause,
60+
}),
61+
ManagedRelay.ManagedRelayTokenProofCreationError.fromTarget({
62+
method: "POST",
63+
url: dpopTarget,
64+
cause,
65+
}),
66+
ManagedRelay.ManagedRelayRequestProofCreationError.fromTarget({
67+
method: "POST",
68+
url: dpopTarget,
69+
cause,
70+
}),
71+
];
72+
73+
expect(invalidUrlError).toMatchObject({
74+
relayUrlInputLength: relayUrl.length,
75+
relayUrlProtocol: "http:",
76+
relayUrlHostname: "relay.example.test",
77+
});
78+
const invalidUrlDiagnostics = JSON.stringify(invalidUrlError);
79+
for (const secret of [
80+
"relay-user",
81+
"relay-password",
82+
"/private/workspace",
83+
"relay-secret",
84+
"relay-fragment",
85+
]) {
86+
expect(invalidUrlDiagnostics).not.toContain(secret);
87+
expect(invalidUrlError.message).not.toContain(secret);
88+
}
89+
90+
for (const error of proofErrors) {
91+
expect(error.requestTarget).toBe("https://relay.example.test/v1/client/dpop-token");
92+
expect(error.cause).toBe(cause);
93+
const diagnostics = JSON.stringify(error);
94+
for (const secret of ["dpop-user", "dpop-password", "dpop-secret", "dpop-fragment"]) {
95+
expect(diagnostics).not.toContain(secret);
96+
expect(error.message).not.toContain(secret);
97+
}
98+
}
99+
});
100+
42101
it.effect("owns tracing at service and implementation boundaries", () => {
43102
const spanNames: Array<string> = [];
44103
const tracer = Tracer.make({
@@ -124,7 +183,9 @@ describe("ManagedRelayClient", () => {
124183

125184
expect(error).toMatchObject({
126185
_tag: "ManagedRelayUrlInvalidError",
127-
relayUrl: "http://relay.example.test",
186+
relayUrlInputLength: "http://relay.example.test".length,
187+
relayUrlProtocol: "http:",
188+
relayUrlHostname: "relay.example.test",
128189
message: "Relay URL must be a secure absolute HTTPS origin.",
129190
});
130191
expect(requestCount).toBe(0);
@@ -161,7 +222,7 @@ describe("ManagedRelayClient", () => {
161222
thumbprint: Effect.succeed("client-thumbprint"),
162223
createProof: (input) =>
163224
Effect.fail(
164-
new ManagedRelay.ManagedRelayDpopKeyLoadError({
225+
ManagedRelay.ManagedRelayDpopKeyLoadError.fromTarget({
165226
keyStore: "indexed-db",
166227
method: input.method,
167228
url: input.url,

0 commit comments

Comments
 (0)