8000 g304: use a more convincing example · securego/securego.github.io@5eba943 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5eba943

Browse files
g304: use a more convincing example
With this change, the tweaked example shows how an attacker can make the code read from an unsafe path by adding `..` to their path. Signed-off-by: Dan Kenigsberg <danken@redhat.com>
1 parent b4b40a2 commit 5eba943

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

docs/rules/g304_file-path_provided_as_taint_input.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: G304: File path provided as taint input
66
Trying to open a file provided as an input in a variable. The content of this variable might be controlled by an attacker who could change it to hold unauthorised file paths from the system. In this way, it is possible to exfiltrate confidential information or such.
77

88
## Example problematic code:
9-
9+
This code lets an attacker read a `/private/path`
1010
```
1111
package main
1212
@@ -17,7 +17,10 @@ import (
1717
)
1818
1919
func main() {
20-
repoFile := "path_of_file"
20+
repoFile := "/safe/path/../../private/path"
21+
if !strings.HasPrefix(repoFile, "/safe/path/") {
22+
panic(fmt.Errorf("Unsafe input"))
23+
}
2124
byContext, err := ioutil.ReadFile(repoFile)
2225
if err != nil {
2326
panic(err)
@@ -34,7 +37,7 @@ func main() {
3437
```
3538

3639
## The right way
37-
40+
This code panics if `/safe/path` was removed by an attacker
3841
```
3942
package main
4043
@@ -46,15 +49,18 @@ import (
4649
)
4750
4851
func main() {
49-
repoFile := "path_of_file"
50-
byContext, err := ioutil.ReadFile(filepath.Clean(repoFile))
52+
repoFile := "/safe/path/../../private/path"
53+
repoFile = filepath.Clean(repoFile)
54+
if !strings.HasPrefix(repoFile, "/safe/path/") {
55+
panic(fmt.Errorf("Unsafe input"))
56+
}
57+
byContext, err := ioutil.ReadFile(repoFile)
5158
if err != nil {
5259
panic(err)
5360
}
54-
fmt.Printf("%s", string(byContext))
55-
}
61+
fmt.Printf("%s", string(byContext))}
5662
```
5763

5864
## See also
5965

60-
* https://pkg.go.dev/path/filepath?tab=doc#Clean
66+
* https://pkg.go.dev/path/filepath?tab=doc#Clean

0 commit comments

Comments
 (0)
0