SKY-D253 · Security in code
Secret compared with == or ===
=== and == stop at the first character that differs, so comparing a secret this way takes slightly longer the more leading characters match. SKY-D253 reports an equality check in TypeScript or JavaScript that puts a secret the server holds (an environment or config credential, an HMAC it computes, a stored token or password) against a value the attacker sends. In Python it recognizes a keyed digest produced by the standard hmac module compared with a runtime value using ordinary equality or inequality.
- Languages
- TypeScript/JavaScript, Python
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
- Catalog name
- Timing-unsafe comparison
Why it matters
That time difference is a "timing side channel". An attacker who can send many requests and measure the answers can guess a webhook signature or an API key one character at a time. crypto.timingSafeEqual() always takes the same time.
What Skylos flags
import crypto from "node:crypto";import type { Request, Response } from "express"; const webhookKey = process.env.WEBHOOK_KEY ?? ""; export function receiveWebhook(req: Request, res: Response) { const signature = req.get("x-signature") ?? ""; const body = JSON.stringify(req.body); const expected = crypto.createHmac("sha256", webhookKey).update(body).digest("hex"); if (signature !== expected) { return res.status(401).end(); } res.status(204).end();}What Skylos leaves alone
import crypto from "node:crypto";import type { Request, Response } from "express"; const webhookKey = process.env.WEBHOOK_KEY ?? ""; export function receiveWebhook(req: Request, res: Response) { const signature = Buffer.from(req.get("x-signature") ?? "", "hex"); const body = JSON.stringify(req.body); const expected = crypto.createHmac("sha256", webhookKey).update(body).digest(); if ( signature.length !== expected.length || !crypto.timingSafeEqual(signature, expected) ) { return res.status(401).end(); } res.status(204).end();}Why Skylos leaves it alone
signature.length !== expected.length compares two lengths, which are public (timingSafeEqual needs equal lengths): a length is neither the secret nor the request's guess. The secret bytes themselves are compared with crypto.timingSafeEqual().
How Skylos decides
- Runs with
--dangeron TypeScript and JavaScript files. Finds every==,===,!=and!==expression. - Classifies each side. A secret is an environment or config value whose key names a credential (
process.env.API_KEY,config.get('x.secret')), an HMAC built withcreateHmac, a member the function filled from one of those, or a credential-named value that is neither request data nor a parameter (user.resetToken,storedHash). The guess is request data in the handler (req.body.token,req.get('x-signature'),request.headers.get(...),formData.get(...), a destructured({ body }: Request)field) or a local bound from it. - Reports MEDIUM when request data meets a secret, or when a parameter meets an environment, config or HMAC secret (
key === process.env.ADMIN_KEY). One finding per comparison. - Skips literals,
null/undefinedand booleans, two request fields (a password and its confirmation), two values of unknown origin, hash or digest calls (hash(input) === stored: the attacker cannot steer digest bytes) and names that only end in token or hash (location.hash,ts.SyntaxKind.ExclamationToken).
How to fix it
Turn both values into Buffers, check that the lengths match, and compare the bytes with crypto.timingSafeEqual(). For passwords, use a password-hashing library such as bcrypt or argon2, whose verify call already compares safely.
import crypto from "node:crypto";import type { Request, Response } from "express"; const webhookKey = process.env.WEBHOOK_KEY ?? ""; export function receiveWebhook(req: Request, res: Response) {Removed: const signature = req.get("x-signature") ?? "";Added: const signature = Buffer.from(req.get("x-signature") ?? "", "hex"); const body = JSON.stringify(req.body);Removed: const expected = crypto.createHmac("sha256", webhookKey).update(body).digest("hex");Removed: if (signature !== expected) {Added: const expected = crypto.createHmac("sha256", webhookKey).update(body).digest();Added: const valid =Added: signature.length === expected.length &&Added: crypto.timingSafeEqual(signature, expected);Added: if (!valid) { return res.status(401).end(); } res.status(204).end();}When it can be wrong
- A request value compared with a stored value whose name ends in token, hash or secret is reported even when that value is public, such as
req.query.etag === file.hash. - Missed: secrets in variables with neutral names (
if (a === b)), secrets passed through helpers of other files, secrets added by a schema.transform(),Buffer.equals(), Python comparisons other than an hmac digest, and comparisons in Go or Java.
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 the reported line and say why timing does not matter there.
TypeScript if (fileHash !== cachedHash) { // skylos: ignore[SKY-D253] cache key, not a secret[tool.skylos] ignore = ["SKY-D253"]turns the rule off for the whole project.skylos baseline .and then--baselineaccept the existing comparisons and report only new ones.