Skip to content

Commit 5c01dfe

Browse files
harcheclaude
andcommitted
Fix path traversal vulnerabilities in update-readme tool
Add input validation to prevent path traversal attacks in the update-readme internal tool: - Clean file path using filepath.Clean to remove path traversal sequences - Validate that only README.md files can be updated - Add argument count validation This fixes Snyk code scan findings: - MEDIUM severity path traversal in os.ReadFile (line 28) - MEDIUM severity path traversal in os.WriteFile (line 84) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 7107a24 commit 5c01dfe

File tree

1 file changed

+15
-2
lines changed
  • internal/tools/update-readme

1 file changed

+15
-2
lines changed

internal/tools/update-readme/main.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"maps"
77
"os"
8+
"path/filepath"
89
"slices"
910
"strings"
1011

@@ -25,7 +26,19 @@ func (o *OpenShift) IsOpenShift(ctx context.Context) bool {
2526
var _ internalk8s.Openshift = (*OpenShift)(nil)
2627

2728
func main() {
28-
readme, err := os.ReadFile(os.Args[1])
29+
if len(os.Args) < 2 {
30+
panic("Usage: update-readme <path-to-readme>")
31+
}
32+
33+
// Sanitize the file path to prevent path traversal
34+
readmePath := filepath.Clean(os.Args[1])
35+
36+
// Validate that the file path is README.md to prevent arbitrary file access
37+
if filepath.Base(readmePath) != "README.md" {
38+
panic("Error: This tool can only update README.md files")
39+
}
40+
41+
readme, err := os.ReadFile(readmePath)
2942
if err != nil {
3043
panic(err)
3144
}
@@ -81,7 +94,7 @@ func main() {
8194
toolsetTools.String(),
8295
)
8396

84-
if err := os.WriteFile(os.Args[1], []byte(updated), 0o644); err != nil {
97+
if err := os.WriteFile(readmePath, []byte(updated), 0o644); err != nil {
8598
panic(err)
8699
}
87100
}

0 commit comments

Comments
 (0)