-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 NEFARIOUSPLAN-CANONICAL-V1 {"body_md":"## The gate was the previous fix\n\nThe function that fails in `tmp` 0.2.6 did not exist in 0.2.5. Commit `7ef2728` (April 6, 2026), titled \"Check for relative values,\" is upstream maintainer KARASZI István's response to GHSA-ph9p-34f9-6g65, an earlier path-handling advisory against the same package. The commit adds a five-line helper to `lib/tmp.js`:\n\n```javascript\nfunction _assertPath(path) {\n if (path.includes(\"..\")) {\n throw new Error(\"Relative value not allowed\");\n }\n\n return path;\n}\n```\n\nThe same commit wires `_assertPath` into `_assertOptionsBase` for the three caller-controlled name fragments:\n\n```javascript\noptions.prefix = _isUndefined(options.prefix) ? '' : _assertPath(options.prefix);\noptions.postfix = _isUndefined(options.postfix) ? '' : _assertPath(options.postfix);\noptions.template = _isUndefined(options.template) ? undefined : _assertPath(options.template);\n```\n\n`tmp` is the temporary-file generator that has lived in the dependency graph of build tools, test runners, CLI scaffolds, and ORMs since 2011. Its documented API accepts `prefix`, `postfix`, and `template` as user-controllable strings that contribute to the generated path. The 0.2.6 patch is the maintainer's commitment that those strings cannot contain `..`. The commitment is encoded as `.includes('..')`. The two diverge on the first non-string the caller sends.\n\n## `Array.prototype.includes` is element equality\n\n`String.prototype.includes(substring)` returns true when the argument appears as a contiguous substring of the receiver. `Array.prototype.includes(searchElement)` returns true when the argument is `===`-equal (more precisely, SameValueZero) to any element of the receiver. They share a name. They do not share a contract.\n\nThe PoC published with the advisory is one line:\n\n```javascript\ntmp.fileSync({ tmpdir: baseDir, prefix: ['../escape'] });\n```\n\n`_assertPath(['../escape'])` evaluates `['../escape'].includes(\"..\")`. The array has one element, the string `'../escape'`. The element is not strictly equal to `'..'`. The check returns false; the throw is skipped; the function returns the array unchanged.\n\n`_assertOptionsBase` writes the array back into `options.prefix` and the call proceeds into `_generateTmpName`:\n\n```javascript\nfunction _generateTmpName(opts) {\n const tmpDir = opts.tmpdir;\n\n if (!_isUndefined(opts.name)) {\n return path.join(tmpDir, opts.dir, opts.name);\n }\n\n if (!_isUndefined(opts.template)) {\n return path.join(tmpDir, opts.dir, opts.template).replace(TEMPLATE_PATTERN, _randomChars(6));\n }\n\n // prefix and postfix\n const name = [\n opts.prefix ? opts.prefix : 'tmp',\n '-',\n process.pid,\n '-',\n _randomChars(12),\n opts.postfix ? '-' + opts.postfix : ''\n ].join('');\n\n return path.join(tmpDir, opts.dir, name);\n}\n```\n\nThe outer array now contains a nested array at index 0. `Array.prototype.join('')` invokes the `String` abstract operation on each element. For an array, `String([...])` is `[...].toString()`, which is `[...].join(',')`. The inner `['../escape']` flattens to the literal string `'../escape'`. The resulting `name` reads `../escape--<12 random hex chars>`.\n\n`path.join(tmpDir, opts.dir, name)` resolves the leading `..` against `tmpDir`'s parent. The file lands one directory above the temporary base the caller asked for. The caller did not see the array stringify; the caller did not see the path resolve; the caller sees a `tmp.fileSync` call that returned successfully with a path one directory outside the sandbox they passed in.\n\nThe duck-typed object case is the same primitive on a different shape: `{ includes: () => false, toString: () => '../escape' }` defeats the check with a custom `.includes` method that returns false unconditionally, and supplies the traversal payload through the same `toString` coercion `Array.prototype.join` performs on every element. The gate calls the receiver's own `.includes`. The receiver decides what `.includes` returns.\n\n## The pattern is Gate Before Canonicalize\n\nThe shape repeats across the catalog: a security check inspects the input for a forbidden form, a downstream subsystem canonicalizes the same input into that forbidden form, and the input crosses the boundary between the two by carrying a representation neither parser shared with the other.\n\nMost prior exhibits of [gate-before-canonicalize](/patterns/gate-before-canonicalize) live in HTTP routing. [Alfresco's regex denylist that missed `..;/` because Tomcat parses path parameters after the check runs](/posts/alfresco-share-blacklist-misses-the-semicolon). [Spring's `uriDecode` truncating sixteen-bit chars before its own gate inspected the result](/posts/spring-uridecode-ghost-bits-jetty-finishes). [Gotenberg's case-sensitive regex against `HTTPS://` that `net/url.Parse` lowercased on the way to the dial](/posts/gotenberg-cve-2026-40280-named-fix-adds-the-bug). The two parses each lived in a different component; the attacker rode the disagreement between them.\n\nCVE-2026-49982 is the same shape on a different substrate. The canonicalizer is not URL parsing. It is JavaScript's automatic string coercion via `Array.prototype.join`. The gate ran `.includes('..')` against the array shape; the downstream `_generateTmpName` ran `path.join` against the string the same array stringifies to. Two parses, one function, the disagreement reified as the difference between `Array.prototype.includes` and `String.prototype.includes`.\n\nThe closest sibling on the catalog is [vm2 CVE-2026-43998](/posts/vm2-cve-2026-43998-pnpm-was-already-the-symlink), which puts the pattern on `path.resolve` versus `fs.realpathSync` inside Node's `require`. Both exhibits live in the same language. Both involve a gate written against one of two coexisting representations of the same logical value. In vm2 the two representations were the lexical path and its symlink-resolved form. In tmp the two are the array literal and its string coercion.\n\n## What the April patch did not catch\n\n0.2.6's security commit added more than `_assertPath`. The same commit reworked `_getRelativePath` and `_getRelativePathSync` to verify that the resolved path does not escape `tmpdir` after the name is built:\n\n```javascript\nconst relativePath = path.relative(tmpDir, resolvedPath);\nif (relativePath.startsWith('..') || path.isAbsolute(relativePath)) {\n throw new Error(`${option} option must be relative to \"${tmpDir}\", found \"${relativePath}\".`);\n}\n```\n\nThat is the defense-in-depth. If the gate misses, the path-relative check is supposed to catch the escape after the path has been resolved. The check is real. It is wired into `options.dir`, `options.template`, and `options.name`. It is not wired into `options.prefix` or `options.postfix`.\n\n`prefix` and `postfix` flow into `_generateTmpName`'s inline name-builder and from there directly into `path.join(tmpDir, opts.dir, name)`. There is no post-resolution verification step on the way out. The 0.2.6 patch added a perimeter check (`_assertPath`) and an interior check (`_getRelativePath`); the interior check covers three of the five caller-controlled fields and the perimeter check is the only thing between the array bypass and the file system.\n\nFor `template`, the secondary check would in principle catch a bypass that reached it. In practice, an Array-shaped template throws a `TypeError` earlier, at `options.template.match(TEMPLATE_PATTERN)`, because `Array.prototype.match` does not exist. The 0.2.7 fix addresses this same `match` call by inserting the same `typeof` check ahead of it. The exploitable primitive on 0.2.6 is the `prefix` or `postfix` path, where neither the secondary check nor the regex match stands between the gate and the file system.\n\n## The fix monomorphises the gate\n\nCommit `ce787f3` (May 27, 2026), authored by `tonghuaroot`, is the patch for the patch. It rewrites `_assertPath` to require its argument to be a string before the substring check runs:\n\n```diff\n-function _assertPath(path) {\n- if (path.includes(\"..\")) {\n+function _assertPath(option, value) {\n+ if (typeof value !== 'string') {\n+ throw new Error(`${option} option must be a string, got \"${typeof value}\".`);\n+ }\n+\n+ if (value.includes(\"..\")) {\n throw new Error(\"Relative value not allowed\");\n }\n\n- return path;\n+ return value;\n }\n```\n\nThe structural move matches the resolution prior gate-before-canonicalize exhibits chose: refuse the input shapes the two parsers disagree about, leave the rest of the pipeline alone. `tmp` 0.2.7 does not change `_generateTmpName`, does not change `_getRelativePath`, does not change `path.join`. It removes the polymorphism by narrowing the gate's argument to the one type the gate was written for. The check that was written assuming string semantics now runs only against strings.\n\nThe April 6 commit author is the project maintainer. The May 27 commit author is a separate researcher who reported the bypass, supplied the patch, and got the advisory credit. Seven weeks separate the gate's introduction from the gate's narrowing. The package's prior advisory was the one 0.2.6 was the response to; the advisory before that (GHSA-52f5-9888-hmc6) was a different path-handling primitive in the same neighborhood of `lib/tmp.js`, fixed in April 2025. Three GHSAs in the same component in fourteen months, each fix exposing the next. The design-debt driver is the inline name-builder array at the bottom of `_generateTmpName`: caller-supplied values flow into `path.join` through `Array.prototype.join('')` with no type discipline at the boundary, and every gate the maintainers have shipped is a perimeter check on the shape of the demonstrated bypass rather than a refactor of the builder.\n\nPoC: [advisories/GHSA-7c78-jf6q-g5cm](https://github.com/advisories/GHSA-7c78-jf6q-g5cm)","closing_line":"The patch narrowed the gate to the type it was written for. The two `.includes` methods that disagree about what `..` means are both still in the standard library.","hook_md":"The `tmp` package's `_assertPath` function does one thing. It calls `path.includes('..')` on the caller-supplied prefix, postfix, or template and throws if the substring is found. The check was added in `lib/tmp.js` on April 6, 2026 as the response to a prior path-traversal advisory; the author wrote it for strings, and JavaScript also exposes `.includes` on `Array.prototype`, where the method checks element equality rather than substring presence. CVE-2026-49982 is what the gate does when the caller's prefix is `['../escape']`.","post_id":638,"slug":"tmp-cve-2026-49982-includes-was-not-substring","title":"CVE-2026-49982: tmp's _assertPath Called .includes('..'). Array.prototype.includes Is Element Equality.","type":"initial","unreadable_sentence":"`Array.prototype.includes` is element equality. `String.prototype.includes` is substring search. They share a name."} -----BEGIN PGP SIGNATURE----- iHQEARYIAB0WIQRf0htP5+SjynlxywneZjl4jgkQJgUCaxIfCwAKCRDeZjl4jgkQ JlIpAP9nxeRuhBMmjXIrDf4dZeGn+3SqWxMr3P3oeXh1oYP2bAD3THO+aYUG5YT/ +iv9qQ0bFSrDmC66YdI28Iiu7t78Ag== =sJLu -----END PGP SIGNATURE-----