|
| 1 | +## Introduction |
| 2 | + |
| 3 | +Alerts can include a filter expression to control which notifications are dispatched |
| 4 | +based on their content. Filter expressions are written in [CEL] (Common Expression Language), |
| 5 | +the same language used for [policy compliance expressions](../policy-compliance/expressions.md). |
| 6 | + |
| 7 | +Without a filter expression, an alert matches all notifications that satisfy its scope, group, |
| 8 | +level, and project or tag restrictions. A filter expression adds a further condition: the |
| 9 | +notification is only dispatched when the expression evaluates to `true`. |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +## Syntax |
| 14 | + |
| 15 | +Filter expressions use the same [CEL syntax](../policy-compliance/expressions.md#syntax) as |
| 16 | +policy compliance expressions. CEL is not [Turing-complete] and does not support constructs |
| 17 | +like `if` statements or loops. It compensates for this with [macros] like `all`, `exists`, |
| 18 | +`exists_one`, `map`, and `filter`. |
| 19 | + |
| 20 | +Refer to the official [language definition] for a thorough description of the syntax. |
| 21 | + |
| 22 | +## Evaluation Context |
| 23 | + |
| 24 | +The context in which filter expressions are evaluated contains the following variables: |
| 25 | + |
| 26 | +| Variable | Type | Description | |
| 27 | +|:------------|:-------------------------------------------------------------|:---------------------------------------------------------------------------------------| |
| 28 | +| `level` | [`Level`](../../reference/schemas/notification.md#level) | The notification level, as an integer enum value. Use named constants (see below). | |
| 29 | +| `scope` | [`Scope`](../../reference/schemas/notification.md#scope) | The notification scope, as an integer enum value. Use named constants (see below). | |
| 30 | +| `group` | [`Group`](../../reference/schemas/notification.md#group) | The notification group, as an integer enum value. Use named constants (see below). | |
| 31 | +| `title` | `string` | The notification title. | |
| 32 | +| `content` | `string` | The notification content. | |
| 33 | +| `timestamp` | [`google.protobuf.Timestamp`][protobuf-ts-docs] | The time at which the notification was created. | |
| 34 | +| `subject` | dynamic | The notification subject, typed according to the notification group (see below). | |
| 35 | + |
| 36 | +### Enum Constants |
| 37 | + |
| 38 | +The `level`, `scope`, and `group` variables hold integer values. To compare them in a readable way, |
| 39 | +use the named constants from the [notification schema](../../reference/schemas/notification.md#enums): |
| 40 | + |
| 41 | +```js |
| 42 | +level == Level.LEVEL_INFORMATIONAL |
| 43 | +``` |
| 44 | + |
| 45 | +```js |
| 46 | +group == Group.GROUP_NEW_VULNERABILITY |
| 47 | +``` |
| 48 | + |
| 49 | +```js |
| 50 | +scope == Scope.SCOPE_PORTFOLIO |
| 51 | +``` |
| 52 | + |
| 53 | +### Subject Types |
| 54 | + |
| 55 | +The `subject` variable holds the notification's subject, which varies depending on the notification |
| 56 | +group. Refer to the [notification schema reference](../../reference/schemas/notification.md#subjects) |
| 57 | +for full details of each subject type and its fields. |
| 58 | + |
| 59 | +| Group | Subject Type | |
| 60 | +|:--------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 61 | +| `BOM_CONSUMED`, `BOM_PROCESSED` | [BomConsumedOrProcessedSubject](../../reference/schemas/notification.md#bomconsumedorprocessedsubject) | |
| 62 | +| `BOM_PROCESSING_FAILED` | [BomProcessingFailedSubject](../../reference/schemas/notification.md#bomprocessingfailedsubject) | |
| 63 | +| `BOM_VALIDATION_FAILED` | [BomValidationFailedSubject](../../reference/schemas/notification.md#bomvalidationfailedsubject) | |
| 64 | +| `NEW_VULNERABILITY` | [NewVulnerabilitySubject](../../reference/schemas/notification.md#newvulnerabilitysubject) | |
| 65 | +| `NEW_VULNERABLE_DEPENDENCY` | [NewVulnerableDependencySubject](../../reference/schemas/notification.md#newvulnerabledependencysubject) | |
| 66 | +| `POLICY_VIOLATION` | [PolicyViolationSubject](../../reference/schemas/notification.md#policyviolationsubject) | |
| 67 | +| `PROJECT_AUDIT_CHANGE` | [VulnerabilityAnalysisDecisionChangeSubject](../../reference/schemas/notification.md#vulnerabilityanalysisdecisionchangesubject) or [PolicyViolationAnalysisDecisionChangeSubject](../../reference/schemas/notification.md#policyviolationanalysisdecisionchangesubject) | |
| 68 | +| `PROJECT_VULN_ANALYSIS_COMPLETE` | [ProjectVulnAnalysisCompleteSubject](../../reference/schemas/notification.md#projectvulnanalysiscompletesubject) | |
| 69 | +| `VEX_CONSUMED`, `VEX_PROCESSED` | [VexConsumedOrProcessedSubject](../../reference/schemas/notification.md#vexconsumedorprocessedsubject) | |
| 70 | +| `USER_CREATED`, `USER_DELETED` | [UserSubject](../../reference/schemas/notification.md#usersubject) | |
| 71 | + |
| 72 | +## Validation |
| 73 | + |
| 74 | +Filter expressions are validated when an alert is saved. If the expression contains syntax errors |
| 75 | +or references types that do not exist, the save operation will fail and the errors will be reported |
| 76 | +with their exact location (line and column). |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +The maximum length of a filter expression is 2048 characters. |
| 81 | + |
| 82 | +## Failure Behaviour |
| 83 | + |
| 84 | +If a filter expression fails to evaluate at dispatch time (for example due to accessing a field |
| 85 | +that does not exist on the subject), the alert matches the notification regardless. |
| 86 | +This "fail-open" strategy ensures that a broken expression causes over-notification rather than |
| 87 | +silently suppressing notifications. Evaluation failures are logged as warnings. |
| 88 | + |
| 89 | +!!! warning |
| 90 | + An alert with an expression that consistently fails will behave as though |
| 91 | + it has no filter expression at all. Check the application logs for evaluation warnings |
| 92 | + if an alert appears to match more broadly than expected. |
| 93 | + |
| 94 | +## Filtering Order |
| 95 | + |
| 96 | +When a notification is dispatched, Dependency-Track evaluates the following filters in order: |
| 97 | + |
| 98 | +1. **Scope, group, and level** matching (configured on the alert). |
| 99 | +2. **Project and tag restrictions** (if the alert is limited to specific projects or tags). |
| 100 | +3. **Filter expression** (if the alert has one). |
| 101 | + |
| 102 | +The filter expression is only evaluated when the notification has already passed the preceding |
| 103 | +checks. This means that project and tag restrictions are always enforced, regardless of what the |
| 104 | +expression contains. |
| 105 | + |
| 106 | +## Examples |
| 107 | + |
| 108 | +### Only critical and high severity vulnerabilities |
| 109 | + |
| 110 | +The following expression matches `NEW_VULNERABILITY` notifications where the vulnerability |
| 111 | +severity is `CRITICAL` or `HIGH`: |
| 112 | + |
| 113 | +```js linenums="1" |
| 114 | +subject.vulnerability.severity in ["CRITICAL", "HIGH"] |
| 115 | +``` |
| 116 | + |
| 117 | +### Vulnerabilities with a CVSS v3 score above a threshold |
| 118 | + |
| 119 | +```js linenums="1" |
| 120 | +subject.vulnerability.cvss_v3 >= 7.0 |
| 121 | +``` |
| 122 | + |
| 123 | +### Notifications for projects matching a name prefix |
| 124 | + |
| 125 | +The following expression matches notifications whose subject contains a project with a name |
| 126 | +starting with `acme-`: |
| 127 | + |
| 128 | +```js linenums="1" |
| 129 | +subject.project.name.startsWith("acme-") |
| 130 | +``` |
| 131 | + |
| 132 | +!!! tip |
| 133 | + For simple project-based filtering, consider using project and tag restrictions on the |
| 134 | + alert instead. Filter expressions are more useful for content-based conditions |
| 135 | + that cannot be expressed through project or tag restrictions alone. |
| 136 | + |
| 137 | +### Vulnerabilities with a specific CWE |
| 138 | + |
| 139 | +The following expression matches `NEW_VULNERABILITY` notifications where the vulnerability |
| 140 | +has CWE-79 (Cross-site Scripting) among its CWEs: |
| 141 | + |
| 142 | +```js linenums="1" |
| 143 | +subject.vulnerability.cwes.exists(cwe, cwe.cwe_id == 79) |
| 144 | +``` |
| 145 | + |
| 146 | +### Combining multiple conditions |
| 147 | + |
| 148 | +The following expression matches `NEW_VULNERABILITY` notifications for `CRITICAL` vulnerabilities |
| 149 | +with a network attack vector in CVSSv3: |
| 150 | + |
| 151 | +```js linenums="1" |
| 152 | +subject.vulnerability.severity == "CRITICAL" |
| 153 | + && subject.vulnerability.cvss_v3_vector.matches(".*/AV:N/.*") |
| 154 | +``` |
| 155 | + |
| 156 | +### Scheduled alerts: only when critical vulnerabilities were found |
| 157 | + |
| 158 | +For scheduled alerts that produce vulnerability summaries, the subject contains |
| 159 | +an overview with vulnerability counts grouped by severity. The following expression matches |
| 160 | +only when at least one `CRITICAL` vulnerability was found in the reporting period: |
| 161 | + |
| 162 | +```js linenums="1" |
| 163 | +"CRITICAL" in subject.overview.new_vulnerabilities_count_by_severity |
| 164 | +``` |
| 165 | + |
| 166 | +### Optional field checking |
| 167 | + |
| 168 | +CEL does not have a concept of `null`. Accessing a field that is not set returns its default |
| 169 | +value (e.g. `""` for strings, `0` for numbers), which can lead to misleading matches. |
| 170 | +Use the `has()` macro to check for field presence before accessing it: |
| 171 | + |
| 172 | +```js linenums="1" |
| 173 | +has(subject.vulnerability.cvss_v3_vector) |
| 174 | + && subject.vulnerability.cvss_v3_vector.matches(".*/AV:N/.*") |
| 175 | +``` |
| 176 | + |
| 177 | +[CEL]: https://cel.dev/ |
| 178 | +[Turing-complete]: https://en.wikipedia.org/wiki/Turing_completeness |
| 179 | +[language definition]: https://github.com/google/cel-spec/blob/v0.13.0/doc/langdef.md#language-definition |
| 180 | +[macros]: https://github.com/google/cel-spec/blob/v0.13.0/doc/langdef.md#macros |
| 181 | +[protobuf-ts-docs]: https://protobuf.dev/reference/protobuf/google.protobuf/#timestamp |
0 commit comments