Skip to content

Commit b917261

Browse files
committed
chore/security : re-clean
1 parent eabc054 commit b917261

1 file changed

Lines changed: 22 additions & 75 deletions

File tree

Lines changed: 22 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,40 @@
11
name: 'Fix Trivy SARIF File Paths'
2-
description: 'Dynamically fixes file paths in Trivy SARIF output for proper GitHub Security tab display'
2+
description: 'Fixes file paths in Trivy SARIF output for GitHub Security tab'
33
inputs:
44
sarif-file:
55
description: 'Path to the SARIF file to fix'
66
required: true
77
scan-path:
88
description: 'The relative path that was scanned (e.g., web/lrm/client)'
99
required: true
10-
backup:
11-
description: 'Create a backup of the original SARIF file'
12-
required: false
13-
default: 'true'
1410

1511
runs:
1612
using: 'composite'
1713
steps:
18-
- name: Fix SARIF file paths dynamically
14+
- name: Fix SARIF file paths
1915
shell: bash
2016
run: |
21-
echo "=== Dynamically fixing SARIF file paths ==="
2217
SARIF_FILE="${{ inputs.sarif-file }}"
2318
SCAN_PATH="${{ inputs.scan-path }}"
24-
CREATE_BACKUP="${{ inputs.backup }}"
2519
26-
if [ -f "$SARIF_FILE" ]; then
27-
echo "Found SARIF file: $SARIF_FILE"
28-
echo "Scan path prefix: $SCAN_PATH"
29-
30-
# Create backup if requested
31-
if [ "$CREATE_BACKUP" = "true" ]; then
32-
BACKUP_FILE="${SARIF_FILE%.sarif}-original.sarif"
33-
cp "$SARIF_FILE" "$BACKUP_FILE"
34-
echo "Backup created: $BACKUP_FILE"
35-
fi
36-
37-
# Use jq to dynamically fix all file paths in the SARIF
38-
jq --arg prefix "${SCAN_PATH}/" '
39-
# Fix artifact locations in results
40-
(.runs[]?.results[]?.locations[]?.physicalLocation?.artifactLocation?.uri?) |=
41-
if . and (startswith($prefix) | not) and (. != "") then
42-
if startswith("file://") then
43-
"file://" + $prefix + (. | sub("^file://"; ""))
44-
else
45-
$prefix + .
46-
end
47-
else
48-
.
49-
end |
50-
51-
# Fix artifact locations in artifacts array
52-
(.runs[]?.artifacts[]?.location?.uri?) |=
53-
if . and (startswith($prefix) | not) and (. != "") then
54-
if startswith("file://") then
55-
"file://" + $prefix + (. | sub("^file://"; ""))
56-
else
57-
$prefix + .
58-
end
59-
else
60-
.
61-
end |
62-
63-
# Fix any other uri references in the SARIF structure
64-
(.runs[]?.tool?.driver?.rules[]?.properties?.uri?) |=
65-
if . and (startswith($prefix) | not) and (. != "") then
66-
if startswith("file://") then
67-
"file://" + $prefix + (. | sub("^file://"; ""))
68-
else
69-
$prefix + .
70-
end
71-
else
72-
.
73-
end
74-
' "$SARIF_FILE" > "${SARIF_FILE}.tmp" && mv "${SARIF_FILE}.tmp" "$SARIF_FILE"
75-
76-
echo "✅ SARIF file paths updated dynamically with prefix: ${SCAN_PATH}/"
77-
78-
# Show before/after comparison if backup exists
79-
if [ "$CREATE_BACKUP" = "true" ] && [ -f "$BACKUP_FILE" ]; then
80-
echo ""
81-
echo "Before fix (first few file references):"
82-
jq -r '.runs[]?.results[]?.locations[]?.physicalLocation?.artifactLocation?.uri?' "$BACKUP_FILE" 2>/dev/null | grep -v "^null$" | head -5 || echo "No file references found in original"
83-
echo ""
84-
echo "After fix (first few file references):"
85-
jq -r '.runs[]?.results[]?.locations[]?.physicalLocation?.artifactLocation?.uri?' "$SARIF_FILE" 2>/dev/null | grep -v "^null$" | head -5 || echo "No file references found in fixed"
86-
fi
87-
88-
echo ""
89-
echo "✅ SARIF file paths have been fixed for GitHub Security tab compatibility"
90-
else
91-
echo "❌ SARIF file not found: $SARIF_FILE"
20+
if [ ! -f "$SARIF_FILE" ]; then
21+
echo "::error::SARIF file not found: $SARIF_FILE"
9222
exit 1
93-
fi
23+
fi
24+
25+
echo "Fixing SARIF file paths with prefix: ${SCAN_PATH}/"
26+
27+
# Fix all URI paths in the SARIF file
28+
jq --arg prefix "${SCAN_PATH}/" '
29+
def fix_uri:
30+
if . and (startswith($prefix) | not) and (. != "") then
31+
$prefix + .
32+
else
33+
.
34+
end;
35+
36+
(.runs[]?.results[]?.locations[]?.physicalLocation?.artifactLocation?.uri?) |= fix_uri |
37+
(.runs[]?.artifacts[]?.location?.uri?) |= fix_uri
38+
' "$SARIF_FILE" > "${SARIF_FILE}.tmp" && mv "${SARIF_FILE}.tmp" "$SARIF_FILE"
39+
40+
echo "✅ SARIF file paths fixed successfully"

0 commit comments

Comments
 (0)