mirror of
https://github.com/securego/gosec.git
synced 2024-11-05 19:45:51 +00:00
Add a helper function which extracts the string parameters values of a call expression
This commit is contained in:
parent
2695567487
commit
f14f17fb1d
1 changed files with 39 additions and 0 deletions
39
helpers.go
39
helpers.go
|
@ -165,6 +165,45 @@ func GetCallInfo(n ast.Node, ctx *Context) (string, string, error) {
|
||||||
return "", "", fmt.Errorf("unable to determine call info")
|
return "", "", fmt.Errorf("unable to determine call info")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetCallStringArgsValues returns the values of strings arguments if they can be resolved
|
||||||
|
func GetCallStringArgsValues(n ast.Node, ctx *Context) []string {
|
||||||
|
values := []string{}
|
||||||
|
switch node := n.(type) {
|
||||||
|
case *ast.CallExpr:
|
||||||
|
for _, arg := range node.Args {
|
||||||
|
switch param := arg.(type) {
|
||||||
|
case *ast.BasicLit:
|
||||||
|
value, err := GetString(param)
|
||||||
|
if err == nil {
|
||||||
|
values = append(values, value)
|
||||||
|
}
|
||||||
|
case *ast.Ident:
|
||||||
|
obj := param.Obj
|
||||||
|
if obj != nil {
|
||||||
|
switch decl := obj.Decl.(type) {
|
||||||
|
case *ast.ValueSpec:
|
||||||
|
for _, v := range decl.Values {
|
||||||
|
value, err := GetString(v)
|
||||||
|
if err == nil {
|
||||||
|
values = append(values, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case *ast.AssignStmt:
|
||||||
|
for _, v := range decl.Rhs {
|
||||||
|
value, err := GetString(v)
|
||||||
|
if err == nil {
|
||||||
|
values = append(values, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return values
|
||||||
|
}
|
||||||
|
|
||||||
// GetImportedName returns the name used for the package within the
|
// GetImportedName returns the name used for the package within the
|
||||||
// code. It will resolve aliases and ignores initialization only imports.
|
// code. It will resolve aliases and ignores initialization only imports.
|
||||||
func GetImportedName(path string, ctx *Context) (string, bool) {
|
func GetImportedName(path string, ctx *Context) (string, bool) {
|
||||||
|
|
Loading…
Reference in a new issue