Skip to content
This repository was archived by the owner on Feb 27, 2026. It is now read-only.

Commit 905c174

Browse files
committed
Add the G304 rule example
Signed-off-by: Aisuko <urakiny@gmail.com>
1 parent c2bad26 commit 905c174

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
id: g304
3+
title: G304: File path provided as taint input
4+
---
5+
6+
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 form the system. In this way, it is possible to exfiltrate confidential information or such.
7+
8+
## Example problematic code:
9+
10+
```
11+
package main
12+
13+
import (
14+
"fmt"
15+
"io/ioutil"
16+
"strings"
17+
)
18+
19+
func main() {
20+
repoFile := "path_of_file"
21+
byContext, err := ioutil.ReadFile(repoFile)
22+
if err != nil {
23+
panic(err)
24+
}
25+
fmt.Printf("%s", string(byContext))
26+
}
27+
```
28+
29+
## Gosec command line output
30+
31+
```
32+
[examples/main.go:11] - G304 (CWE-22): Potential file inclusion via variable (Confidence: HIGH, Severity: MEDIUM)
33+
> ioutil.ReadFile(repoFile)
34+
```
35+
36+
## The right way
37+
38+
```
39+
package main
40+
41+
import (
42+
"fmt"
43+
"io/ioutil"
44+
"path/filepath"
45+
"strings"
46+
)
47+
48+
func main() {
49+
repoFile := "path_of_file"
50+
byContext, err := ioutil.ReadFile(filepath.Clean(repoFile))
51+
if err != nil {
52+
panic(err)
53+
}
54+
fmt.Printf("%s", string(byContext))
55+
}
56+
```
57+
58+
## See also
59+
60+
* https://pkg.go.dev/path/filepath?tab=doc#Clean

website/sidebars.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"rules/g103",
88
"rules/g104",
99
"rules/g107",
10-
"rules/g201-g202"
10+
"rules/g201-g202",
11+
"rules/g304"
1112
]
1213
}
1314
}

0 commit comments

Comments
 (0)