Skip to content

Commit 1db8fe4

Browse files
fix(ui): reject blank required strings in Control UI update readers (#124264)
* fix(ui): reject blank required strings in Control UI update readers The Control UI update readers accepted `""` for fields the canonical UpdateAvailableSchema/UpdateScheduleStateSchema declare as NonEmptyString, so a malformed Gateway payload rendered an update banner with blank version text. Root cause is the reader treating "is a string" as the contract for fields whose canonical contract is "is a non-empty string". Fixed at that boundary with the protocol's own dependency-free `isNonEmptyProtocolString` primitive: required NonEmptyString fields now reject the payload when blank, and optional ones are dropped rather than discarding the whole payload. Deliberately does NOT re-derive the canonical schema in the browser: - `additionalProperties: false` is a producer-side contract the Gateway enforces on its own outbound results (src/gateway/server-methods/update.ts). Rejecting unknown keys client-side would turn every additive protocol field into a blank overlay, since a service-worker-cached document keeps an older bundle across a Gateway upgrade. - Canonical `maxLength` counts grapheme clusters (typebox 1.3.6 guard/string.mjs), so the previous `subject.length <= 120` copy dropped valid emoji subjects the schema accepts. The bound is producer-side and is gone rather than approximated. - typebox itself stays out of this module: it is in the Control UI startup graph, which has a hard gzip budget (scripts/check-control-ui-performance.mts). Drift is now pinned in tests instead of duplicated in production: the boundary suite runs fixtures through both the reader and Value.Check over the canonical schemas, asserting that anything the schema accepts still reaches the overlay. Production LOC: net -7 against main. * fix(ui): restore the five-entry cap on tolerant commit filtering readUpdateAvailableValue's per-entry filter/map dropped the existing five-commit bound while switching from all-or-nothing rejection to tolerant filtering. The canonical schema caps commits at 5 (packages/gateway-protocol/src/schema/config.ts), and the Updates page renders every entry this reader returns, so the cap is the protocol's own render-side contract, not extra strictness. Slice to MAX_COMMITS after filtering and cover an eight-entry valid payload.
1 parent 5374c11 commit 1db8fe4

2 files changed

Lines changed: 304 additions & 100 deletions

File tree

‎ui/src/app/update-overlay-helpers.test.ts‎

Lines changed: 202 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import { Value } from "typebox/value";
2+
import { afterEach, describe, expect, it, vi } from "vitest";
13
// @vitest-environment node
24
// Control UI tests cover localized update and recovery status copy.
3-
import { afterEach, describe, expect, it, vi } from "vitest";
5+
import {
6+
UpdateAvailableSchema,
7+
UpdateScheduleStateSchema,
8+
} from "../../../packages/gateway-protocol/src/schema/config.js";
49
import type { GatewayBrowserClient, GatewayHelloOk } from "../api/gateway.ts";
510
import { i18n } from "../i18n/index.ts";
611
import type {
@@ -12,7 +17,12 @@ import {
1217
formatUpdateCampaignLabel,
1318
resolveUpdateStatusBanner,
1419
} from "./update-overlay-helpers.ts";
15-
import { readUpdateAvailable, readUpdateSchedule } from "./update-schedule-dto.ts";
20+
import {
21+
readUpdateAvailable,
22+
readUpdateAvailableValue,
23+
readUpdateSchedule,
24+
readUpdateScheduleValue,
25+
} from "./update-schedule-dto.ts";
1626

1727
const translations: Record<string, string> = {
1828
"updates.status": "Update {status}: {reason}. {guidance}",
@@ -209,6 +219,196 @@ describe("update schedule hydration", () => {
209219
),
210220
).toBe("Update held · resumes in 12:41");
211221
});
222+
223+
it.each([
224+
[
225+
"availability channel",
226+
{ currentVersion: "2026.8.1", latestVersion: "2026.8.2", channel: "" },
227+
],
228+
[
229+
"availability currentVersion",
230+
{ currentVersion: "", latestVersion: "2026.8.2", channel: "s" },
231+
],
232+
])("rejects a blank required %s, as the canonical schema does", (_label, payload) => {
233+
expect(readUpdateAvailableValue(payload)).toBeNull();
234+
expect(Value.Check(UpdateAvailableSchema, payload)).toBe(false);
235+
});
236+
237+
it("rejects a blank required schedule channel, as the canonical schema does", () => {
238+
const blankSchedule = { channel: "", autoEnabled: true };
239+
expect(readUpdateScheduleValue(blankSchedule)).toBeNull();
240+
expect(Value.Check(UpdateScheduleStateSchema, blankSchedule)).toBe(false);
241+
});
242+
243+
it("drops blank optional strings instead of discarding the whole payload", () => {
244+
expect(
245+
readUpdateAvailableValue({
246+
currentVersion: "2026.8.1",
247+
latestVersion: "2026.8.2",
248+
channel: "stable",
249+
currentSha: "",
250+
upstreamRef: "",
251+
}),
252+
).toEqual({ currentVersion: "2026.8.1", latestVersion: "2026.8.2", channel: "stable" });
253+
});
254+
255+
// The canonical schemas are closed, but they are a producer-side contract the
256+
// Gateway enforces on its own outbound results. A service-worker-cached
257+
// document keeps an older bundle across a Gateway upgrade, so an additive
258+
// field must never blank the overlay.
259+
it("keeps rendering when a newer Gateway adds an unknown field", () => {
260+
const withFutureField = {
261+
currentVersion: "2026.8.1",
262+
latestVersion: "2026.8.2",
263+
channel: "stable",
264+
releaseNotesUrl: "https://example.invalid/notes",
265+
};
266+
expect(readUpdateAvailableValue(withFutureField)).toEqual({
267+
currentVersion: "2026.8.1",
268+
latestVersion: "2026.8.2",
269+
channel: "stable",
270+
});
271+
272+
const scheduleWithFutureField = {
273+
channel: "dev",
274+
autoEnabled: true,
275+
install: { kind: "git", git: { status: "current", futureNested: 1 } },
276+
rolloutCohort: "canary",
277+
};
278+
expect(readUpdateScheduleValue(scheduleWithFutureField)).toEqual({
279+
channel: "dev",
280+
autoEnabled: true,
281+
install: { kind: "git", git: { status: "current" } },
282+
});
283+
});
284+
285+
it("ignores prototype-named wire keys without polluting the result", () => {
286+
const hostile = JSON.parse(
287+
'{"currentVersion":"2026.8.1","latestVersion":"2026.8.2","channel":"stable","__proto__":{"polluted":true},"constructor":"x","toString":"y"}',
288+
);
289+
const parsed = readUpdateAvailableValue(hostile);
290+
expect(parsed).toEqual({
291+
currentVersion: "2026.8.1",
292+
latestVersion: "2026.8.2",
293+
channel: "stable",
294+
});
295+
expect(Object.hasOwn(parsed as object, "toString")).toBe(false);
296+
expect(({} as { polluted?: boolean }).polluted).toBeUndefined();
297+
});
298+
299+
// Canonical maxLength counts grapheme clusters; String#length counts UTF-16
300+
// code units, so a length-based copy of that rule drops valid emoji subjects.
301+
it("preserves a commit subject the canonical schema accepts but String#length overcounts", () => {
302+
const subject = "\u{1F44D}".repeat(100);
303+
const payload = {
304+
currentVersion: "2026.8.1",
305+
latestVersion: "2026.8.2",
306+
channel: "stable",
307+
commits: [{ sha: "abc1234", subject }],
308+
};
309+
expect(subject.length).toBeGreaterThan(120);
310+
expect(Value.Check(UpdateAvailableSchema, payload)).toBe(true);
311+
expect(readUpdateAvailableValue(payload)?.commits).toEqual([{ sha: "abc1234", subject }]);
312+
});
313+
314+
it("keeps valid commits when a sibling entry is malformed", () => {
315+
expect(
316+
readUpdateAvailableValue({
317+
currentVersion: "2026.8.1",
318+
latestVersion: "2026.8.2",
319+
channel: "stable",
320+
commits: [{ sha: "", subject: "dropped" }, { sha: "abc", subject: "kept" }, { sha: 7 }],
321+
})?.commits,
322+
).toEqual([{ sha: "abc", subject: "kept" }]);
323+
});
324+
325+
// The canonical schema caps commits at 5 entries (maxItems: 5) and the
326+
// Updates page renders every entry this reader returns. An out-of-contract
327+
// producer payload past that cap must not grow the rendered list.
328+
it("caps commits at five entries even when every entry is valid", () => {
329+
const commits = Array.from({ length: 8 }, (_, index) => ({
330+
sha: `sha${index}`,
331+
subject: `commit ${index}`,
332+
}));
333+
expect(
334+
readUpdateAvailableValue({
335+
currentVersion: "2026.8.1",
336+
latestVersion: "2026.8.2",
337+
channel: "stable",
338+
commits,
339+
})?.commits,
340+
).toEqual(commits.slice(0, 5));
341+
});
342+
343+
// Drift guard: whatever the canonical schema accepts must still reach the
344+
// overlay. This fails if a schema change outgrows the reader.
345+
it.each([
346+
[
347+
"availability",
348+
{ currentVersion: "2026.8.1", latestVersion: "2026.8.2", channel: "stable" },
349+
UpdateAvailableSchema,
350+
readUpdateAvailableValue,
351+
],
352+
[
353+
"availability with git detail",
354+
{
355+
currentVersion: "2026.8.1",
356+
latestVersion: "2026.8.2",
357+
channel: "dev",
358+
currentSha: "aaa",
359+
upstreamRef: "origin/main",
360+
upstreamSha: "bbb",
361+
commitsBehind: 3,
362+
commits: [{ sha: "abc", subject: "fix things" }],
363+
},
364+
UpdateAvailableSchema,
365+
readUpdateAvailableValue,
366+
],
367+
[
368+
"schedule with package target",
369+
{
370+
channel: "beta",
371+
autoEnabled: true,
372+
install: { kind: "package" },
373+
target: { kind: "package", version: "2026.8.1-beta.1" },
374+
},
375+
UpdateScheduleStateSchema,
376+
readUpdateScheduleValue,
377+
],
378+
[
379+
"schedule with diverged git install and campaign",
380+
{
381+
channel: "dev",
382+
autoEnabled: false,
383+
install: {
384+
kind: "git",
385+
git: {
386+
status: "diverged",
387+
currentSha: "aaa",
388+
commitAtMs: 1,
389+
installedAtMs: 2,
390+
commitsAhead: 1,
391+
commitsBehind: 2,
392+
},
393+
},
394+
target: { kind: "git", upstreamRef: "origin/main", upstreamSha: "bbb", commitsBehind: 2 },
395+
campaign: {
396+
id: "c1",
397+
state: "countdown",
398+
announcedAtMs: 1,
399+
applyAtMs: 2,
400+
holdUntilMs: 3,
401+
forceAtMs: 4,
402+
updatedAtMs: 5,
403+
},
404+
},
405+
UpdateScheduleStateSchema,
406+
readUpdateScheduleValue,
407+
],
408+
])("round-trips canonical-valid %s unchanged", (_label, payload, schema, read) => {
409+
expect(Value.Check(schema, payload)).toBe(true);
410+
expect(read(payload)).toEqual(payload);
411+
});
212412
});
213413

214414
describe("update status localization", () => {

0 commit comments

Comments
 (0)