Session cookies are signed with a secret written in the source
What it finds
app.use(session({ secret: "keyboard cat" })): the session middleware signs the session cookie with this secret. Whoever reads it in the source or its history can sign cookies of their own and act as any user (CWE-798). A fallback such as process.env.SESSION_SECRET || "dev" does the same wherever the variable is missing.
Reported are the secret option of session(...) in files that use express-session, and the secret and keys options of cookieSession(...) in files that use cookie-session, when the value is a non-empty string literal, a constant initialized with one, || or ?? with one on the right, or an array of such values. Test files are left alone.
From the rule's source, hardcoded_session_secret.rs.
import express from "express";import session from "express-session";import cookieSession from "cookie-session";const app = express();app.use(session({ secret: "keyboard cat", resave: false }));tx0 reports: this session secret is written in the sourceapp.use(session({ secret: process.env.SESSION_SECRET || "dev", resave: false }));tx0 reports: this session secret falls back to a value written in the sourceapp.use(cookieSession({ name: "s", keys: ["key1", process.env.KEY2 as string] }));tx0 reports: this session secret is written in the sourceapp.use(session({ secret: process.env.SESSION_SECRET as string, resave: false }));app.use(session({ secret: [String(process.env.A)], resave: false })); Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/ts:hardcoded-session-secret" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/ts:hardcoded-session-secret] reason="..."tx0 explain tx0/ts:hardcoded-session-secret shows the same in the terminal. Found a false positive? Report it.