JS: Some sanitizer guards no longer apply after `if (!check(x)) x = default` (false positives since 2.20.2)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 48/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- javascript
- 領域
- security, testing-qa
調査の方向性
Start by reading the referenced JavaScript data-flow code in BarrierGuards.qll and sharedlib/Ssa.qll, then compare it with the older barrierGuardBlocksEdge implementation in Configuration.qll and the examples in the issue. Reproduce the reported behavior on the sample guards and inspect existing tests for barrier guards on phi inputs. Done means the default-assignment cases are no longer reported while the early-return and unguarded control cases retain their stated behavior.
索引モデルが issue の本文から書いたものです。
説明
Description of the false positive
Since CodeQL 2.20.2, a sanitizer guard no longer protects a variable when the branch where the check fails assigns a safe default instead of returning, as in if (!["en", "de"].includes(lang)) { lang = "en"; }. After the if, lang is either the default or a value that passed the check, so it is safe, but js/reflected-xss reports the use. The same check followed by an early return is not reported. CodeQL 2.20.1 and earlier did not report it; 2.20.2 and every later release I tried, up to the latest 2.27.2, do. The pattern is common for user-selected settings with a fallback (language, theme, sort key, page size).
It is not specific to one guard or query: a /^\w+$/.test(name) guard behaves the same way, and so does js/resource-exhaustion with an upper-bound check that clamps instead of returning (if (n > 1024) n = 1024;). There n is at most 1024 afterwards, or NaN or negative, which Buffer.alloc rejects with ERR_OUT_OF_RANGE. Assigning the default in an explicit else branch of if (["en", "de"].includes(lang)) is reported as well. Comparisons with constants are not affected: if (lang !== "en" && lang !== "de") lang = "en"; is still not reported, presumably because such conditions get SSA refinement nodes.
Code samples or links to source code
app.js:
const express = require("express");
const app = express();
app.get("/a", (req, res) => {
let lang = req.query.lang;
if (!["en", "de"].includes(lang)) {
lang = "en";
}
res.send("<p>" + lang + "</p>"); // reported since the shared data-flow library; lang is "en" or "de" here
});
app.get("/b", (req, res) => {
let name = req.query.name;
if (!/^\w+$/.test(name)) {
name = "guest";
}
res.send("<p>Hello " + name + "</p>"); // reported likewise; name matches /^\w+$/ or is "guest"
});
app.get("/c", (req, res) => {
const lang = req.query.lang;
if (!["en", "de"].includes(lang)) {
return res.send("unsupported");
}
res.send("<p>" + lang + "</p>"); // not reported (same guard, early return)
});
app.get("/d", (req, res) => {
res.send("<p>" + req.query.lang + "</p>"); // reported (control)
});
app.listen(3000);
size.js:
const http = require("http");
const url = require("url");
http.createServer((req, res) => {
let n = parseInt(url.parse(req.url, true).query.n);
if (n > 1024) {
n = 1024;
}
Buffer.alloc(n); // js/resource-exhaustion: reported since the shared data-flow library; n <= 1024 or NaN here
const m = parseInt(url.parse(req.url, true).query.m);
if (m > 1024) {
return res.end("too large");
}
Buffer.alloc(m); // not reported (same guard, early return)
res.end("ok");
}).listen(8080);
| CodeQL | js/reflected-xss on app.js |
js/resource-exhaustion on size.js |
|---|---|---|
| 2.16.2, 2.20.1 | line 29 | none |
| 2.20.2, 2.27.2 | lines 9, 17, 29 | line 9 |
The pattern occurs in real code. Two examples, each scanned as a whole repository with both versions, where 2.27.2 reports alerts that 2.20.1 does not:
- HCDP/hcdp_api (API of the Hawaii Climate Data Portal):
if(!mesonetLocations.includes(location)) { location = "hawaii"; }, wheremesonetLocationsis["hawaii", "american_samoa"](config.ts), and thenpath.join(rawDataRoot, "sff",${location}_sff.csv).js/path-injectionreportsraw.ts:148andraw.ts:161, and the same check at L193-L195 leads to a third alert atutil.ts:46. - flukexp/PiperTTS-API-Wrapper:
if (!voices.includes(voice)) { voice = DEFAULT_VOICE; }, wherevoicesare the model file names in a local directory, beforevoiceis used in anexeccommand line (L86-L95).js/command-line-injectionreports L95. The POST handler in the same file performs the same check as a conditional expression (L134) and is not reported.
2.20.2 is the release in which JavaScript moved to the shared data-flow library (codeql/javascript-all 2.3.0, whose change note says users may observe differences in the alerts).
As far as I can tell, the old library handled this case in barrierGuardBlocksEdge, which blocked the flow into a phi node from an input whose use is controlled by the guard. The note in the current BarrierGuards.qll says that this predicate was not carried over because "dataflow2 does not support barrier edges". Barrier guards on phi inputs are handled for other languages (for example #16677 for C++), but the JavaScript integration of the shared SSA library in sharedlib/Ssa.qll is only used for use-use flow after a post-update. For comparison, Java and Ruby handle the same pattern with 2.27.2: java/ssrf does not report if (!ALLOWED.contains(url)) { url = "https://a.example/"; } followed by new URL(url).openConnection(), and rb/command-line-injection does not report lang = "en" unless ["en", "de"].include?(lang) followed by system("echo " + lang).
Is this a known consequence of not carrying over barrierGuardBlocksEdge? If so, is there a plan to support guards on phi inputs for JavaScript, as in the other languages?
- 主要言語
- CodeQL
- スター
- 10.2k
- フォーク
- 2.1k
- 平均マージ
- 2日 18時間
- マージ済み PR(30日)
- 142
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
github/codeql のほかの issue
-
false-positive javascript
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
github/codeql#22632 · コメント 3 件 · リアクション 1 件 · 担当者 2 名 ·
メンテナーはふだん 1 日以内に返信
-
Add AlertSuppression.ql for Rust (inline // codeql[...] suppression)対応中かも @cnuss が 202 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
github/codeql#21637 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
false-positive
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
github/codeql#21076 · コメント 3 件 · リアクション 3 件 ·
メンテナーはふだん 1 日以内に返信
-
question
難易度 4/5 3〜5日 初心者へのやさしさ 52/100
メンテナーはふだん 1 日以内に返信
-
question
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
メンテナーはふだん 1 日以内に返信
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 69/100
carbon-design-system/carbon-ai-chat#2615 ·
メンテナーはふだん 1 日以内に返信
-
area/auth
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
Apicurio/apicurio-registry#10654 ·
メンテナーはふだん 1 日以内に返信
-
fix(session-flow): redact.py misses bare sk-ant-oat01- tokens and stateless ghs_ tokens with dotsオープンneeds-human
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
melodic-software/claude-code-plugins#9026 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug screener-policy
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
ditto-assistant/ditto-subnet#3067 ·
メンテナーはふだん 1 日以内に返信
-
[Bug]: redact_sensitive_text misses prefix tokens followed by an underscore (shared lookahead class)オープンarea/auth comp/agent P3 type/security
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
NousResearch/hermes-agent#142852 · コメント 3 件 ·