diff --git a/control/src/domain/mm-enums.ts b/control/src/domain/mm-enums.ts index 4cc1576..15b577d 100644 --- a/control/src/domain/mm-enums.ts +++ b/control/src/domain/mm-enums.ts @@ -171,11 +171,19 @@ export interface Mm3gppLacCi { // with a different token count is not a shape this build should guess at. const LAC_CI_TOKENS = 5; +// What each token can be. MCC is `%.3s` of the operator code and MNC the rest of it, so +// they are decimal digits; LAC, CI and TAC are `%lX`, so hex of at most 64 bits. An empty +// LAC or TAC is a RAT that has none; an empty CI is not a cell. +const MCC_PATTERN = /^\d{3}$/; +const MNC_PATTERN = /^\d{2,3}$/; +const HEX_PATTERN = /^[0-9A-Fa-f]{1,16}$/; + /** * Decode the `3gpp-lac-ci` location string, or `undefined` when it is not that shape. * - * A token count other than five, or an empty MCC/MNC/CI, decodes to nothing rather than - * to a partially-populated record: a cell identifier that is wrong is worse than one + * A token count other than five, or a token that is not what MM prints in that position + * (a three-digit MCC, a two- or three-digit MNC, hex LAC, CI and TAC), decodes to nothing + * rather than to a partially-populated record: a cell identifier that is wrong is worse than one * that is missing, because only the second is visible as missing. */ export function decode3gppLacCi(value: string | undefined): Mm3gppLacCi | undefined { @@ -185,7 +193,15 @@ export function decode3gppLacCi(value: string | undefined): Mm3gppLacCi | undefi const [mcc, mnc, locationAreaCode, cellId, trackingAreaCode] = tokens.map((token) => token.trim(), ) as [string, string, string, string, string]; - if (mcc === '' || mnc === '' || cellId === '') return undefined; + if (!MCC_PATTERN.test(mcc) || !MNC_PATTERN.test(mnc) || !HEX_PATTERN.test(cellId)) { + return undefined; + } + if ( + (locationAreaCode !== '' && !HEX_PATTERN.test(locationAreaCode)) || + (trackingAreaCode !== '' && !HEX_PATTERN.test(trackingAreaCode)) + ) { + return undefined; + } return { mcc, mnc, locationAreaCode, cellId, trackingAreaCode }; } diff --git a/control/src/fuzz/parsers.property.test.ts b/control/src/fuzz/parsers.property.test.ts index b20f809..2f85483 100644 --- a/control/src/fuzz/parsers.property.test.ts +++ b/control/src/fuzz/parsers.property.test.ts @@ -106,10 +106,36 @@ describe('numeric decoders are total', () => { }); }); +// Mostly five-token strings, since anything else is rejected before the tokens matter. +const lacCiText = fc.oneof( + text, + fc + .array(fc.oneof(fc.stringMatching(/^[0-9A-Fa-f]{0,4}$/), fc.string({ maxLength: 4 })), { + minLength: 5, + maxLength: 5, + }) + .map((tokens) => tokens.join(',')), +); + describe('3GPP location context', () => { + test('an accepted value has the shape MM prints', () => { + fc.assert( + fc.property(lacCiText, (input) => { + const decoded = decode3gppLacCi(input); + if (decoded === undefined) return; + expect(decoded.mcc).toMatch(/^\d{3}$/); + expect(decoded.mnc).toMatch(/^\d{2,3}$/); + expect(decoded.cellId).toMatch(/^[0-9A-Fa-f]{1,16}$/); + expect(decoded.locationAreaCode).toMatch(/^[0-9A-Fa-f]{0,16}$/); + expect(decoded.trackingAreaCode).toMatch(/^[0-9A-Fa-f]{0,16}$/); + }), + budget(), + ); + }); + test('an accepted value reproduces its source tokens', () => { fc.assert( - fc.property(text, (input) => { + fc.property(lacCiText, (input) => { const decoded = decode3gppLacCi(input); if (decoded === undefined) return; expect(decoded.mcc).not.toBe(''); diff --git a/control/src/observations/registration-context.test.ts b/control/src/observations/registration-context.test.ts index 6c3befc..4004ef2 100644 --- a/control/src/observations/registration-context.test.ts +++ b/control/src/observations/registration-context.test.ts @@ -102,6 +102,22 @@ describe('decode3gppLacCi — MM 1.24.2 emits exactly five comma-separated token expect(decode3gppLacCi('732,101,2B1C,,4E5F')).toBeUndefined(); }); + test('tokens that are not what MM prints in that position are not a decode', () => { + for (const value of [ + 'abc,101,2B1C,0A1B2C3D,4E5F', + '73,101,2B1C,0A1B2C3D,4E5F', + '7320,101,2B1C,0A1B2C3D,4E5F', + '732,1,2B1C,0A1B2C3D,4E5F', + '732,1011,2B1C,0A1B2C3D,4E5F', + '732,101,zz,0A1B2C3D,4E5F', + '732,101,2B1C,not-hex,4E5F', + '732,101,2B1C,0A1B2C3D,4G5F', + '732,101,2B1C,0A1B2C3D0A1B2C3D0,4E5F', + ]) { + expect(decode3gppLacCi(value)).toBeUndefined(); + } + }); + test('an EMPTY tracking-area code still decodes — a 2G/3G attach has none', () => { expect(decode3gppLacCi('732,101,2B1C,0A1B2C3D,')?.trackingAreaCode).toBe(''); });