Skip to content
This repository was archived by the owner on Jun 26, 2026. It is now read-only.

Commit a815790

Browse files
committed
fix(console): bail on shrink for nested-immutable arrays, handle root wildcard
Two concrete data-corruption shapes surfaced in review: 1. When the user shrinks an array with a per-element-nested-immutable path (e.g. disks[].name), index-aligned overlay re-anchors source names onto the surviving entries — silently changing which element was deleted. We now detect length shrink at the wildcard segment and skip the overlay for that path with a console.warn. The UI's ui:disabled still prevents interactive edits; admission stays as the authoritative enforcer for YAML/devtools bypass. 2. A top-level wildcard immutable path (root array or map carrying the rule on items/additionalProperties) used to no-op silently — the overlay loop discarded the wildcard return value. Mirror the root-immutable handling: warn and replace wholesale. Existing tests for grow and same-length nested overlay still pass; the shrink and root-wildcard cases get explicit regression tests. Link the three remaining known-broken behaviours from FIXME tests to the tracking issues filed for them (#8, #9, #10). Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 3dffc2e commit a815790

2 files changed

Lines changed: 101 additions & 7 deletions

File tree

‎apps/console/src/lib/immutable-paths.test.ts‎

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ describe("findImmutablePaths", () => {
140140
expect(findImmutablePaths(cel)).toEqual([[]])
141141
})
142142

