diff --git a/control/src/fuzz/parsers.property.test.ts b/control/src/fuzz/parsers.property.test.ts index b20f809..8975ee9 100644 --- a/control/src/fuzz/parsers.property.test.ts +++ b/control/src/fuzz/parsers.property.test.ts @@ -1,7 +1,7 @@ import { describe, expect, test } from 'bun:test'; import fc from 'fast-check'; import { budget } from '../../test-support/fuzz-budget'; -import { fixtureContext, MM_FIXTURE } from '../../test-support/observation-fixtures'; +import { fixtureContext, MM_FIXTURE, UFI_FIXTURE } from '../../test-support/observation-fixtures'; import { parseMmVersion } from '../backend/features'; import { parseNmVersion } from '../backend/nm-auto-apn'; import { parseTerse } from '../backend/nmcli-runner'; @@ -24,7 +24,12 @@ import { parseZteSignal, } from '../hardware/router-parsers'; import { parseNmeaFix } from '../location/nmea'; -import { normalizeModemManagerObservation, type RawFieldValue } from '../observations'; +import { + normalizeModemManagerObservation, + normalizeUfiObservation, + type RawFieldValue, +} from '../observations'; +import { viewEnvelope } from '../observations/envelope'; import { flattenXmlBody, parseJsonRecord } from '../observations/raw'; import { parseSmsListOutput, parseSmsRecordOutput } from '../sms/mmcli-parse'; import { parseUsbDevices } from '../usb-mode/usb-devices-parse'; @@ -196,6 +201,54 @@ describe('router signal models carry only finite readings', () => { }); }); +describe('JSON records', () => { + test('every field of a JSON object survives as a field', () => { + const names = fc.oneof( + fc.constantFrom('__proto__', 'constructor', 'toString', 'hasOwnProperty'), + fc.string({ maxLength: 8 }), + ); + fc.assert( + fc.property( + fc.array(fc.tuple(names, fc.jsonValue({ maxDepth: 2 })), { maxLength: 6 }), + (entries) => { + const body = `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${JSON.stringify(v)}`).join(',')}}`; + const expected = Object.keys(JSON.parse(body)); + expect(Object.keys(parseJsonRecord(body) ?? {})).toEqual(expected); + }, + ), + budget(), + ); + }); +}); + +describe('UFI bodies', () => { + test('every params field of a UFI reply is retained', () => { + const names = fc.oneof( + fc.constantFrom('__proto__', 'constructor', 'toString'), + fc.string({ minLength: 1, maxLength: 8 }), + ); + fc.assert( + fc.property( + fc.array(fc.tuple(names, fc.jsonValue({ maxDepth: 1 })), { maxLength: 5 }), + (params) => { + const body = `{"reply":"ok","params":{${params + .map(([k, v]) => `${JSON.stringify(k)}:${JSON.stringify(v)}`) + .join(',')}}}`; + const view = viewEnvelope( + normalizeUfiObservation({ ...UFI_FIXTURE, sysinfo: body }, fixtureContext()), + ); + if (view.kind === 'unavailable') throw new Error('UFI normalization was unavailable'); + const { raw } = view.value.diagnostics; + for (const key of Object.keys(JSON.parse(body).params)) { + expect(`sysinfo.${key}` in raw).toBe(true); + } + }, + ), + budget(), + ); + }); +}); + describe('ModemManager observation normalization', () => { const rawValue: fc.Arbitrary = fc.letrec<{ v: RawFieldValue }>((tie) => ({ v: fc.oneof( diff --git a/control/src/observations/normalization.test.ts b/control/src/observations/normalization.test.ts index 22bc054..b9648f7 100644 --- a/control/src/observations/normalization.test.ts +++ b/control/src/observations/normalization.test.ts @@ -20,6 +20,7 @@ import { viewEnvelope } from './envelope'; import { metricUnknownClass, type NormalizedMetric } from './metric'; import type { NormalizedModemObservation } from './model'; import { redactObservationDiagnostics } from './provenance'; +import { parseJsonRecord } from './raw'; import { normalizeHilinkObservation } from './sources/hilink'; import { normalizeModemManagerObservation } from './sources/modemmanager'; import { normalizeUfiObservation } from './sources/ufi'; @@ -343,3 +344,24 @@ describe('no raw vendor field is dropped during normalization', () => { } }); }); + +describe('JSON record bodies', () => { + test('a field named __proto__ stays a field and does not become the prototype', () => { + const record = parseJsonRecord('{"__proto__":[1,2],"signalbar":"4"}'); + expect(Object.keys(record ?? {})).toEqual(['__proto__', 'signalbar']); + expect(Object.getPrototypeOf(record)).toBe(Object.prototype); + }); + + test('UFI keeps a params field named __proto__ as a field', () => { + const envelope = normalizeUfiObservation( + { + ...UFI_FIXTURE, + sysinfo: '{"reply":"ok","__proto__":[1],"params":{"__proto__":[2],"cellid":"3344"}}', + }, + CONTEXT, + ); + const { raw } = observation(envelope).diagnostics; + expect(raw['sysinfo.__proto__']).toBeDefined(); + expect(raw['sysinfo.cellid']).toBe('3344'); + }); +}); diff --git a/control/src/observations/raw.ts b/control/src/observations/raw.ts index e51e0f6..6fce5c4 100644 --- a/control/src/observations/raw.ts +++ b/control/src/observations/raw.ts @@ -184,11 +184,11 @@ export function parseJsonRecord(body: string): Readonly = {}; - for (const [field, value] of Object.entries(parsed)) { - out[field] = normalizeRawValue(value); - } - return out; + // `fromEntries` defines own properties, so a device field named `__proto__` is kept as + // a field rather than swapping the record's prototype and vanishing from its keys. + return Object.fromEntries( + Object.entries(parsed).map(([field, value]) => [field, normalizeRawValue(value)]), + ); } /** diff --git a/control/src/observations/sources/ufi.ts b/control/src/observations/sources/ufi.ts index 313a708..d0e0fd6 100644 --- a/control/src/observations/sources/ufi.ts +++ b/control/src/observations/sources/ufi.ts @@ -146,22 +146,25 @@ export function normalizeUfiObservation( }); } -/** `{reply, params}` flattened to one level — `params` members keep their own names. */ +/** + * `{reply, params}` flattened to one level — `params` members keep their own names. + * Built with `Object.fromEntries` so a device field named `__proto__` stays a field. + */ function flattenUfiEnvelope( parsed: Readonly> | undefined, ): Record { - const out: Record = {}; if (parsed === undefined) { - return out; + return {}; } + const entries: [string, RawFieldValue][] = []; for (const [key, value] of Object.entries(parsed)) { if (key === 'params' && typeof value === 'object' && value !== null && !Array.isArray(value)) { for (const [field, param] of Object.entries(value as Record)) { - out[field] = normalizeRawValue(param); + entries.push([field, normalizeRawValue(param)]); } continue; } - out[key] = normalizeRawValue(value); + entries.push([key, normalizeRawValue(value)]); } - return out; + return Object.fromEntries(entries); }