Skip to content

Commit 6ea4fd3

Browse files
juliusmarmingecodex
andcommitted
Redact relay discovery endpoint diagnostics
Co-authored-by: codex <[email protected]>
1 parent 62192e0 commit 6ea4fd3

2 files changed

Lines changed: 61 additions & 4 deletions

File tree

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

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as Deferred from "effect/Deferred";
88
import * as Effect from "effect/Effect";
99
import * as Fiber from "effect/Fiber";
1010
import * as Layer from "effect/Layer";
11+
import * as Logger from "effect/Logger";
1112
import * as Option from "effect/Option";
1213
import * as Ref from "effect/Ref";
1314
import * as Stream from "effect/Stream";
@@ -55,7 +56,9 @@ function status(
5556
};
5657
}
5758

58-
const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
59+
const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* (
60+
testEnvironments: ReadonlyArray<RelayClientEnvironmentRecord> = environments,
61+
) {
5962
const networkStatus = yield* SubscriptionRef.make<NetworkStatus>("online");
6063
const listCalls = yield* Ref.make(0);
6164
const listFailure = yield* Ref.make<ManagedRelay.ManagedRelayClientError | null>(null);
@@ -74,7 +77,7 @@ const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
7477
Deferred.Deferred<RelayEnvironmentStatusResponse, ManagedRelay.ManagedRelayClientError>
7578
>(),
7679
);
77-
for (const environment of environments) {
80+
for (const environment of testEnvironments) {
7881
const request = yield* Deferred.make<
7982
RelayEnvironmentStatusResponse,
8083
ManagedRelay.ManagedRelayClientError
@@ -98,7 +101,7 @@ const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
98101
if (failure) {
99102
return yield* failure;
100103
}
101-
return environments;
104+
return testEnvironments;
102105
}),
103106
getEnvironmentStatus: ({ environmentId }) =>
104107
Ref.get(statusRequests).pipe(
@@ -170,6 +173,52 @@ const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
170173
});
171174

172175
describe("RelayEnvironmentDiscovery", () => {
176+
it.effect("keeps managed endpoint secrets out of offline warnings", () => {
177+
const httpBaseUrl =
178+
"https://endpoint-user:[email protected]/private/workspace?access_token=endpoint-secret#endpoint-fragment";
179+
const environment = {
180+
...environments[0]!,
181+
endpoint: {
182+
...environments[0]!.endpoint,
183+
httpBaseUrl,
184+
},
185+
} satisfies RelayClientEnvironmentRecord;
186+
const capturedLogs: Array<ReadonlyArray<unknown>> = [];
187+
const logger = Logger.make(({ message }) => {
188+
capturedLogs.push(Array.isArray(message) ? message : [message]);
189+
});
190+
191+
return Effect.gen(function* () {
192+
const harness = yield* makeHarness([environment]);
193+
yield* Effect.gen(function* () {
194+
const discovery = yield* RelayEnvironmentDiscovery.RelayEnvironmentDiscovery;
195+
const refreshFiber = yield* Effect.forkChild(discovery.refresh);
196+
const requests = yield* Ref.get(harness.statusRequests);
197+
yield* Deferred.succeed(
198+
requests.get(environment.environmentId)!,
199+
status(environment, "offline"),
200+
);
201+
yield* Fiber.join(refreshFiber);
202+
203+
expect(capturedLogs).toHaveLength(1);
204+
const logFields = capturedLogs[0]?.find(
205+
(value): value is Record<string, unknown> => typeof value === "object" && value !== null,
206+
);
207+
expect(logFields).toMatchObject({
208+
environmentId: environment.environmentId,
209+
endpointInputLength: httpBaseUrl.length,
210+
endpointProtocol: "https:",
211+
endpointHostname: "offline.example.test",
212+
});
213+
expect(logFields).not.toHaveProperty("endpoint");
214+
}).pipe(
215+
Effect.provide(
216+
Layer.merge(harness.layer, Logger.layer([logger], { mergeWithExisting: false })),
217+
),
218+
);
219+
});
220+
});
221+
173222
it.effect("publishes each environment status as soon as that lookup completes", () =>
174223
Effect.gen(function* () {
175224
const harness = yield* makeHarness();

‎packages/client-runtime/src/relay/discovery.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
RelayEnvironmentStatusResponse,
44
} from "@t3tools/contracts/relay";
55
import { decodeRelayJwt } from "@t3tools/shared/relayJwt";
6+
import { getUrlDiagnostics } from "@t3tools/shared/urlDiagnostics";
67
import {
78
RelayEnvironmentConnectScope,
89
RelayEnvironmentStatusScope,
@@ -178,9 +179,16 @@ export const make = Effect.fn("RelayEnvironmentDiscovery.make")(function* () {
178179
return [true, new Map(current).set(environment.environmentId, fingerprint)];
179180
});
180181
if (shouldReport) {
182+
const endpointDiagnostics = getUrlDiagnostics(result.success.endpoint.httpBaseUrl);
181183
yield* Effect.logWarning("Relay environment health check reported offline", {
182184
environmentId: result.success.environmentId,
183-
endpoint: result.success.endpoint.httpBaseUrl,
185+
endpointInputLength: endpointDiagnostics.inputLength,
186+
...(endpointDiagnostics.protocol === undefined
187+
? {}
188+
: { endpointProtocol: endpointDiagnostics.protocol }),
189+
...(endpointDiagnostics.hostname === undefined
190+
? {}
191+
: { endpointHostname: endpointDiagnostics.hostname }),
184192
message: result.success.error,
185193
traceId: result.success.traceId,
186194
});

0 commit comments

Comments
 (0)