MCPcopy Create free account
hub / github.com/bytebase/bytebase / exportCspHashes

Function exportCspHashes

frontend/vite-plugin-export-csp-hashes.ts:72–213  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

70 * 4. Exports everything to a JSON file that the backend reads
71 */
72export function exportCspHashes(): Plugin {
73 let outDir = "";
74
75 return {
76 name: "export-csp-hashes",
77 apply: "build",
78 enforce: "post", // Run after other plugins including @vitejs/plugin-legacy
79
80 configResolved(config) {
81 outDir = config.build.outDir;
82 },
83
84 async closeBundle() {
85 try {
86 const allHashes = new Set<string>();
87 const inlineScriptSources: Array<{
88 file: string;
89 content: string;
90 hash: string;
91 }> = [];
92
93 // 1. Get CSP hashes from @vitejs/plugin-legacy
94 try {
95 const legacyPlugin = await import("@vitejs/plugin-legacy");
96 const cspHashes = legacyPlugin.cspHashes;
97
98 if (cspHashes && cspHashes.length > 0) {
99 cspHashes.forEach((hash: string) => {
100 allHashes.add(`'sha256-${hash}'`);
101 });
102 console.log(
103 `✓ Loaded ${cspHashes.length} hashes from @vitejs/plugin-legacy`
104 );
105 }
106 } catch (error) {
107 console.warn(
108 "⚠️ Could not load hashes from @vitejs/plugin-legacy:",
109 error
110 );
111 }
112
113 // 2. Hash dynamically-injected scripts (e.g., iframe content)
114 const dynamicScripts = [
115 "src/components/MarkdownEditor/resize-observer.ts",
116 ];
117 console.log(
118 `✓ Computing hashes for ${dynamicScripts.length} dynamically-injected scripts...`
119 );
120
121 for (const scriptPath of dynamicScripts) {
122 try {
123 const fullPath = resolve(__dirname, scriptPath);
124 const scriptContent = readFileSync(fullPath, "utf-8");
125 const hash = computeSha256(scriptContent);
126 const cspHash = `'sha256-${hash}'`;
127
128 allHashes.add(cspHash);
129 inlineScriptSources.push({

Callers 1

vite.config.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected