forked from mxx1111/mdlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshare-sanitize.ts
More file actions
38 lines (34 loc) · 1.32 KB
/
Copy pathshare-sanitize.ts
File metadata and controls
38 lines (34 loc) · 1.32 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
/** CSP for read-only share pages (no scripts, inline styles only). */
export const SHARE_PAGE_CSP = [
`default-src 'self'`,
`script-src 'none'`,
`style-src 'unsafe-inline'`,
`img-src 'self' https: data: blob:`,
`font-src https: data:`,
`connect-src 'none'`,
`frame-src 'none'`,
`object-src 'none'`,
`base-uri 'none'`,
`form-action 'self'`,
].join(`; `)
export function sharePageCspMeta(): string {
return `<meta http-equiv="Content-Security-Policy" content="${SHARE_PAGE_CSP}" />`
}
const SCRIPT_TAG = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi
const DANGEROUS_TAGS = /<\/?(?:iframe|object|embed|form|meta|link|base)\b[^>]*>/gi
const EVENT_HANDLERS = /\son\w+\s*=\s*("[^"]*"|'[^']*'|[^\s>]+)/gi
const DANGEROUS_URL_ATTRS = /\s(href|src|xlink:href)\s*=\s*(?:("|')\s*)?(?:javascript|vbscript):[^"'>\s]*/gi
const STYLE_BREAKOUT = /<\/style>/gi
const DANGEROUS_CSS_IMPORT = /@import\s[^;]*(?:javascript|data:text\/html)/gi
export function sanitizeHtmlSnapshot(html: string): string {
return html
.replace(SCRIPT_TAG, ``)
.replace(DANGEROUS_TAGS, ``)
.replace(EVENT_HANDLERS, ``)
.replace(DANGEROUS_URL_ATTRS, ``)
}
export function sanitizeStylesSnapshot(stylesHtml: string): string {
return sanitizeHtmlSnapshot(stylesHtml)
.replace(STYLE_BREAKOUT, ``)
.replace(DANGEROUS_CSS_IMPORT, ``)
}