Skip to content

Commit

Permalink
fix(golang): fix bugs identified during rules testing
Browse files Browse the repository at this point in the history
  • Loading branch information
cfabianski committed Oct 28, 2023
1 parent f18c46d commit cc32f74
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions internal/languages/golang/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions internal/languages/golang/detectors/string/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() == "+" {
Expand All @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions internal/languages/golang/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{"_"}
)
Expand Down Expand Up @@ -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",
}

Expand Down

0 comments on commit cc32f74

Please sign in to comment.