aboutsummaryrefslogtreecommitdiff
path: root/src/patcher.ts
diff options
context:
space:
mode:
authorJustice Almanzar <superdash993@gmail.com>2022-12-02 10:43:37 -0500
committerGitHub <noreply@github.com>2022-12-02 16:43:37 +0100
commit41dddc9eee6f19fb5055545811aff1e282790a9c (patch)
treebf38148e87242e169adfa39919aa636a9d0551ce /src/patcher.ts
parent4760af7f0ee275caa1eee440f4945032057d2b56 (diff)
downloadVencord-41dddc9eee6f19fb5055545811aff1e282790a9c.tar.gz
Vencord-41dddc9eee6f19fb5055545811aff1e282790a9c.tar.bz2
Vencord-41dddc9eee6f19fb5055545811aff1e282790a9c.zip
feat(plugin): ShikiCodeblocks (#267)
Co-authored-by: ArjixWasTaken <53124886+ArjixWasTaken@users.noreply.github.com> Co-authored-by: Ven <vendicated@riseup.net>
Diffstat (limited to 'src/patcher.ts')
-rw-r--r--src/patcher.ts30
1 files changed, 25 insertions, 5 deletions
diff --git a/src/patcher.ts b/src/patcher.ts
index 0849e5a..0cf7e24 100644
--- a/src/patcher.ts
+++ b/src/patcher.ts
@@ -109,16 +109,36 @@ if (!process.argv.includes("--vanilla")) {
// Remove CSP
+ type PolicyResult = Record<string, string[]>;
+
+ const parsePolicy = (policy: string): PolicyResult => {
+ const result: PolicyResult = {};
+ policy.split(";").forEach(directive => {
+ const [directiveKey, ...directiveValue] = directive.trim().split(/\s+/g);
+ if (directiveKey && !Object.prototype.hasOwnProperty.call(result, directiveKey)) {
+ result[directiveKey] = directiveValue;
+ }
+ });
+ return result;
+ };
+ const stringifyPolicy = (policy: PolicyResult): string =>
+ Object.entries(policy)
+ .filter(([, values]) => values?.length)
+ .map(directive => directive.flat().join(" "))
+ .join("; ");
+
function patchCsp(headers: Record<string, string[]>, header: string) {
if (header in headers) {
- let patchedHeader = headers[header][0];
- for (const directive of ["style-src", "connect-src", "img-src", "font-src", "media-src"]) {
- patchedHeader = patchedHeader.replace(new RegExp(`${directive}.+?;`), `${directive} * blob: data: 'unsafe-inline';`);
+ const csp = parsePolicy(headers[header][0]);
+
+ for (const directive of ["style-src", "connect-src", "img-src", "font-src", "media-src", "worker-src"]) {
+ csp[directive] = ["*", "blob:", "data:", "'unsafe-inline'"];
}
// TODO: Restrict this to only imported packages with fixed version.
// Perhaps auto generate with esbuild
- patchedHeader = patchedHeader.replace(/script-src.+?(?=;)/, "$& 'unsafe-eval' https://unpkg.com https://cdnjs.cloudflare.com");
- headers[header] = [patchedHeader];
+ csp["script-src"] ??= [];
+ csp["script-src"].push("'unsafe-eval'", "https://unpkg.com", "https://cdnjs.cloudflare.com");
+ headers[header] = [stringifyPolicy(csp)];
}
}