Skip to content

Commit d76b08e

Browse files
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.
1 parent 8f807ae commit d76b08e

2 files changed

Lines changed: 275 additions & 100 deletions

File tree

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

Lines changed: 184 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,178 @@ 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+
// Drift guard: whatever the canonical schema accepts must still reach the
326+
// overlay. This fails if a schema change outgrows the reader.
327+
it.each([
328+
[
329+
"availability",
330+
{ currentVersion: "2026.8.1", latestVersion: "2026.8.2", channel: "stable" },
331+
UpdateAvailableSchema,
332+
readUpdateAvailableValue,
333+
],
334+
[
335+
"availability with git detail",
336+
{
337+
currentVersion: "2026.8.1",
338+
latestVersion: "2026.8.2",
339+
channel: "dev",
340+
currentSha: "aaa",
341+
upstreamRef: "origin/main",
342+
upstreamSha: "bbb",
343+
commitsBehind: 3,
344+
commits: [{ sha: "abc", subject: "fix things" }],
345+
},
346+
UpdateAvailableSchema,
347+
readUpdateAvailableValue,
348+
],
349+
[
350+
"schedule with package target",
351+
{
352+
channel: "beta",
353+
autoEnabled: true,
354+
install: { kind: "package" },
355+
target: { kind: "package", version: "2026.8.1-beta.1" },
356+
},
357+
UpdateScheduleStateSchema,
358+
readUpdateScheduleValue,
359+
],
360+
[
361+
"schedule with diverged git install and campaign",
362+
{
363+
channel: "dev",
364+
autoEnabled: false,
365+
install: {
366+
kind: "git",
367+
git: {
368+
status: "diverged",
369+
currentSha: "aaa",
370+
commitAtMs: 1,
371+
installedAtMs: 2,
372+
commitsAhead: 1,
373+
commitsBehind: 2,
374+
},
375+
},
376+
target: { kind: "git", upstreamRef: "origin/main", upstreamSha: "bbb", commitsBehind: 2 },
377+
campaign: {
378+
id: "c1",
379+
state: "countdown",
380+
announcedAtMs: 1,
381+
applyAtMs: 2,
382+
holdUntilMs: 3,
383+
forceAtMs: 4,
384+
updatedAtMs: 5,
385+
},
386+
},
387+
UpdateScheduleStateSchema,
388+
readUpdateScheduleValue,
389+
],
390+
])("round-trips canonical-valid %s unchanged", (_label, payload, schema, read) => {
391+
expect(Value.Check(schema, payload)).toBe(true);
392+
expect(read(payload)).toEqual(payload);
393+
});
212394
});
213395

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

0 commit comments

Comments
 (0)