forked from darkreader/darkreader
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsp.ts
More file actions
50 lines (46 loc) · 1.44 KB
/
Copy pathcsp.ts
File metadata and controls
50 lines (46 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import {HOMEPAGE_URL, BLOG_URL, DEVTOOLS_DOCS_URL, GITHUB_URL, PRIVACY_URL, TWITTER_URL, UNINSTALL_URL, HELP_URL} from './links';
enum CSP {
NONE = "'none'",
SELF = "'self'"
}
function check() {
const prefixed = [BLOG_URL, PRIVACY_URL, UNINSTALL_URL, HELP_URL];
if (prefixed.some((url) => !url.startsWith(HOMEPAGE_URL))) {
throw new Error('Some navigation URL is not within main site!');
}
}
export function prepareCSPMV3(): chrome.runtime.ManifestV3['content_security_policy'] {
check();
const result: any = {};
const policy: any = {
extension_pages: {
'default-src': [CSP.NONE],
'script-src': [CSP.SELF],
'style-src': [CSP.SELF],
'img-src': [
'*',
'data:',
],
'connect-src': ['*'],
'navigate-to': [
CSP.SELF,
`${HOMEPAGE_URL}/*`,
DEVTOOLS_DOCS_URL,
GITHUB_URL,
TWITTER_URL,
],
'media-src': [CSP.NONE],
'child-src': [CSP.NONE],
'worker-src': [CSP.NONE],
'object-src': [CSP.NONE],
},
};
for (const p in policy) {
const outputs: string[] = [];
for (const directive in policy[p]) {
outputs.push(`${directive} ${policy[p][directive].join(' ')}`);
}
result[p] = outputs.join('; ');
}
return result;
}