Security rules
disabled-auto-escaping
Section titled “disabled-auto-escaping”Finds template and Markdown options that turn off HTML escaping: noEscape: true in handlebars.compile, sanitize: false in marked.setOptions and kramed.Renderer, html: true in markdown-it, autoescape: false in swig.setDefaults, and a Mustache.escape that returns its input unchanged.
On in the recommended and all configs.
Without escaping, a value that holds HTML runs as markup in the page, so an attacker can inject scripts.
What to do
Section titled “What to do”Keep the escaping on. Mark only reviewed markup as safe, one value at a time.
Example
Section titled “Example”The call Handlebars.compile(source, { noEscape: true }) reports unescaped. Use Handlebars.compile(source).
dynamic-templates
Section titled “dynamic-templates”Finds compile and render calls of pug and ejs whose template text is not a literal: a template literal with an expression, a concatenation with a computed part, or any other value that is not written in the code.
On in the recommended and all configs.
A template engine runs the code inside a template, so a template built from input lets an attacker run code on the server.
What to do
Section titled “What to do”Keep templates in fixed files or constants, and pass the values as template data.
Example
Section titled “Example”The call pug.render("p Hello " + name) reports builtTemplate. Use pug.render("p Hello #{name}", { name }).
no-code-eval
Section titled “no-code-eval”Finds code that runs a string as code: eval, Function, and the vm module functions called with an argument that is not a literal, and javascript: URLs built from a template literal or a concatenation with a computed part.
On in the recommended and all configs.
Code built at run time can run whatever an attacker puts into its input.
What to do
Section titled “What to do”Call the code directly, or select it from a fixed table of functions. Pass data as data, such as through JSON.parse.
Example
Section titled “Example”The call eval(userInput) reports dynamicCode. Call the function that the input names from a fixed table, such as handlers[name]().
no-dynamic-sql-queries
Section titled “no-dynamic-sql-queries”Finds query calls of pg, mysql, mysql2, sqlite3, better-sqlite3, mssql, oracledb, pg-promise, knex, sequelize, and typeorm whose query text is a template literal with an expression, a concatenation with a computed part, or the result of concat or replace.
On in the recommended and all configs.
A query built from values lets an attacker who controls a value change the query: read, change, or delete other data.
What to do
Section titled “What to do”Write the query text as a constant, and pass the values as query parameters.
Example
Section titled “Example”The call client.query(`SELECT * FROM users WHERE id = ${id}`) reports builtQuery. Use client.query("SELECT * FROM users WHERE id = $1", [id]).
no-path-command
Section titled “no-path-command”Reports child_process commands whose literal executable depends on PATH lookup.
On in the all config.
A writable directory in PATH can replace the intended program with an attacker-controlled executable.
What to do
Section titled “What to do”Use an explicit executable path and ensure its directory cannot be changed by untrusted users.
Example
Section titled “Example”spawn("/usr/bin/git", ["status"]) names the executable explicitly.
no-public-file-permissions
Section titled “no-public-file-permissions”Finds chmod, fchmod, and lchmod calls, and their Sync forms, that give other users any permission, and process.umask calls that do not remove every permission of other users.
On in the recommended and all configs.
A file that every user of the machine can read or change leaks its data or lets another process change it.
What to do
Section titled “What to do”Give no permission to other users: end the mode in 0, such as 0o750, and the umask in 7, such as 0o027.
Example
Section titled “Example”The call fs.chmodSync("/tmp/report", 0o777) reports permissive. Use fs.chmodSync("/tmp/report", 0o750).
no-public-temporary-path
Section titled “no-public-temporary-path”Reports public temporary directory literals and process.env temporary-directory values.
On in the all config.
Other users can create or replace files in shared writable directories.
What to do
Section titled “What to do”Create a private temporary directory with restrictive permissions and use its returned path.
Example
Section titled “Example”mkdtemp(join(tmpdir(), "job-")) creates a unique private directory for the job.
xml-parser-xxe
Section titled “xml-parser-xxe”Finds parseXml and parseXmlString calls of libxmljs and libxmljs2 whose options set noent to true.
On in the recommended and all configs.
The noent option replaces external entities with the content that they point to, so a crafted document can read files from the server or make it send requests.
What to do
Section titled “What to do”Remove noent: true, so that the parser keeps external entities unresolved.
Example
Section titled “Example”The call libxmljs.parseXmlString(text, { noent: true }) reports entities. Use libxmljs.parseXmlString(text).