143-
it("FIXME: does NOT walk into properties beneath oneOf/anyOf/allOf branches", () => {
143+
it("FIXME(#8): does NOT walk into properties beneath oneOf/anyOf/allOf branches", () => {
144144
// The walker recognises *parent* immutability when any branch carries
145145
// the rule, but it does not descend into a branch's `properties` to
146146
// discover per-field immutability inside the branch. CRDs that use
@@ -278,9 +278,11 @@ describe("overlayImmutable", () => {
278278
).toEqual({ storageClass: "fast" })
279279
})
280280

281-
it("shrinks array as the user requested even when nested elements are immutable", () => {
282-
// User deleted the second disk; nested field "name" is immutable per element.
283-
// Overlay must respect the deletion (no zombie restore from original).
281+
it("skips overlay (with warn) when the user shrunk an array with per-element-nested-immutable fields", () => {
282+
// Index-aligned overlay corrupts which element was deleted when the
283+
// length changed (it re-anchors source values onto surviving indices).
284+
// Skip the overlay for this path and let admission enforce the rule.
285+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {})
284286
const submitted = { disks: [{ name: "renamed", size: "10Gi" }] }
285287
const original = {
286288
disks: [
@@ -290,7 +292,41 @@ describe("overlayImmutable", () => {
290292
}
291293
expect(
292294
overlayImmutable(submitted, original, [["disks", "*", "name"]]),
293-
).toEqual({ disks: [{ name: "disk1", size: "10Gi" }] })
295+
).toEqual({ disks: [{ name: "renamed", size: "10Gi" }] })
296+
expect(warn).toHaveBeenCalled()
297+
warn.mockRestore()
298+
})
299+
300+
it("skips overlay (with warn) when the user deleted the first of two immutable-named entries", () => {
301+
// Direct repro of the index-anchored swap bug discovered in review:
302+
// submitted shrinks from 2 to 1 and the naive overlay would assign
303+
// source[0].name to the surviving entry. We bail instead.
304+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {})
305+
const submitted = { disks: [{ name: "b", size: "2Gi" }] }
306+
const original = {
307+
disks: [
308+
{ name: "a", size: "1Gi" },
309+
{ name: "b", size: "2Gi" },
310+
],
311+
}
312+
expect(
313+
overlayImmutable(submitted, original, [["disks", "*", "name"]]),
314+
).toEqual({ disks: [{ name: "b", size: "2Gi" }] })
315+
expect(warn).toHaveBeenCalled()
316+
warn.mockRestore()
317+
})
318+
319+
it("warns and clones original when a top-level wildcard immutable path is supplied", () => {
320+
// findImmutablePaths emits [["*"]] for schemas whose root is an array
321+
// or map carrying the rule on items/additionalProperties. The naive
322+
// overlay would no-op (the loop's return value was discarded). Match
323+
// the root-immutable handling: warn and replace wholesale.
324+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {})
325+
const submitted = ["x", "y"]
326+
const original = ["a", "b"]
327+
expect(overlayImmutable(submitted, original, [["*"]])).toEqual(original)
328+
expect(warn).toHaveBeenCalled()
329+
warn.mockRestore()
294330
})
295331

296332
it("freezes a whole additionalProperties map when * is the last segment", () => {
@@ -338,7 +374,7 @@ describe("overlayImmutable", () => {
338374
})
339375
})
340376

341-
it("FIXME: does NOT materialise an immutable leaf when its ancestor is missing in target", () => {
377+
it("FIXME(#9): does NOT materialise an immutable leaf when its ancestor is missing in target", () => {
342378
// Defence-in-depth gap: if the YAML editor strips the parent object
343379
// entirely, overlay cannot reach the leaf to copy the original value.
344380
// Pin behaviour so a future contributor fixing the gap notices the
@@ -353,7 +389,7 @@ describe("overlayImmutable", () => {
353389
expect(result).toEqual({ spec: {} })
354390
})
355391

356-
it("FIXME: array reordering by the user with index-aligned overlay re-anchors source values to the new index", () => {
392+
it("FIXME(#10): array reordering by the user with index-aligned overlay re-anchors source values to the new index", () => {
357393
// The overlay walks by index, not by content identity. Reordering an
358394
// immutable array maps source[i] onto target[i]'s new value — which
359395
// for a per-element-name-immutable schema swaps the names back to the

‎apps/console/src/lib/immutable-paths.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,71 @@ export function overlayImmutable<T>(
8888
)
8989
return deepClone(original)
9090
}
91+
if (paths.some((p) => p[0] === "*")) {
92+
// A leading wildcard would land at the very top of the structure and
93+
// overlay nothing useful (the root is an array or map, not an object
94+
// we can deepen into). Mirror the root-immutable handling: warn and
95+
// hand back a clone of original.
96+
console.warn(
97+
"overlayImmutable: a top-level wildcard immutable path was supplied; " +
98+
"the submitted value is replaced wholesale by the original.",
99+
)
100+
return deepClone(original)
101+
}
91102
const next = deepClone(submitted)
92103
for (const path of paths) {
104+
if (wildcardArrayLengthChanged(next, original, path)) {
105+
// Index-aligned overlay corrupts the user's deletion/insertion when
106+
// a per-element-nested-immutable path crosses an array whose length
107+
// changed. The UI greys those fields out, so the only ways to land
108+
// here are the YAML editor or devtools — the API server will reject
109+
// a per-element mutation via CEL. Skip the overlay (warn for diag)
110+
// rather than silently re-anchor source values to user indices.
111+
console.warn(
112+
"overlayImmutable: array length changed at wildcard segment of",
113+
path.join("."),
114+
"— skipping overlay for this path; admission will enforce.",
115+
)
116+
continue
117+
}
93118
overlayPath(next, original, path, 0)
94119
}
95120
return next
96121
}
97122

123+
function wildcardArrayLengthChanged(
124+
submitted: unknown,
125+
original: unknown,
126+
path: ImmutablePath,
127+
): boolean {
128+
let subCur: unknown = submitted
129+
let origCur: unknown = original
130+
for (let i = 0; i < path.length; i++) {
131+
const seg = path[i]
132+
if (seg === "*") {
133+
// Whole-array immutability (*-last) is fine to overlay even when the
134+
// user changed the length — the semantics is "freeze, replace from
135+
// source", and that's exactly what the *-last branch does.
136+
const isLast = i === path.length - 1
137+
if (isLast) return false
138+
// Per-element-nested immutability (*-not-last) is the case that can
139+
// silently corrupt on SHRINK (we'd re-anchor source values onto the
140+
// user's surviving indices, which may belong to a different element
141+
// than the user thought they kept). On grow the shared indices stay
142+
// put and the new entries past source.length keep the user's values,
143+
// so growth is safe to overlay.
144+
if (Array.isArray(subCur) && Array.isArray(origCur)) {
145+
return subCur.length < origCur.length
146+
}
147+
return false
148+
}
149+
if (!isPlainObject(subCur) || !isPlainObject(origCur)) return false
150+
subCur = (subCur as Record<string, unknown>)[seg]
151+
origCur = (origCur as Record<string, unknown>)[seg]
152+
}
153+
return false
154+
}
155+
98156
function overlayPath(
99157
target: unknown,
100158
source: unknown,

0 commit comments

Comments
 (0)