项目文件夹

文件

项目文件夹

0
T

忽略 .git-blame-ignore-revs 的修订。点击 绕过 并查看正常的 Blame 视图。

67 行
2.0 KiB
TypeScript

2024-09-10 19:37:36 -07:00
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { MessagePoster } from './messaging';
import { SettingsManager } from './settings';
import { getStrings } from './strings';
/**
* Shows an alert when there is a content security policy violation.
*/
export class CspAlerter {
2025-02-28 18:01:53 -08:00
private _didShow = false;
private _didHaveCspWarning = false;
2024-09-10 19:37:36 -07:00
2025-02-28 18:01:53 -08:00
private _messaging?: MessagePoster;
2024-09-10 19:37:36 -07:00
constructor(
2025-02-28 18:01:53 -08:00
private readonly _settingsManager: SettingsManager,
2024-09-10 19:37:36 -07:00
) {
document.addEventListener('securitypolicyviolation', () => {
2025-02-28 18:01:53 -08:00
this._onCspWarning();
2024-09-10 19:37:36 -07:00
});
window.addEventListener('message', (event) => {
if (event && event.data && event.data.name === 'vscode-did-block-svg') {
2025-02-28 18:01:53 -08:00
this._onCspWarning();
2024-09-10 19:37:36 -07:00
}
});
}
public setPoster(poster: MessagePoster) {
2025-02-28 18:01:53 -08:00
this._messaging = poster;
if (this._didHaveCspWarning) {
this._showCspWarning();
2024-09-10 19:37:36 -07:00
}
}
2025-02-28 18:01:53 -08:00
private _onCspWarning() {
this._didHaveCspWarning = true;
this._showCspWarning();
2024-09-10 19:37:36 -07:00
}
2025-02-28 18:01:53 -08:00
private _showCspWarning() {
2024-09-10 19:37:36 -07:00
const strings = getStrings();
2025-02-28 18:01:53 -08:00
const settings = this._settingsManager.settings;
2024-09-10 19:37:36 -07:00
2025-02-28 18:01:53 -08:00
if (this._didShow || settings.disableSecurityWarnings || !this._messaging) {
2024-09-10 19:37:36 -07:00
return;
}
2025-02-28 18:01:53 -08:00
this._didShow = true;
2024-09-10 19:37:36 -07:00
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 = () => {
2025-02-28 18:01:53 -08:00
this._messaging!.postMessage('showPreviewSecuritySelector', { source: settings.source });
2024-09-10 19:37:36 -07:00
};
document.body.appendChild(notification);
}
}