Skip to content

Commit

Permalink
fix: SARIF format require issue column >= 1 (#4775)
Browse files Browse the repository at this point in the history
Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
  • Loading branch information
Zxilly and ldez authored May 30, 2024
1 parent b9a67e6 commit e1a8055
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 4 additions & 2 deletions pkg/printers/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ func (p Sarif) Print(issues []result.Issue) error {
PhysicalLocation: sarifPhysicalLocation{
ArtifactLocation: sarifArtifactLocation{URI: issue.FilePath()},
Region: sarifRegion{
StartLine: issue.Line(),
StartColumn: issue.Column(),
StartLine: issue.Line(),
// If startColumn is absent, it SHALL default to 1.
// https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/sarif-v2.1.0-errata01-os-complete.html#_Toc141790941
StartColumn: max(1, issue.Column()),
},
},
},
Expand Down
12 changes: 11 additions & 1 deletion pkg/printers/sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ func TestSarif_Print(t *testing.T) {
Column: 5,
},
},
{
FromLinter: "linter-c",
Severity: "error",
Text: "some issue without column",
Pos: token.Position{
Filename: "path/to/filed.go",
Offset: 3,
Line: 11,
},
},
}

buf := new(bytes.Buffer)
Expand All @@ -60,7 +70,7 @@ func TestSarif_Print(t *testing.T) {
err := printer.Print(issues)
require.NoError(t, err)

expected := `{"version":"2.1.0","$schema":"https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.6.json","runs":[{"tool":{"driver":{"name":"golangci-lint"}},"results":[{"ruleId":"linter-a","level":"warning","message":{"text":"some issue"},"locations":[{"physicalLocation":{"artifactLocation":{"uri":"path/to/filea.go","index":0},"region":{"startLine":10,"startColumn":4}}}]},{"ruleId":"linter-b","level":"error","message":{"text":"another issue"},"locations":[{"physicalLocation":{"artifactLocation":{"uri":"path/to/fileb.go","index":0},"region":{"startLine":300,"startColumn":9}}}]},{"ruleId":"linter-a","level":"error","message":{"text":"some issue 2"},"locations":[{"physicalLocation":{"artifactLocation":{"uri":"path/to/filec.go","index":0},"region":{"startLine":11,"startColumn":5}}}]}]}]}
expected := `{"version":"2.1.0","$schema":"https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.6.json","runs":[{"tool":{"driver":{"name":"golangci-lint"}},"results":[{"ruleId":"linter-a","level":"warning","message":{"text":"some issue"},"locations":[{"physicalLocation":{"artifactLocation":{"uri":"path/to/filea.go","index":0},"region":{"startLine":10,"startColumn":4}}}]},{"ruleId":"linter-b","level":"error","message":{"text":"another issue"},"locations":[{"physicalLocation":{"artifactLocation":{"uri":"path/to/fileb.go","index":0},"region":{"startLine":300,"startColumn":9}}}]},{"ruleId":"linter-a","level":"error","message":{"text":"some issue 2"},"locations":[{"physicalLocation":{"artifactLocation":{"uri":"path/to/filec.go","index":0},"region":{"startLine":11,"startColumn":5}}}]},{"ruleId":"linter-c","level":"error","message":{"text":"some issue without column"},"locations":[{"physicalLocation":{"artifactLocation":{"uri":"path/to/filed.go","index":0},"region":{"startLine":11,"startColumn":1}}}]}]}]}
`

assert.Equal(t, expected, buf.String())
Expand Down

0 comments on commit e1a8055

Please sign in to comment.