2021-12-26 21:41:09 +00:00
|
|
|
import electron from "electron";
|
2022-04-18 11:03:26 +00:00
|
|
|
import {getConfig} from "../utils";
|
2021-12-26 21:41:09 +00:00
|
|
|
|
2022-11-21 05:50:09 +00:00
|
|
|
const unstrictCSP = async () => {
|
2022-03-04 17:53:18 +00:00
|
|
|
console.log("Setting up CSP unstricter...");
|
2021-12-26 18:58:05 +00:00
|
|
|
|
2022-11-21 05:50:09 +00:00
|
|
|
const cspAllowAll = ["style-src", "connect-src", "img-src", "font-src", "media-src"];
|
2021-12-26 18:58:05 +00:00
|
|
|
|
2022-11-21 05:50:09 +00:00
|
|
|
const isVencord = await getConfig("mods").then((s) => s.includes("vencord"));
|
|
|
|
electron.session.defaultSession.webRequest.onHeadersReceived(({responseHeaders}, done) => {
|
2022-03-04 17:53:18 +00:00
|
|
|
let csp = responseHeaders!["content-security-policy"];
|
2021-12-26 18:58:05 +00:00
|
|
|
|
2022-03-04 17:53:18 +00:00
|
|
|
if (csp) {
|
2022-11-21 05:50:09 +00:00
|
|
|
for (const directive of cspAllowAll) {
|
|
|
|
csp[0] = csp[0].replace(new RegExp(`${directive}.+?;`), `${directive} * blob: data: 'unsafe-inline';`);
|
2022-03-04 17:53:18 +00:00
|
|
|
}
|
2021-12-26 18:58:05 +00:00
|
|
|
|
2022-11-21 05:50:09 +00:00
|
|
|
if (isVencord) {
|
|
|
|
// unpkg and cdnjs are used for QuickCss and some plugins' dependencies (e.g. GifEncoder & APNG for FakeNitro)
|
|
|
|
csp[0] = csp[0].replace(
|
|
|
|
/script-src.+?(?=;)/,
|
|
|
|
"$& 'unsafe-eval' https://unpkg.com https://cdnjs.cloudflare.com"
|
|
|
|
);
|
|
|
|
}
|
2022-03-04 17:53:18 +00:00
|
|
|
// Fix Discord's broken CSP which disallows unsafe-inline due to having a nonce (which they don't even use?)
|
2022-11-21 05:50:09 +00:00
|
|
|
csp[0] = csp[0].replace(/'nonce-.+?' /, "");
|
2022-03-04 17:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
done({responseHeaders});
|
|
|
|
});
|
2021-12-26 21:41:09 +00:00
|
|
|
};
|
2022-04-18 11:03:26 +00:00
|
|
|
|
|
|
|
electron.app.whenReady().then(async () => {
|
|
|
|
if (await getConfig("armcordCSP")) {
|
2022-03-04 17:53:18 +00:00
|
|
|
unstrictCSP();
|
|
|
|
} else {
|
2022-07-04 14:39:22 +00:00
|
|
|
console.log("ArmCord CSP is disabled. The CSP should be managed by a third-party plugin(s).");
|
2022-03-04 17:53:18 +00:00
|
|
|
}
|
2021-12-26 21:41:09 +00:00
|
|
|
});
|