Fixed content-security-policy for youtube #279

This commit is contained in:
ManeraKai 2022-06-11 13:03:46 +03:00
parent 3aac443d68
commit 0ab646ac68
No known key found for this signature in database
GPG Key ID: 5ABC31FFD562E337
2 changed files with 50 additions and 15 deletions

View File

@ -420,21 +420,56 @@ function pastePipedMaterialLocalStorage() {
}) })
} }
function removeXFrameOptions(e) { function removeXFrameOptions(e) {
if (e.type != 'sub_frame') return;
const url = new URL(e.url);
const protocolHost = utils.protocolHost(url);
if (!all().includes(protocolHost)) return;
let isChanged = false; let isChanged = false;
for (const i in e.responseHeaders) {
if (e.responseHeaders[i].name == 'x-frame-options') { if (e.type == 'main_frame') {
e.responseHeaders.splice(i, 1); for (const i in e.responseHeaders) {
isChanged = true; if (e.responseHeaders[i].name == 'content-security-policy') {
let instancesList;
if (youtubeFrontend == 'invidious') {
if (youtubeProtocol == 'normal') instancesList = [...invidiousNormalRedirectsChecks, ...invidiousNormalCustomRedirects];
else if (youtubeProtocol == 'tor') instancesList = [...invidiousTorRedirectsChecks, ...invidiousTorCustomRedirects];
}
else if (youtubeFrontend == 'piped') {
if (youtubeProtocol == 'normal') instancesList = [...pipedNormalRedirectsChecks, ...pipedNormalCustomRedirects];
else if (youtubeProtocol == 'tor') instancesList = [...pipedTorRedirectsChecks, ...pipedTorCustomRedirects];
}
else if (youtubeFrontend == 'pipedMaterial') {
if (youtubeProtocol == 'normal') instancesList = [...pipedMaterialNormalRedirectsChecks, ...pipedMaterialNormalCustomRedirects];
else if (youtubeProtocol == 'tor') instancesList = [...pipedMaterialTorRedirectsChecks, ...pipedMaterialTorCustomRedirects];
}
let securityPolicyList = e.responseHeaders[i].value.split(';');
for (const i in securityPolicyList) securityPolicyList[i] = securityPolicyList[i].trim();
let newSecurity = '';
for (const item of securityPolicyList) {
if (item.trim() == '') continue
console.log('item', item);
let [, key, vals] = item.match(/([a-z-]{0,}) (.*)/);
if (key == 'frame-src') vals = vals + ' ' + instancesList.join(' ');
newSecurity += key + ' ' + vals + '; ';
}
e.responseHeaders[i].value = newSecurity;
isChanged = true;
}
} }
else if (e.responseHeaders[i].name == 'content-security-policy') { if (isChanged) return { responseHeaders: e.responseHeaders };
e.responseHeaders.splice(i, 1); }
isChanged = true; else if (e.type == 'sub_frame') {
const url = new URL(e.url);
const protocolHost = utils.protocolHost(url);
if (!all().includes(protocolHost)) return;
for (const i in e.responseHeaders) {
if (e.responseHeaders[i].name == 'x-frame-options') {
e.responseHeaders.splice(i, 1);
isChanged = true;
}
else if (e.responseHeaders[i].name == 'content-security-policy') {
e.responseHeaders.splice(i, 1);
isChanged = true;
}
} }
} }
if (isChanged) return { responseHeaders: e.responseHeaders }; if (isChanged) return { responseHeaders: e.responseHeaders };

View File

@ -162,7 +162,7 @@ browser.webRequest.onHeadersReceived.addListener(
if (!response) response = youtubeHelper.removeXFrameOptions(e); if (!response) response = youtubeHelper.removeXFrameOptions(e);
return response; return response;
}, },
{ urls: ["<all_urls>"], }, { urls: ["<all_urls>"] },
["blocking", "responseHeaders"] ["blocking", "responseHeaders"]
); );
@ -201,7 +201,7 @@ browser.webRequest.onResponseStarted.addListener(
if (!await isAutoRedirect()) return null; if (!await isAutoRedirect()) return null;
if (details.type == 'main_frame' && details.statusCode >= 500) redirectOfflineInstance(new URL(details.url), details.tabId); if (details.type == 'main_frame' && details.statusCode >= 500) redirectOfflineInstance(new URL(details.url), details.tabId);
}, },
{ urls: ["<all_urls>"], } { urls: ["<all_urls>"] }
) )
browser.webRequest.onErrorOccurred.addListener( browser.webRequest.onErrorOccurred.addListener(
@ -209,7 +209,7 @@ browser.webRequest.onErrorOccurred.addListener(
if (!await isAutoRedirect()) return; if (!await isAutoRedirect()) return;
if (details.type == 'main_frame') redirectOfflineInstance(new URL(details.url), details.tabId); if (details.type == 'main_frame') redirectOfflineInstance(new URL(details.url), details.tabId);
}, },
{ urls: ["<all_urls>"], } { urls: ["<all_urls>"] }
) )
browser.commands.onCommand.addListener( browser.commands.onCommand.addListener(