gosec: G307: gosec starts detecting G307 (CWE-703) even with proposed way to safely handle errors
Summary
gosec v2.9.1 starts detecting G307 (CWE-703): Deferring unsafe method "Close" on type "*os.File" (Confidence: HIGH, Severity: MEDIUM)
which could have been avoided by following https://github.com/securego/gosec/issues/512 with previous version of gosec.
Steps to reproduce the behavior
package main
import (
"log"
"os"
)
func main() {
f, err := os.Open("./testfile.txt")
if err != nil {
log.Fatal(err)
return
}
defer func() {
if err := f.Close(); err != nil {
log.Fatal("failed to close file")
}
}()
log.Println("success")
return
}
(~/work/achiku/gosec-issue)
❯❯❯ ll
total 8
-rw-r--r-- 1 chiku staff 268 10 18 11:32 main.go
-rw-r--r-- 1 chiku staff 0 10 18 11:31 testfile.txt
(~/work/achiku/gosec-issue)
❯❯❯ go run main.go
2021/10/18 11:32:22 success
(~/work/achiku/gosec-issue)
❯❯❯ gosec .
[gosec] 2021/10/18 11:32:27 Including rules: default
[gosec] 2021/10/18 11:32:27 Excluding rules: default
[gosec] 2021/10/18 11:32:27 Import directory: /Users/chiku/work/achiku/gosec-issue
[gosec] 2021/10/18 11:32:28 Checking package: main
[gosec] 2021/10/18 11:32:28 Checking file: /Users/chiku/work/achiku/gosec-issue/main.go
Results:
[/Users/chiku/work/achiku/gosec-issue/main.go:14-18] - G307 (CWE-703): Deferring unsafe method "Close" on type "*os.File" (Confidence: HIGH, Severity: MEDIUM)
13: }
> 14: defer func() {
> 15: if err := f.Close(); err != nil {
> 16: log.Fatal("failed to close file")
> 17: }
> 18: }()
19: log.Println("success")
Summary:
Gosec : 2.9.1
Files : 1
Lines : 21
Nosec : 0
Issues : 1
with v2.8.1
(~/work/achiku/gosec-issue)
❯❯❯ curl -sfL https://raw.githubusercontent.com/securego/gosec/master/install.sh | sh -s -- -b $GOPATH/bin v2.8.1
securego/gosec info checking GitHub for tag 'v2.8.1'
securego/gosec info found version: 2.8.1 for v2.8.1/darwin/amd64
securego/gosec info installed /Users/chiku/sdk/go1.16.7/bin/gosec
(~/work/achiku/gosec-issue)
❯❯❯ gosec .
[gosec] 2021/10/18 11:32:43 Including rules: default
[gosec] 2021/10/18 11:32:43 Excluding rules: default
[gosec] 2021/10/18 11:32:43 Import directory: /Users/chiku/work/achiku/gosec-issue
[gosec] 2021/10/18 11:32:44 Checking package: main
[gosec] 2021/10/18 11:32:44 Checking file: /Users/chiku/work/achiku/gosec-issue/main.go
Results:
Summary:
Gosec : 2.8.1
Files : 1
Lines : 21
Nosec : 0
Issues : 0
gosec version
Go version (output of ‘go version’)
❯❯❯ go version
go version go1.16.7 darwin/amd64
Operating system / Environment
❯❯❯ uname -a
Darwin FVFZR19DLYWP 20.5.0 Darwin Kernel Version 20.5.0: Sat May 8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64 x86_64
Expected behavior
No errors, or solve this error.
Actual behavior
gosec detects G307 (CWE-703).
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 12
- Comments: 20 (7 by maintainers)
Commits related to this issue
- Ignore warning of the latest gosec tool This looks like a possible false positives, at least until we learn more at https://github.com/securego/gosec/issues/714 Addressing: [/github/workspace/exampl... — committed to isimluk/gofalcon by isimluk 3 years ago
- Ignore warning of the latest gosec tool This looks like a possible false positives, at least until we learn more at https://github.com/securego/gosec/issues/714 Addressing: [/github/workspace/exampl... — committed to isimluk/gofalcon by isimluk 3 years ago
- Ignore warning of the latest gosec tool This looks like a possible false positives, at least until we learn more at https://github.com/securego/gosec/issues/714 Addressing: [/github/workspace/exampl... — committed to isimluk/gofalcon by isimluk 3 years ago
- Ignore warning of the latest gosec tool This looks like a possible false positives, at least until we learn more at https://github.com/securego/gosec/issues/714 Addressing: [/github/workspace/exampl... — committed to isimluk/gofalcon by isimluk 3 years ago
- common: Ignore gosec bug See https://github.com/securego/gosec/issues/714. — committed to croissanne/image-builder by croissanne 3 years ago
- common: Ignore gosec bug See https://github.com/securego/gosec/issues/714. — committed to croissanne/image-builder by croissanne 3 years ago
- common: Ignore gosec bug See https://github.com/securego/gosec/issues/714. — committed to croissanne/image-builder by croissanne 3 years ago
- common: ignore gosec bug See securego/gosec#714 — committed to kingsleyzissou/image-builder by kingsleyzissou 3 years ago
- common: Ignore gosec bug See https://github.com/securego/gosec/issues/714. — committed to croissanne/image-builder by croissanne 3 years ago
- common: ignore gosec bug See securego/gosec#714 — committed to kingsleyzissou/image-builder by kingsleyzissou 3 years ago
- common: ignore gosec bug See securego/gosec#714 — committed to osbuild/image-builder by kingsleyzissou 3 years ago
- common: Ignore gosec bug See https://github.com/securego/gosec/issues/714. — committed to croissanne/image-builder by croissanne 3 years ago
- .builds: ignore buggy gosec warning Currently gosec is throwing warnings for any defer statement that closes a file (even if the error is handled) due to a bug. Once securego/gosec#714 is fixed and a... — committed to mellium/xmpp by SamWhited 3 years ago
- .builds: ignore buggy gosec warning Currently gosec is throwing warnings for any defer statement that closes a file (even if the error is handled) due to a bug. Once securego/gosec#714 is fixed and a... — committed to mellium/xmpp by SamWhited 3 years ago
- Use gosec v2.8.1 ref https://github.com/securego/gosec/issues/714 — committed to hirose31/s3surfer by hirose31 3 years ago
- Ignore gosec G307 ref https://github.com/securego/gosec/issues/714 — committed to hirose31/s3surfer by hirose31 3 years ago
- Downgrade gosec due to https://github.com/securego/gosec/issues/714 — committed to Jacalz/rymdport by Jacalz 3 years ago
- Fix https://github.com/securego/gosec/issues/714 — committed to elgohr/gosec by elgohr 3 years ago
- Make sure to handle error returned by io.Closer.Close() in 'defer' statements This is considered unsafe by gosec otherwise. [1] https://github.com/securego/gosec/issues/512 [2] https://github.com/se... — committed to redhat-developer/odo by rm3l a year ago
- Go: Bump github.com/securego/gosec/v2 from 2.14.0 to 2.15.0 (#6686) * Go: Bump github.com/securego/gosec/v2 from 2.14.0 to 2.15.0 Bumps [github.com/securego/gosec/v2](https://github.com/securego/gos... — committed to redhat-developer/odo by dependabot[bot] a year ago
@danieljmt new version released https://github.com/securego/gosec/releases/tag/v2.9.3
@lootek It is stated in the docs. In fact the docs were specifically updated because of this pattern. See: https://github.com/golang/go/issues/32427
This is how I typically open and close files. It gets flagged even though I thought it was idiomatic. However I am new to Go, so I’m open to being told this is poor coding style.
The
defer f.Close()
is used to close the file even if there is a panic before directly callingf.Close()
. It is apparently safe to call Close() twice. If the code is already panicking then the close error is ignored.More details:
Example source:
Result:
@elgohr I merged your pull request.
You can reopen it if you want to rework it.
To my point of view a solution would be to revert the change and start again with the initial PR (including the failing false-positive-test)
I guess I already mentioned this https://github.com/securego/gosec/pull/661#issuecomment-889913968
This is not necessarily a false positive: https://www.joeshaw.org/dont-defer-close-on-writable-files/
Though
gosec
does not accept even the named parameters workaround so this issue here should be addressed anywayWe hit the same issue, we use the Github Action with the
@master
version. And I belive we can ignore this check first by add/* #nosec G307 */
(only in the situation below, you already check the error return by file close function)The error
I am seeing this as well. I would expect a defer func() to pass gosec without issue, because I am checking the error. This was working before, not sure what version.