Fixed bug where websites with strict CSPs would not allow for the redirected instance embed to be loaded

Closes https://github.com/libredirect/libredirect/issues/481
This commit is contained in:
Hygna 2022-10-13 16:52:16 +01:00
parent 9a284bbe01
commit 3a16c0a2c2
No known key found for this signature in database
2 changed files with 49 additions and 0 deletions

View File

@ -742,6 +742,44 @@ function processUpdate() {
})
}
// For websites that have a strict policy that would not normally allow these frontends to be embedded within the website.
function modifyContentSecurityPolicy(details) {
let isChanged = false
if (details.type == "main_frame") {
for (const header in details.responseHeaders) {
if (details.responseHeaders[header].name == "content-security-policy") {
let instancesList = []
for (const service in config.services) {
if (config.services[service].embeddable) {
for (const frontend in config.services[service].frontends) {
if (config.services[service].frontends[frontend].embeddable) {
for (const network in config.networks) {
instancesList.push(...options[frontend][network].enabled, ...options[frontend][network].custom)
}
}
}
}
}
let securityPolicyList = details.responseHeaders[header].value.split(";")
for (const i in securityPolicyList) securityPolicyList[i] = securityPolicyList[i].trim()
let newSecurity = ""
for (const item of securityPolicyList) {
if (item.trim() == "") continue
let regex = item.match(/([a-z-]{0,}) (.*)/)
if (regex == null) continue
let [, key, vals] = regex
if (key == "frame-src") vals = vals + " " + instancesList.join(" ")
newSecurity += key + " " + vals + "; "
}
details.responseHeaders[header].value = newSecurity
isChanged = true
}
}
if (isChanged) return { responseHeaders: details.responseHeaders }
}
}
export default {
redirect,
computeService,
@ -752,4 +790,5 @@ export default {
initDefaults,
upgradeOptions,
processUpdate,
modifyContentSecurityPolicy,
}

View File

@ -245,6 +245,16 @@ browser.contextMenus.onClicked.addListener((info, tab) => {
})
})
browser.webRequest.onHeadersReceived.addListener(
e => {
let response = servicesHelper.modifyContentSecurityPolicy(e)
if (!response) response = servicesHelper.modifyContentSecurityPolicy(e)
return response
},
{ urls: ["<all_urls>"] },
["blocking", "responseHeaders"]
)
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.function === "unify") utils.unify(false).then(r => sendResponse({ response: r }))
return true