Skip to content

Commit 3d9edf0

Browse files
juliusmarmingecodex
andcommitted
Redact analytics endpoint diagnostics
Co-authored-by: codex <[email protected]>
1 parent 49dd46d commit 3d9edf0

2 files changed

Lines changed: 58 additions & 12 deletions

File tree

‎apps/server/src/telemetry/AnalyticsService.test.ts‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,17 @@ it.layer(NodeServices.layer)("AnalyticsService test", (it) => {
158158
assert.isDefined(deliveryLog);
159159
assert.equal(
160160
deliveryLog?.message,
161-
"Failed to deliver 1 analytics event to PostHog at '/batch/'.",
161+
"Failed to deliver 1 analytics event to PostHog (endpoint input length 7).",
162162
);
163+
assert.equal("endpoint" in deliveryLog.annotations, false);
164+
assert.equal(deliveryLog.annotations.endpointInputLength, 7);
163165

164166
const error = deliveryLog?.annotations.cause;
165167
assert.instanceOf(error, AnalyticsService.AnalyticsBatchDeliveryError);
166168
if (isAnalyticsBatchDeliveryError(error)) {
167-
assert.equal(error.endpoint, "/batch/");
169+
assert.equal(error.endpointInputLength, 7);
170+
assert.equal(error.endpointProtocol, undefined);
171+
assert.equal(error.endpointHostname, undefined);
168172
assert.equal(error.eventCount, 1);
169173
assert.instanceOf(error.cause, HttpClientError.HttpClientError);
170174
if (error.cause instanceof HttpClientError.HttpClientError) {
@@ -175,3 +179,21 @@ it.layer(NodeServices.layer)("AnalyticsService test", (it) => {
175179
}),
176180
);
177181
});
182+
183+
it("keeps configured PostHog endpoint secrets out of direct error and log context", () => {
184+
const endpoint =
185+
"https://user:[email protected]/private/project?api_key=secret#fragment";
186+
const cause = new Error("delivery failed");
187+
const error = AnalyticsService.AnalyticsBatchDeliveryError.fromEndpoint({
188+
endpoint,
189+
eventCount: 2,
190+
cause,
191+
});
192+
193+
assert.equal(error.endpointInputLength, endpoint.length);
194+
assert.equal(error.endpointProtocol, "https:");
195+
assert.equal(error.endpointHostname, "posthog.example.test");
196+
assert.equal(error.cause, cause);
197+
assert.equal("endpoint" in error, false);
198+
assert.equal(/user|password|private|project|api_key|secret|fragment/.test(error.message), false);
199+
});

‎apps/server/src/telemetry/AnalyticsService.ts‎

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @module AnalyticsService
88
*/
99
import { HostProcessArchitecture, HostProcessPlatform } from "@t3tools/shared/hostProcess";
10+
import { getUrlDiagnostics } from "@t3tools/shared/urlDiagnostics";
1011
import * as Config from "effect/Config";
1112
import * as Context from "effect/Context";
1213
import * as DateTime from "effect/DateTime";
@@ -47,14 +48,32 @@ const TelemetryEnvConfig = Config.all({
4748
export class AnalyticsBatchDeliveryError extends Schema.TaggedErrorClass<AnalyticsBatchDeliveryError>()(
4849
"AnalyticsBatchDeliveryError",
4950
{
50-
endpoint: Schema.String,
51+
endpointInputLength: Schema.Number,
52+
endpointProtocol: Schema.optionalKey(Schema.String),
53+
endpointHostname: Schema.optionalKey(Schema.String),
5154
eventCount: Schema.Int.check(Schema.isGreaterThan(0)),
5255
cause: Schema.Defect(),
5356
},
5457
) {
58+
static fromEndpoint(input: {
59+
readonly endpoint: string;
60+
readonly eventCount: number;
61+
readonly cause: unknown;
62+
}): AnalyticsBatchDeliveryError {
63+
const diagnostics = getUrlDiagnostics(input.endpoint);
64+
return new AnalyticsBatchDeliveryError({
65+
endpointInputLength: diagnostics.inputLength,
66+
...(diagnostics.protocol === undefined ? {} : { endpointProtocol: diagnostics.protocol }),
67+
...(diagnostics.hostname === undefined ? {} : { endpointHostname: diagnostics.hostname }),
68+
eventCount: input.eventCount,
69+
cause: input.cause,
70+
});
71+
}
72+
5573
override get message(): string {
5674
const eventLabel = this.eventCount === 1 ? "event" : "events";
57-
return `Failed to deliver ${this.eventCount} analytics ${eventLabel} to PostHog at '${this.endpoint}'.`;
75+
const destination = this.endpointHostname ? ` at ${this.endpointHostname}` : "";
76+
return `Failed to deliver ${this.eventCount} analytics ${eventLabel} to PostHog${destination} (endpoint input length ${this.endpointInputLength}).`;
5877
}
5978
}
6079

@@ -146,13 +165,12 @@ export const make = Effect.gen(function* () {
146165
HttpClientRequest.bodyJson(payload),
147166
Effect.flatMap(httpClient.execute),
148167
Effect.flatMap(HttpClientResponse.filterStatusOk),
149-
Effect.mapError(
150-
(cause) =>
151-
new AnalyticsBatchDeliveryError({
152-
endpoint: batchEndpoint,
153-
eventCount: events.length,
154-
cause,
155-
}),
168+
Effect.mapError((cause) =>
169+
AnalyticsBatchDeliveryError.fromEndpoint({
170+
endpoint: batchEndpoint,
171+
eventCount: events.length,
172+
cause,
173+
}),
156174
),
157175
);
158176
});
@@ -186,7 +204,13 @@ export const make = Effect.gen(function* () {
186204
AnalyticsBatchDeliveryError: (error) =>
187205
Effect.logError(error.message).pipe(
188206
Effect.annotateLogs({
189-
endpoint: error.endpoint,
207+
endpointInputLength: error.endpointInputLength,
208+
...(error.endpointProtocol === undefined
209+
? {}
210+
: { endpointProtocol: error.endpointProtocol }),
211+
...(error.endpointHostname === undefined
212+
? {}
213+
: { endpointHostname: error.endpointHostname }),
190214
eventCount: error.eventCount,
191215
cause: error,
192216
}),

0 commit comments

Comments
 (0)