From cc32f7426c35a24adfd8a2017e09efbd7b6ed073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Fabianski?= Date: Sat, 28 Oct 2023 22:55:09 +0200 Subject: [PATCH] fix(golang): fix bugs identified during rules testing --- internal/languages/golang/analyzer/analyzer.go | 4 ++-- internal/languages/golang/detectors/string/string.go | 3 +++ internal/languages/golang/pattern/pattern.go | 11 +++++++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/internal/languages/golang/analyzer/analyzer.go b/internal/languages/golang/analyzer/analyzer.go index 219ec037b..f550eb467 100644 --- a/internal/languages/golang/analyzer/analyzer.go +++ b/internal/languages/golang/analyzer/analyzer.go @@ -45,7 +45,7 @@ func (analyzer *analyzer) Analyze(node *sitter.Node, visitChildren func() error) return analyzer.analyzeSwitch(node, visitChildren) case "expression_case", "default_case": return analyzer.analyzeGenericConstruct(node, visitChildren) - case "argument_list", "binary_expression": + case "argument_list", "binary_expression", "expression_list": return analyzer.analyzeGenericOperation(node, visitChildren) case "return_statement", "go_statement", "defer_statement", "if_statement": // statements don't have results return visitChildren() @@ -125,7 +125,7 @@ func (analyzer *analyzer) analyzeShortVarDeclaration(node *sitter.Node, visitChi right := node.ChildByFieldName("right") for _, child := range analyzer.builder.ChildrenFor(left) { - if !slices.Contains([]string{"_", "err"}, analyzer.builder.ContentFor(child)) { + if !slices.Contains([]string{"_", ",", "err"}, analyzer.builder.ContentFor(child)) { analyzer.scope.Declare(analyzer.builder.ContentFor(child), child) analyzer.scope.Assign(analyzer.builder.ContentFor(child), node) } diff --git a/internal/languages/golang/detectors/string/string.go b/internal/languages/golang/detectors/string/string.go index 01c85bc3d..311676536 100644 --- a/internal/languages/golang/detectors/string/string.go +++ b/internal/languages/golang/detectors/string/string.go @@ -5,6 +5,7 @@ import ( "github.com/bearer/bearer/internal/scanner/ast/tree" "github.com/bearer/bearer/internal/scanner/ruleset" "github.com/bearer/bearer/internal/util/stringutil" + "github.com/rs/zerolog/log" "github.com/bearer/bearer/internal/scanner/detectors/common" "github.com/bearer/bearer/internal/scanner/detectors/types" @@ -26,6 +27,7 @@ func (detector *stringDetector) DetectAt( node *tree.Node, detectorContext types.Context, ) ([]interface{}, error) { + log.Error().Msgf("string detector %s", node.Type()) switch node.Type() { case "binary_expression": if node.Children()[1].Content() == "+" { @@ -38,6 +40,7 @@ func (detector *stringDetector) DetectAt( case "interpreted_string_literal", "raw_string_literal": value := stringutil.StripQuotes(node.Content()) + log.Error().Msgf("interpreted_string_literal %s", value) return []interface{}{common.String{ Value: value, IsLiteral: true, diff --git a/internal/languages/golang/pattern/pattern.go b/internal/languages/golang/pattern/pattern.go index 3ddc0b283..475ae956b 100644 --- a/internal/languages/golang/pattern/pattern.go +++ b/internal/languages/golang/pattern/pattern.go @@ -17,7 +17,14 @@ var ( matchNodeRegex = regexp.MustCompile(`\$`) ellipsisRegex = regexp.MustCompile(`\$<\.\.\.>`) unanchoredPatternNodeTypes = []string{"import_spec"} - patternMatchNodeContainerTypes = []string{"parameter_declaration", "parameter_list", "var_spec", "import_spec"} + patternMatchNodeContainerTypes = []string{ + "parameter_declaration", + "argument_list", + "expression_list", + "parameter_list", + "var_spec", + "import_spec", + } allowedPatternQueryTypes = []string{"_"} ) @@ -134,7 +141,7 @@ func (*Pattern) IsAnchored(node *tree.Node) (bool, bool) { // function declaration_list unAnchored := []string{ "function_declaration", - "argument_list", + // "argument_list", "var_declaration", }