Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(text-tracks): set withCredentials on XHR if crossOrigin='use-credentials' #6588

Merged
merged 3 commits into from
Apr 22, 2020
Merged
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
6 changes: 6 additions & 0 deletions src/js/tracks/text-track.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ const loadTrack = function(src, track) {
opts.cors = crossOrigin;
}

const withCredentials = track.tech_.crossOrigin() === 'use-credentials';

if (withCredentials) {
opts.withCredentials = withCredentials;
}

XHR(opts, Fn.bind(this, function(err, response, responseBody) {
if (err) {
return log.error(err, response);
Expand Down
3 changes: 3 additions & 0 deletions test/unit/tech/tech-faker.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ class TechFaker extends Tech {
ended() {
return false;
}
crossOrigin() {
return null;
}

// Support everything except for "video/unsupported-format"
static isSupported() {
Expand Down
54 changes: 47 additions & 7 deletions test/unit/tracks/text-track-list-converter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ if (Html5.supportsNativeTextTracks()) {
kind: 'captions',
label: 'English',
language: 'en',
tech: {}
tech: {
crossOrigin() {
return null;
}
}
});

const nativeTrack = document.createElement('track');
Expand All @@ -71,6 +75,9 @@ if (Html5.supportsNativeTextTracks()) {
}
};
},
crossOrigin() {
return null;
},
textTracks() {
return tt;
}
Expand Down Expand Up @@ -98,7 +105,11 @@ if (Html5.supportsNativeTextTracks()) {
label: 'English',
language: 'en',
src: 'example.com/english.vtt',
tech: {}
tech: {
crossOrigin() {
return null;
}
}
});

const nativeTrack = document.createElement('track');
Expand Down Expand Up @@ -126,6 +137,9 @@ if (Html5.supportsNativeTextTracks()) {
}
};
},
crossOrigin() {
return null;
},
textTracks() {
return tt;
},
Expand All @@ -152,7 +166,11 @@ QUnit.test('trackToJson_ produces correct representation for emulated track obje
label: 'English',
language: 'en',
src: 'example.com/english.vtt',
tech: {}
tech: {
crossOrigin() {
return null;
}
}
});

assert.deepEqual(cleanup(c.trackToJson_(track)), {
Expand All @@ -170,15 +188,23 @@ QUnit.test('textTracksToJson produces good json output for emulated only', funct
label: 'English',
language: 'en',
src: 'example.com/english.vtt',
tech: {}
tech: {
crossOrigin() {
return null;
}
}
});

const anotherTrack = new TextTrack({
src: 'example.com/spanish.vtt',
kind: 'captions',
srclang: 'es',
label: 'Spanish',
tech: {}
tech: {
crossOrigin() {
return null;
}
}
});

const tt = new TextTrackList();
Expand All @@ -198,6 +224,9 @@ QUnit.test('textTracksToJson produces good json output for emulated only', funct
}
};
},
crossOrigin() {
return null;
},
textTracks() {
return tt;
}
Expand Down Expand Up @@ -227,15 +256,23 @@ QUnit.test('jsonToTextTracks calls addRemoteTextTrack on the tech with emulated
label: 'English',
language: 'en',
src: 'example.com/english.vtt',
tech: {}
tech: {
crossOrigin() {
return null;
}
}
});

const anotherTrack = new TextTrack({
src: 'example.com/spanish.vtt',
kind: 'captions',
srclang: 'es',
label: 'Spanish',
tech: {}
tech: {
crossOrigin() {
return null;
}
}
});

const tt = new TextTrackList();
Expand All @@ -256,6 +293,9 @@ QUnit.test('jsonToTextTracks calls addRemoteTextTrack on the tech with emulated
}
};
},
crossOrigin() {
return null;
},
textTracks() {
return tt;
},
Expand Down
58 changes: 57 additions & 1 deletion test/unit/tracks/text-track.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,12 +447,66 @@ QUnit.test('tracks are parsed if vttjs is loaded', function(assert) {
src: 'http://example.com'
});

reqs.pop().respond(200, null, 'WEBVTT\n');
const req = reqs.pop();

req.respond(200, null, 'WEBVTT\n');

assert.ok(parserCreated, 'WebVTT is loaded, so we can just parse');
assert.notOk(req.withCredentials, 'the request defaults not to send credentials');

clock.restore();
tt.off();
window.WebVTT = oldVTT;
});

QUnit.test('tracks are loaded withCredentials is crossorigin is set to use-credentials', function(assert) {
const clock = sinon.useFakeTimers();
const oldVTT = window.WebVTT;
const reqs = [];

this.xhr.onCreate = function(req) {
reqs.push(req);
};

window.WebVTT = () => {};
window.WebVTT.StringDecoder = () => {};
window.WebVTT.Parser = () => {
return {
oncue() {},
onparsingerror() {},
onflush() {},
parse() {},
flush() {}
};
};

this.tech.crossOrigin = () => 'use-credentials';

const tt = new TextTrack({
tech: this.tech,
src: 'http://example.com'
});

const req = reqs.pop();

assert.ok(req.withCredentials, 'the request was made withCredentials');

this.tech.crossOrigin = () => 'anonymous';

const tt2 = new TextTrack({
tech: this.tech,
src: 'http://example.com'
});

const req2 = reqs.pop();

assert.notOk(req2.withCredentials, 'the request was not made withCredentials');

req.abort();
req2.abort();
clock.restore();
tt.off();
tt2.off();
window.WebVTT = oldVTT;
});

Expand All @@ -472,6 +526,7 @@ QUnit.test('tracks are parsed once vttjs is loaded', function(assert) {

testTech.textTracks = () => {};
testTech.currentTime = () => {};
testTech.crossOrigin = () => null;

const tt = new TextTrack({
tech: testTech,
Expand Down Expand Up @@ -526,6 +581,7 @@ QUnit.test('stops processing if vttjs loading errored out', function(assert) {

testTech.textTracks = () => {};
testTech.currentTime = () => {};
testTech.crossOrigin = () => null;

sinon.stub(testTech, 'off');
testTech.off.withArgs('vttjsloaded');
Expand Down