mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 12:10:49 +00:00
Let modifyNpmInfoRequestHeaders return the header collection as well.
This commit is contained in:
parent
5834229427
commit
44ee58aa9b
3 changed files with 18 additions and 10 deletions
|
|
@ -10,14 +10,14 @@ import { EventEmitter } from "events";
|
|||
* @typedef {Object} RequestInterceptionContext
|
||||
* @property {string} targetUrl
|
||||
* @property {(packageName: string | undefined, version: string | undefined) => void} blockMalware
|
||||
* @property {(modificationFunc: (headers: NodeJS.Dict<string | string[]>) => void) => void} modifyRequestHeaders
|
||||
* @property {(modificationFunc: (headers: NodeJS.Dict<string | string[]>) => NodeJS.Dict<string | string[]>) => void} modifyRequestHeaders
|
||||
* @property {(modificationFunc: (body: Buffer, headers: NodeJS.Dict<string | string[]> | undefined) => Buffer) => void} modifyBody
|
||||
* @property {() => RequestInterceptionHandler} build
|
||||
*
|
||||
*
|
||||
* @typedef {Object} RequestInterceptionHandler
|
||||
* @property {{statusCode: number, message: string} | undefined} blockResponse
|
||||
* @property {(headers: NodeJS.Dict<string | string[]> | undefined) => void} modifyRequestHeaders
|
||||
* @property {(headers: NodeJS.Dict<string | string[]> | undefined) => NodeJS.Dict<string | string[]> | undefined} modifyRequestHeaders
|
||||
* @property {() => boolean} modifiesResponse
|
||||
* @property {(body: Buffer, headers: NodeJS.Dict<string | string[]> | undefined) => Buffer} modifyBody
|
||||
*/
|
||||
|
|
@ -65,7 +65,7 @@ function buildInterceptor(requestHandlers) {
|
|||
function createRequestContext(targetUrl, eventEmitter) {
|
||||
/** @type {{statusCode: number, message: string} | undefined} */
|
||||
let blockResponse = undefined;
|
||||
/** @type {Array<(headers: NodeJS.Dict<string | string[]>) => void>} */
|
||||
/** @type {Array<(headers: NodeJS.Dict<string | string[]>) => NodeJS.Dict<string | string[]>>} */
|
||||
let reqheaderModificationFuncs = [];
|
||||
/** @type {Array<(body: Buffer, headers: NodeJS.Dict<string | string[]> | undefined) => Buffer>} */
|
||||
let modifyBodyFuncs = [];
|
||||
|
|
@ -91,15 +91,20 @@ function createRequestContext(targetUrl, eventEmitter) {
|
|||
|
||||
/** @returns {RequestInterceptionHandler} */
|
||||
function build() {
|
||||
/** @param {NodeJS.Dict<string | string[]> | undefined} headers */
|
||||
/**
|
||||
* @param {NodeJS.Dict<string | string[]> | undefined} headers
|
||||
* @returns {NodeJS.Dict<string | string[]> | undefined}
|
||||
*/
|
||||
function modifyRequestHeaders(headers) {
|
||||
if (!headers) return;
|
||||
|
||||
if (headers) {
|
||||
for (const func of reqheaderModificationFuncs) {
|
||||
func(headers);
|
||||
}
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Buffer} body
|
||||
* @param {NodeJS.Dict<string | string[]> | undefined} headers
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ const state = {
|
|||
|
||||
/**
|
||||
* @param {NodeJS.Dict<string | string[]>} headers
|
||||
* @returns {NodeJS.Dict<string | string[]>}
|
||||
*/
|
||||
export function modifyNpmInfoRequestHeaders(headers) {
|
||||
const accept = getHeaderValueAsString(headers, "accept");
|
||||
|
|
@ -17,6 +18,7 @@ export function modifyNpmInfoRequestHeaders(headers) {
|
|||
// Force the registry to return the full metadata by changing the Accept header.
|
||||
headers["accept"] = "application/json";
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -151,11 +151,12 @@ function forwardRequest(req, hostname, res, requestHandler) {
|
|||
* @returns {import("http").ClientRequest}
|
||||
*/
|
||||
function createProxyRequest(hostname, req, res, requestHandler) {
|
||||
const headers = { ...req.headers };
|
||||
/** @type {NodeJS.Dict<string | string[]> | undefined} */
|
||||
let headers = { ...req.headers };
|
||||
if (headers.host) {
|
||||
delete headers.host;
|
||||
}
|
||||
requestHandler.modifyRequestHeaders(headers);
|
||||
headers = requestHandler.modifyRequestHeaders(headers);
|
||||
|
||||
/** @type {import("http").RequestOptions} */
|
||||
const options = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue