Skip to content

Commit e1a8055

Browse files
Zxillyldez
andauthored
fix: SARIF format require issue column >= 1 (#4775)
Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent b9a67e6 commit e1a8055

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

pkg/printers/sarif.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ func (p Sarif) Print(issues []result.Issue) error {
8989
PhysicalLocation: sarifPhysicalLocation{
9090
ArtifactLocation: sarifArtifactLocation{URI: issue.FilePath()},
9191
Region: sarifRegion{
92-
StartLine: issue.Line(),
93-
StartColumn: issue.Column(),
92+
StartLine: issue.Line(),
93+
// If startColumn is absent, it SHALL default to 1.
94+
// https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/sarif-v2.1.0-errata01-os-complete.html#_Toc141790941
95+
StartColumn: max(1, issue.Column()),
9496
},
9597
},
9698
},

pkg/printers/sarif_test.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ func TestSarif_Print(t *testing.T) {
5151
Column: 5,
5252
},
5353
},
54+
{
55+
FromLinter: "linter-c",
56+
Severity: "error",
57+
Text: "some issue without column",
58+
Pos: token.Position{
59+
Filename: "path/to/filed.go",
60+
Offset: 3,
61+
Line: 11,
62+
},
63+
},
5464
}
5565

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

63-
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}}}]}]}]}
73+
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}}}]}]}]}
6474
`
6575

6676
assert.Equal(t, expected, buf.String())

0 commit comments

Comments
 (0)