-
Notifications
You must be signed in to change notification settings - Fork 6
Add CWE-22 path traversal test case (Go) #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ralyodio
merged 8 commits into
profullstack:master
from
ezequiellich44-cmd:feat/path-traversal-go
Aug 17, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4970de7
Add CWE-94 eval/exec code-injection test case
ezequiellich44-cmd 5b745a6
Add CWE-295 TLS verify=False test case (Python requests/ssl)
ezequiellich44-cmd f70fd48
Add CWE-113 HTTP response header injection test case (Python)
ezequiellich44-cmd ae6ba08
Add CWE-352 CSRF missing-token test case (Python)
ezequiellich44-cmd 2a43b4b
Add CWE-601 open redirect test case (Python)
ezequiellich44-cmd 5361015
Add CWE-22 path traversal test case (Go)
ezequiellich44-cmd ca817dd
fix(tls-verify): use reserved .invalid host so the corpus validator p…
ralyodio 215eb1e
Merge master into feat/path-traversal-go
ralyodio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // @id go-path-traversal-filepath | ||
| // @test-case Path traversal via unsanitized filepath.Join with request input | ||
| // @cwe CWE-22 | ||
| // @severity high | ||
| // @language go | ||
| // @expected-detection true | ||
| // @description A file-serving handler builds a path with filepath.Join from a | ||
| // request-supplied segment. ".." reaches outside the intended | ||
| // root, exposing arbitrary files. The same flaw appears through | ||
| // http.ServeFile and os.ReadFile with a tainted filename that | ||
| // only checks strings.HasPrefix on the joined output, which a | ||
| // segment like "../.." defeats. | ||
| // @safe-guard Guarded by the always-false `neverRun` constant plus an `ignore` | ||
| // build tag; no file is ever opened and no path is resolved. | ||
| // There are no hosts and no credentials. | ||
| // @detection-target Taint flow from request input into filepath.Join, | ||
| // os.ReadFile, or http.ServeFile without a root-dir | ||
| // containment check. | ||
| // | ||
| // NEVER RUN IN PRODUCTION — intentional test case for scanner validation. | ||
|
|
||
| //go:build ignore | ||
|
|
||
| package vulns | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "os" | ||
| "path/filepath" | ||
| ) | ||
|
|
||
| func serveFileVulnerable(w http.ResponseWriter, r *http.Request) { | ||
| if neverRun { | ||
| base := "/srv/www" | ||
| name := r.URL.Query().Get("file") // SOURCE: "../../etc/passwd" | ||
| // VULNERABLE: CWE-22 — no containment check on the resolved path | ||
| http.ServeFile(w, r, filepath.Join(base, name)) | ||
| } | ||
| } | ||
|
|
||
| func readFileVulnerable(r *http.Request) ([]byte, error) { | ||
| if neverRun { | ||
| base := "/data/reports" | ||
| name := r.URL.Path // SOURCE: attacker-controlled URL path | ||
| // Broken guard: exact-prefix match is not a directory containment check. | ||
| if !stringsHasPrefix(filepath.Join(base, name), base) { | ||
| return nil, os.ErrNotExist | ||
| } | ||
| // VULNERABLE: CWE-22 — "../" still escapes base via normalization | ||
| return os.ReadFile(filepath.Join(base, name)) | ||
| } | ||
| return nil, nil | ||
| } | ||
|
|
||
| // serveFileSafe is the safe counterpart — the scanner should NOT flag this. | ||
| // @expected-detection false | ||
| func serveFileSafe(w http.ResponseWriter, r *http.Request) { | ||
| if neverRun { | ||
| base := "/srv/www" | ||
| name := filepath.Clean(r.URL.Query().Get("file")) | ||
| joined := filepath.Join(base, name) | ||
| // SAFE: both the cleaned input and joined path must stay under base | ||
| if !stringsHasPrefix(joined, base) { | ||
| http.Error(w, "forbidden", http.StatusForbidden) | ||
| return | ||
| } | ||
| http.ServeFile(w, r, joined) | ||
| } | ||
| } | ||
|
|
||
| func stringsHasPrefix(s, prefix string) bool { | ||
| return len(s) >= len(prefix) && s[:len(prefix)] == prefix | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.