8000 Add documentation about rule G104 (#5) · securego/securego.github.io@f027517 · GitHub
[go: up one dir, main page]

Skip to content

Commit f027517

Browse files
MVrachevgcmurphy
authored andcommitted
Add documentation about rule G104 (#5)
I decided to add two examples because I wanted to show a little more complicated example. Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
1 parent 6e73335 commit f027517

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

docs/rules/g104_unchecked_erros.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
id: g104
3+
title: G104: Audit errors not checked
4+
---
5+
6+
# G104: Audit errors not checked
7+
8+
Really useful feature of Golang is the ability to return a tuple of a result and an error value from a function. There is an unspoken rule in Golang that the result of a function is unsafe until you make check the error value. Many security exploits can be performed when the error value is not checked.
9+
10+
## Example code:
11+
12+
```
13+
package main
14+
import "fmt"
15+
func test() (int,error) {
16+
return 0, nil
17+
}
18+
func main() {
19+
v, _ := test()
20+
fmt.Println(v)
21+
}
22+
```
23+
24+
other example:
25+
26+
```
27+
package main
28+
29+
import (
30+
"fmt"
31+
"io/ioutil"
32+
"os"
33+
)
34+
35+
func a() error {
36+
return fmt.Errorf("This is an error")
37+
}
38+
39+
func b() {
40+
fmt.Println("b")
41+
ioutil.WriteFile("foo.txt", []byte("bar"), os.ModeExclusive)
42+
}
43+
44+
func c() string {
45+
return fmt.Sprintf("This isn't anything")
46+
}
47+
48+
func main() {
49+
_ = a()
50+
a()
51+
b()
52+
c()
53+
}
54+
```
55+
56+
## Gosec command line output
57+
58+
The Gosec output from the first example:
59+
60+
```
61+
[examples/main.go:9] - G104: Errors unhandled. (Confidence: HIGH, Severity: LOW)
62+
> v, _ := test()
63+
```
64+
65+
The output from the second example:
66+
67+
```
68+
[examples/main.go:14] - G104: Errors unhandled. (Confidence: HIGH, Severity: LOW)
69+
> ioutil.WriteFile("foo.txt", []byte("bar"), os.ModeExclusive)
70+
71+
[examples/main.go:20] - G104: Errors unhandled. (Confidence: HIGH, Severity: LOW)
72+
> _ = a()
73+
74+
[examples/main.go:21] - G104: Errors unhandled. (Confidence: HIGH, Severity: LOW)
75+
> a()
76+
```
77+
78+
## See also:
79+
80+
* https://blog.golang.org/error-handling-and-go
81+
* https://blog.golang.org/errors-are-values

0 commit comments

Comments
 (0)
0