Skip to content

Commit

Permalink
Refactor: Simplify and optimize CSP header modification logic
Browse files Browse the repository at this point in the history
  • Loading branch information
donneypr committed Dec 24, 2024
1 parent 23fb1b8 commit 032708c
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions desktop-app/src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,36 +142,31 @@ const createWindow = async () => {
// Add BROWSER_SYNC_HOST to the allowed Content-Security-Policy origins
mainWindow.webContents.session.webRequest.onHeadersReceived(
async (details, callback) => {
if (details.responseHeaders?.['content-security-policy']) {
let cspHeader = details.responseHeaders['content-security-policy'][0];

cspHeader = cspHeader.replace(
'default-src',
`default-src ${BROWSER_SYNC_HOST}`
);
cspHeader = cspHeader.replace(
'script-src',
`script-src ${BROWSER_SYNC_HOST}`
);
cspHeader = cspHeader.replace(
'script-src-elem',
`script-src-elem ${BROWSER_SYNC_HOST}`
const cspKey = 'content-security-policy';

Check failure on line 146 in desktop-app/src/main/main.ts

View workflow job for this annotation

GitHub Actions / test (macos-latest)

Delete `··`
if (details.responseHeaders?.[cspKey]) {
const cspHeader = details.responseHeaders[cspKey][0];

Check failure on line 149 in desktop-app/src/main/main.ts

View workflow job for this annotation

GitHub Actions / test (macos-latest)

Delete `··`
// Define the rules to replace dynamically
const replacements: Record<string, string> = {
'default-src': `default-src ${BROWSER_SYNC_HOST}`,
'script-src': `script-src ${BROWSER_SYNC_HOST}`,
'script-src-elem': `script-src-elem ${BROWSER_SYNC_HOST}`,
'connect-src': `connect-src ${BROWSER_SYNC_HOST} wss://${BROWSER_SYNC_HOST} ws://${BROWSER_SYNC_HOST}`,
'child-src': `child-src ${BROWSER_SYNC_HOST}`,
'worker-src': `worker-src ${BROWSER_SYNC_HOST}`,
};

Check failure on line 159 in desktop-app/src/main/main.ts

View workflow job for this annotation

GitHub Actions / test (macos-latest)

Delete `··`
// Apply replacements
const updatedCSPHeader = Object.entries(replacements).reduce(
(header, [key, value]) => header.replace(key, value),
cspHeader
);
cspHeader = cspHeader.replace(
'connect-src',
`connect-src ${BROWSER_SYNC_HOST} wss://${BROWSER_SYNC_HOST} ws://${BROWSER_SYNC_HOST}`
);
cspHeader = cspHeader.replace(
'child-src',
`child-src ${BROWSER_SYNC_HOST}`
);
cspHeader = cspHeader.replace(
'worker-src',
`worker-src ${BROWSER_SYNC_HOST}`
); // Required when/if the browser-sync script is eventually relocated to a web worker

details.responseHeaders['content-security-policy'][0] = cspHeader;

Check failure on line 165 in desktop-app/src/main/main.ts

View workflow job for this annotation

GitHub Actions / test (macos-latest)

Delete `··`
// Update the response headers
details.responseHeaders[cspKey][0] = updatedCSPHeader;
}

Check failure on line 169 in desktop-app/src/main/main.ts

View workflow job for this annotation

GitHub Actions / test (macos-latest)

Delete `··`
callback({ responseHeaders: details.responseHeaders });
}
);
Expand Down

0 comments on commit 032708c

Please sign in to comment.