mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 20:20:49 +00:00
56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
import {
|
|
getNpmCustomRegistries,
|
|
skipMinimumPackageAge,
|
|
} from "../../../config/settings.js";
|
|
import { isMalwarePackage } from "../../../scanning/audit/index.js";
|
|
import { interceptRequests } from "../interceptorBuilder.js";
|
|
import {
|
|
isPackageInfoUrl,
|
|
modifyNpmInfoRequestHeaders,
|
|
modifyNpmInfoResponse,
|
|
} from "./modifyNpmInfo.js";
|
|
import { parseNpmPackageUrl } from "./parseNpmPackageUrl.js";
|
|
|
|
const knownJsRegistries = [
|
|
"registry.npmjs.org",
|
|
"registry.yarnpkg.com",
|
|
"registry.npmjs.com",
|
|
];
|
|
|
|
/**
|
|
* @param {string} url
|
|
* @returns {import("../interceptorBuilder.js").Interceptor | undefined}
|
|
*/
|
|
export function npmInterceptorForUrl(url) {
|
|
const registry = [...knownJsRegistries, ...getNpmCustomRegistries()].find(
|
|
(reg) => url.includes(reg)
|
|
);
|
|
|
|
if (registry) {
|
|
return buildNpmInterceptor(registry);
|
|
}
|
|
|
|
return undefined;
|
|
}
|
|
|
|
/**
|
|
* @param {string} registry
|
|
* @returns {import("../interceptorBuilder.js").Interceptor}
|
|
*/
|
|
function buildNpmInterceptor(registry) {
|
|
return interceptRequests(async (reqContext) => {
|
|
const { packageName, version } = parseNpmPackageUrl(
|
|
reqContext.targetUrl,
|
|
registry
|
|
);
|
|
|
|
if (await isMalwarePackage(packageName, version)) {
|
|
reqContext.blockMalware(packageName, version);
|
|
}
|
|
|
|
if (!skipMinimumPackageAge() && isPackageInfoUrl(reqContext.targetUrl)) {
|
|
reqContext.modifyRequestHeaders(modifyNpmInfoRequestHeaders);
|
|
reqContext.modifyBody(modifyNpmInfoResponse);
|
|
}
|
|
});
|
|
}
|