Skip to content

Commit 60d7db6

Browse files
authored
Use CompilerError formatting for zizmor security scanner output (#2667)
1 parent cf30d7f commit 60d7db6

2 files changed

Lines changed: 46 additions & 23 deletions

File tree

β€Žpkg/cli/zizmor.goβ€Ž

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,33 +178,61 @@ func parseAndDisplayZizmorOutput(stdout, stderr string, verbose bool) (int, erro
178178
continue
179179
}
180180

181-
// Format: 🌈 zizmor xx warnings in <filepath>
182-
warningText := "warnings"
183-
if count == 1 {
184-
warningText = "warning"
181+
// Read file content for context display
182+
fileContent, err := os.ReadFile(filePath)
183+
var fileLines []string
184+
if err == nil {
185+
fileLines = strings.Split(string(fileContent), "\n")
185186
}
186-
fmt.Fprintf(os.Stderr, "🌈 zizmor %d %s in %s\n", count, warningText, filePath)
187187

188-
// Display detailed findings
188+
// Display detailed findings using CompilerError format
189189
for _, finding := range findings {
190190
severity := finding.Determinations.Severity
191191
ident := finding.Ident
192192
desc := finding.Desc
193193

194194
// Find the primary location (first location in the list)
195-
var locationInfo string
196195
if len(finding.Locations) > 0 {
197196
loc := finding.Locations[0]
198197
row := loc.Concrete.Location.StartPoint.Row
199198
col := loc.Concrete.Location.StartPoint.Column
200199
// Zizmor uses 0-based indexing, convert to 1-based for user display
201-
if row > 0 || col > 0 {
202-
locationInfo = fmt.Sprintf(" at line %d, column %d", row+1, col+1)
200+
lineNum := row + 1
201+
colNum := col + 1
202+
203+
// Create context lines around the error
204+
var context []string
205+
if len(fileLines) > 0 && lineNum > 0 && lineNum <= len(fileLines) {
206+
startLine := max(1, lineNum-2)
207+
endLine := min(len(fileLines), lineNum+2)
208+
209+
for i := startLine; i <= endLine; i++ {
210+
if i-1 < len(fileLines) {
211+
context = append(context, fileLines[i-1])
212+
}
213+
}
214+
}
215+
216+
// Map severity to error type
217+
errorType := "warning"
218+
if severity == "High" || severity == "Critical" {
219+
errorType = "error"
203220
}
204-
}
205221

206-
errorMsg := fmt.Sprintf(" - [%s] %s%s: %s", severity, ident, locationInfo, desc)
207-
fmt.Fprintln(os.Stderr, console.FormatErrorMessage(errorMsg))
222+
// Create and format CompilerError
223+
compilerErr := console.CompilerError{
224+
Position: console.ErrorPosition{
225+
File: filePath,
226+
Line: lineNum,
227+
Column: colNum,
228+
},
229+
Type: errorType,
230+
Message: fmt.Sprintf("[%s] %s: %s", severity, ident, desc),
231+
Context: context,
232+
}
233+
234+
fmt.Fprint(os.Stderr, console.FormatError(compilerErr))
235+
}
208236
}
209237
}
210238

β€Žpkg/cli/zizmor_test.goβ€Ž

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ func TestParseAndDisplayZizmorOutput(t *testing.T) {
5050
]`,
5151
stderr: " INFO audit: zizmor: 🌈 completed ./.github/workflows/test.lock.yml\n",
5252
expectedOutput: []string{
53-
"🌈 zizmor 1 warning in ./.github/workflows/test.lock.yml",
54-
"βœ— - [Medium] excessive-permissions at line 7, column 5: overly broad permissions",
53+
"./.github/workflows/test.lock.yml:7:5: warning: [Medium] excessive-permissions: overly broad permissions",
5554
},
5655
expectError: false,
5756
},
@@ -117,9 +116,8 @@ func TestParseAndDisplayZizmorOutput(t *testing.T) {
117116
]`,
118117
stderr: " INFO audit: zizmor: 🌈 completed ./.github/workflows/test.lock.yml\n",
119118
expectedOutput: []string{
120-
"🌈 zizmor 2 warnings in ./.github/workflows/test.lock.yml",
121-
"βœ— - [Medium] excessive-permissions at line 7, column 5: overly broad permissions",
122-
"βœ— - [High] template-injection at line 12, column 24: template injection with untrusted input",
119+
"./.github/workflows/test.lock.yml:7:5: warning: [Medium] excessive-permissions: overly broad permissions",
120+
"./.github/workflows/test.lock.yml:12:24: error: [High] template-injection: template injection with untrusted input",
123121
},
124122
expectError: false,
125123
},
@@ -194,10 +192,8 @@ func TestParseAndDisplayZizmorOutput(t *testing.T) {
194192
]`,
195193
stderr: " INFO audit: zizmor: 🌈 completed ./.github/workflows/test1.lock.yml\n INFO audit: zizmor: 🌈 completed ./.github/workflows/test2.lock.yml\n",
196194
expectedOutput: []string{
197-
"🌈 zizmor 1 warning in ./.github/workflows/test1.lock.yml",
198-
"βœ— - [Medium] excessive-permissions at line 7, column 5: overly broad permissions",
199-
"🌈 zizmor 1 warning in ./.github/workflows/test2.lock.yml",
200-
"βœ— - [High] template-injection at line 12, column 24: template injection with untrusted input",
195+
"./.github/workflows/test1.lock.yml:7:5: warning: [Medium] excessive-permissions: overly broad permissions",
196+
"./.github/workflows/test2.lock.yml:12:24: error: [High] template-injection: template injection with untrusted input",
201197
},
202198
expectError: false,
203199
},
@@ -253,8 +249,7 @@ func TestParseAndDisplayZizmorOutput(t *testing.T) {
253249
]`,
254250
stderr: " INFO audit: zizmor: 🌈 completed ./.github/workflows/test.lock.yml\n",
255251
expectedOutput: []string{
256-
"🌈 zizmor 1 warning in ./.github/workflows/test.lock.yml",
257-
"βœ— - [Medium] excessive-permissions at line 7, column 5: overly broad permissions",
252+
"./.github/workflows/test.lock.yml:7:5: warning: [Medium] excessive-permissions: overly broad permissions",
258253
},
259254
expectError: false,
260255
},

0 commit comments

Comments
Β (0)