Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive on G304 (CWE-22) after filepath.EvalSymlinks #1127

Closed
roelvandergoot opened this issue Apr 19, 2024 · 0 comments · Fixed by #1137
Closed

False positive on G304 (CWE-22) after filepath.EvalSymlinks #1127

roelvandergoot opened this issue Apr 19, 2024 · 0 comments · Fixed by #1137

Comments

@roelvandergoot
Copy link

Summary

According to the function documentation:
// EvalSymlinks returns the path name after the evaluation of any symbolic
// links.
// If path is relative the result will be relative to the current directory,
// unless one of the components is an absolute symbolic link.
// EvalSymlinks calls [Clean] on the result.

Steps to reproduce the behavior

// Parse creates a configuration from a configuration file with name fileName.
func (c *Config) parse(fileName string) error {
    info, err := os.Lstat(fileName)
    if os.IsNotExist(err) {
        return err
    }
    mode := info.Mode()
    var filePath string
    if mode&os.ModeSymlink != 0 {
        var err error
        if filePath, err = filepath.EvalSymlinks(fileName); err != nil {
            return err
        }
        // Somehow gosec doesn't know that filepath.EvalSymlinks calls
        // filepath.Clean.
        filePath = filepath.Clean(filePath)
    } else {
        filePath = filepath.Clean(fileName)
    }

    file, err := os.ReadFile(filePath)
    if err != nil {
        return err
    }

    // ...
}

gosec version

go install github.com/securego/gosec/v2/cmd/gosec@latest

or

> gosec -version
Version: dev
Git tag:
Build date:

Go version (output of 'go version')

go version go1.22.2 linux/amd64

Operating system / Environment

Ubuntu 22.04

Expected behavior

I was hoping not to have to include the following three lines in my code example:

        // Somehow gosec doesn't know that filepath.EvalSymlinks calls
        // filepath.Clean.
        filePath = filepath.Clean(filePath)

Actual behavior

Without those lines gosec complains:

[/home/roel/git/rp/pkg/config/config.go:70] - G304 (CWE-22): Potential file inclusion via variable (Confidence: HIGH, Severity: MEDIUM)
    69:
  > 70:         file, err := os.ReadFile(filePath)
    71:         if err != nil {

@roelvandergoot roelvandergoot changed the title Gosec False positive on G304 (CWE-22) after filepath.EvalSymlinks False positive on G304 (CWE-22) after filepath.EvalSymlinks Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant