Chapter 5. Logical conditions, condition chaining, and custom variables
You can create complex conditions in rules by using the following:
- Logical conditions inform the provider how to handle more than one condition in a when block. You can aggregate or filter conditions using logical operations.
- Condition chaining uses the output of one condition as the input in another condition of the when block. Assign the output to a variable in one condition and reuse it in other conditions in the when block.
- Nested conditions to create conditions that depend on the evaluation of other conditions.
- Custom variables to capture specific information from the code that is evaluated by a rule. You can use the custom variable in the message displayed for the code if it contains a violation defined by the rule.
5.1. Logical conditions Copy linkLink copied to clipboard!
The analyzer provides two basic logical conditions, and and or, which you can use to aggregate results of other conditions.
5.2. AND condition Copy linkLink copied to clipboard!
The and condition performs a logical AND operation on the results of an array of conditions.
The and condition matches when all of its child conditions match, for example:
when:
and:
- <condition1>
- <condition2>
Example
when:
and:
- java.dependency:
name: junit.junit
upperbound: 4.12.2
lowerbound: 4.4.0
- java.referenced:
location: IMPORT
pattern: junit.junit
5.3. OR condition Copy linkLink copied to clipboard!
The or condition performs a logical OR operation on the results of an array of conditions.
The or condition matches when any of its child conditions match, for example:
when:
or:
- <condition1>
- <condition2>
Example
when:
or:
- java.dependency:
name: junit.junit
upperbound: 4.12.2
lowerbound: 4.4.0
- java.referenced:
location: IMPORT
pattern: junit.junit
5.4. Chaining Condition Variables Copy linkLink copied to clipboard!
You can use the output of one condition as the input for filtering another one in the and and or conditions. This is called condition chaining.
when:
or:
- builtin.xml:
xpath: "//dependencies/dependency"
filepaths: "{{poms.filepaths}}"
from: poms
- builtin.file:
pattern: pom.xml
as: poms
ignore: true
In the above example, the output of the builtin.file condition is saved as poms:
[...]
as: poms
[...]
The variables of builtin.file can then be used in the builtin.xml condition, by writing from and then using mustache templates in the provider_ condition block.
This is how this particular condition knows how to use the variable set to the name poms.
[...]
from: poms
[...]
Then you can use the variables by setting them as mustached templates in any of the inputs to the provider condition.
[...]
filepaths: "{{poms.filepaths}}"
[...]
If you only want to use the values of a condition as a chain, you can set ignore: true.
This will tell the engine not to use this condition to determine whether the rule has been violated or not:
[...]
ignore: true
[...]
5.5. Chaining in the Java provider Copy linkLink copied to clipboard!
In the java provider, the filepaths variable must be uppercased, for example:
when:
and:
- java.referenced:
pattern: org.springframework.web.bind.annotation.RequestMapping
location: ANNOTATION
as: annotation
- java.referenced:
pattern: org.springframework.stereotype.Controller
location: ANNOTATION
filepaths: "{{annotation.Filepaths}}"
5.6. Nested conditions Copy linkLink copied to clipboard!
Conditions can also be nested within other conditions.
when:
and:
- and:
- go.referenced: "*CustomResourceDefinition*"
- java.referenced:
pattern: "*CustomResourceDefinition*"
- go.referenced: "*CustomResourceDefinition*"
5.7. Custom variables Copy linkLink copied to clipboard!
Provider conditions can have associated custom variables. You can use custom variables to capture relevant information from the matched line in the source code. The values of these variables are interpolated with data matched in the source code. These values can be used to generate detailed template messages in a rule’s action (see Message actions). They can be added to a rule in the customVariables field:
- ruleID: lang-ref-004
customVariables:
- pattern: '([A-z]+)\.get\(\)'
name: VariableName
message: "Found generic call - {{ VariableName }}"
when:
java.referenced:
location: METHOD_CALL
pattern: com.example.apps.GenericClass.get
where:
pattern- A regular expression pattern that is matched on the source code line when a match is found.
name- The name of the variable that can be used in templates.
message- A template for a message by using a custom variable.