Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions control/src/fuzz/parsers.property.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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<RawFieldValue> = fc.letrec<{ v: RawFieldValue }>((tie) => ({
v: fc.oneof(
Expand Down
22 changes: 22 additions & 0 deletions control/src/observations/normalization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');
});
});
10 changes: 5 additions & 5 deletions control/src/observations/raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ export function parseJsonRecord(body: string): Readonly<Record<string, RawFieldV
if (parsed === undefined) {
return undefined;
}
const out: Record<string, RawFieldValue> = {};
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)]),
);
}

/**
Expand Down
15 changes: 9 additions & 6 deletions control/src/observations/sources/ufi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, unknown>> | undefined,
): Record<string, RawFieldValue> {
const out: Record<string, RawFieldValue> = {};
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<string, unknown>)) {
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);
}