Skip to main content

SKY-D245 · Security in code

Dynamic require

High

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
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

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

src/server.js · JavaScriptReported on line 7
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

src/server.js · JavaScript
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

  1. Parses each TypeScript and JavaScript file and finds calls to the global name require whose arguments include a bare identifier, such as require(name).
  2. Reports each match as HIGH, whatever the variable holds. The rule does not follow where the value comes from.
  3. A string literal is never reported. A template literal (` require(./plugins/${name}) ), a + expression and dynamic import()` 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.

src/server.js · JavaScript · the fix, against the flagged file
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 // noqa does not hide HIGH security findings.

    JavaScript
    const plugin = require(name); // skylos: ignore[SKY-D245] name from PLUGINS
  • Accept the findings you already have with skylos baseline ., then scan with --baseline so only new ones are listed.

  • [tool.skylos] ignore = ["SKY-D245"] turns the rule off for the whole project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.