fix(proton): harden generated protobuf decoders

Reject malformed varints, out-of-bounds fields, and invalid groups before they escape their parent frame. Aggregate unknown-field metadata and decode signed int32 values while preserving SnowLuma's existing outbound encoding bytes.
This commit is contained in:
DrSmoothl committed 2026-07-10 13:40:53 +08:00
1 parent bb56030979
commit b7d648ea0f
7 files changed
+311 -32

No files matched your search

+68
View File
@@ -95,6 +95,74 @@ function __zigZagEncode64(value) {
function __zigZagDecode64(value) {
return BigInt.asIntN(64, (value >> 1n) ^ -(value & 1n));
}
function __readVarint32(data, offset, end) {
let value = 0;
for (let i = 0; i < 5; i++) {
if (offset >= end) throw new Error('protobuf truncated uint32 varint');
const byte = data[offset++];
if (i === 4 && (byte & 0xf0) !== 0) throw new Error('protobuf uint32 varint overflow');
value += (byte & 0x7f) * (2 ** (i * 7));
if ((byte & 0x80) === 0) return [value >>> 0, offset];
}
throw new Error('protobuf uint32 varint overflow');
}
function __readVarint64(data, offset, end) {
let value = 0n;
for (let i = 0; i < 10; i++) {
if (offset >= end) throw new Error('protobuf truncated uint64 varint');
const byte = data[offset++];
if (i === 9 && byte > 1) throw new Error('protobuf uint64 varint overflow');
value |= BigInt(byte & 0x7f) << BigInt(i * 7);
if ((byte & 0x80) === 0) return [value, offset];
}
throw new Error('protobuf uint64 varint overflow');
}
function __skipVarint(data, offset, end) {
for (let i = 0; i < 10; i++) {
if (offset >= end) throw new Error('protobuf truncated varint');
const byte = data[offset++];
if (i === 9 && byte > 1) throw new Error('protobuf varint overflow');
if ((byte & 0x80) === 0) return offset;
}
throw new Error('protobuf varint overflow');
}
function __checkedEnd(offset, length, end) {
if (length > end - offset) throw new Error('protobuf length-delimited field exceeds parent bounds');
return offset + length;
}
function __skipUnknownField(data, offset, end, wireType, fieldNumber, depth = 0) {
if (depth > 64) throw new Error('protobuf group nesting exceeds 64');
if (wireType === 0) return __skipVarint(data, offset, end);
if (wireType === 1) {
if (end - offset < 8) throw new Error('protobuf truncated fixed64 field');
return offset + 8;
}
if (wireType === 2) {
const [length, next] = __readVarint32(data, offset, end);
return __checkedEnd(next, length, end);
}
if (wireType === 3) {
while (offset < end) {
const [tag, next] = __readVarint32(data, offset, end);
offset = next;
const nestedField = tag >>> 3;
const nestedWire = tag & 0x7;
if (nestedField === 0) throw new Error('protobuf field number 0 is invalid');
if (nestedWire === 4) {
if (nestedField !== fieldNumber) throw new Error('protobuf mismatched end-group tag');
return offset;
}
offset = __skipUnknownField(data, offset, end, nestedWire, nestedField, depth + 1);
}
throw new Error('protobuf unterminated group');
}
if (wireType === 4) throw new Error('protobuf unexpected end-group tag');
if (wireType === 5) {
if (end - offset < 4) throw new Error('protobuf truncated fixed32 field');
return offset + 4;
}
throw new Error('protobuf invalid wire type ' + wireType);
}
function __writeFloat32(buf, offset, value) {
__scratch.setFloat32(0, value, true);
buf[offset++] = __scratch.getUint8(0);