Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions backend/internal/api/handlers/crowdsec_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,12 @@ func TestPathWithin(t *testing.T) {
assert.False(t, pathWithin("/a/b", "/a"))
assert.False(t, pathWithin("/a/b", "/x/y"))
assert.False(t, pathWithin("/a/b", "relative"))
// A name that merely starts with two dots is inside the folder; real parent traversal is not.
assert.True(t, pathWithin("/a/b", "/a/b/..foo"))
assert.True(t, pathWithin("/a/b", "/a/b/..foo/bar"))
assert.False(t, pathWithin("/a/b", "/a/b/../c"))
assert.False(t, pathWithin("/a/b", "/a/b/../../etc/passwd"))
assert.False(t, pathWithin("/a/b", "/a/b_sibling/config.yaml"))
}

func TestCrowdsecFiles_Read_UnreadableFileIs500(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions backend/internal/api/handlers/crowdsec_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2216,7 +2216,7 @@ func (h *CrowdsecHandler) DiagnosticsConfig(c *gin.Context) {
if configPath := crowdsec.FindConfigFile(h.DataDir); configPath != "" {
// Path traversal protection: ensure path is within DataDir
cleanConfigPath = filepath.Clean(configPath)
if !strings.HasPrefix(cleanConfigPath, filepath.Clean(h.DataDir)) {
if !pathWithin(filepath.Clean(h.DataDir), cleanConfigPath) {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid config path"})
return
}
Expand Down Expand Up @@ -2262,7 +2262,7 @@ func (h *CrowdsecHandler) DiagnosticsConfig(c *gin.Context) {
cleanDataDir := filepath.Clean(h.DataDir)
// Path traversal protection
cleanAcquisPath := filepath.Clean(acquisPath)
if !strings.HasPrefix(cleanAcquisPath, cleanDataDir) {
if !pathWithin(cleanDataDir, cleanAcquisPath) {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid acquis path"})
return
}
Expand Down
Loading