Skip to content

Commit 1d94b4a

Browse files
G-Rathanother-rex
andauthored
fix: use correct path separator in SARIF output when on Windows (google#1294)
Currently we always output a Unix path separator even when on Windows - this changes us to use the path separator based on the OS. Resolves google#604 Co-authored-by: Rex P <[email protected]>
1 parent 9cb6791 commit 1d94b4a

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

internal/output/sarif.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type HelpTemplateData struct {
2525
AliasedVulns []VulnDescription
2626
HasFixedVersion bool
2727
FixedVersionTable string
28+
PathSeparator string
2829
}
2930

3031
type FixedPkgTableData struct {
@@ -81,7 +82,7 @@ See the format and more options in our documentation here: https://google.github
8182
Add or append these values to the following config files to ignore this vulnerability:
8283
8384
{{range .AffectedPackagePaths -}}
84-
""{{.}}/osv-scanner.toml""
85+
""{{.}}{{$.PathSeparator}}osv-scanner.toml""
8586
8687
""""""
8788
[[IgnoredVulns]]
@@ -213,6 +214,7 @@ func createSARIFHelpText(gv *groupedSARIFFinding) string {
213214
HasFixedVersion: hasFixedVersion,
214215
FixedVersionTable: createSARIFFixedPkgTable(fixedPkgTableData).RenderMarkdown(),
215216
AffectedPackagePaths: affectedPackagePaths,
217+
PathSeparator: string(filepath.Separator),
216218
})
217219

218220
if err != nil {

internal/output/sarif_internal_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ func Test_createSARIFHelpText(t *testing.T) {
1717
{
1818
args: testutility.LoadJSONFixture[groupedSARIFFinding](t, "fixtures/vuln-grouped.json"),
1919
want: testutility.NewSnapshot().WithWindowsReplacements(map[string]string{
20-
"\\path\\to\\sub-rust-project/osv-scanner.toml": "/path/to/sub-rust-project/osv-scanner.toml",
20+
"\\path\\to\\sub-rust-project\\osv-scanner.toml": "/path/to/sub-rust-project/osv-scanner.toml",
2121
}),
2222
},
2323
{
2424
args: testutility.LoadJSONFixture[groupedSARIFFinding](t, "fixtures/commit-grouped.json"),
2525
want: testutility.NewSnapshot().WithWindowsReplacements(map[string]string{
26-
"<rootdir>\\Documents\\Project\\engine/osv-scanner.toml": "<rootdir>/Documents/Project/engine/osv-scanner.toml",
26+
"<rootdir>\\Documents\\Project\\engine\\osv-scanner.toml": "<rootdir>/Documents/Project/engine/osv-scanner.toml",
2727
}),
2828
},
2929
}

internal/output/sarif_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ func TestPrintSARIFReport(t *testing.T) {
6464
map[string]string{
6565
"lockfile:D:\\\\path\\\\to\\\\sub-rust-project\\\\Cargo.lock": "lockfile:/path/to/sub-rust-project/Cargo.lock",
6666
"lockfile:D:\\\\path\\\\to\\\\go.mod": "lockfile:/path/to/go.mod",
67-
"D:\\\\path\\\\to\\\\sub-rust-project/osv-scanner.toml": "/path/to/sub-rust-project/osv-scanner.toml",
68-
"D:\\\\path\\\\to/osv-scanner.toml": "/path/to/osv-scanner.toml",
67+
"D:\\\\path\\\\to\\\\sub-rust-project\\\\osv-scanner.toml": "/path/to/sub-rust-project/osv-scanner.toml",
68+
"D:\\\\path\\\\to\\\\osv-scanner.toml": "/path/to/osv-scanner.toml",
6969
"file:///D:/path/to": "file:///path/to",
7070
},
7171
),
@@ -100,9 +100,9 @@ func TestPrintSARIFReport_WithVulnerabilities(t *testing.T) {
100100

101101
testutility.NewSnapshot().WithWindowsReplacements(
102102
map[string]string{
103-
"path\\\\to\\\\my\\\\first/osv-scanner.toml": "path/to/my/first/osv-scanner.toml",
104-
"path\\\\to\\\\my\\\\second/osv-scanner.toml": "path/to/my/second/osv-scanner.toml",
105-
"path\\\\to\\\\my\\\\third/osv-scanner.toml": "path/to/my/third/osv-scanner.toml",
103+
"path\\\\to\\\\my\\\\first\\\\osv-scanner.toml": "path/to/my/first/osv-scanner.toml",
104+
"path\\\\to\\\\my\\\\second\\\\osv-scanner.toml": "path/to/my/second/osv-scanner.toml",
105+
"path\\\\to\\\\my\\\\third\\\\osv-scanner.toml": "path/to/my/third/osv-scanner.toml",
106106
}).MatchText(t, outputWriter.String())
107107
})
108108
}
@@ -122,9 +122,9 @@ func TestPrintSARIFReport_WithLicenseViolations(t *testing.T) {
122122

123123
testutility.NewSnapshot().WithWindowsReplacements(
124124
map[string]string{
125-
"path\\\\to\\\\my\\\\first/osv-scanner.toml": "path/to/my/first/osv-scanner.toml",
126-
"path\\\\to\\\\my\\\\second/osv-scanner.toml": "path/to/my/second/osv-scanner.toml",
127-
"path\\\\to\\\\my\\\\third/osv-scanner.toml": "path/to/my/third/osv-scanner.toml",
125+
"path\\\\to\\\\my\\\\first\\\\osv-scanner.toml": "path/to/my/first/osv-scanner.toml",
126+
"path\\\\to\\\\my\\\\second\\\\osv-scanner.toml": "path/to/my/second/osv-scanner.toml",
127+
"path\\\\to\\\\my\\\\third\\\\osv-scanner.toml": "path/to/my/third/osv-scanner.toml",
128128
}).MatchText(t, outputWriter.String())
129129
})
130130
}
@@ -144,9 +144,9 @@ func TestPrintSARIFReport_WithMixedIssues(t *testing.T) {
144144

145145
testutility.NewSnapshot().WithWindowsReplacements(
146146
map[string]string{
147-
"path\\\\to\\\\my\\\\first/osv-scanner.toml": "path/to/my/first/osv-scanner.toml",
148-
"path\\\\to\\\\my\\\\second/osv-scanner.toml": "path/to/my/second/osv-scanner.toml",
149-
"path\\\\to\\\\my\\\\third/osv-scanner.toml": "path/to/my/third/osv-scanner.toml",
147+
"path\\\\to\\\\my\\\\first\\\\osv-scanner.toml": "path/to/my/first/osv-scanner.toml",
148+
"path\\\\to\\\\my\\\\second\\\\osv-scanner.toml": "path/to/my/second/osv-scanner.toml",
149+
"path\\\\to\\\\my\\\\third\\\\osv-scanner.toml": "path/to/my/third/osv-scanner.toml",
150150
}).MatchText(t, outputWriter.String())
151151
})
152152
}

0 commit comments

Comments
 (0)