Skip to content

Commit 226c3f4

Browse files
committed
fix(kernel): recognise a failed viewport read without its origin (#3182)
The published shape carries no origin, so a wire payload with the CGRectInfinite extents and no coordinates used to mint (0, 0) around them and pass the guard, handing the caller the largest number on the wire as the screen every rect is measured in (#2891). Both viewport gates now read the extents themselves. kernel/record keeps its inlined rect rule and points at the other half of that seam.
1 parent 344d202 commit 226c3f4

4 files changed

Lines changed: 46 additions & 4 deletions

File tree

‎packages/kernel/src/record.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ export function readSnapshotKeyboardBandFact(value: unknown): SnapshotKeyboardBa
109109
const frame = parseRect(value.frame);
110110
// Same rule as `isPositiveFiniteRect` in kernel/rect, inlined: this module is a leaf that
111111
// many facades evaluate, and a rect import here would land in every one of their closures.
112+
// The viewport re-read (#3182) is the other side of that seam and lives in kernel/rect as
113+
// `readSnapshotViewportSize`, because a viewport IS a rect; readers take the rect module there.
112114
const plottable =
113115
frame !== undefined &&
114116
[frame.x, frame.y, frame.width, frame.height].every(Number.isFinite) &&

‎packages/kernel/src/rect.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ test('snapshotViewportSizeFrom publishes only a box the rect guard accepts (#318
8282
'a non-finite extent',
8383
);
8484
assert.equal(snapshotViewportSizeFrom(CG_RECT_INFINITE), undefined, 'the failed-read sentinel');
85+
// A viewport carries no origin, so the producer that hands over the box it still holds after a
86+
// refused read arrives with plausible coordinates beside the sentinel's extents. The extents alone
87+
// have to refuse it, or the largest number on the wire becomes the screen (#2891).
88+
assert.equal(
89+
snapshotViewportSizeFrom({ x: 0, y: 0, width: Number.MAX_VALUE, height: Number.MAX_VALUE }),
90+
undefined,
91+
'failed-read extents beside a plausible origin',
92+
);
93+
assert.equal(
94+
snapshotViewportSizeFrom({ x: 0, y: 0, width: 390, height: Number.MAX_VALUE }),
95+
undefined,
96+
'one failed-read extent',
97+
);
8598
});
8699

87100
test('readSnapshotViewportSize accepts only a guard-approved pair from a wire payload (#3182)', () => {
@@ -110,6 +123,19 @@ test('readSnapshotViewportSize accepts only a guard-approved pair from a wire pa
110123
undefined,
111124
'the infinite sentinel with its origin',
112125
);
126+
// The published shape has no origin at all, so the ordinary broken payload is the originless one:
127+
// the extents have to be enough to recognise a failed read, or `x ?? 0` would mint the largest
128+
// finite box on the wire as the screen every rect is measured in.
129+
assert.equal(
130+
readSnapshotViewportSize({ width: Number.MAX_VALUE, height: Number.MAX_VALUE }),
131+
undefined,
132+
'failed-read extents with no origin',
133+
);
134+
assert.equal(
135+
readSnapshotViewportSize({ width: 390, height: Number.MAX_VALUE }),
136+
undefined,
137+
'one failed-read extent',
138+
);
113139
assert.equal(readSnapshotViewportSize({ width: 1, height: 1, x: '0' }), undefined);
114140
});
115141

‎packages/kernel/src/rect.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ export function isPositiveFiniteRect(rect: Rect | undefined): rect is Rect {
4040
return !isCGRectInfinite(rect);
4141
}
4242

43+
/**
44+
* A failed read keeps the maximal extents of `CGRectInfinite` whatever happened to its origin, and a
45+
* size field carries no origin of its own: a producer that hands over the box it still holds after a
46+
* refused read, or a wire payload that arrives with the extents and no coordinates, would otherwise
47+
* publish the largest number on the wire as the screen every rect is measured in (#2891, #3182).
48+
*/
49+
function reportsFailedReadExtents(width: number, height: number): boolean {
50+
return width >= CG_RECT_INFINITE.width || height >= CG_RECT_INFINITE.height;
51+
}
52+
4353
/**
4454
* The ONE construction path for the viewport a snapshot response publishes (#3182): a producer hands
4555
* over the box it read, and a box this guard refuses yields `undefined` — the absence that means
@@ -49,6 +59,7 @@ export function isPositiveFiniteRect(rect: Rect | undefined): rect is Rect {
4959
*/
5060
export function snapshotViewportSizeFrom(box: Rect | undefined): SnapshotViewportSize | undefined {
5161
if (!isPositiveFiniteRect(box)) return undefined;
62+
if (reportsFailedReadExtents(box.width, box.height)) return undefined;
5263
return { width: box.width, height: box.height };
5364
}
5465

@@ -58,17 +69,17 @@ export function snapshotViewportSizeFrom(box: Rect | undefined): SnapshotViewpor
5869
* unknown. A reader that trusted the payload could otherwise hand a consumer the `width: 0` a broken
5970
* producer wrote, which is the claim {@link snapshotViewportSizeFrom} exists to make unrepresentable.
6071
*
61-
* The published shape carries no origin, but a producer shipping the failed-read box it still holds
62-
* would ship the `CGRectInfinite` origin beside the maximal extents; when an `x`/`y` is present the
63-
* guard sees it, so the sentinel #2891 exists for is refused here too rather than being flattened
64-
* into the largest finite box on the wire.
72+
* The published shape carries no origin, so the failed read has to be recognised from its extents
73+
* alone: the extents check covers the payload with no coordinates as well as one that arrived with
74+
* the `CGRectInfinite` origin beside them.
6575
*/
6676
export function readSnapshotViewportSize(value: unknown): SnapshotViewportSize | undefined {
6777
if (typeof value !== 'object' || value === null || Array.isArray(value)) return undefined;
6878
const { x, y, width, height } = value as Record<string, unknown>;
6979
if (typeof width !== 'number' || typeof height !== 'number') return undefined;
7080
if (x !== undefined && typeof x !== 'number') return undefined;
7181
if (y !== undefined && typeof y !== 'number') return undefined;
82+
if (reportsFailedReadExtents(width, height)) return undefined;
7283
const box = { x: x ?? 0, y: y ?? 0, width, height };
7384
return isPositiveFiniteRect(box) ? { width, height } : undefined;
7485
}

‎src/__tests__/client-snapshot-viewport.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ test('client capture.snapshot restates an unusable viewport payload as absence (
4242
x: -Number.MAX_VALUE / 2,
4343
y: -Number.MAX_VALUE / 2,
4444
},
45+
// The published shape carries no origin, so a broken producer's failed-read box usually arrives
46+
// as bare maximal extents; minting an origin for them must not mint a screen either.
47+
{ width: Number.MAX_VALUE, height: Number.MAX_VALUE },
4548
]) {
4649
const client = clientAnswering({ truncated: false, viewport });
4750
const result = await client.capture.snapshot();

0 commit comments

Comments
 (0)