Skip to content

Commit

Permalink
Pass X-Cors-Headers between the client and the remote
Browse files Browse the repository at this point in the history
  • Loading branch information
infinity0 committed Oct 8, 2023
1 parent 70aaa22 commit 1fbaade
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/cors-anywhere.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,17 @@ function onProxyResponse(proxy, proxyReq, proxyRes, req, res) {
}
}

let extraHeaders = {}

// Strip cookies
delete proxyRes.headers['set-cookie'];
delete proxyRes.headers['set-cookie2'];
for (let k of ['set-cookie', 'set-cookie2']) {
if (k in proxyRes.headers) {
extraHeaders[k] = proxyRes.headers[k];
delete proxyRes.headers[k];
}
}

proxyRes.headers['x-cors-headers'] = JSON.stringify(extraHeaders);
proxyRes.headers['x-final-url'] = requestState.location.href;
withCORS(proxyRes.headers, req);
return true;
Expand Down Expand Up @@ -392,6 +399,13 @@ function getHandler(options, proxy) {
var isRequestedOverHttps = req.connection.encrypted || /^\s*https/.test(req.headers['x-forwarded-proto']);
var proxyBaseUrl = (isRequestedOverHttps ? 'https://' : 'http://') + req.headers.host;

try {
let extraHeaders = JSON.parse(req.headers['x-cors-headers'] || "{}");
for (let [k, v] of Object.entries(extraHeaders)) {
req.headers[k] = v;
}
} catch (e) {}

corsAnywhere.removeHeaders.forEach(function(header) {
delete req.headers[header];
});
Expand Down

0 comments on commit 1fbaade

Please sign in to comment.