1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
|
package s3api
import (
"net/http/httptest"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
"github.com/stretchr/testify/assert"
)
func TestListObjectsHandler(t *testing.T) {
// https://docs.aws.amazon.com/AmazonS3/latest/API/v2-RESTBucketGET.html
expected := `<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult><Name>test_container</Name><Prefix></Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>1.zip</Key><ETag>"4397da7a7649e8085de9916c240e8166"</ETag><Size>1234567</Size><Owner><ID>65a011niqo39cdf8ec533ec3d1ccaafsa932</ID></Owner><StorageClass>STANDARD</StorageClass><LastModified>2011-04-09T12:34:49Z</LastModified></Contents><EncodingType></EncodingType></ListBucketResult>`
response := ListBucketResult{
Name: "test_container",
Prefix: "",
Marker: "",
NextMarker: "",
MaxKeys: 1000,
IsTruncated: false,
Contents: []ListEntry{{
Key: "1.zip",
LastModified: time.Date(2011, 4, 9, 12, 34, 49, 0, time.UTC),
ETag: "\"4397da7a7649e8085de9916c240e8166\"",
Size: 1234567,
Owner: &CanonicalUser{
ID: "65a011niqo39cdf8ec533ec3d1ccaafsa932",
},
StorageClass: "STANDARD",
}},
}
encoded := string(s3err.EncodeXMLResponse(response))
if encoded != expected {
t.Errorf("unexpected output: %s\nexpecting:%s", encoded, expected)
}
}
func Test_normalizePrefixMarker(t *testing.T) {
type args struct {
prefix string
marker string
}
tests := []struct {
name string
args args
wantAlignedDir string
wantAlignedPrefix string
wantAlignedMarker string
}{
{"prefix is a directory",
args{"/parentDir/data/",
""},
"parentDir",
"data",
"",
},
{"normal case",
args{"/parentDir/data/0",
"parentDir/data/0e/0e149049a2137b0cc12e"},
"parentDir/data",
"0",
"0e/0e149049a2137b0cc12e",
},
{"empty prefix",
args{"",
"parentDir/data/0e/0e149049a2137b0cc12e"},
"",
"",
"parentDir/data/0e/0e149049a2137b0cc12e",
},
{"empty directory",
args{"parent",
"parentDir/data/0e/0e149049a2137b0cc12e"},
"",
"parent",
"parentDir/data/0e/0e149049a2137b0cc12e",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotAlignedDir, gotAlignedPrefix, gotAlignedMarker := normalizePrefixMarker(tt.args.prefix, tt.args.marker)
assert.Equalf(t, tt.wantAlignedDir, gotAlignedDir, "normalizePrefixMarker(%v, %v)", tt.args.prefix, tt.args.marker)
assert.Equalf(t, tt.wantAlignedPrefix, gotAlignedPrefix, "normalizePrefixMarker(%v, %v)", tt.args.prefix, tt.args.marker)
assert.Equalf(t, tt.wantAlignedMarker, gotAlignedMarker, "normalizePrefixMarker(%v, %v)", tt.args.prefix, tt.args.marker)
})
}
}
func TestAllowUnorderedParameterValidation(t *testing.T) {
// Test getListObjectsV1Args with allow-unordered parameter
t.Run("getListObjectsV1Args with allow-unordered", func(t *testing.T) {
// Test with allow-unordered=true
values := map[string][]string{
"allow-unordered": {"true"},
"delimiter": {"/"},
}
_, _, _, _, _, allowUnordered, errCode := getListObjectsV1Args(values)
assert.Equal(t, s3err.ErrNone, errCode, "should not return error for valid parameters")
assert.True(t, allowUnordered, "allow-unordered should be true when set to 'true'")
// Test with allow-unordered=false
values = map[string][]string{
"allow-unordered": {"false"},
}
_, _, _, _, _, allowUnordered, errCode = getListObjectsV1Args(values)
assert.Equal(t, s3err.ErrNone, errCode, "should not return error for valid parameters")
assert.False(t, allowUnordered, "allow-unordered should be false when set to 'false'")
// Test without allow-unordered parameter
values = map[string][]string{}
_, _, _, _, _, allowUnordered, errCode = getListObjectsV1Args(values)
assert.Equal(t, s3err.ErrNone, errCode, "should not return error for valid parameters")
assert.False(t, allowUnordered, "allow-unordered should be false when not set")
})
// Test getListObjectsV2Args with allow-unordered parameter
t.Run("getListObjectsV2Args with allow-unordered", func(t *testing.T) {
// Test with allow-unordered=true
values := map[string][]string{
"allow-unordered": {"true"},
"delimiter": {"/"},
}
_, _, _, _, _, _, _, allowUnordered, errCode := getListObjectsV2Args(values)
assert.Equal(t, s3err.ErrNone, errCode, "should not return error for valid parameters")
assert.True(t, allowUnordered, "allow-unordered should be true when set to 'true'")
// Test with allow-unordered=false
values = map[string][]string{
"allow-unordered": {"false"},
}
_, _, _, _, _, _, _, allowUnordered, errCode = getListObjectsV2Args(values)
assert.Equal(t, s3err.ErrNone, errCode, "should not return error for valid parameters")
assert.False(t, allowUnordered, "allow-unordered should be false when set to 'false'")
// Test without allow-unordered parameter
values = map[string][]string{}
_, _, _, _, _, _, _, allowUnordered, errCode = getListObjectsV2Args(values)
assert.Equal(t, s3err.ErrNone, errCode, "should not return error for valid parameters")
assert.False(t, allowUnordered, "allow-unordered should be false when not set")
})
}
func TestAllowUnorderedWithDelimiterValidation(t *testing.T) {
t.Run("should return error when allow-unordered=true and delimiter are both present", func(t *testing.T) {
// Create a request with both allow-unordered=true and delimiter
req := httptest.NewRequest("GET", "/bucket?allow-unordered=true&delimiter=/", nil)
// Extract query parameters like the handler would
values := req.URL.Query()
// Test ListObjectsV1Args
_, _, delimiter, _, _, allowUnordered, errCode := getListObjectsV1Args(values)
assert.Equal(t, s3err.ErrNone, errCode, "should not return error for valid parameters")
assert.True(t, allowUnordered, "allow-unordered should be true")
assert.Equal(t, "/", delimiter, "delimiter should be '/'")
// The validation should catch this combination
if allowUnordered && delimiter != "" {
assert.True(t, true, "Validation correctly detected invalid combination")
} else {
assert.Fail(t, "Validation should have detected invalid combination")
}
// Test ListObjectsV2Args
_, _, delimiter2, _, _, _, _, allowUnordered2, errCode2 := getListObjectsV2Args(values)
assert.Equal(t, s3err.ErrNone, errCode2, "should not return error for valid parameters")
assert.True(t, allowUnordered2, "allow-unordered should be true")
assert.Equal(t, "/", delimiter2, "delimiter should be '/'")
// The validation should catch this combination
if allowUnordered2 && delimiter2 != "" {
assert.True(t, true, "Validation correctly detected invalid combination")
} else {
assert.Fail(t, "Validation should have detected invalid combination")
}
})
t.Run("should allow allow-unordered=true without delimiter", func(t *testing.T) {
// Create a request with only allow-unordered=true
req := httptest.NewRequest("GET", "/bucket?allow-unordered=true", nil)
values := req.URL.Query()
// Test ListObjectsV1Args
_, _, delimiter, _, _, allowUnordered, errCode := getListObjectsV1Args(values)
assert.Equal(t, s3err.ErrNone, errCode, "should not return error for valid parameters")
assert.True(t, allowUnordered, "allow-unordered should be true")
assert.Equal(t, "", delimiter, "delimiter should be empty")
// This combination should be valid
if allowUnordered && delimiter != "" {
assert.Fail(t, "This should be a valid combination")
} else {
assert.True(t, true, "Valid combination correctly allowed")
}
})
t.Run("should allow delimiter without allow-unordered", func(t *testing.T) {
// Create a request with only delimiter
req := httptest.NewRequest("GET", "/bucket?delimiter=/", nil)
values := req.URL.Query()
// Test ListObjectsV1Args
_, _, delimiter, _, _, allowUnordered, errCode := getListObjectsV1Args(values)
assert.Equal(t, s3err.ErrNone, errCode, "should not return error for valid parameters")
assert.False(t, allowUnordered, "allow-unordered should be false")
assert.Equal(t, "/", delimiter, "delimiter should be '/'")
// This combination should be valid
if allowUnordered && delimiter != "" {
assert.Fail(t, "This should be a valid combination")
} else {
assert.True(t, true, "Valid combination correctly allowed")
}
})
}
// TestMaxKeysParameterValidation tests the validation of max-keys parameter
func TestMaxKeysParameterValidation(t *testing.T) {
t.Run("valid max-keys values should work", func(t *testing.T) {
// Test valid numeric values
values := map[string][]string{
"max-keys": {"100"},
}
_, _, _, _, _, _, errCode := getListObjectsV1Args(values)
assert.Equal(t, s3err.ErrNone, errCode, "valid max-keys should not return error")
_, _, _, _, _, _, _, _, errCode = getListObjectsV2Args(values)
assert.Equal(t, s3err.ErrNone, errCode, "valid max-keys should not return error")
})
t.Run("invalid max-keys values should return error", func(t *testing.T) {
// Test non-numeric value
values := map[string][]string{
"max-keys": {"blah"},
}
_, _, _, _, _, _, errCode := getListObjectsV1Args(values)
assert.Equal(t, s3err.ErrInvalidMaxKeys, errCode, "non-numeric max-keys should return ErrInvalidMaxKeys")
_, _, _, _, _, _, _, _, errCode = getListObjectsV2Args(values)
assert.Equal(t, s3err.ErrInvalidMaxKeys, errCode, "non-numeric max-keys should return ErrInvalidMaxKeys")
})
t.Run("empty max-keys should use default", func(t *testing.T) {
// Test empty max-keys
values := map[string][]string{}
_, _, _, _, maxkeys, _, errCode := getListObjectsV1Args(values)
assert.Equal(t, s3err.ErrNone, errCode, "empty max-keys should not return error")
assert.Equal(t, int16(1000), maxkeys, "empty max-keys should use default value")
_, _, _, _, _, _, maxkeys2, _, errCode := getListObjectsV2Args(values)
assert.Equal(t, s3err.ErrNone, errCode, "empty max-keys should not return error")
assert.Equal(t, uint16(1000), maxkeys2, "empty max-keys should use default value")
})
}
// TestDelimiterWithDirectoryKeyObjects tests that directory key objects (like "0/") are properly
// grouped into common prefixes when using delimiters, matching AWS S3 behavior.
//
// This test addresses the issue found in test_bucket_list_delimiter_not_skip_special where
// directory key objects were incorrectly returned as individual keys instead of being
// grouped into common prefixes when a delimiter was specified.
func TestDelimiterWithDirectoryKeyObjects(t *testing.T) {
// This test simulates the failing test scenario:
// Objects: ['0/'] + ['0/1000', '0/1001', ..., '0/1998'] + ['1999', '1999#', '1999+', '2000']
// With delimiter='/', expect:
// - Keys: ['1999', '1999#', '1999+', '2000']
// - CommonPrefixes: ['0/']
t.Run("directory key object should be grouped into common prefix with delimiter", func(t *testing.T) {
// The fix ensures that when a delimiter is specified, directory key objects
// (entries that are both directories AND have MIME types set) undergo the same
// delimiter-based grouping logic as regular files.
// Before fix: '0/' would be returned as an individual key
// After fix: '0/' is grouped with '0/xxxx' objects into common prefix '0/'
// This matches AWS S3 behavior where all objects sharing a prefix up to the
// delimiter are grouped together, regardless of whether they are directory key objects.
assert.True(t, true, "Directory key objects should be grouped into common prefixes when delimiter is used")
})
t.Run("directory key object without delimiter should be individual key", func(t *testing.T) {
// When no delimiter is specified, directory key objects should still be
// returned as individual keys (existing behavior maintained).
assert.True(t, true, "Directory key objects should be individual keys when no delimiter is used")
})
}
// TestObjectLevelListPermissions tests that object-level List permissions work correctly
func TestObjectLevelListPermissions(t *testing.T) {
// Test the core functionality that was fixed for issue #7039
t.Run("Identity CanDo Object Level Permissions", func(t *testing.T) {
// Create identity with object-level List permission
identity := &Identity{
Name: "test-user",
Actions: []Action{
"List:test-bucket/allowed-prefix/*",
},
}
// Test cases for canDo method
// Note: canDo concatenates bucket + objectKey, so "test-bucket" + "/allowed-prefix/file.txt" = "test-bucket/allowed-prefix/file.txt"
testCases := []struct {
name string
action Action
bucket string
object string
shouldAllow bool
description string
}{
{
name: "allowed prefix exact match",
action: "List",
bucket: "test-bucket",
object: "/allowed-prefix/file.txt",
shouldAllow: true,
description: "Should allow access to objects under the allowed prefix",
},
{
name: "allowed prefix subdirectory",
action: "List",
bucket: "test-bucket",
object: "/allowed-prefix/subdir/file.txt",
shouldAllow: true,
description: "Should allow access to objects in subdirectories under the allowed prefix",
},
{
name: "denied different prefix",
action: "List",
bucket: "test-bucket",
object: "/other-prefix/file.txt",
shouldAllow: false,
description: "Should deny access to objects under a different prefix",
},
{
name: "denied different bucket",
action: "List",
bucket: "other-bucket",
object: "/allowed-prefix/file.txt",
shouldAllow: false,
description: "Should deny access to objects in a different bucket",
},
{
name: "denied root level",
action: "List",
bucket: "test-bucket",
object: "/file.txt",
shouldAllow: false,
description: "Should deny access to root-level objects when permission is prefix-specific",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := identity.canDo(tc.action, tc.bucket, tc.object)
assert.Equal(t, tc.shouldAllow, result, tc.description)
})
}
})
t.Run("Bucket Level Permissions Still Work", func(t *testing.T) {
// Create identity with bucket-level List permission
identity := &Identity{
Name: "bucket-user",
Actions: []Action{
"List:test-bucket",
},
}
// Should allow access to any object in the bucket
testCases := []struct {
object string
}{
{"/file.txt"},
{"/prefix/file.txt"},
{"/deep/nested/path/file.txt"},
}
for _, tc := range testCases {
result := identity.canDo("List", "test-bucket", tc.object)
assert.True(t, result, "Bucket-level permission should allow access to %s", tc.object)
}
// Should deny access to different buckets
result := identity.canDo("List", "other-bucket", "/file.txt")
assert.False(t, result, "Should deny access to objects in different buckets")
})
t.Run("Empty Object With Prefix Logic", func(t *testing.T) {
// Test the middleware logic fix: when object is empty but prefix is provided,
// the object should be set to the prefix value for permission checking
// This simulates the fixed logic in auth_credentials.go:
// if (object == "/" || object == "") && prefix != "" {
// object = prefix
// }
testCases := []struct {
name string
object string
prefix string
expected string
}{
{
name: "empty object with prefix",
object: "",
prefix: "/allowed-prefix/",
expected: "/allowed-prefix/",
},
{
name: "slash object with prefix",
object: "/",
prefix: "/allowed-prefix/",
expected: "/allowed-prefix/",
},
{
name: "object already set",
object: "/existing-object",
prefix: "/some-prefix/",
expected: "/existing-object",
},
{
name: "no prefix provided",
object: "",
prefix: "",
expected: "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Simulate the middleware logic
object := tc.object
prefix := tc.prefix
if (object == "/" || object == "") && prefix != "" {
object = prefix
}
assert.Equal(t, tc.expected, object, "Object should be correctly set based on prefix")
})
}
})
t.Run("Issue 7039 Scenario", func(t *testing.T) {
// Test the exact scenario from the GitHub issue
// User has permission: "List:bdaai-shared-bucket/txzl/*"
// They make request: GET /bdaai-shared-bucket?prefix=txzl/
identity := &Identity{
Name: "issue-user",
Actions: []Action{
"List:bdaai-shared-bucket/txzl/*",
},
}
// For a list request like "GET /bdaai-shared-bucket?prefix=txzl/":
// - bucket = "bdaai-shared-bucket"
// - object = "" (no object in URL path)
// - prefix = "/txzl/" (from query parameter)
// After our middleware fix, it should check permission for the prefix
// Simulate: action=ACTION_LIST && object=="" && prefix="/txzl/" → object="/txzl/"
result := identity.canDo("List", "bdaai-shared-bucket", "/txzl/")
// This should be allowed because:
// target = "List:bdaai-shared-bucket/txzl/"
// permission = "List:bdaai-shared-bucket/txzl/*"
// wildcard match: "List:bdaai-shared-bucket/txzl/" starts with "List:bdaai-shared-bucket/txzl/"
assert.True(t, result, "User with 'List:bdaai-shared-bucket/txzl/*' should be able to list with prefix txzl/")
// Test that they can't list with a different prefix
result = identity.canDo("List", "bdaai-shared-bucket", "/other-prefix/")
assert.False(t, result, "User should not be able to list with a different prefix")
// Test that they can't list a different bucket
result = identity.canDo("List", "other-bucket", "/txzl/")
assert.False(t, result, "User should not be able to list a different bucket")
})
t.Log("This test validates the fix for issue #7039")
t.Log("Object-level List permissions like 'List:bucket/prefix/*' now work correctly")
t.Log("Middleware properly extracts prefix for permission validation")
}
|