Skip to content

Commit ba0f554

Browse files
Martin Jamboncandrews
andauthored
fix(sarif): helpUri fallback to source-rule-url (was #10891) (#10934)
This is contribution by @candrews. When exporting Semgrep findings to SARIF, the `metadata.source-rule-url` field of the Semgrep rule is now used as a fallback to populate the `helpURI` field in the output if `metadata.source` isn't available. --------- Co-authored-by: Craig Andrews <candrews@integralblue.com>
1 parent 907b549 commit ba0f554

18 files changed

Lines changed: 242 additions & 290 deletions

File tree

changelog.d/gh-10891.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
In a Semgrep rule's `metadata` section, two fields may provide URLs:
2+
3+
- `source`: populated dynamically by the Semgrep registry serving the rule, it's a URL that
4+
offers information about the rule.
5+
- `source-rule-url`: optional string, a URL for the source of inspiration for the rule.
6+
7+
The SARIF format supports only one URL under the field `helpUri`.
8+
Previously, Semgrep populated the SARIF `helpUri` field only with `metadata.source`.
9+
This fix is to use `metadata.source` if available, otherwise falling back to `metadata.source-rule-url`.
10+
11+
Contributed by @candrews.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This test rule is for checking that the SARIF output contains
2+
# the URL provided by metadata.source-rule-url
3+
rules:
4+
- id: source-rule-url
5+
pattern: print(...)
6+
message: "found"
7+
languages: [python]
8+
severity: ERROR
9+
metadata:
10+
source-rule-url: https://example.com/print
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This test rule is for checking that the SARIF output contains
2+
# the URL provided by metadata.source
3+
rules:
4+
- id: source-url
5+
pattern: print(...)
6+
message: "found"
7+
languages: [python]
8+
severity: ERROR
9+
metadata:
10+
source-rule-url: https://example.com/incorrect-url
11+
# This URL should be preferred:
12+
source: https://example.com/correct-url

cli/tests/default/e2e/snapshots/test_output_sarif/test_sarif_output/False-rule_and_target3/results.sarif

Lines changed: 0 additions & 74 deletions
This file was deleted.

cli/tests/default/e2e/snapshots/test_output_sarif/test_sarif_output/True-rule_and_target3/results.sarif

Lines changed: 0 additions & 212 deletions
This file was deleted.

cli/tests/default/e2e/snapshots/test_output_sarif/test_sarif_output/False-rule_and_target1/results.sarif renamed to cli/tests/default/e2e/snapshots/test_output_sarif/test_sarif_output/notrace-cwe_tag/results.sarif

File renamed without changes.

cli/tests/default/e2e/snapshots/test_output_sarif/test_sarif_output/False-rule_and_target0/results.sarif renamed to cli/tests/default/e2e/snapshots/test_output_sarif/test_sarif_output/notrace-eqeq/results.sarif

File renamed without changes.

cli/tests/default/e2e/snapshots/test_output_sarif/test_sarif_output/False-rule_and_target2/results.sarif renamed to cli/tests/default/e2e/snapshots/test_output_sarif/test_sarif_output/notrace-metavariable_type/results.sarif

File renamed without changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"$schema": "https://docs.oasis-open.org/sarif/sarif/v2.1.0/os/schemas/sarif-schema-2.1.0.json",
3+
"runs": [
4+
{
5+
"invocations": [
6+
{
7+
"executionSuccessful": true,
8+
"toolExecutionNotifications": []
9+
}
10+
],
11+
"results": [],
12+
"tool": {
13+
"driver": {
14+
"name": "Semgrep OSS",
15+
"rules": [
16+
{
17+
"defaultConfiguration": {
18+
"level": "error"
19+
},
20+
"fullDescription": {
21+
"text": "found"
22+
},
23+
"help": {
24+
"markdown": "found\n\n<b>References:</b>\n - [Semgrep Rule](https://example.com/print)\n",
25+
"text": "found"
26+
},
27+
"helpUri": "https://example.com/print",
28+
"id": "rules.source-rule-url",
29+
"name": "rules.source-rule-url",
30+
"properties": {
31+
"precision": "very-high",
32+
"tags": []
33+
},
34+
"shortDescription": {
35+
"text": "Semgrep Finding: rules.source-rule-url"
36+
}
37+
}
38+
],
39+
"semanticVersion": "<MASKED>"
40+
}
41+
}
42+
}
43+
],
44+
"version": "2.1.0"
45+
}

0 commit comments

Comments
 (0)