Go: DotDotReplaceAll ignores the replacement value and causes false-negative path-injection alerts
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Accessibilité débutants
- 68/100
Piste de recherche
Commencez par DotDotReplaceAll dans go/ql/lib/semmle/go/security/TaintedPathCustomizations.qll et par le modèle ReplaceAll dans StringOps.qll, puis exécutez codeql test run go/ql/test/query-tests/Security/CWE-022/TaintedPath.qlref. Vérifiez les cas concernés dans TaintedPath.go et mettez à jour le modèle ou les attentes afin que les résultats non sûrs de ReplaceAll produisent l’alerte go/path-injection attendue sans supprimer les sanitizers valides.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Description of the issue
The Go go/path-injection query treats expressions modeled by
StringOps::ReplaceAll as sanitized when the replaced string is "." or
"..". The sanitizer does not check the replacement string or establish that
the resulting path is relative or contained within a trusted directory.
This causes false negatives for at least two independent reasons:
- The replacement can make the path more dangerous, because the model checks
only the string being replaced and not the replacement value. - Even replacing
".."with the empty string can leave or create an
attacker-controlled absolute path.
Affected sanitizer
DotDotReplaceAll in TaintedPathCustomizations.qll:
/**
* A replacement of the form `!strings.ReplaceAll(nd, "..")` or
* `!strings.ReplaceAll(nd, ".")`, considered as a sanitizer for path traversal.
*/
class DotDotReplaceAll extends StringOps::ReplaceAll, Sanitizer {
DotDotReplaceAll() { this.getReplacedString() = ["..", "."] }
}
StringOps::ReplaceAll.getReplacedString() represents the old argument. For
strings.ReplaceAll(s, old, new), the model exposes and checks old, but not
new:
class ReplaceAll extends DataFlow::Node instanceof ReplaceAll::Range {
/** Gets the `old` in `strings.ReplaceAll(s, old, new)`. */
string getReplacedString() { result = super.getReplacedString() }
}
The standard-library implementation of this range
similarly obtains only argument 1 (old):
override string getReplacedString() { result = this.getArgument(1).getStringValue() }
Consequently, any replacement value is accepted by DotDotReplaceAll.
False negative when the replacement increases traversal
For example:
func handler(w http.ResponseWriter, r *http.Request) {
taintedPath := r.URL.Query().Get("path")
path := strings.ReplaceAll(taintedPath, "..", "../..")
data, _ := os.ReadFile(path) // expected: go/path-injection
w.Write(data)
}
For taintedPath = "../secret", the replacement produces
"../../secret". It increases the number of parent-directory components, but
CodeQL treats the return value as sanitized because the old argument is
"..".
I also verified this directly in the existing TaintedPath.go test by changing
the replacement at line 37 to "../.." and adding an expected
go/path-injection alert. With DotDotReplaceAll enabled, the test reports:
| TaintedPath.go:37:77:37:105 | comment | Missing result: Alert[go/path-injection] |
False negative with an empty replacement
The existing test uses an empty replacement:
// GOOD: Sanitized by strings.ReplaceAll and replaces all .. with empty string
data, _ = ioutil.ReadFile(strings.ReplaceAll(tainted_path, "..", ""))
w.Write(data)
Removing ".." does not ensure that the path is safe:
strings.ReplaceAll("..../etc/passwd", "..", "") // "/etc/passwd"
strings.ReplaceAll("/etc/passwd", "..", "") // "/etc/passwd"
In both cases the result is an attacker-controlled absolute path. This is
consistent with the query help, which says that absolute paths can point
anywhere on the file system and similarly warns that naive removal of traversal
sequences can be insufficient:
TaintedPath.qhelp: absolute paths can point anywhere on the file systemTaintedPath.qhelp: warning about naive traversal-sequence removal
Steps to reproduce using the existing test
-
Run the existing test:
codeql test run go/ql/test/query-tests/Security/CWE-022/TaintedPath.qlrefThe test passes and no alert is reported for the
ReplaceAllresult at
TaintedPath.goline 37. -
Remove only the
DotDotReplaceAllclass from
go/ql/lib/semmle/go/security/TaintedPathCustomizations.qlland rerun the
same test. -
The test fails with the new source-to-sink result:
| TaintedPath.go:37:28:37:69 | call to ReplaceAll | ... | user-provided value | | TaintedPath.go:37:28:37:69 | call to ReplaceAll | Unexpected result: Alert |
The individual ablation changes no other alert locations in this test. This
confirms that DotDotReplaceAll suppresses the flow from the URL-derived path
through strings.ReplaceAll to ioutil.ReadFile.
Expected behavior
Replacing "." or ".." should not be treated as an unconditional
path-injection sanitizer. In particular, a replacement operation must not
stop taint without considering the replacement value and the properties of the
resulting path.
Since even an empty replacement does not prove that the result is relative or
contained within a safe directory, a possible fix is to remove
DotDotReplaceAll and update the affected test expectation. Sound validation
can instead rely on checks that establish locality or containment.
Environment
- CodeQL CLI 2.25.6
- CodeQL repository test baseline:
f6f45d1536 - Present in
mainat commit42843f155e95d25e690aba9ae4620b5a5986a951 - Go 1.22.12 on Linux/amd64
- Langage dominant
- CodeQL
- Étoiles
- 10.1k
- Forks
- 2.1k
- Merge moyen
- 2 j 11 h
- PR mergées (30 j)
- 129
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de github/codeql
-
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
-
C#: cs/simplifiable-boolean-expression false positive on Nullable<bool> compared with a literal Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
-
false-positive
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
-
False positive Ouvertefalse-positive
Difficulté 4/5 3-5 jours Accessibilité débutants 15/100
Toutes les issues de github/codeql
Issues similaires
-
스택 PR 머지 시 하위 PR base 재지정 단계 부재 Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
idean3885/claude-ops-agent#521 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
0xMiden/bridge-portal#132 ·
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
newrelic-experimental/preflight#793 · 1 commentaire ·
-
enhancement
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
babalae/bettergi-scripts-list#3674 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
caddyserver/caddy#8046 ·