Skip to content

Commit

Permalink
also fix headers (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
zviadm authored May 15, 2024
1 parent 031e66b commit a57ad10
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,20 @@ function createMainWindow() {
// but that is a bigger security relaxation compared to just overriding request headers.
const corsFilter = {urls: CORSByPassURLs}
window.webContents.session.webRequest.onBeforeSendHeaders(
corsFilter, (details, callback) => {
details.requestHeaders['Origin'] = ''
callback({ requestHeaders: details.requestHeaders })
corsFilter,
(details, callback) => {
const { requestHeaders } = details;
UpsertKeyValue(requestHeaders, 'Origin', '*');
callback({ requestHeaders })
}
)
window.webContents.session.webRequest.onHeadersReceived(
corsFilter,
(details, callback) => {
const { responseHeaders } = details;
UpsertKeyValue(responseHeaders, 'Access-Control-Allow-Origin', ['*']);
UpsertKeyValue(responseHeaders, 'Access-Control-Allow-Headers', ['*']);
callback({ responseHeaders })
}
)

Expand Down Expand Up @@ -194,3 +205,19 @@ if (!gotLock) {

setupAutoUpdater()
}

// Headers can have keys with different capitalization. This function makes sure that we replace
// the correct key no matter the capitalization.
function UpsertKeyValue<T>(headers: Record<string, T> | undefined, keyToChange: string, value: T) {
if (!headers) {
return
}
const keyLowered = keyToChange.toLowerCase()
for (const key of Object.keys(headers)) {
if (key.toLowerCase() === keyLowered) {
headers[key] = value
return
}
}
headers[keyToChange] = value
}

0 comments on commit a57ad10

Please sign in to comment.