SKY-D245 · Security in code
Dynamic require
In Node.js, require() loads a module and runs its top-level code. When the module name is a variable, whoever controls that variable decides which code runs. SKY-D245 reports require() calls whose argument is a plain variable instead of a fixed string.
- Languages
- TypeScript/JavaScript
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
Why it matters
A route that does require(req.params.format) lets a visitor load any installed package or any .js file the server can read, such as ../../uploads/evil. Loading it runs its code inside the server process.
What Skylos flags
const express = require("express"); const app = express(); app.get("/convert/:format", (req, res) => { const format = req.params.format; const converter = require(format); res.json(converter.convert(String(req.query.text ?? "")));}); app.listen(3000);What Skylos leaves alone
const express = require("express"); const converters = { csv: require("./converters/csv"), json: require("./converters/json"),};const app = express(); app.get("/convert/:format", (req, res) => { const converter = converters[req.params.format]; if (!converter) { res.status(404).end(); return; } res.json(converter.convert(String(req.query.text ?? "")));}); app.listen(3000);Why Skylos leaves it alone
Every require() gets a string literal, so the set of modules is fixed when the file is written. The request only picks one of them from a lookup table.
How Skylos decides
- Parses each TypeScript and JavaScript file and finds calls to the global name
requirewhose arguments include a bare identifier, such asrequire(name). - Reports each match as HIGH, whatever the variable holds. The rule does not follow where the value comes from.
- A string literal is never reported. A template literal (`
require(./plugins/${name})), a+expression and dynamicimport()` are not matched by this rule.
How to fix it
Load modules with fixed strings and pick one from a lookup table, or check the name against a literal allowlist and load from a fixed folder.
const express = require("express"); Added: const converters = {Added: csv: () => require("./converters/csv"),Added: json: () => require("./converters/json"),Added: };const app = express(); app.get("/convert/:format", (req, res) => {Removed: const format = req.params.format;Removed: const converter = require(format);Removed: res.json(converter.convert(String(req.query.text ?? "")));Added: const load = converters[req.params.format];Added: if (!load) {Added: res.status(404).end();Added: return;Added: }Added: res.json(load().convert(String(req.query.text ?? "")));}); app.listen(3000);When it can be wrong
- A command-line tool that loads a config file named by its own user, such as
require(configPath), is reported even though no visitor can reach it. - A plugin loader that only passes names from a fixed list is reported when it calls
require(name)with a variable.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
Mark a reviewed line and say why. A bare
// noqadoes not hide HIGH security findings.JavaScript const plugin = require(name); // skylos: ignore[SKY-D245] name from PLUGINSAccept the findings you already have with
skylos baseline ., then scan with--baselineso only new ones are listed.[tool.skylos] ignore = ["SKY-D245"]turns the rule off for the whole project.