Skip to content

Commit dc03b5d

Browse files
fix: harden release notes handling against malformed payloads
- DesktopUpdates: leave releaseNotes unvalidated in the UpdateInfo schema so a malformed payload can never fail the decode and leave the updater stuck in checking/downloading without surfacing the update - releaseNotes: validate entries defensively with a type guard, dropping malformed ones instead of throwing, and cap version groups after filtering empty ones so boilerplate-only groups no longer hide real notes - SidebarUpdatePill: key changelog bullets by index to avoid duplicate React keys when a version group repeats the same line - extend releaseNotes tests with malformed-entry and boilerplate-cap cases
1 parent 88b4261 commit dc03b5d

4 files changed

Lines changed: 46 additions & 17 deletions

File tree

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,12 @@ const AUTO_UPDATE_POLL_INTERVAL = "4 minutes";
4848
const AppUpdateYmlConfig = Schema.Record(Schema.String, Schema.String);
4949
type AppUpdateYmlConfig = typeof AppUpdateYmlConfig.Type;
5050

51-
const UpdateReleaseNoteInfo = Schema.Struct({
52-
version: Schema.String,
53-
note: Schema.NullOr(Schema.String),
54-
});
55-
5651
const UpdateInfo = Schema.Struct({
5752
version: Schema.String,
58-
releaseNotes: Schema.optional(
59-
Schema.NullOr(Schema.Union([Schema.String, Schema.Array(UpdateReleaseNoteInfo)])),
60-
),
53+
// Left unvalidated on purpose: a malformed release-notes payload must never
54+
// fail the decode and block the update state transition. The shape is
55+
// validated defensively in normalizeDesktopUpdateReleaseNotes.
56+
releaseNotes: Schema.optional(Schema.Unknown),
6157
});
6258

6359
const DownloadProgressInfo = Schema.Struct({

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,32 @@ describe("normalizeDesktopUpdateReleaseNotes", () => {
3131
expect(notes).toEqual([{ version: "1.0.0", items: ["Fix & polish 😀"] }]);
3232
});
3333

34+
it("ignores malformed entries instead of throwing", () => {
35+
const notes = normalizeDesktopUpdateReleaseNotes(
36+
[
37+
{ version: "1.2.3", note: "- Valid change" },
38+
{ version: 42, note: "- Bad version type" },
39+
{ version: "1.2.1", note: { html: "<p>object note</p>" } },
40+
"not an object",
41+
null,
42+
],
43+
"1.2.3",
44+
);
45+
expect(notes).toEqual([{ version: "1.2.3", items: ["Valid change"] }]);
46+
});
47+
48+
it("returns non-empty groups even when preceded by many boilerplate-only groups", () => {
49+
const boilerplate = Array.from({ length: 7 }, (_, index) => ({
50+
version: `1.3.${9 - index}`,
51+
note: "Full changelog: https://example.com/compare/x...y",
52+
}));
53+
const notes = normalizeDesktopUpdateReleaseNotes(
54+
[...boilerplate, { version: "1.3.2", note: "- Older but real change" }],
55+
"1.3.9",
56+
);
57+
expect(notes).toEqual([{ version: "1.3.2", items: ["Older but real change"] }]);
58+
});
59+
3460
it("does not throw on out-of-range numeric entities and keeps the literal", () => {
3561
expect(() =>
3662
normalizeDesktopUpdateReleaseNotes("- Broken entity &#9999999999;", "1.0.0"),

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ import type { DesktopUpdateReleaseNote } from "@t3tools/contracts";
22

33
interface ElectronReleaseNoteInfo {
44
readonly version: string;
5-
readonly note: string | null;
5+
readonly note: string | null | undefined;
66
}
77

8-
type ElectronReleaseNotes = string | ReadonlyArray<ElectronReleaseNoteInfo> | null | undefined;
8+
function isElectronReleaseNoteInfo(value: unknown): value is ElectronReleaseNoteInfo {
9+
if (typeof value !== "object" || value === null) return false;
10+
const candidate = value as { readonly version?: unknown; readonly note?: unknown };
11+
return (
12+
typeof candidate.version === "string" &&
13+
(typeof candidate.note === "string" || candidate.note === null || candidate.note === undefined)
14+
);
15+
}
916

1017
const MAX_RELEASE_NOTE_GROUPS = 6;
1118
const MAX_RELEASE_NOTE_ITEMS_PER_GROUP = 8;
@@ -80,7 +87,7 @@ function isIgnoredReleaseNoteLine(line: string): boolean {
8087
);
8188
}
8289

83-
function extractReleaseNoteItems(note: string | null): ReadonlyArray<string> {
90+
function extractReleaseNoteItems(note: string | null | undefined): ReadonlyArray<string> {
8491
if (!note) return [];
8592

8693
const items: string[] = [];
@@ -98,21 +105,21 @@ function extractReleaseNoteItems(note: string | null): ReadonlyArray<string> {
98105
}
99106

100107
export function normalizeDesktopUpdateReleaseNotes(
101-
releaseNotes: ElectronReleaseNotes,
108+
releaseNotes: unknown,
102109
fallbackVersion: string,
103110
): ReadonlyArray<DesktopUpdateReleaseNote> {
104111
const rawNotes =
105112
typeof releaseNotes === "string"
106113
? [{ version: fallbackVersion, note: releaseNotes }]
107114
: Array.isArray(releaseNotes)
108-
? releaseNotes
115+
? releaseNotes.filter(isElectronReleaseNoteInfo)
109116
: [];
110117

111118
return rawNotes
112-
.slice(0, MAX_RELEASE_NOTE_GROUPS)
113119
.map((entry) => ({
114120
version: entry.version,
115121
items: extractReleaseNoteItems(entry.note),
116122
}))
117-
.filter((entry) => entry.items.length > 0);
123+
.filter((entry) => entry.items.length > 0)
124+
.slice(0, MAX_RELEASE_NOTE_GROUPS);
118125
}

‎apps/web/src/components/sidebar/SidebarUpdatePill.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ function SidebarUpdateReleaseNotesTooltip({
4343
{index === 0 ? "What's changed" : `Changes in ${releaseNote.version}`}
4444
</h3>
4545
<ul className="mt-2 space-y-1.5 pl-4 text-xs leading-5 text-popover-foreground/90">
46-
{releaseNote.items.map((item) => (
47-
<li className="list-disc break-words" key={`${releaseNote.version}-${item}`}>
46+
{releaseNote.items.map((item, itemIndex) => (
47+
<li className="list-disc break-words" key={`${releaseNote.version}-${itemIndex}`}>
4848
{item}
4949
</li>
5050
))}

0 commit comments

Comments
 (0)