Skip to content

Commit

Permalink
fix(classification): improve php datatype detections
Browse files Browse the repository at this point in the history
  • Loading branch information
didroe committed Oct 18, 2023
1 parent e1084dd commit 8d5704d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
23 changes: 11 additions & 12 deletions internal/languages/php/detectors/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/bearer/bearer/internal/scanner/ast/tree"

"github.com/bearer/bearer/internal/scanner/detectors/common"
detectorscommon "github.com/bearer/bearer/internal/scanner/detectors/common"
"github.com/bearer/bearer/internal/scanner/detectors/types"
"github.com/bearer/bearer/internal/scanner/ruleset"
)
Expand Down Expand Up @@ -51,9 +50,9 @@ func New(querySet *query.Set) types.Detector {
// $user->name;
// $user->name();
fieldAccessQuery := querySet.Add(`[
(member_access_expression object: (_) @object name: (name) @field) @root
(member_call_expression object: (_) @object name: (name) @field) @root
]`)
(member_access_expression object: (_) @object name: (name) @field)
(member_call_expression object: (_) @object name: (name) @field)
] @root`)

// array('foo' => 'bar');
// [ 'foo' => 'bar' ];
Expand Down Expand Up @@ -110,35 +109,35 @@ func (detector *objectDetector) getArrayCreation(
return nil, nil
}

var properties []detectorscommon.Property
var properties []common.Property
for _, result := range results {
pairNode := result["key"]
name := result["value"].Content()
keyNode := result["key"]
name := keyNode.Content()

propertyObjects, err := detectorContext.Scan(result["value"], ruleset.BuiltinObjectRule, traversalstrategy.Cursor)
if err != nil {
return nil, err
}

if len(propertyObjects) == 0 {
properties = append(properties, detectorscommon.Property{
properties = append(properties, common.Property{
Name: name,
Node: pairNode,
Node: keyNode,
})

continue
}

for _, propertyObject := range propertyObjects {
properties = append(properties, detectorscommon.Property{
properties = append(properties, common.Property{
Name: name,
Node: pairNode,
Node: keyNode,
Object: propertyObject,
})
}
}

return []interface{}{detectorscommon.Object{Properties: properties}}, nil
return []interface{}{common.Object{Properties: properties}}, nil
}

func (detector *objectDetector) getAssignment(
Expand Down
12 changes: 5 additions & 7 deletions internal/languages/php/detectors/object/projection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"github.com/bearer/bearer/internal/util/stringutil"

"github.com/bearer/bearer/internal/scanner/detectors/common"
detectorscommon "github.com/bearer/bearer/internal/scanner/detectors/common"
"github.com/bearer/bearer/internal/scanner/detectors/types"
)

func (detector *objectDetector) getProjections( // Deal with subscriptExpressionQuery here / Check Ruby
func (detector *objectDetector) getProjections(
node *tree.Node,
detectorContext types.Context,
) ([]interface{}, error) {
Expand Down Expand Up @@ -47,7 +46,7 @@ func (detector *objectDetector) getProjections( // Deal with subscriptExpression
return nil, nil
}

objects, err := detectorscommon.ProjectObject(
objects, err := common.ProjectObject(
node,
detectorContext,
objectNode,
Expand All @@ -66,15 +65,14 @@ func (detector *objectDetector) getProjections( // Deal with subscriptExpression
}

func getObjectName(objectNode *tree.Node) string {
switch objectNode.Type() {
// $user->name()
// $user->name
if objectNode.Type() == "variable_name" {
// user->name
case "variable_name", "name":
return objectNode.Content()
}

// $user->foo->name
// $user->foo()->name
switch objectNode.Type() {
case "member_access_expression", "member_call_expression":
return objectNode.ChildByFieldName("name").Content()
}
Expand Down
7 changes: 6 additions & 1 deletion internal/util/normalize_key/normalize_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package normalize_key

import (
"regexp"
"slices"
"strings"
)

var (
normalizeCaseRegexp = regexp.MustCompile(`[A-Z][A-Z][a-z]|[a-z][A-Z]`) // Matches "AP(INa)me" or "firs(tN)ame"
normalizeSeparatorRegexp = regexp.MustCompile(`[_\-.,\s:0-9]+`)
normalizeSeparatorRegexp = regexp.MustCompile(`[$_\-.,\s:0-9]+`)
)

func Normalize(key string) string {
Expand All @@ -22,6 +23,10 @@ func Normalize(key string) string {

pieces = append(pieces, normalizeKeyPiece(key[start:]))

if len(pieces) != 0 && (pieces[0] == "get" || pieces[0] == "set") {
pieces = slices.Delete(pieces, 0, 1)
}

return strings.Join(pieces, " ")
}

Expand Down

0 comments on commit 8d5704d

Please sign in to comment.