UNPKG

@aikidosec/firewall

Version:

Zen by Aikido is an embedded Web Application Firewall that autonomously protects Node.js apps against common and critical attacks

61 lines (60 loc) 2.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkContextForPathTraversal = checkContextForPathTraversal; const Source_1 = require("../../agent/Source"); const attackPath_1 = require("../../helpers/attackPath"); const extractStringsFromUserInputCached_1 = require("../../helpers/extractStringsFromUserInputCached"); const detectPathTraversal_1 = require("./detectPathTraversal"); /** * This function goes over all the different input types in the context and checks * if it possibly implies Path Traversal, if so the function returns an InterceptorResult */ function checkContextForPathTraversal({ filename, operation, context, checkPathStart = true, }) { const isUrl = filename instanceof URL; const pathString = pathToString(filename); if (!pathString) { return; } for (const source of Source_1.SOURCES) { const userInput = (0, extractStringsFromUserInputCached_1.extractStringsFromUserInputCached)(context, source); if (!userInput) { continue; } for (const str of userInput) { if ((0, detectPathTraversal_1.detectPathTraversal)(pathString, str, checkPathStart, isUrl)) { return { operation: operation, kind: "path_traversal", source: source, pathsToPayload: (0, attackPath_1.getPathsToPayload)(str, context[source]), metadata: { filename: pathString, }, payload: str, }; } } } } /** * Convert a fs path argument (string, Buffer, URL) to a string */ function pathToString(path) { if (typeof path === "string") { return path; } if (path instanceof URL) { return path.pathname; } if (path instanceof Buffer) { try { return new TextDecoder("utf-8", { fatal: true, }).decode(path); } catch { return undefined; } } return undefined; }