Skip to content

Commit

Permalink
fix: ignore non-finite numeric values
Browse files Browse the repository at this point in the history
see #45

Signed-off-by: Casey Occhialini <[email protected]>
  • Loading branch information
littlespex committed Nov 29, 2023
1 parent 6aaae88 commit 897e5c5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion lib/src/cta/utils/isValid.ts
Original file line number Diff line number Diff line change
@@ -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;
};
4 changes: 4 additions & 0 deletions lib/src/structuredfield/serialize/serializeBareItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/test/cmcd/encodeCmcd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
2 changes: 2 additions & 0 deletions lib/test/structuredfield/serializeBareItem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
});

0 comments on commit 897e5c5

Please sign in to comment.