Use much stricter, whitelist based CSP (#3162)

This commit is contained in:
Vending Machine 2025-06-06 18:30:19 +02:00 committed by GitHub
parent 0ce7772500
commit e7076f5aee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 221 additions and 74 deletions

View file

@ -0,0 +1,34 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2025 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { useLayoutEffect } from "@webpack/common";
import { useForceUpdater } from "./react";
const cssRelevantDirectives = ["style-src", "img-src", "font-src"] as const;
export const CspBlockedUrls = new Set<string>();
const CspErrorListeners = new Set<() => void>();
document.addEventListener("securitypolicyviolation", ({ effectiveDirective, blockedURI }) => {
if (!blockedURI || !cssRelevantDirectives.includes(effectiveDirective as any)) return;
CspBlockedUrls.add(blockedURI);
CspErrorListeners.forEach(listener => listener());
});
export function useCspErrors() {
const forceUpdate = useForceUpdater();
useLayoutEffect(() => {
CspErrorListeners.add(forceUpdate);
return () => void CspErrorListeners.delete(forceUpdate);
}, [forceUpdate]);
return [...CspBlockedUrls] as const;
}