Fix proxy for npm 10.0.0 -> 10.4.0

This commit is contained in:
Sander Declerck 2025-10-01 08:10:49 +02:00
parent 3b145a4695
commit 95663dc5f4
No known key found for this signature in database

View file

@ -17,7 +17,8 @@ function createHttpsServer(hostname, isAllowed) {
const cert = generateCertForHost(hostname); const cert = generateCertForHost(hostname);
async function handleRequest(req, res) { async function handleRequest(req, res) {
const targetUrl = `https://${hostname}${req.url}`; const pathAndQuery = getRequestPathAndQuery(req.url);
const targetUrl = `https://${hostname}${pathAndQuery}`;
if (!(await isAllowed(targetUrl))) { if (!(await isAllowed(targetUrl))) {
res.writeHead(403, "Forbidden - blocked by safe-chain"); res.writeHead(403, "Forbidden - blocked by safe-chain");
@ -38,6 +39,14 @@ function createHttpsServer(hostname, isAllowed) {
); );
} }
function getRequestPathAndQuery(url) {
if (url.startsWith("http://") || url.startsWith("https://")) {
const parsedUrl = new URL(url);
return parsedUrl.pathname + parsedUrl.search + parsedUrl.hash;
}
return url;
}
function forwardRequest(req, hostname, res) { function forwardRequest(req, hostname, res) {
const proxyReq = createProxyRequest(hostname, req, res); const proxyReq = createProxyRequest(hostname, req, res);