Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support const init and const invoke #1482

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions internal/languages/php/.snapshots/TestConst--main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
low:
- rule:
cwe_ids: []
id: php_rule_logger_test
title: ""
description: ""
documentation_url: ""
line_number: 7
full_filename: main.php
filename: main.php
source:
location:
start: 7
end: 7
column:
start: 10
end: 38
sink:
location:
start: 7
end: 7
column:
start: 10
end: 38
content: hash( self::ALGO, $content )
parent_line_number: 7
snippet: hash( self::ALGO, $content )
fingerprint: b1e6825cdfdbf302da0f7c9887efd995_0
old_fingerprint: b1e6825cdfdbf302da0f7c9887efd995_0

27 changes: 26 additions & 1 deletion internal/languages/php/analyzer/analyzer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package analyzer

import (
"slices"

sitter "github.com/smacker/go-tree-sitter"

"github.com/bearer/bearer/internal/scanner/ast/tree"
Expand Down Expand Up @@ -45,6 +47,8 @@ func (analyzer *analyzer) Analyze(node *sitter.Node, visitChildren func() error)
return analyzer.analyzeGenericConstruct(node, visitChildren)
case "switch_label":
return visitChildren()
case "const_declaration":
return analyzer.analyzeConstDeclaration(node, visitChildren)
case "dynamic_variable_name":
return analyzer.analyzeDynamicVariableName(node, visitChildren)
case "subscript_expression":
Expand Down Expand Up @@ -197,6 +201,27 @@ func (analyzer *analyzer) analyzeSubscript(node *sitter.Node, visitChildren func
return visitChildren()
}

func (analyzer *analyzer) analyzeConstDeclaration(node *sitter.Node, visitChildren func() error) error {
child := node.NamedChild(0)

if child.Type() == "visibility_modifier" {
child = node.NamedChild(1)
}

left := child.NamedChild(0)
right := child.NamedChild(1)
analyzer.lookupVariable(right)

err := visitChildren()

if left.Type() == "name" {
analyzer.builder.Alias(left, right)
analyzer.scope.Declare("self::"+analyzer.builder.ContentFor(left), right)
}

return err
}

// catch(FooException | BarException $e) {}
func (analyzer *analyzer) analyzeCatchClause(node *sitter.Node, visitChildren func() error) error {
return analyzer.withScope(language.NewScope(analyzer.scope), func() error {
Expand Down Expand Up @@ -258,7 +283,7 @@ func (analyzer *analyzer) withScope(newScope *language.Scope, body func() error)
}

func (analyzer *analyzer) lookupVariable(node *sitter.Node) {
if node == nil || node.Type() != "variable_name" {
if node == nil || !slices.Contains([]string{"variable_name", "class_constant_access_expression"}, node.Type()) {
return
}

Expand Down
16 changes: 8 additions & 8 deletions internal/languages/php/detectors/.snapshots/TestPHPString-string
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ children:
- type: const_declaration
id: 7
range: 3:5 - 3:36
dataflow_sources:
- 8
- 9
- 16
children:
- type: '"const"'
id: 8
Expand All @@ -53,6 +49,8 @@ children:
id: 10
range: 3:11 - 3:19
content: Greeting
alias_of:
- 12
- type: '"="'
id: 11
range: 3:20 - 3:21
Expand Down Expand Up @@ -185,6 +183,8 @@ children:
- 41
- 43
- 44
alias_of:
- 12
children:
- type: relative_scope
id: 41
Expand Down Expand Up @@ -533,8 +533,8 @@ children:
- node: 52
content: $s .= "!!"
data:
value: !!!
isliteral: false
value: Hello World!!!
isliteral: true
- node: 74
content: $s2 .= $args[0]
data:
Expand All @@ -548,8 +548,8 @@ children:
- node: 39
content: self::Greeting . "!"
data:
value: !
isliteral: false
value: Hello World!
isliteral: true
- node: 57
content: '"!!"'
data:
Expand Down
7 changes: 7 additions & 0 deletions internal/languages/php/php_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ var loggerRule []byte
//go:embed testdata/scope_rule.yml
var scopeRule []byte

//go:embed testdata/md.yml
var mdRule []byte

func TestFlow(t *testing.T) {
testhelper.GetRunner(t, loggerRule, "PHP").RunTest(t, "./testdata/testcases/flow", ".snapshots/flow/")
}
Expand All @@ -30,6 +33,10 @@ func TestScope(t *testing.T) {
testhelper.GetRunner(t, scopeRule, "PHP").RunTest(t, "./testdata/scope", ".snapshots/")
}

func TestConst(t *testing.T) {
testhelper.GetRunner(t, mdRule, "PHP").RunTest(t, "./testdata/md", ".snapshots/")
}

func TestAnalyzer(t *testing.T) {
for _, test := range []struct{ name, code string }{
{"foreach", `<?php
Expand Down
11 changes: 11 additions & 0 deletions internal/languages/php/testdata/md.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type: "risk"
languages:
- php
patterns:
- pattern: |
hash($<ALGORITHM>$<...>)
filters:
- variable: ALGORITHM
string_regex: md\d
metadata:
id: php_rule_logger_test
10 changes: 10 additions & 0 deletions internal/languages/php/testdata/md/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
class ContentsHasher {
private const ALGO = 'md4';

public static function getFileContentsHash( $content ) {

return hash( self::ALGO, $content );
}
}
?>
Loading