forked from securego/gosec
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rule which detects a potential path traversal when extracting zip arc…
…hives (securego#208) * Add a rule which detects file path traversal when extracting zip archive * Detect if any argument is derived from zip.File * Drop support for Go version 1.8
- Loading branch information
Showing
6 changed files
with
160 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
language: go | ||
|
||
go: | ||
- 1.8 | ||
- 1.9 | ||
- "1.10" | ||
- tip | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package rules | ||
|
||
import ( | ||
"go/ast" | ||
"go/types" | ||
|
||
"github.com/GoASTScanner/gas" | ||
) | ||
|
||
type archive struct { | ||
gas.MetaData | ||
calls gas.CallList | ||
argType string | ||
} | ||
|
||
func (a *archive) ID() string { | ||
return a.MetaData.ID | ||
} | ||
|
||
// Match inspects AST nodes to determine if the filepath.Joins uses any argument derived from type zip.File | ||
func (a *archive) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) { | ||
if node := a.calls.ContainsCallExpr(n, c); node != nil { | ||
for _, arg := range node.Args { | ||
var argType types.Type | ||
if selector, ok := arg.(*ast.SelectorExpr); ok { | ||
argType = c.Info.TypeOf(selector.X) | ||
} else if ident, ok := arg.(*ast.Ident); ok { | ||
if ident.Obj != nil && ident.Obj.Kind == ast.Var { | ||
decl := ident.Obj.Decl | ||
if assign, ok := decl.(*ast.AssignStmt); ok { | ||
if selector, ok := assign.Rhs[0].(*ast.SelectorExpr); ok { | ||
argType = c.Info.TypeOf(selector.X) | ||
} | ||
} | ||
} | ||
} | ||
|
||
if argType != nil && argType.String() == a.argType { | ||
return gas.NewIssue(c, n, a.ID(), a.What, a.Severity, a.Confidence), nil | ||
} | ||
} | ||
} | ||
return nil, nil | ||
} | ||
|
||
// NewArchive creates a new rule which detects the file traversal when extracting zip archives | ||
func NewArchive(id string, conf gas.Config) (gas.Rule, []ast.Node) { | ||
calls := gas.NewCallList() | ||
calls.Add("path/filepath", "Join") | ||
return &archive{ | ||
calls: calls, | ||
argType: "*archive/zip.File", | ||
MetaData: gas.MetaData{ | ||
ID: id, | ||
Severity: gas.Medium, | ||
Confidence: gas.High, | ||
What: "File traversal when extracting zip archive", | ||
}, | ||
}, []ast.Node{(*ast.CallExpr)(nil)} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters