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 multiple required detections #1659

Merged
merged 1 commit into from
Jul 30, 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
19 changes: 15 additions & 4 deletions pkg/commands/process/settings/policies/risk_policy.rego
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,33 @@ presence_failures contains detector if {
# # Build policy failures
policy_failure contains item if {
input.rule.trigger.match_on == "absence"

count(input.rule.trigger.required_detections) == count({ required_detection |
required_detection := input.rule.trigger.required_detections[_]
some y in input.dataflow.risks
y.detector_id == required_detection
})

some detector in input.dataflow.risks
detector.detector_id == input.rule.trigger.required_detections[0]

detector.detector_id == input.rule.trigger.required_detection
some init_location in detector.locations

x := {other | other := input.dataflow.risks[_]; other.detector_id == input.rule.id}
count(x) == 0

item := data.bearer.common.build_item(init_location)
}

policy_failure contains item if {
input.rule.trigger.match_on == "absence"
some detector in input.dataflow.risks
count(input.rule.trigger.required_detections) == count({ required_detection |
required_detection := input.rule.trigger.required_detections[_]
some x in input.dataflow.risks
x.detector_id == required_detection
})

detector.detector_id == input.rule.trigger.required_detection
some detector in input.dataflow.risks
detector.detector_id == input.rule.trigger.required_detections[0]

some init_location in detector.locations
some other_detector in input.dataflow.risks
Expand Down
5 changes: 4 additions & 1 deletion pkg/commands/process/settings/rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,11 @@ func BuildRules(
if definition.Trigger.DataTypesRequired != nil {
ruleTrigger.DataTypesRequired = *definition.Trigger.DataTypesRequired
}

// concat any required detections
ruleTrigger.RequiredDetections = definition.Trigger.RequiredDetections
if definition.Trigger.RequiredDetection != nil {
ruleTrigger.RequiredDetection = definition.Trigger.RequiredDetection
ruleTrigger.RequiredDetections = append(ruleTrigger.RequiredDetections, *definition.Trigger.RequiredDetection)
}
}

Expand Down
13 changes: 7 additions & 6 deletions pkg/commands/process/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,16 @@ const (
)

type RuleTrigger struct {
MatchOn MatchOn `mapstructure:"match_on" json:"match_on" yaml:"match_on"`
DataTypesRequired bool `mapstructure:"data_types_required" json:"data_types_required" yaml:"data_types_required"`
RequiredDetection *string `mapstructure:"required_detection" json:"required_detection" yaml:"required_detection"`
MatchOn MatchOn `mapstructure:"match_on" json:"match_on" yaml:"match_on"`
DataTypesRequired bool `mapstructure:"data_types_required" json:"data_types_required" yaml:"data_types_required"`
RequiredDetections []string `mapstructure:"required_detections" json:"required_detections" yaml:"required_detections"`
}

type RuleDefinitionTrigger struct {
MatchOn *MatchOn `mapstructure:"match_on" json:"match_on" yaml:"match_on"`
RequiredDetection *string `mapstructure:"required_detection" json:"required_detection" yaml:"required_detection"`
DataTypesRequired *bool `mapstructure:"data_types_required" json:"data_types_required" yaml:"data_types_required"`
MatchOn *MatchOn `mapstructure:"match_on" json:"match_on" yaml:"match_on"`
RequiredDetection *string `mapstructure:"required_detection" json:"required_detection" yaml:"required_detection"`
RequiredDetections []string `mapstructure:"required_detections" json:"required_detections" yaml:"required_detections"`
DataTypesRequired *bool `mapstructure:"data_types_required" json:"data_types_required" yaml:"data_types_required"`
}

type RuleMetadata struct {
Expand Down
6 changes: 3 additions & 3 deletions pkg/report/output/testhelper/testhelper.go

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions pkg/scanner/ruleset/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ func getTriggerRuleIDs(languageRules []*settings.Rule) set.Set[string] {
triggerRuleIDs := set.New[string]()

for _, settingsRule := range languageRules {
if settingsRule.Trigger.RequiredDetection != nil {
triggerRuleIDs.Add(*settingsRule.Trigger.RequiredDetection)
}
triggerRuleIDs.AddAll(settingsRule.Trigger.RequiredDetections)
}

return triggerRuleIDs
Expand Down
Loading