Skip to content

Commit c69013d

Browse files
committed
Validate M3U8 CORS taking into account requested origin
1 parent 3ea7149 commit c69013d

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

lib/plugins/validators/async/21_checkContentType.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ export default {
122122
link.error = 'MP4s should allow partial download via accept ranges';
123123
} */
124124

125-
if ((data.type == CONFIG.T.stream_apple_mpegurl || data.type == CONFIG.T.stream_x_mpegurl) && data.allow_origin !== '*') {
125+
if ((data.type == CONFIG.T.stream_apple_mpegurl || data.type == CONFIG.T.stream_x_mpegurl)
126+
&& data.allow_origin !== '*'
127+
&& (!data.request_origin && !data.allow_origin || data.allow_origin.indexOf(data.request_origin) == -1)) {
126128
link.error = 'CORS headers on the player are not configured correctly';
127129
}
128130

lib/utils.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ export function prepareRequestOptions(request_options, options) {
236236
if (!options.asBuffer) {
237237
stream.setEncoding("binary");
238238
}
239-
callbacks.onResponse && callbacks.onResponse(stream);
239+
callbacks.onResponse && callbacks.onResponse(stream, request_options);
240240
})
241241
.catch(error => {
242242
callbacks.onError && callbacks.onError(error);
@@ -282,7 +282,7 @@ var getHead = function(url, options, callbacks) {
282282
try {
283283
fetchStreamAuthorized(request_options)
284284
.then(response => {
285-
callbacks.onResponse && callbacks.onResponse(response);
285+
callbacks.onResponse && callbacks.onResponse(response, request_options);
286286
})
287287
.catch(error => {
288288
callbacks.onError && callbacks.onError(error);
@@ -779,6 +779,7 @@ export function getContentType(uriForCache, uriOriginal, options, cb) {
779779
if (headers['x-frame-options']) data.x_frame_options = headers['x-frame-options'];
780780
if (headers['content-security-policy']) data.csp = headers['content-security-policy'];
781781
if (headers['access-control-allow-origin']) data.allow_origin = headers['access-control-allow-origin'];
782+
if (headers.request_headers?.origin) data.request_origin = headers.request_headers.origin;
782783
if (headers['accept-ranges']) data.accept_ranges = headers['accept-ranges'];
783784
if (headers['url'] && headers['url'] !== uriOriginal) data.url = headers['url'];
784785

@@ -805,7 +806,7 @@ export function getContentType(uriForCache, uriOriginal, options, cb) {
805806
methodCaller(uri, {
806807
timeout: options.timeout || CONFIG.RESPONSE_TIMEOUT
807808
}, {
808-
onResponse: function(res) {
809+
onResponse: function(res, request_options) {
809810

810811
abortController = res.abortController;
811812

@@ -835,6 +836,10 @@ export function getContentType(uriForCache, uriOriginal, options, cb) {
835836
res.headers.url = uri;
836837
}
837838

839+
if (request_options?.headers && res.headers) {
840+
res.headers.request_headers = Object.fromEntries(Object.entries(request_options.headers).map(([k, v]) => [k.toLowerCase(), v]));
841+
}
842+
838843
finish(error, res.headers);
839844
},
840845

0 commit comments

Comments
 (0)