diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cc55c7f..17823827 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- CMCD does not protect against non-Finite numeric values [#45](https://github.com/streaming-video-technology-alliance/common-media-library/issues/45) + ## [0.5.1] - 2023-11-16 diff --git a/lib/src/cta/utils/isValid.ts b/lib/src/cta/utils/isValid.ts index a6f7183e..10fb12c4 100644 --- a/lib/src/cta/utils/isValid.ts +++ b/lib/src/cta/utils/isValid.ts @@ -1,3 +1,9 @@ import { CmValue } from '../CmValue'; -export const isValid = (value: CmValue) => value != null && value !== '' && value !== false; +export const isValid = (value: CmValue) => { + if (typeof value === 'number') { + return Number.isFinite(value); + } + + return value != null && value !== '' && value !== false; +}; diff --git a/lib/src/structuredfield/serialize/serializeBareItem.ts b/lib/src/structuredfield/serialize/serializeBareItem.ts index 0a27c5ed..d5b444dc 100644 --- a/lib/src/structuredfield/serialize/serializeBareItem.ts +++ b/lib/src/structuredfield/serialize/serializeBareItem.ts @@ -39,6 +39,10 @@ import { serializeToken } from './serializeToken.js'; export function serializeBareItem(value: any) { switch (typeof value) { case 'number': + if (!Number.isFinite(value)) { + throw serializeError(value, BARE_ITEM); + } + if (Number.isInteger(value)) { return serializeInteger(value); } diff --git a/lib/test/cmcd/encodeCmcd.test.ts b/lib/test/cmcd/encodeCmcd.test.ts index f52da3d5..0a457ad8 100644 --- a/lib/test/cmcd/encodeCmcd.test.ts +++ b/lib/test/cmcd/encodeCmcd.test.ts @@ -10,6 +10,11 @@ describe('encodeCmcd', () => { equal(encodeCmcd(null as any), ''); }); + it('ignore invalid values', () => { + // @ts-expect-error + equal(encodeCmcd({ mtp: NaN, br: Infinity, nor: '', sid: undefined, cid: null, su: false }), ''); + }); + it('returns encoded string', () => { equal(encodeCmcd(CMCD_INPUT), CMCD_STRING); }); diff --git a/lib/test/structuredfield/serializeBareItem.test.ts b/lib/test/structuredfield/serializeBareItem.test.ts index 59268473..b109f50a 100644 --- a/lib/test/structuredfield/serializeBareItem.test.ts +++ b/lib/test/structuredfield/serializeBareItem.test.ts @@ -5,4 +5,6 @@ import { serializeBareItem } from '../../src/structuredfield/serialize/serialize test('serializeBareItem', () => { assert.throws(() => serializeBareItem([]), /failed to serialize "\[\]" as Bare Item/); assert.throws(() => serializeBareItem({}), /failed to serialize "{}" as Bare Item/); + assert.throws(() => serializeBareItem(NaN), /failed to serialize "NaN" as Bare Item/); + assert.throws(() => serializeBareItem(Infinity), /failed to serialize "Infinity" as Bare Item/); });