Skip to content

Commit

Permalink
feat: remove content from detectors
Browse files Browse the repository at this point in the history
  • Loading branch information
cfabianski committed Mar 7, 2024
1 parent 2af3b49 commit 794650a
Show file tree
Hide file tree
Showing 34 changed files with 54 additions and 97 deletions.
3 changes: 0 additions & 3 deletions internal/commands/process/settings/policies/common.rego
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ build_item(location) := {
"sink": {
"start": location.source.start_line_number,
"end": location.source.end_line_number,
"content": location.source.content,
"column": {
"start": location.source.start_column_number,
"end": location.source.end_column_number,
Expand Down Expand Up @@ -49,7 +48,6 @@ build_local_item(location, data_type) := {
"sink": {
"start": location.source.start_line_number,
"end": location.source.end_line_number,
"content": location.source.content,
"column": {
"start": location.source.start_column_number,
"end": location.source.end_column_number,
Expand All @@ -75,7 +73,6 @@ build_item(location) := {
"sink": {
"start": location.source.start_line_number,
"end": location.source.end_line_number,
"content": location.source.content,
"column": {
"start": location.source.start_column_number,
"end": location.source.end_column_number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ policy_failure contains item if {
"sink": {
"start": location.source.start_line_number,
"end": location.source.end_line_number,
"content": location.source.content,
"column": {
"start": location.source.start_column_number,
"end": location.source.end_column_number,
Expand Down
2 changes: 1 addition & 1 deletion internal/detectors/beego/beego.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func extractDatabases(drivers []*databaseDriver, fileInfo *fileInfo, report repo
Package: driverPackage,
DriverName: targetdriver,
TypeConstant: driverTypeConstant,
}, functionNode.Source(true))
}, functionNode.Source())

}

Expand Down
9 changes: 3 additions & 6 deletions internal/detectors/custom/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,22 +306,19 @@ func (detector *Detector) extractData(captures []parser.Captures, rule config.Co
}

if rule.DetectPresence {
content := capture["rule"].Source(false)
content.Text = &rule.Pattern

content := capture["rule"].Source()
var schemaSource *schema.Source
var source source.Source
if !rule.OmitParent {
source = capture["rule"].Source(true)
source = capture["rule"].Source()
schemaSource = &schema.Source{
StartLineNumber: *source.StartLineNumber,
EndLineNumber: *source.EndLineNumber,
StartColumnNumber: *source.StartColumnNumber,
EndColumnNumber: *source.EndColumnNumber,
Content: *source.Text,
}
} else {
source = capture["rule"].Source(false)
source = capture["rule"].Source()
schemaSource = &schema.Source{
StartLineNumber: *source.StartLineNumber,
StartColumnNumber: *source.StartColumnNumber,
Expand Down
2 changes: 1 addition & 1 deletion internal/detectors/django/django.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (detector *detector) ProcessFile(file *file.FileInfo, dir *file.Path, repor
report.AddFramework(detectors.DetectorDjango, django.TypeDatabase, django.Database{
Name: name,
Engine: engine,
}, nameNode.Source(false))
}, nameNode.Source())

return err
})
Expand Down
2 changes: 1 addition & 1 deletion internal/detectors/dotnet/dotnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (detector *detector) ProcessFile(file *file.FileInfo, dir *file.Path, repor
report.AddFramework(detectors.DetectorDotnet, dotnet.TypeDatabase, dotnet.DBContext{
TypeName: typeName,
UseDbMethodName: useDbMethodName,
}, nameNode.Source(false))
}, nameNode.Source())

return nil
})
Expand Down
2 changes: 1 addition & 1 deletion internal/detectors/envfile/envfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (detector *detector) ProcessFile(file *file.FileInfo, dir *file.Path, repor
Value: parsedValue,
Type: interfaceType,
VariableName: name,
}, detectionNode.Source(true))
}, detectionNode.Source())
}

return nil
Expand Down
3 changes: 0 additions & 3 deletions internal/detectors/gitleaks/gitleaks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gitleaks
import (
_ "embed"
"log"
"strings"

"github.com/bearer/bearer/internal/detectors/types"
"github.com/bearer/bearer/internal/parser/nodeid"
Expand Down Expand Up @@ -52,7 +51,6 @@ func (detector *detector) ProcessFile(file *file.FileInfo, dir *file.Path, repor
}

for _, finding := range findings {
text := strings.TrimPrefix(finding.Line, "\n")
report.AddSecretLeak(secret.Secret{
Description: finding.Description,
}, source.Source{
Expand All @@ -61,7 +59,6 @@ func (detector *detector) ProcessFile(file *file.FileInfo, dir *file.Path, repor
StartColumnNumber: &finding.StartColumn,
EndLineNumber: &finding.EndLine,
EndColumnNumber: &finding.EndColumn,
Text: &text,
})
}

Expand Down
4 changes: 2 additions & 2 deletions internal/detectors/graphql/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (detector *detector) ExtractFromSchema(
}

if !report.SchemaGroupIsOpen() {
source := objectNode.Source(true)
source := objectNode.Source()
report.SchemaGroupBegin(
detectors.DetectorGraphQL,
objectNode,
Expand All @@ -109,7 +109,7 @@ func (detector *detector) ExtractFromSchema(
nil,
)
}
source := fieldNode.Source(true)
source := fieldNode.Source()
report.SchemaGroupAddItem(
fieldNode,
currentSchema,
Expand Down
2 changes: 1 addition & 1 deletion internal/detectors/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func extractScripts(report report.Report, tree *parser.Tree, file *file.FileInfo
report.AddInterface(detectors.DetectorHTML, interfacetype.Interface{
Value: parsedValue,
Type: interfaceType,
}, tagNode.Source(true))
}, tagNode.Source())
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/detectors/openapi/queries/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func AnnotateObjects(request ObjectsRequest) error {
}

request.FoundValues[*propertyName] = &schemahelper.Schema{
Source: propertyName.Source(true),
Source: propertyName.Source(),
Value: schema.Schema{
FieldName: propertyName.Content(),
FieldUUID: request.NodeIDMap.ValueForNode(propertyName.ID()),
Expand Down
2 changes: 1 addition & 1 deletion internal/detectors/openapi/queries/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func AnnotatePaths(request PathsRequest) error {
requestType := capture["param_request_type"]

request.FoundValues[*requestType] = &operationshelper.Operation{
Source: requestType.Source(true),
Source: requestType.Source(),
Value: operations.Operation{
Path: stringutil.StripQuotes(path.Content()),
Type: stringutil.StripQuotes(requestType.Content()),
Expand Down
2 changes: 1 addition & 1 deletion internal/detectors/openapi/queries/v2parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func AnnotateV2Paramaters(nodeIDMap *nodeid.Map, tree *parser.Tree, foundValues
typeNode := capture["param_type"]

foundValues[*nameNode] = &schemahelper.Schema{
Source: nameNode.Source(true),
Source: nameNode.Source(),
Value: schema.Schema{
FieldName: nameNode.Content(),
FieldType: typeNode.Content(),
Expand Down
2 changes: 1 addition & 1 deletion internal/detectors/openapi/queries/v3parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func AnnotateV3Paramaters(
}

foundValues[*nameNode] = &schemahelper.Schema{
Source: nameNode.Source(true),
Source: nameNode.Source(),
Value: schema.Schema{
FieldName: nameNode.Content(),
FieldType: fieldType,
Expand Down
4 changes: 2 additions & 2 deletions internal/detectors/proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (detector *detector) ExtractFromSchema(
}

if !report.SchemaGroupIsOpen() {
source := objectNode.Source(true)
source := objectNode.Source()
report.SchemaGroupBegin(
detectors.DetectorProto,
objectNode,
Expand All @@ -113,7 +113,7 @@ func (detector *detector) ExtractFromSchema(
nil,
)
}
source := fieldNode.Source(true)
source := fieldNode.Source()
report.SchemaGroupAddItem(
fieldNode,
currentSchema,
Expand Down
2 changes: 1 addition & 1 deletion internal/detectors/rails/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func ExtractCaches(file *file.FileInfo, report report.Report) error {

report.AddFramework(detectors.DetectorRails, rails.TypeCache, rails.Cache{
Type: typeName,
}, typeSymbol.Source(false))
}, typeSymbol.Source())

return nil
})
Expand Down
4 changes: 2 additions & 2 deletions internal/detectors/rails/schema_rb/schema_rb.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func ExtractFromDatabaseSchema(
}

if !report.SchemaGroupIsOpen() {
source := tableNode.Source(false)
source := tableNode.Source()
report.SchemaGroupBegin(
detectors.DetectorSchemaRb,
tableNode,
Expand All @@ -93,7 +93,7 @@ func ExtractFromDatabaseSchema(
ruleNode,
)
}
source := columnNode.Source(false)
source := columnNode.Source()
report.SchemaGroupAddItem(
columnNode,
currentSchema,
Expand Down
1 change: 0 additions & 1 deletion internal/detectors/simple/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ func extractURLs(fileInfo *file.FileInfo, line string, lineNumber int, report re
LanguageType: fileInfo.LanguageTypeString(),
StartLineNumber: &lineNumber,
StartColumnNumber: pointers.Int(globalOffset + startOffset),
Text: &url,
})
}

Expand Down
1 change: 0 additions & 1 deletion internal/detectors/spring/spring.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ func extractDataStoresFromProperties(file *file.FileInfo, report report.Report)
LanguageType: file.LanguageTypeString(),
Filename: file.RelativePath,
StartLineNumber: &property.lineNumber,
Text: &property.text,
})

return nil
Expand Down
6 changes: 3 additions & 3 deletions internal/detectors/typescript/datatype/knex/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ func detectFunctionTypes(report report.Report, tree *parser.Tree, language *sitt
}

sort.Slice(sortedTypes, func(i, j int) bool {
lineNumberA := sortedTypes[i].node.Source(false).StartLineNumber
lineNumberB := sortedTypes[j].node.Source(false).StartLineNumber
lineNumberA := sortedTypes[i].node.Source().StartLineNumber
lineNumberB := sortedTypes[j].node.Source().StartLineNumber
return *lineNumberA < *lineNumberB
})

for _, functionType := range sortedTypes {
for _, dataType := range functionType.types {
report.AddFramework(detectors.DetectorTypescript, reportknex.TypeFunction, reportknex.Function{DataType: dataType}, functionType.node.Source(false))
report.AddFramework(detectors.DetectorTypescript, reportknex.TypeFunction, reportknex.Function{DataType: dataType}, functionType.node.Source())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/detectors/typescript/datatype/knex/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func detectTableDeclarationModule(report report.Report, tree *parser.Tree, langu
report.AddFramework(detectors.DetectorTypescript, reportknex.TypeSchema, reportknex.Schema{
DataType: propertyType.Content(),
PropertyName: propertyName.Content(),
}, tableChild.Source(false))
}, tableChild.Source())
}
}
}
2 changes: 1 addition & 1 deletion internal/detectors/yamlconfig/yamlconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func extractInterfaces(report report.Report, tree *parser.Tree) error {
Type: interfaceType,
Value: parsedValue,
VariableName: key,
}, definitionNode.Source(true))
}, definitionNode.Source())
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/languages/javascript/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (*Pattern) ExtractVariables(input string) (string, []language.PatternVariab
return replaced, params, nil
}

func produceDummyValue(i int, nodeType string) string {
func produceDummyValue(i int, _ string) string {
return "BearerVar" + fmt.Sprint(i)
}

Expand Down
16 changes: 8 additions & 8 deletions internal/parser/datatype/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ func (scope *Scope) toSortedDatatypes() [][]datatype.DataTypable {
}

sort.Slice(sortedDatatypes, func(i, j int) bool {
lineNumberA := sortedDatatypes[i][0].GetNode().Source(false).StartLineNumber
lineNumberB := sortedDatatypes[j][0].GetNode().Source(false).StartLineNumber
lineNumberA := sortedDatatypes[i][0].GetNode().Source().StartLineNumber
lineNumberB := sortedDatatypes[j][0].GetNode().Source().StartLineNumber

if *lineNumberA != *lineNumberB {
return *lineNumberA < *lineNumberB
}

columnNumberA := sortedDatatypes[i][0].GetNode().Source(false).StartColumnNumber
columnNumberB := sortedDatatypes[j][0].GetNode().Source(false).StartColumnNumber
columnNumberA := sortedDatatypes[i][0].GetNode().Source().StartColumnNumber
columnNumberB := sortedDatatypes[j][0].GetNode().Source().StartColumnNumber

return *columnNumberA < *columnNumberB
})
Expand Down Expand Up @@ -147,15 +147,15 @@ func UnifyUUID(datatypes []datatype.DataTypable, idGenerator nodeid.Generator) {

func SortScopes(input []*Scope) {
sort.Slice(input, func(i, j int) bool {
lineNumberA := input[i].Node.Source(false).StartLineNumber
lineNumberB := input[j].Node.Source(false).StartLineNumber
lineNumberA := input[i].Node.Source().StartLineNumber
lineNumberB := input[j].Node.Source().StartLineNumber

if *lineNumberA != *lineNumberB {
return *lineNumberA < *lineNumberB
}

columnNumberA := input[i].Node.Source(false).StartColumnNumber
columnNumberB := input[j].Node.Source(false).StartColumnNumber
columnNumberA := input[i].Node.Source().StartColumnNumber
columnNumberB := input[j].Node.Source().StartColumnNumber

return *columnNumberA < *columnNumberB
})
Expand Down
2 changes: 1 addition & 1 deletion internal/parser/interfacedetector/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Detect(req *Request) error {
req.Report.AddInterface(req.DetectorType, reportinterface.Interface{
Type: interfaceType,
Value: value,
}, node.Source(true))
}, node.Source())
}
})
}
Expand Down
8 changes: 1 addition & 7 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,20 +244,14 @@ func (node *Node) EndColumnNumber() int {
return int(node.sitter.EndPoint().Column + 1)
}

func (node *Node) Source(includeText bool) source.Source {
text := ""
if includeText {
text = strings.TrimSpace(node.Content())
}

func (node *Node) Source() source.Source {
return source.New(
node.tree.fileInfo,
node.tree.File(),
node.StartLineNumber(),
node.StartColumnNumber(),
node.EndLineNumber(),
node.EndColumnNumber(),
text,
)
}

Expand Down
5 changes: 0 additions & 5 deletions internal/report/output/dataflow/risks/risks.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,10 @@ func (holder *Holder) AddRiskPresence(detection detections.Detection) {
StartColumnNumber: *detection.Source.StartColumnNumber,
EndLineNumber: *detection.Source.EndLineNumber,
EndColumnNumber: *detection.Source.EndColumnNumber,
Content: *detection.Source.Text,
}
} else {
// parent can be nil
source = extractCustomRiskParent(detection.Value)
content = *detection.Source.Text
}

holder.addDatatype(
Expand Down Expand Up @@ -195,9 +193,6 @@ func (holder *Holder) addDatatype(
line := file.startLineNumber[startLineNumber]
// create datatype source entry if it doesn't exist
sourceKey := "undefined_source"
if schema.Source != nil {
sourceKey = schema.Source.Content
}

if _, exists := line.source[sourceKey]; !exists {
line.source[sourceKey] = sourceHolder{
Expand Down
1 change: 0 additions & 1 deletion internal/report/output/privacy/privacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ func dummyDataflow() *outputtypes.DataFlow {
StartColumnNumber: 10,
EndLineNumber: 38,
EndColumnNumber: 28,
Content: "Sentry.set_user(email: current_user.email)",
},
DataTypes: []types.RiskDatatype{
{
Expand Down
Loading

0 comments on commit 794650a

Please sign in to comment.