Skip to content

Commit

Permalink
fix: add more rules for G204 (securego#677)
Browse files Browse the repository at this point in the history
* fix: add more rules for G204

* fix: add extra test and comment
  • Loading branch information
nanikjava authored Aug 16, 2021
1 parent 9f30bb6 commit 5a131be
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
28 changes: 26 additions & 2 deletions rules/subproc.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,32 @@ func (r *subprocess) Match(n ast.Node, c *gosec.Context) (*gosec.Issue, error) {
for _, arg := range args {
if ident, ok := arg.(*ast.Ident); ok {
obj := c.Info.ObjectOf(ident)
if _, ok := obj.(*types.Var); ok && !gosec.TryResolve(ident, c) {
return gosec.NewIssue(c, n, r.ID(), "Subprocess launched with variable", gosec.Medium, gosec.High), nil

// need to cast and check whether it is for a variable ?
_, variable := obj.(*types.Var)

// .. indeed it is a variable then processing is different than a normal
// field assignment
if variable {
switch ident.Obj.Decl.(type) {
case *ast.AssignStmt:
_, assignment := ident.Obj.Decl.(*ast.AssignStmt)
if variable && assignment {
if !gosec.TryResolve(ident, c) {
return gosec.NewIssue(c, n, r.ID(), "Subprocess launched with variable", gosec.Medium, gosec.High), nil
}
}
case *ast.Field:
_, field := ident.Obj.Decl.(*ast.Field)
if variable && field {
// check if the variable exist in the scope
vv, vvok := obj.(*types.Var)

if vvok && vv.Parent().Lookup(ident.Name) == nil {
return gosec.NewIssue(c, n, r.ID(), "Subprocess launched with variable", gosec.Medium, gosec.High), nil
}
}
}
}
} else if !gosec.TryResolve(arg, c) {
// the arg is not a constant or a variable but instead a function call or os.Args[i]
Expand Down
31 changes: 30 additions & 1 deletion testutils/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -1369,7 +1369,36 @@ func RunCmd(command string) {
func main() {
RunCmd("sleep")
}`}, 1, gosec.NewConfig()},
}`}, 0, gosec.NewConfig()},
{[]string{`
package main
import (
"log"
"os/exec"
)
func RunCmd(a string, c string) {
cmd := exec.Command(c)
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
log.Printf("Waiting for command to finish...")
err = cmd.Wait()
cmd = exec.Command(a)
err = cmd.Start()
if err != nil {
log.Fatal(err)
}
log.Printf("Waiting for command to finish...")
err = cmd.Wait()
}
func main() {
RunCmd("ll", "ls")
}`}, 0, gosec.NewConfig()},
{[]string{`
// syscall.Exec function called with harcoded arguments
// shouldn't be consider as a command injection
Expand Down

0 comments on commit 5a131be

Please sign in to comment.