Skip to content

Commit d5a9c73

Browse files
authored
Remove rule G307 which checks when an error is not handled when a file or socket connection is closed (#935)
* Remove read only types from unsafe defer rules * Remove rule G307 which checks when an error is not handled when a file or socket connection is closed This doesn't seem to bring much value from security perspective, and it caused a lot of controversy since is a very common pattern in Go. * Mentioned in documentation that rule G307 is retired * Clean up the test for rule G307
1 parent 27bf0e4 commit d5a9c73

File tree

6 files changed

+1
-156
lines changed

6 files changed

+1
-156
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ directory you can supply `./...` as the input argument.
157157
- G304: File path provided as taint input
158158
- G305: File traversal when extracting zip/tar archive
159159
- G306: Poor file permissions used when writing to a new file
160-
- G307: Deferring a method which returns an error
161160
- G401: Detect the usage of DES, RC4, MD5 or SHA1
162161
- G402: Look for bad TLS connection settings
163162
- G403: Ensure minimum RSA key length of 2048 bits
@@ -172,6 +171,7 @@ directory you can supply `./...` as the input argument.
172171
### Retired rules
173172

174173
- G105: Audit the use of math/big.Int.Exp - [CVE is fixed](https://github.com/golang/go/issues/15184)
174+
- G307: Deferring a method which returns an error - causing more inconvenience than fixing a security issue, despite the details from this [blog post](https://www.joeshaw.org/dont-defer-close-on-writable-files/)
175175

176176
### Selecting rules
177177

issue/issue.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ var ruleToCWE = map[string]string{
7777
"G304": "22",
7878
"G305": "22",
7979
"G306": "276",
80-
"G307": "703",
8180
"G401": "326",
8281
"G402": "295",
8382
"G403": "310",

rules/bad_defer.go

Lines changed: 0 additions & 97 deletions
This file was deleted.

rules/rulelist.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ func Generate(trackSuppressions bool, filters ...RuleFilter) RuleList {
9191
{"G304", "File path provided as taint input", NewReadFile},
9292
{"G305", "File path traversal when extracting zip archive", NewArchive},
9393
{"G306", "Poor file permissions used when writing to a file", NewWritePerms},
94-
{"G307", "Unsafe defer call of a method returning an error", NewDeferredClosing},
9594

9695
// crypto
9796
{"G401", "Detect the usage of DES, RC4, MD5 or SHA1", NewUsesWeakCryptography},

rules/rules_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,6 @@ var _ = Describe("gosec rules", func() {
146146
runner("G306", testutils.SampleCodeG306)
147147
})
148148

149-
It("should detect unsafe defer of os.Close", func() {
150-
runner("G307", testutils.SampleCodeG307)
151-
})
152-
153149
It("should detect weak crypto algorithms", func() {
154150
runner("G401", testutils.SampleCodeG401)
155151
})

testutils/source.go

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2777,58 +2777,6 @@ func main() {
27772777
27782778
}`}, 1, gosec.NewConfig()},
27792779
}
2780-
// SampleCodeG307 - Unsafe defer of os.Close
2781-
SampleCodeG307 = []CodeSample{
2782-
{[]string{`package main
2783-
import (
2784-
"bufio"
2785-
"fmt"
2786-
"io/ioutil"
2787-
"os"
2788-
)
2789-
func check(e error) {
2790-
if e != nil {
2791-
panic(e)
2792-
}
2793-
}
2794-
func main() {
2795-
d1 := []byte("hello\ngo\n")
2796-
err := ioutil.WriteFile("/tmp/dat1", d1, 0744)
2797-
check(err)
2798-
allowed := ioutil.WriteFile("/tmp/dat1", d1, 0600)
2799-
check(allowed)
2800-
f, err := os.Create("/tmp/dat2")
2801-
check(err)
2802-
defer f.Close()
2803-
d2 := []byte{115, 111, 109, 101, 10}
2804-
n2, err := f.Write(d2)
2805-
defer check(err)
2806-
fmt.Printf("wrote %d bytes\n", n2)
2807-
n3, err := f.WriteString("writes\n")
2808-
fmt.Printf("wrote %d bytes\n", n3)
2809-
f.Sync()
2810-
w := bufio.NewWriter(f)
2811-
n4, err := w.WriteString("buffered\n")
2812-
fmt.Printf("wrote %d bytes\n", n4)
2813-
w.Flush()
2814-
}`}, 1, gosec.NewConfig()}, {[]string{`
2815-
package main
2816-
2817-
import (
2818-
"net"
2819-
"net/http"
2820-
)
2821-
2822-
func main() {
2823-
response, _ := http.Get("https://127.0.0.1")
2824-
2825-
defer response.Body.Close() // io.ReadCloser
2826-
2827-
conn, _ := net.Dial("tcp", "127.0.0.1:8080")
2828-
defer conn.Close() // net.Conn
2829-
2830-
}`}, 2, gosec.NewConfig()},
2831-
}
28322780

28332781
// SampleCodeG401 - Use of weak crypto MD5
28342782
SampleCodeG401 = []CodeSample{

0 commit comments

Comments
 (0)