Skip to content

Commit

Permalink
Update csp.ts to utilise WeakRef.
Browse files Browse the repository at this point in the history
This should make cache not to block GCC.
  • Loading branch information
SpacingBat3 committed Oct 6, 2024
1 parent 1fbe62b commit f6b270d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
55 changes: 24 additions & 31 deletions sources/code/main/modules/csp.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import type { cspTP } from "./config";

const cspKeys = [
"default-src",
"script-src",
"worker-src",
"connect-src",
"style-src",
"img-src",
"font-src",
"media-src",
"frame-src",
"child-src"
] as const;

const cspKeysRegExp = new RegExp("(?<="+cspKeys.join("|")+")\\s+");

type cspObject = Partial<Record<(typeof cspKeys)[number],string>>;
type cspObject = Partial<Record<string,string>>;

/**
* A class used to manipulate and build Content Security Policy from objects
Expand All @@ -29,11 +14,11 @@ class CSPBuilder {
#string2object(value: string):cspObject {
return Object.fromEntries(value
.split(/;\s+/)
.map(element => element.split(cspKeysRegExp,2) as [string|undefined, string|undefined])
.filter((element => element[0] !== undefined && element[1] !== undefined &&
cspKeys.includes(element[0] as (typeof cspKeys)[number])) as
(v:[string|undefined,string|undefined]) => v is [typeof cspKeys[number],string]
)
.map(element => {
const i = element.indexOf(" ");
return [element.slice(0,i),element.slice(i)] satisfies [string,string];
})
.filter((element => element[0] !== "" && element[1] !== ""))
);
}
/**
Expand All @@ -46,9 +31,8 @@ class CSPBuilder {
.join("; ");
}
public set value(value: string|cspObject) {
if(value instanceof Object && Object
.keys(value)
.map(key => cspKeys.includes(key as typeof cspKeys[number]))
if(value instanceof Object && Object.values(value)
.map(key => typeof key === "string")
.reduce((prev,cur) => prev && cur, true)
)
this.#value = value;
Expand Down Expand Up @@ -77,7 +61,7 @@ class CSPBuilder {
.filter(value => Object.keys(value).length !== 0)
.reduce((prev,cur,index) => {
if(index === 0) return cur;
(Object.keys(cur) as (keyof cspObject)[]).forEach(key => {
(Object.keys(cur)).forEach(key => {
const policy = cur[key];
if(policy === undefined)
return;
Expand All @@ -94,6 +78,13 @@ class CSPBuilder {
constructor(value: string|cspObject = {}) {
this.value = value;
}
[Symbol.toPrimitive](hint:"string"|"number"|"default") {
switch(hint) {
case "string": return this.build();
case "number": return NaN;
default: return this;
}
}
}

const builders: {base:CSPBuilder}&cspTP<CSPBuilder> = {
Expand Down Expand Up @@ -211,16 +202,17 @@ const builders: {base:CSPBuilder}&cspTP<CSPBuilder> = {
})
};

let cache:[key:symbol,content:string]|null = null;
let cache:[key:symbol,content:WeakRef<CSPBuilder>]|null = null;

export function getWebCordCSP(configValues: cspTP<boolean>,...additionalPolicies: CSPBuilder[]) {
type parties = keyof typeof configValues;
type cspFilter = (value:CSPBuilder|undefined) => value is CSPBuilder;
const key = Symbol.for(Object.values(configValues).join("")+additionalPolicies.map(policy => policy.build()).join(""));
if(cache?.[0] === key)
return cache[1];
let val = cache?.[1].deref();
if(val && cache?.[0] === key)
return val;
console.debug("[CSP] Caching function arguments...");
cache = [key, CSPBuilder.merge(
val = CSPBuilder.merge(
builders.base,
...Object.keys(configValues)
.map((key) => {
Expand All @@ -231,6 +223,7 @@ export function getWebCordCSP(configValues: cspTP<boolean>,...additionalPolicies
})
.filter(((value) => value instanceof CSPBuilder) as cspFilter),
...additionalPolicies
).build()];
return cache[1];
);
cache = [key, new WeakRef(val)];
return val;
}
2 changes: 1 addition & 1 deletion sources/code/main/windows/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export default function createMainWindow(...flags:MainWindowFlags): BrowserWindo
const {csp,cspThirdParty} = appConfig.value.settings.advanced;
const responseHeaders = details.responseHeaders??{};
if(csp.enabled)
responseHeaders["Content-Security-Policy"] = [getWebCordCSP(cspThirdParty)];
responseHeaders["Content-Security-Policy"] = [getWebCordCSP(cspThirdParty).build()];
callback({ responseHeaders });
});

Expand Down

0 comments on commit f6b270d

Please sign in to comment.