Skip to content

Commit 6ced19b

Browse files
committed
fix a CSP issue in corpus-view
1 parent aa3388b commit 6ced19b

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

tests/corpus-view.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ const WEBEXTENSION_POLYFILL_REPLACEMENT =
3939
'console.warn("Webextension-polyfill check bypassed for corpus testing")'
4040
const BROWSER_API_MOCKS =
4141
'window.chrome=window.chrome||{runtime:{getURL:path=>"chrome-extension://gitcasso-test/"+path,onMessage:{addListener:()=>{}},sendMessage:()=>Promise.resolve(),id:"gitcasso-test"}};window.browser=window.chrome;'
42-
const PERMISSIVE_CSP = "default-src 'self' 'unsafe-inline' 'unsafe-eval' data: blob: http: https:;"
42+
const PERMISSIVE_CSP =
43+
"default-src 'self' 'unsafe-inline' 'unsafe-eval' data: blob: http: https:; connect-src 'self' http: https:; script-src 'self' 'unsafe-inline' 'unsafe-eval';"
4344

4445
// UI Styles
4546
const REBUILD_BUTTON_STYLES = `
@@ -237,6 +238,10 @@ app.get('/corpus/:key/:mode(clean|gitcasso)', async (req, res) => {
237238

238239
// Replace external URLs with local asset URLs
239240
let html = mainEntry.response.content.text!
241+
242+
// Strip CSP headers that might block our injected scripts
243+
html = stripCSPFromHTML(html)
244+
240245
domains.forEach((domain) => {
241246
const escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
242247
const regex = new RegExp(`https?://${escapedDomain}`, 'g')
@@ -245,6 +250,13 @@ app.get('/corpus/:key/:mode(clean|gitcasso)', async (req, res) => {
245250
if (mode === 'gitcasso') {
246251
html = injectGitcassoScriptForHAR(key, html)
247252
}
253+
254+
// Set permissive headers for HAR corpus to allow rebuild requests
255+
res.set({
256+
'Content-Security-Policy': PERMISSIVE_CSP,
257+
'X-Content-Type-Options': 'nosniff',
258+
})
259+
248260
return res.send(html)
249261
} else if (entry.type === 'html') {
250262
// Handle HTML corpus
@@ -385,12 +397,15 @@ app.listen(PORT, () => {
385397

386398
// Strip CSP meta tags and headers from HTML that might block our scripts
387399
function stripCSPFromHTML(html: string): string {
388-
// Remove CSP meta tags
389-
html = html.replace(/<meta[^>]*http-equiv\s*=\s*["']content-security-policy["'][^>]*>/gi, '')
390-
html = html.replace(/<meta[^>]*name\s*=\s*["']content-security-policy["'][^>]*>/gi, '')
400+
// Remove CSP meta tags - more comprehensive patterns
401+
html = html.replace(/<meta[^>]*http-equiv\s*=\s*["']?content-security-policy["']?[^>]*>/gi, '')
402+
html = html.replace(/<meta[^>]*name\s*=\s*["']?content-security-policy["']?[^>]*>/gi, '')
403+
404+
// Also match patterns where content-security-policy appears anywhere in the meta tag
405+
html = html.replace(/<meta[^>]*content-security-policy[^>]*>/gi, '')
391406

392407
// Remove any other restrictive security meta tags
393-
html = html.replace(/<meta[^>]*http-equiv\s*=\s*["']x-content-type-options["'][^>]*>/gi, '')
408+
html = html.replace(/<meta[^>]*http-equiv\s*=\s*["']?x-content-type-options["']?[^>]*>/gi, '')
394409

395410
return html
396411
}

0 commit comments

Comments
 (0)