Skip to content
Open
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
9 changes: 9 additions & 0 deletions internal/handlers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package httphandlers

import (
"strings"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/poyrazk/thecloud/internal/errors"
Expand Down Expand Up @@ -53,5 +55,12 @@ func getBucketAndKeyRequired(c *gin.Context) (bucket, key string, ok bool) {
return "", "", false
}

// Prevent path traversal attacks (e.g., "../../../etc/passwd")
// Note: Gin glob pattern *key captures the path with leading /, so we only check for ".."
if strings.Contains(key, "..") {
httputil.Error(c, errors.New(errors.InvalidInput, "invalid key"))
return "", "", false
}

return bucket, key, true
}
24 changes: 24 additions & 0 deletions internal/handlers/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,28 @@ func TestGetBucketAndKeyRequired(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, w.Body.String(), "key is required")
})

t.Run("path traversal rejected", func(t *testing.T) {
traversalKeys := []string{
"../etc/passwd",
"foo/../bar",
"foo\\..\\bar",
"foo/../../etc/passwd",
}
for _, key := range traversalKeys {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Params = []gin.Param{
{Key: "bucket", Value: testBucket},
{Key: "key", Value: key},
}

bucket, key, ok := getBucketAndKeyRequired(c)

assert.False(t, ok, "key %q should be rejected", key)
assert.Empty(t, bucket)
assert.Empty(t, key)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
})
}
Loading