forked from asciidoctor/asciidoctor-vscode
/
csp.ts
63 lines (52 loc) · 1.88 KB
/
csp.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
/*---------------------------------------------------------------------------------------------
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { MessagePoster } from './messaging'
import { getSettings } from './settings'
import { getStrings } from './strings'
/**
* Shows an alert when there is a content security policy violation.
*/
export class CspAlerter {
private didShow = false;
private didHaveCspWarning = false;
private messaging?: MessagePoster;
constructor () {
document.addEventListener('securitypolicyviolation', () => {
this.onCspWarning()
})
window.addEventListener('message', (event) => {
if (event && event.data && event.data.name === 'vscode-did-block-svg') {
this.onCspWarning()
}
})
}
public setPoster (poster: MessagePoster) {
this.messaging = poster
if (this.didHaveCspWarning) {
this.showCspWarning()
}
}
private onCspWarning () {
this.didHaveCspWarning = true
this.showCspWarning()
}
private showCspWarning () {
const strings = getStrings()
const settings = getSettings()
if (this.didShow || settings.disableSecurityWarnings || !this.messaging) {
return
}
this.didShow = true
const notification = document.createElement('a')
notification.innerText = strings.cspAlertMessageText
notification.setAttribute('id', 'code-csp-warning')
notification.setAttribute('title', strings.cspAlertMessageTitle)
notification.setAttribute('role', 'button')
notification.setAttribute('aria-label', strings.cspAlertMessageLabel)
notification.onclick = () => {
this.messaging!.postMessage('showPreviewSecuritySelector', { source: settings.source })
}
document.body.appendChild(notification)
}
}