perf(browser-transfer): remove guards that earlier checks already make unreachable (Q-B)
- image-header-metadata: `=== undefined` / `?? 0` / `?? null` guards on bytes and boxes whose index was bounds-checked a few lines earlier (PNG IHDR, JPEG marker, WebP VP8X/VP8L, AVIF ftyp/meta/iprp/ipma, ascii, readUint24LittleEndian). A differential fuzz of the old and new parser over 720k valid and mutated PNG/JPEG/WebP/AVIF inputs found no difference. - incremental-sha256: `?? 0` on in-range Uint32Array reads and `>>> 0` on Uint32Array stores, which already reduce modulo 2^32. Checked against node:crypto for 0..1 MiB inputs, one-shot and streamed. - p256 verifier: the alphabet index can't be negative after the base64url pattern check, and the decoded signature is already an owned, exactly sized buffer, so it is no longer copied again. - browser-image-probe: once the bitmap matches the header dimensions, which are positive and inside the decode budget, the repeated positivity and budget checks on the bitmap cannot fail. file-transfer reference runtime: 54894 -> 54754 gzip bytes. Other reference runtimes unchanged. Verified: check:architecture, check:types:app, check:types:test, lint, test:unit, test:component, check:adapter-inventory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
6c69df572b
commit
834308c29b
@@ -241,16 +241,10 @@ export function createBrowserImageProbe(
|
||||
);
|
||||
bitmap = await awaitOwned(scope, decodeTask, closeBitmap);
|
||||
if (
|
||||
!positiveSafeInteger(bitmap.width) ||
|
||||
!positiveSafeInteger(bitmap.height) ||
|
||||
// The header dimensions are positive and already within the
|
||||
// decode budget, so an exact match is the whole check.
|
||||
bitmap.width !== metadata.width ||
|
||||
bitmap.height !== metadata.height ||
|
||||
!withinDecodeBudget(
|
||||
bitmap.width,
|
||||
bitmap.height,
|
||||
request.maxDecodedPixels,
|
||||
request.maxDecodedBytes,
|
||||
)
|
||||
bitmap.height !== metadata.height
|
||||
) {
|
||||
return browserDataFailure(
|
||||
"INTEGRITY_FAILED",
|
||||
|
||||
@@ -73,8 +73,6 @@ function parsePng(
|
||||
const interlace = bytes[payloadStart + 12];
|
||||
if (
|
||||
!dimensions ||
|
||||
bitDepth === undefined ||
|
||||
colorType === undefined ||
|
||||
!validPngColorDepth(colorType, bitDepth) ||
|
||||
compression !== 0 ||
|
||||
filter !== 0 ||
|
||||
@@ -144,7 +142,7 @@ function parseJpeg(
|
||||
if (offset >= bytes.byteLength) return null;
|
||||
const marker = bytes[offset];
|
||||
offset += 1;
|
||||
if (marker === undefined || marker === 0x00) return null;
|
||||
if (marker === 0x00) return null;
|
||||
if (marker === 0xd9) return null;
|
||||
if (marker === 0xda) return dimensions;
|
||||
if (
|
||||
@@ -227,7 +225,6 @@ function parseWebp(
|
||||
chunkIndex !== 0 ||
|
||||
extendedHeaderSeen ||
|
||||
length !== 10 ||
|
||||
bytes[payloadStart] === undefined ||
|
||||
(bytes[payloadStart] & 0xc3) !== 0
|
||||
) {
|
||||
return null;
|
||||
@@ -260,15 +257,7 @@ function parseWebp(
|
||||
const byte2 = bytes[payloadStart + 2];
|
||||
const byte3 = bytes[payloadStart + 3];
|
||||
const byte4 = bytes[payloadStart + 4];
|
||||
if (
|
||||
byte1 === undefined ||
|
||||
byte2 === undefined ||
|
||||
byte3 === undefined ||
|
||||
byte4 === undefined ||
|
||||
(byte4 & 0xe0) !== 0
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
if ((byte4 & 0xe0) !== 0) return null;
|
||||
candidate = validDimensions(
|
||||
1 + byte1 + ((byte2 & 0x3f) << 8),
|
||||
1 +
|
||||
@@ -306,14 +295,8 @@ function parseAvif(
|
||||
return null;
|
||||
}
|
||||
const fileType = boxes[0];
|
||||
const fileTypeLength = fileType
|
||||
? fileType.payloadEnd - fileType.payloadStart
|
||||
: 0;
|
||||
if (
|
||||
!fileType ||
|
||||
fileTypeLength < 8 ||
|
||||
(fileTypeLength - 8) % 4 !== 0
|
||||
) {
|
||||
const fileTypeLength = fileType.payloadEnd - fileType.payloadStart;
|
||||
if (fileTypeLength < 8 || (fileTypeLength - 8) % 4 !== 0) {
|
||||
return null;
|
||||
}
|
||||
const brands: string[] = [
|
||||
@@ -343,7 +326,6 @@ function parseAvif(
|
||||
const metadataBox = metadataBoxes[0];
|
||||
if (
|
||||
metadataBoxes.length !== 1 ||
|
||||
!metadataBox ||
|
||||
metadataBox.payloadStart + 4 > metadataBox.payloadEnd ||
|
||||
!zeroFullBoxFlags(bytes, metadataBox.payloadStart)
|
||||
) {
|
||||
@@ -413,9 +395,7 @@ function parseAvif(
|
||||
const dimensions = state.properties.get(propertyIndex);
|
||||
if (dimensions) associatedExtents.push(dimensions);
|
||||
}
|
||||
return associatedExtents.length === 1
|
||||
? (associatedExtents[0] ?? null)
|
||||
: null;
|
||||
return associatedExtents.length === 1 ? associatedExtents[0] : null;
|
||||
}
|
||||
|
||||
type IsoBox = Readonly<{
|
||||
@@ -532,11 +512,7 @@ function parseAvifItemProperties(
|
||||
(child) => child.type === "ipma",
|
||||
);
|
||||
const propertyContainer = propertyContainers[0];
|
||||
if (
|
||||
propertyContainers.length !== 1 ||
|
||||
associationBoxes.length < 1 ||
|
||||
!propertyContainer
|
||||
) {
|
||||
if (propertyContainers.length !== 1 || associationBoxes.length < 1) {
|
||||
return false;
|
||||
}
|
||||
const properties = parseBoxes(
|
||||
@@ -581,9 +557,7 @@ function parseAvifPropertyAssociations(
|
||||
const version = bytes[start];
|
||||
if (version !== 0 && version !== 1) return false;
|
||||
const flags =
|
||||
((bytes[start + 1] ?? 0) << 16) |
|
||||
((bytes[start + 2] ?? 0) << 8) |
|
||||
(bytes[start + 3] ?? 0);
|
||||
(bytes[start + 1] << 16) | (bytes[start + 2] << 8) | bytes[start + 3];
|
||||
if ((flags & ~1) !== 0) return false;
|
||||
const wideAssociation = (flags & 1) === 1;
|
||||
const view = dataView(bytes);
|
||||
@@ -598,7 +572,6 @@ function parseAvifPropertyAssociations(
|
||||
: view.getUint32(offset);
|
||||
offset += itemIdBytes;
|
||||
const associationCount = bytes[offset];
|
||||
if (associationCount === undefined) return false;
|
||||
offset += 1;
|
||||
const propertyIndices: number[] = [];
|
||||
for (
|
||||
@@ -609,9 +582,7 @@ function parseAvifPropertyAssociations(
|
||||
const associationBytes = wideAssociation ? 2 : 1;
|
||||
if (offset + associationBytes > end) return false;
|
||||
const encoded =
|
||||
associationBytes === 2
|
||||
? view.getUint16(offset)
|
||||
: (bytes[offset] ?? 0);
|
||||
associationBytes === 2 ? view.getUint16(offset) : bytes[offset];
|
||||
const propertyIndex =
|
||||
encoded & (wideAssociation ? 0x7fff : 0x7f);
|
||||
offset += associationBytes;
|
||||
@@ -694,35 +665,23 @@ function validDimensions(
|
||||
: null;
|
||||
}
|
||||
|
||||
/** Callers pass an offset whose three bytes lie inside a checked chunk. */
|
||||
function readUint24LittleEndian(
|
||||
bytes: Uint8Array,
|
||||
offset: number,
|
||||
): number {
|
||||
const byte0 = bytes[offset];
|
||||
const byte1 = bytes[offset + 1];
|
||||
const byte2 = bytes[offset + 2];
|
||||
if (
|
||||
byte0 === undefined ||
|
||||
byte1 === undefined ||
|
||||
byte2 === undefined
|
||||
) {
|
||||
return Number.NaN;
|
||||
}
|
||||
return byte0 | (byte1 << 8) | (byte2 << 16);
|
||||
return (
|
||||
bytes[offset] | (bytes[offset + 1] << 8) | (bytes[offset + 2] << 16)
|
||||
);
|
||||
}
|
||||
|
||||
/** Callers pass a range that the surrounding length checks keep in bounds. */
|
||||
function ascii(
|
||||
bytes: Uint8Array,
|
||||
start: number,
|
||||
end: number,
|
||||
): string {
|
||||
let value = "";
|
||||
for (let offset = start; offset < end; offset += 1) {
|
||||
const byte = bytes[offset];
|
||||
if (byte === undefined) return "";
|
||||
value += String.fromCharCode(byte);
|
||||
}
|
||||
return value;
|
||||
return String.fromCharCode(...bytes.subarray(start, end));
|
||||
}
|
||||
|
||||
function dataView(bytes: Uint8Array): DataView {
|
||||
|
||||
@@ -87,8 +87,7 @@ export function createP256ImageCapabilityVerifier(
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
const signatureBytes = new Uint8Array(signature.byteLength);
|
||||
signatureBytes.set(signature);
|
||||
// `decodeBase64Url` already returns an owned, exactly sized buffer.
|
||||
const payloadBytes = new Uint8Array(
|
||||
request.canonicalPayload.byteLength,
|
||||
);
|
||||
@@ -96,7 +95,7 @@ export function createP256ImageCapabilityVerifier(
|
||||
return await verify(
|
||||
{ name: "ECDSA", hash: "SHA-256" },
|
||||
key,
|
||||
signatureBytes.buffer,
|
||||
signature.buffer,
|
||||
payloadBytes.buffer,
|
||||
);
|
||||
} catch {
|
||||
@@ -106,7 +105,7 @@ export function createP256ImageCapabilityVerifier(
|
||||
});
|
||||
}
|
||||
|
||||
function decodeBase64Url(value: string): Uint8Array | null {
|
||||
function decodeBase64Url(value: string): Uint8Array<ArrayBuffer> | null {
|
||||
if (
|
||||
!/^[A-Za-z0-9_-]+$/u.test(value) ||
|
||||
value.length % 4 === 1
|
||||
@@ -119,9 +118,8 @@ function decodeBase64Url(value: string): Uint8Array | null {
|
||||
let accumulator = 0;
|
||||
let bitCount = 0;
|
||||
for (const character of value) {
|
||||
const index = alphabet.indexOf(character);
|
||||
if (index < 0) return null;
|
||||
accumulator = (accumulator << 6) | index;
|
||||
// The pattern above admits only alphabet characters.
|
||||
accumulator = (accumulator << 6) | alphabet.indexOf(character);
|
||||
bitCount += 6;
|
||||
if (bitCount >= 8) {
|
||||
bitCount -= 8;
|
||||
|
||||
@@ -126,8 +126,8 @@ class Sha256Accumulator {
|
||||
words[index] = view.getUint32(index * 4, false);
|
||||
}
|
||||
for (let index = 16; index < 64; index += 1) {
|
||||
const previous15 = words[index - 15] ?? 0;
|
||||
const previous2 = words[index - 2] ?? 0;
|
||||
const previous15 = words[index - 15];
|
||||
const previous2 = words[index - 2];
|
||||
const sigma0 =
|
||||
rotateRight(previous15, 7) ^
|
||||
rotateRight(previous15, 18) ^
|
||||
@@ -136,22 +136,18 @@ class Sha256Accumulator {
|
||||
rotateRight(previous2, 17) ^
|
||||
rotateRight(previous2, 19) ^
|
||||
(previous2 >>> 10);
|
||||
words[index] =
|
||||
((words[index - 16] ?? 0) +
|
||||
sigma0 +
|
||||
(words[index - 7] ?? 0) +
|
||||
sigma1) >>>
|
||||
0;
|
||||
// A Uint32Array store already reduces modulo 2^32.
|
||||
words[index] = words[index - 16] + sigma0 + words[index - 7] + sigma1;
|
||||
}
|
||||
|
||||
let a = this.#state[0] ?? 0;
|
||||
let b = this.#state[1] ?? 0;
|
||||
let c = this.#state[2] ?? 0;
|
||||
let d = this.#state[3] ?? 0;
|
||||
let e = this.#state[4] ?? 0;
|
||||
let f = this.#state[5] ?? 0;
|
||||
let g = this.#state[6] ?? 0;
|
||||
let h = this.#state[7] ?? 0;
|
||||
let a = this.#state[0];
|
||||
let b = this.#state[1];
|
||||
let c = this.#state[2];
|
||||
let d = this.#state[3];
|
||||
let e = this.#state[4];
|
||||
let f = this.#state[5];
|
||||
let g = this.#state[6];
|
||||
let h = this.#state[7];
|
||||
|
||||
for (let index = 0; index < 64; index += 1) {
|
||||
const sum1 =
|
||||
@@ -161,8 +157,8 @@ class Sha256Accumulator {
|
||||
(h +
|
||||
sum1 +
|
||||
choice +
|
||||
(ROUND_CONSTANTS[index] ?? 0) +
|
||||
(words[index] ?? 0)) >>>
|
||||
ROUND_CONSTANTS[index] +
|
||||
words[index]) >>>
|
||||
0;
|
||||
const sum0 =
|
||||
rotateRight(a, 2) ^ rotateRight(a, 13) ^ rotateRight(a, 22);
|
||||
@@ -178,14 +174,15 @@ class Sha256Accumulator {
|
||||
a = (temporary1 + temporary2) >>> 0;
|
||||
}
|
||||
|
||||
this.#state[0] = ((this.#state[0] ?? 0) + a) >>> 0;
|
||||
this.#state[1] = ((this.#state[1] ?? 0) + b) >>> 0;
|
||||
this.#state[2] = ((this.#state[2] ?? 0) + c) >>> 0;
|
||||
this.#state[3] = ((this.#state[3] ?? 0) + d) >>> 0;
|
||||
this.#state[4] = ((this.#state[4] ?? 0) + e) >>> 0;
|
||||
this.#state[5] = ((this.#state[5] ?? 0) + f) >>> 0;
|
||||
this.#state[6] = ((this.#state[6] ?? 0) + g) >>> 0;
|
||||
this.#state[7] = ((this.#state[7] ?? 0) + h) >>> 0;
|
||||
const state = this.#state;
|
||||
state[0] += a;
|
||||
state[1] += b;
|
||||
state[2] += c;
|
||||
state[3] += d;
|
||||
state[4] += e;
|
||||
state[5] += f;
|
||||
state[6] += g;
|
||||
state[7] += h;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user