Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

Fixed CWE-22 Path Traversal vulnerability

Matt Roberts3e52dcc47a75
diff --git a/app/services/blob/blob.go b/app/services/blob/blob.go
index 07548ddf7..d3ee7e9be 100644
--- a/app/services/blob/blob.go
+++ b/app/services/blob/blob.go
@@ -34,6 +34,10 @@ func ValidateKey(key string) error {
if strings.HasPrefix(key, "/") || strings.HasSuffix(key, "/") {
return ErrInvalidKeyFormat
}
+ // Prevent path traversal attacks by rejecting keys with ".." segments
+ if strings.Contains(key, "..") {
+ return ErrInvalidKeyFormat
+ }
return nil
}
diff --git a/app/services/blob/blob_test.go b/app/services/blob/blob_test.go
index 8f7c9a241..3c3b841bb 100644
--- a/app/services/blob/blob_test.go
+++ b/app/services/blob/blob_test.go
@@ -65,6 +65,7 @@ var tests = []struct {
{"AllOperations", AllOperations},
{"DeleteUnkownFile", DeleteUnkownFile},
{"KeyFormats", KeyFormats},
+ {"PathTraversalOnRead", PathTraversalOnRead},
{"SameKey_DifferentTenant", SameKey_DifferentTenant},
{"SameKey_DifferentTenant_Delete", SameKey_DifferentTenant_Delete},
{"ListBlobsFromTenant", ListBlobsFromTenant},
@@ -303,6 +304,24 @@ func ListUnauthorizedBlobs(ctx context.Context) {
Expect(err).IsNil()
}
+func PathTraversalOnRead(ctx context.Context) {
+ // Test that path traversal attempts are blocked on read operations
+ traversalKeys := []string{
+ "../file.txt",
+ "../../etc/passwd",
+ "path/../file.txt",
+ "path/../../file.txt",
+ "../../../etc/passwd",
+ }
+
+ for _, key := range traversalKeys {
+ q := &query.GetBlobByKey{Key: key}
+ err := bus.Dispatch(ctx, q)
+ Expect(errors.Cause(err)).Equals(blob.ErrInvalidKeyFormat)
+ Expect(q.Result).IsNil()
+ }
+}
+
func KeyFormats(ctx context.Context) {
testCases := []struct {
key string
@@ -340,6 +359,26 @@ func KeyFormats(ctx context.Context) {
key: rand.String(513),
valid: false,
},
+ {
+ key: "../file.txt",
+ valid: false,
+ },
+ {
+ key: "path/../file.txt",
+ valid: false,
+ },
+ {
+ key: "../../etc/passwd",
+ valid: false,
+ },
+ {
+ key: "path/../../file.txt",
+ valid: false,
+ },
+ {
+ key: "path/to/../../file.txt",
+ valid: false,
+ },
}
for _, testCase := range testCases {
diff --git a/app/services/blob/fs/fs.go b/app/services/blob/fs/fs.go
index 9f22d61ea..1dbc46656 100644
--- a/app/services/blob/fs/fs.go
+++ b/app/services/blob/fs/fs.go
@@ -73,6 +73,10 @@ func listBlobs(ctx context.Context, q *query.ListBlobs) error {
}
func getBlobByKey(ctx context.Context, q *query.GetBlobByKey) error {
+ if err := blob.ValidateKey(q.Key); err != nil {
+ return errors.Wrap(err, "failed to validate blob key '%s'", q.Key)
+ }
+
fullPath := keyFullPath(ctx, q.Key)
stats, err := os.Stat(fullPath)
if err != nil {
diff --git a/app/services/blob/s3/s3.go b/app/services/blob/s3/s3.go
index 87c41856c..b04421251 100644
--- a/app/services/blob/s3/s3.go
+++ b/app/services/blob/s3/s3.go
@@ -107,6 +107,10 @@ func listBlobs(ctx context.Context, q *query.ListBlobs) error {
}
func getBlobByKey(ctx context.Context, q *query.GetBlobByKey) error {
+ if err := blob.ValidateKey(q.Key); err != nil {
+ return wrap(err, "failed to validate blob key '%s'", q.Key)
+ }
+
resp, err := DefaultClient.GetObjectWithContext(ctx, &s3.GetObjectInput{
Bucket: aws.String(env.Config.BlobStorage.S3.BucketName),
Key: aws.String(keyFullPathURL(ctx, q.Key)),
diff --git a/app/services/blob/sql/sql.go b/app/services/blob/sql/sql.go
index f85c147d0..37f6baa70 100644
--- a/app/services/blob/sql/sql.go
+++ b/app/services/blob/sql/sql.go
@@ -81,6 +81,10 @@ func listBlobs(ctx context.Context, q *query.ListBlobs) error {
}
func getBlobByKey(ctx context.Context, q *query.GetBlobByKey) error {
+ if err := blob.ValidateKey(q.Key); err != nil {
+ return errors.Wrap(err, "failed to validate blob key '%s'", q.Key)
+ }
+
blob.EnsureAuthorizedPrefix(ctx, q.Key)
return using(ctx, func(tenantID sql.NullInt64) error {