How code scanning decides two results are the same#
A scanner reports results, and the code scanning service decides which results are the same alert across runs. GitHub's SARIF support reference describes the key: the service "uses fingerprints to match results across various runs so they only appear once in the latest run for the selected branch. This makes it possible to match alerts to the correct line of code when files are edited. The ruleId for a result has to be the same across analysis."
The fingerprint lives in a result's partialFingerprints property, and the same page notes that the service "only uses the primaryLocationLineHash." Many tools do not write that field. When it is missing, the upload-sarif action computes it from the source file before upload. The action's fingerprinting code at v4.39.5 says what goes into it: "The hash is computed using the first BLOCK_SIZE non-space/tab characters counted from the start of the line," with BLOCK_SIZE set to 100. The hash depends on the text at and after the flagged line, not on the line number.
The path is a separate input. The reference says: "The filepath has to be consistent across the runs to enable a computation of a stable fingerprint. If the filepaths differ for the same result, each time there is a new analysis a new alert will be created, and the old one will be closed."
Those two rules predict the behaviour of an alert under each kind of edit. Lines inserted above the finding keep the hash and the path, so the alert follows the line. A file moved without edits keeps the hash and changes the path, so the old alert closes and a new one opens. An edit to the flagged text, or to the text just after it, changes the hash.
Reproduce the re-keying with local SARIF#
The example below is constructed. It uses semgrep 1.187.0 with a single-pattern local rule that matches subprocess calls with shell=True, run on a small Git repository:
# app/tasks.py
import subprocess
def push(tag):
return subprocess.call("docker push web:" + tag, shell=True)
Semgrep's SARIF output gives the rule, the path and the line, and leaves the fingerprint to the uploader. Without a login, its own match fingerprint is a placeholder:
$ semgrep scan --metrics=off --disable-version-check --config rule.yml --sarif -q . > s1.sarif
$ jq '.runs[0].results[0] | {ruleId, fingerprints, uri: .locations[0].physicalLocation.artifactLocation.uri, startLine: .locations[0].physicalLocation.region.startLine}' s1.sarif
{
"ruleId": "subprocess-shell-true",
"fingerprints": {
"matchBasedId/v1": "requires login"
},
"uri": "app/tasks.py",
"startLine": 5
}
To see the key the upload would compute, the hash function from fingerprints.ts was copied into a short Node.js script, fp.mjs, that reads each SARIF result and prints primaryLocationLineHash for its line. Three commits then change the file's position, not its flagged text:
$ node fp.mjs s1.sarif . # commit "base"
subprocess-shell-true app/tasks.py:5 primaryLocationLineHash=e8eac6b5e1fbfa01:1
$ node fp.mjs s2.sarif . # commit "shift": three lines added above (an import, a blank line, a logger)
subprocess-shell-true app/tasks.py:8 primaryLocationLineHash=e8eac6b5e1fbfa01:1
$ node fp.mjs s3.sarif . # commit "move": git mv app/tasks.py app/deploy/tasks.py
subprocess-shell-true app/deploy/tasks.py:8 primaryLocationLineHash=e8eac6b5e1fbfa01:1
The line moved from 5 to 8 and the hash stayed, so the "shift" commit is the case the fingerprint exists for: the alert stays open on its new line. After the "move" commit the hash is still identical, but the path changed. Under the documented rule, that upload closes the alert on app/tasks.py and opens a new one on app/deploy/tasks.py, which is how a refactor produces a "new" finding nobody wrote.
Prove it is the same finding#
The evidence that a new alert is a re-keyed old one is the history of the flagged line. Git shows a pure move as a 100% rename:
$ git diff -M --summary 47b3bf1 34bd9c9
rename app/{ => deploy}/tasks.py (100%)
$ git log --follow --oneline -- app/deploy/tasks.py
34bd9c9 move
47b3bf1 shift
44a8720 base
Collect three facts: the rename (or a diff that leaves the flagged line's text unchanged), the same ruleId, and the same primaryLocationLineHash computed on both sides. Then dismiss the new alert as a duplicate and link the closed alert and the commit that moved the file, so the decision made on the old alert carries over. If the old alert was open, the work is not new either: the finding has been in the code since before the move, and its age, for any policy that measures one, is the old alert's age.
The reverse also matters. A closed alert on the old path is not a fix. If the old alert shows as closed or fixed in the same analysis that opened the new one, read it as a path change.
When the move carried new code#
The same refactor can change the flagged code. Here the move also changed push to take a registry from the caller:
$ git diff 47b3bf1:app/tasks.py d5a6fbe:app/deploy/tasks.py
...
-def push(tag):
- return subprocess.call("docker push web:" + tag, shell=True)
+def push(registry, tag):
+ return subprocess.call(f"docker push {registry}/web:{tag}", shell=True)
$ git diff -M --summary 47b3bf1 d5a6fbe
create mode 100644 app/deploy/tasks.py
delete mode 100644 app/tasks.py
$ node fp.mjs s4.sarif .
subprocess-shell-true app/deploy/tasks.py:8 primaryLocationLineHash=4e50981ccd8d5450:1
Git no longer pairs the files as a rename, and the hash differs from e8eac6b5e1fbfa01:1. Both changes point the same way: this is a new instance at the same place, and the change in it matters to the finding. The command now interpolates a second value that comes from the caller, so the review has to establish where registry comes from. Dismissing it as a duplicate of the old alert would carry over a decision that was made about different code.
A changed hash alone is weaker evidence than it looks. Because the hash covers 100 non-space characters starting at the flagged line, an edit below the line can change it while the flagged code stays identical. Appending an unrelated two-line function after push in the moved file changed the hash while line 8 stayed untouched. When the hash changed but the diff of the flagged statement is empty, treat the alert as the old finding; when the statement itself changed, treat it as new.
Decide from the history of the flagged line#
| What the scan shows | Evidence to collect | Decision |
|---|---|---|
| New alert, old alert closed in the same analysis, file renamed | git diff -M --summary showing a rename, same ruleId, same line hash | Dismiss as a duplicate; link the old alert and the moving commit |
| Same alert, different line number | Lines added or removed above it | No action; the fingerprint tracked it |
| New alert after a move, flagged statement edited | The diff of the flagged statement | Review as a new finding |
| New alert, hash changed, statement unchanged | A diff showing only nearby lines changed | Treat as the old finding; link it |
| Duplicate alerts on every upload for the same file | Whether the uploaded paths differ between runs (absolute paths, checkout directory) | Fix the upload paths before triaging alerts |
The fields used here are covered in detail in the guide to SARIF result fields. A finding that vanished after a refactor raises the opposite question, which the guide to findings that disappeared after a refactor answers.