Path: blob/main/extensions/markdown-language-features/preview-src/csp.ts
3291 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { MessagePoster } from './messaging';6import { SettingsManager } from './settings';7import { getStrings } from './strings';89/**10* Shows an alert when there is a content security policy violation.11*/12export class CspAlerter {13private _didShow = false;14private _didHaveCspWarning = false;1516private _messaging?: MessagePoster;1718constructor(19private readonly _settingsManager: SettingsManager,20) {21document.addEventListener('securitypolicyviolation', () => {22this._onCspWarning();23});2425window.addEventListener('message', (event) => {26if (event?.data && event.data.name === 'vscode-did-block-svg') {27this._onCspWarning();28}29});30}3132public setPoster(poster: MessagePoster) {33this._messaging = poster;34if (this._didHaveCspWarning) {35this._showCspWarning();36}37}3839private _onCspWarning() {40this._didHaveCspWarning = true;41this._showCspWarning();42}4344private _showCspWarning() {45const strings = getStrings();46const settings = this._settingsManager.settings;4748if (this._didShow || settings.disableSecurityWarnings || !this._messaging) {49return;50}51this._didShow = true;5253const notification = document.createElement('a');54notification.innerText = strings.cspAlertMessageText;55notification.setAttribute('id', 'code-csp-warning');56notification.setAttribute('title', strings.cspAlertMessageTitle);5758notification.setAttribute('role', 'button');59notification.setAttribute('aria-label', strings.cspAlertMessageLabel);60notification.onclick = () => {61this._messaging!.postMessage('showPreviewSecuritySelector', { source: settings.source });62};63document.body.appendChild(notification);64}65}666768