Skip to content

Commit 7bc7033

Browse files
authored
fix: prevent errors in getKeyIdSet if playlist.contentProtection object is incomplete (#1582)
1 parent 1e721c1 commit 7bc7033

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

src/playlist-loader.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,17 +1317,21 @@ export default class PlaylistLoader extends EventTarget {
13171317
* @return a Set of 32 digit hex strings that represent the unique keyIds for that playlist.
13181318
*/
13191319
getKeyIdSet(playlist) {
1320-
if (playlist.contentProtection) {
1321-
const keyIds = new Set();
1320+
const keyIds = new Set();
13221321

1323-
for (const keysystem in playlist.contentProtection) {
1322+
if (!playlist || !playlist.contentProtection) {
1323+
return keyIds;
1324+
}
1325+
1326+
for (const keysystem in playlist.contentProtection) {
1327+
if (playlist.contentProtection[keysystem] &&
1328+
playlist.contentProtection[keysystem].attributes &&
1329+
playlist.contentProtection[keysystem].attributes.keyId) {
13241330
const keyId = playlist.contentProtection[keysystem].attributes.keyId;
13251331

1326-
if (keyId) {
1327-
keyIds.add(keyId.toLowerCase());
1328-
}
1332+
keyIds.add(keyId.toLowerCase());
13291333
}
1330-
return keyIds;
13311334
}
1335+
return keyIds;
13321336
}
13331337
}

test/playlist-loader.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,39 @@ QUnit.module('Playlist Loader', function(hooks) {
4949
assert.ok(keyIdSet.has(keyId.toLowerCase()), 'keyId is expected hex string');
5050
});
5151

52+
QUnit.test('does not error if keyId or keysystem.attributes is missing', function(assert) {
53+
assert.expect(6);
54+
const loader = new PlaylistLoader('variant.m3u8', this.fakeVhs);
55+
const playlists = [
56+
{
57+
contentProtection: {
58+
'fake.keysystem': {}
59+
}
60+
},
61+
{
62+
contentProtection: {
63+
'fake.keysystem': {
64+
attributes: {}
65+
}
66+
}
67+
}
68+
];
69+
70+
let errorHappened = false;
71+
72+
for (const playlist of playlists) {
73+
let keyIdSet;
74+
75+
try {
76+
keyIdSet = loader.getKeyIdSet(playlist);
77+
} catch (e) {
78+
errorHappened = true;
79+
}
80+
assert.notOk(errorHappened, 'no error was thrown');
81+
assert.notOk(keyIdSet.size, 'keyIdSet is empty');
82+
}
83+
});
84+
5285
QUnit.test('updateSegments copies over properties', function(assert) {
5386
assert.deepEqual(
5487
[

0 commit comments

Comments
 (0)