fix: webRequest.onBeforeSendHeaders not being able to modify reserved headers (#49226)

* fix: `webRequest.onBeforeSendHeaders` not being able to modify reserved headers

* chore: add unit test for reserved header
This commit is contained in:
Samuel Attard
2025-12-19 23:08:40 +13:00
committed by GitHub
parent 7433c14af5
commit 3df3a6a736
3 changed files with 41 additions and 4 deletions

View File

@@ -1,4 +1,4 @@
import { ipcMain, protocol, session, WebContents, webContents } from 'electron/main';
import { ipcMain, net, protocol, session, WebContents, webContents } from 'electron/main';
import { expect } from 'chai';
import * as WebSocket from 'ws';
@@ -455,6 +455,35 @@ describe('webRequest module', () => {
}));
expect(onSendHeadersCalled).to.be.true();
});
it('can inject Proxy-Authorization header for net module requests', async () => {
// Proxy-Authorization is normally rejected by Chromium's network service
// for security reasons. However, for Electron's trusted net module,
// webRequest.onBeforeSendHeaders should be able to inject it via the
// TrustedHeaderClient code path.
const proxyAuthValue = 'Basic test-credentials';
let receivedProxyAuth: string | undefined;
const server = http.createServer((req, res) => {
receivedProxyAuth = req.headers['proxy-authorization'];
res.end('ok');
});
const { url: serverUrl } = await listen(server);
try {
ses.webRequest.onBeforeSendHeaders((details, callback) => {
const requestHeaders = details.requestHeaders;
requestHeaders['Proxy-Authorization'] = proxyAuthValue;
callback({ requestHeaders });
});
const response = await net.fetch(serverUrl, { bypassCustomProtocolHandlers: true });
expect(response.ok).to.be.true();
expect(receivedProxyAuth).to.equal(proxyAuthValue);
} finally {
server.close();
}
});
});
describe('webRequest.onSendHeaders', () => {