Skip to content

Commit d64de0a

Browse files
committed
fix(desktop): preserve updater failure causes in diagnostics
1 parent 511d20c commit d64de0a

4 files changed

Lines changed: 185 additions & 3 deletions

File tree

‎apps/desktop/src/updates/DesktopUpdates.test.ts‎

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,10 @@ describe("DesktopUpdates", () => {
430430
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
431431
});
432432

433-
it.effect("logs bounded updater failure context without exposing the cause", () => {
434-
const cause = new Error(
435-
"request failed for https://user:[email protected]/update?token=secret",
433+
it.effect("logs the redacted cause of a rejected update check", () => {
434+
const cause = Object.assign(
435+
new Error("request failed for https://user:[email protected]/update?token=secret"),
436+
{ code: "ERR_UPDATER_LATEST_VERSION_NOT_FOUND" },
436437
);
437438
const updaterError = new ElectronUpdater.ElectronUpdaterCheckForUpdatesError({
438439
channel: null,
@@ -459,6 +460,10 @@ describe("DesktopUpdates", () => {
459460
assert.isDefined(loggedAnnotation);
460461
assert.equal(loggedAnnotation.errorTag, "ElectronUpdaterCheckForUpdatesError");
461462
assert.isNull(loggedAnnotation.channel);
463+
assert.equal(
464+
loggedAnnotation.cause,
465+
"Error [ERR_UPDATER_LATEST_VERSION_NOT_FOUND]: request failed for [redacted URL]",
466+
);
462467
assert.notProperty(loggedAnnotation, "error");
463468
assert.notInclude(Object.values(loggedAnnotation).map(String).join(" "), "secret");
464469
assert.equal(
@@ -478,6 +483,49 @@ describe("DesktopUpdates", () => {
478483
);
479484
});
480485

486+
it.effect("logs the redacted cause of an updater error event", () => {
487+
const harness = makeHarness();
488+
const cause = Object.assign(new Error("404 Not Found\nHeaders: authorization=secret"), {
489+
code: "ERR_UPDATER_CHANNEL_FILE_NOT_FOUND",
490+
});
491+
const loggedAnnotations: Array<Record<string, unknown>> = [];
492+
const logger = Logger.make(({ fiber }) => {
493+
const annotations = fiber.getRef(References.CurrentLogAnnotations);
494+
if (annotations.errorTag === "DesktopUpdaterReportedError") {
495+
loggedAnnotations.push(annotations);
496+
}
497+
});
498+
499+
return Effect.scoped(
500+
Effect.gen(function* () {
501+
const updates = yield* DesktopUpdates.DesktopUpdates;
502+
yield* updates.configure;
503+
504+
harness.emit("error", cause);
505+
yield* flushCallbacks;
506+
507+
const loggedAnnotation = loggedAnnotations.at(-1);
508+
assert.isDefined(loggedAnnotation);
509+
assert.equal(loggedAnnotation.operation, "background");
510+
assert.equal(
511+
loggedAnnotation.cause,
512+
"Error [ERR_UPDATER_CHANNEL_FILE_NOT_FOUND]: 404 Not Found",
513+
);
514+
assert.notInclude(JSON.stringify(loggedAnnotations), "secret");
515+
const state = yield* updates.getState;
516+
assert.equal(state.message, "Desktop updater background operation reported an error.");
517+
}),
518+
).pipe(
519+
Effect.provide(
520+
Layer.mergeAll(
521+
TestClock.layer(),
522+
harness.layer,
523+
Logger.layer([logger], { mergeWithExisting: false }),
524+
),
525+
),
526+
);
527+
});
528+
481529
it.effect("recovers download state after an unexpected setup failure", () => {
482530
let disableDifferentialCalls = 0;
483531
const harness = makeHarness({

‎apps/desktop/src/updates/DesktopUpdates.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import * as IpcChannels from "../ipc/channels.ts";
3434
import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
3535
import { normalizeDesktopUpdateReleaseNotes } from "./releaseNotes.ts";
3636
import { resolveDefaultDesktopUpdateChannel } from "./updateChannels.ts";
37+
import { describeUpdateErrorCause } from "./updateDiagnostics.ts";
3738
import {
3839
createInitialDesktopUpdateState,
3940
reduceDesktopUpdateStateOnCheckFailure,
@@ -442,6 +443,7 @@ export const make = Effect.gen(function* () {
442443
);
443444
yield* logUpdaterError(error.message, {
444445
errorTag: error._tag,
446+
cause: describeUpdateErrorCause(error.cause),
445447
channel: error.channel,
446448
});
447449
return true;
@@ -485,6 +487,7 @@ export const make = Effect.gen(function* () {
485487
);
486488
yield* logUpdaterError(error.message, {
487489
errorTag: error._tag,
490+
cause: describeUpdateErrorCause(error.cause),
488491
channel: error.channel,
489492
});
490493
return { accepted: true, completed: false };
@@ -507,6 +510,7 @@ export const make = Effect.gen(function* () {
507510
);
508511
yield* logUpdaterError(error.message, {
509512
errorTag: error._tag,
513+
cause: describeUpdateErrorCause(error.cause),
510514
action: error.action,
511515
});
512516
return { accepted: true, completed: false };
@@ -655,6 +659,7 @@ export const make = Effect.gen(function* () {
655659
yield* recoverFailedInstall(error.message);
656660
yield* logUpdaterError(error.message, {
657661
errorTag: error._tag,
662+
cause: describeUpdateErrorCause(error.cause),
658663
channel: error.channel,
659664
isSilent: error.isSilent,
660665
isForceRunAfter: error.isForceRunAfter,
@@ -673,6 +678,7 @@ export const make = Effect.gen(function* () {
673678
yield* recoverFailedInstall(error.message);
674679
yield* logUpdaterError(error.message, {
675680
errorTag: error._tag,
681+
cause: describeUpdateErrorCause(error.cause),
676682
action: error.action,
677683
});
678684
return { accepted: true, completed: false, failed: true };
@@ -712,6 +718,7 @@ export const make = Effect.gen(function* () {
712718
const error = new DesktopUpdatePollerError({ poller: "startup", cause });
713719
return logUpdaterError(error.message, {
714720
errorTag: error._tag,
721+
cause: describeUpdateErrorCause(error.cause),
715722
poller: error.poller,
716723
});
717724
}),
@@ -727,6 +734,7 @@ export const make = Effect.gen(function* () {
727734
const error = new DesktopUpdatePollerError({ poller: "poll", cause });
728735
return logUpdaterError(error.message, {
729736
errorTag: error._tag,
737+
cause: describeUpdateErrorCause(error.cause),
730738
poller: error.poller,
731739
});
732740
}),
@@ -782,6 +790,7 @@ export const make = Effect.gen(function* () {
782790
const error = new DesktopUpdateEventHandlingError({ event: "update-available", cause });
783791
return logUpdaterWarning(error.message, {
784792
errorTag: error._tag,
793+
cause: describeUpdateErrorCause(error.cause),
785794
event: error.event,
786795
});
787796
}),
@@ -811,6 +820,7 @@ export const make = Effect.gen(function* () {
811820
yield* recoverFailedInstall(error.message);
812821
yield* logUpdaterError(error.message, {
813822
errorTag: error._tag,
823+
cause: describeUpdateErrorCause(error.cause),
814824
operation: error.operation,
815825
});
816826
return;
@@ -831,6 +841,7 @@ export const make = Effect.gen(function* () {
831841

832842
yield* logUpdaterError(error.message, {
833843
errorTag: error._tag,
844+
cause: describeUpdateErrorCause(error.cause),
834845
operation: error.operation,
835846
});
836847
});
@@ -861,6 +872,7 @@ export const make = Effect.gen(function* () {
861872
const error = new DesktopUpdateEventHandlingError({ event: "download-progress", cause });
862873
return logUpdaterWarning(error.message, {
863874
errorTag: error._tag,
875+
cause: describeUpdateErrorCause(error.cause),
864876
event: error.event,
865877
});
866878
}),
@@ -885,6 +897,7 @@ export const make = Effect.gen(function* () {
885897
const error = new DesktopUpdateEventHandlingError({ event: "update-downloaded", cause });
886898
return logUpdaterWarning(error.message, {
887899
errorTag: error._tag,
900+
cause: describeUpdateErrorCause(error.cause),
888901
event: error.event,
889902
});
890903
}),
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { assert, describe, it } from "@effect/vitest";
2+
import * as Cause from "effect/Cause";
3+
4+
import { describeUpdateErrorCause } from "./updateDiagnostics.ts";
5+
6+
describe("describeUpdateErrorCause", () => {
7+
it("preserves network and nested failure reasons", () => {
8+
const networkError = Object.assign(new Error("getaddrinfo ENOTFOUND updates.example.com"), {
9+
code: "ENOTFOUND",
10+
});
11+
const cause = new Error("fetch failed", { cause: networkError });
12+
13+
assert.equal(
14+
describeUpdateErrorCause(Cause.fail(cause)),
15+
"Error: fetch failed; caused by Error [ENOTFOUND]: getaddrinfo ENOTFOUND updates.example.com",
16+
);
17+
assert.include(describeUpdateErrorCause(Cause.die(networkError)), "[ENOTFOUND]");
18+
});
19+
20+
it("removes URLs, credentials, response bodies, and headers from summaries", () => {
21+
const messages = [
22+
"Cannot download https://user:[email protected]/update?token=query-secret, status 403",
23+
"Request failed: Bearer bearer-secret",
24+
"Request failed: Basic basic-secret",
25+
"Request failed: Authorization: Bearer header-secret",
26+
"Request failed: Cookie=session-secret",
27+
"Request failed: api_key=\"key-secret\" password='password-secret' access_token=token-secret",
28+
'404 Not Found\n{"body":"body-secret"}\nHeaders: cookie=header-secret',
29+
"Request failed\rHeaders: cookie=header-secret",
30+
];
31+
32+
for (const message of messages) {
33+
const summary = describeUpdateErrorCause(new Error(message));
34+
assert.include(summary, "Error:");
35+
assert.notInclude(summary, "secret");
36+
assert.notInclude(summary, "https://");
37+
assert.notInclude(summary, "Headers:");
38+
}
39+
assert.equal(describeUpdateErrorCause("404 Not Found\nbody-secret"), "404 Not Found");
40+
});
41+
42+
it("bounds long messages, parallel failures, and cyclic causes", () => {
43+
const longError = new Error("x".repeat(10_000));
44+
const parallel = Cause.combine(Cause.fail(longError), Cause.die(longError));
45+
assert.isAtMost(describeUpdateErrorCause(parallel).length, 1024);
46+
47+
const cyclic = new Error("retry failed");
48+
cyclic.cause = cyclic;
49+
assert.include(describeUpdateErrorCause(cyclic), "[truncated cause]");
50+
assert.isAtMost(describeUpdateErrorCause(cyclic).length, 1024);
51+
});
52+
53+
it("does not serialize arbitrary payloads or let unreadable errors break logging", () => {
54+
assert.equal(describeUpdateErrorCause({ payload: "secret" }), "object");
55+
const unreadable = Object.defineProperty({}, "message", {
56+
get() {
57+
throw new Error("unreadable error");
58+
},
59+
});
60+
assert.equal(describeUpdateErrorCause(unreadable), "UnknownError");
61+
});
62+
});
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { errorTag } from "@t3tools/shared/observability";
2+
import * as Cause from "effect/Cause";
3+
4+
function redactMessage(message: string): string {
5+
// Updater HTTP errors append response bodies and headers after the first line.
6+
return (message.split(/[\r\n]/, 1)[0] ?? "")
7+
.replace(/\b(?:https?|file):\/\/[^\s"'<>]+/gi, "[redacted URL]")
8+
.replace(/\b(?:Bearer|Basic)\s+[^\s,;]+/gi, "[redacted credential]")
9+
.replace(
10+
/\b(?:authorization|proxy-authorization|cookie|set-cookie)\s*[:=].*$/gi,
11+
"[redacted header]",
12+
)
13+
.replace(
14+
/\b[\w-]*(?:token|password|secret|credential|api[_-]?key)[\w-]*\s*[:=]\s*(?:"[^"]*"|'[^']*'|[^\s,;]+)/gi,
15+
"[redacted credential]",
16+
)
17+
.slice(0, 512);
18+
}
19+
20+
function summarizeCause(cause: unknown, depth: number): string {
21+
if (depth >= 3) return "[truncated cause]";
22+
if (Cause.isCause(cause)) {
23+
return cause.reasons
24+
.slice(0, 3)
25+
.map((reason) =>
26+
reason._tag === "Fail"
27+
? summarizeCause(reason.error, depth + 1)
28+
: reason._tag === "Die"
29+
? summarizeCause(reason.defect, depth + 1)
30+
: "Interrupted",
31+
)
32+
.join("; ");
33+
}
34+
if (typeof cause === "string") return redactMessage(cause);
35+
if (typeof cause !== "object" || cause === null) return errorTag(cause);
36+
37+
const error = cause as {
38+
readonly message?: unknown;
39+
readonly code?: unknown;
40+
readonly cause?: unknown;
41+
};
42+
const code =
43+
typeof error.code === "string" && /^[A-Z][A-Z0-9_]{0,127}$/.test(error.code)
44+
? ` [${error.code}]`
45+
: "";
46+
const message = typeof error.message === "string" ? `: ${redactMessage(error.message)}` : "";
47+
const nested =
48+
error.cause === undefined ? "" : `; caused by ${summarizeCause(error.cause, depth + 1)}`;
49+
return `${errorTag(cause)}${code}${message}${nested}`;
50+
}
51+
52+
/** Log a bounded cause summary without stacks, response payloads, or request credentials. */
53+
export function describeUpdateErrorCause(cause: unknown): string {
54+
try {
55+
return summarizeCause(cause, 0).slice(0, 1024);
56+
} catch {
57+
return "UnknownError";
58+
}
59+
}

0 commit comments

Comments
 (0)