Skip to content

Commit 386bef3

Browse files
tritoneBrennaEpp
andauthored
fix(storage): add object validation case and test (#9521)
* fix(storage): add object validation case and test Per GCS docs, . and .. are invalid object names. * fix error msg * use quote in error --------- Co-authored-by: Brenna N Epp <brennae@google.com>
1 parent a75c8b6 commit 386bef3

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

storage/storage.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,10 @@ func (o *ObjectHandle) validate() error {
10961096
if !utf8.ValidString(o.object) {
10971097
return fmt.Errorf("storage: object name %q is not valid UTF-8", o.object)
10981098
}
1099+
// Names . and .. are not valid; see https://cloud.google.com/storage/docs/objects#naming
1100+
if o.object == "." || o.object == ".." {
1101+
return fmt.Errorf("storage: object name %q is not valid", o.object)
1102+
}
10991103
return nil
11001104
}
11011105

storage/storage_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,6 +2424,57 @@ func TestParseProjectNumber(t *testing.T) {
24242424
}
24252425
}
24262426

2427+
func TestObjectValidate(t *testing.T) {
2428+
for _, c := range []struct {
2429+
name string
2430+
bucket string
2431+
object string
2432+
wantSuccess bool
2433+
}{
2434+
{
2435+
name: "valid object",
2436+
bucket: "my-bucket",
2437+
object: "my-object",
2438+
wantSuccess: true,
2439+
},
2440+
{
2441+
name: "empty bucket name",
2442+
bucket: "",
2443+
object: "my-object",
2444+
wantSuccess: false,
2445+
},
2446+
{
2447+
name: "empty object name",
2448+
bucket: "my-bucket",
2449+
object: "",
2450+
wantSuccess: false,
2451+
},
2452+
{
2453+
name: "invalid utf-8",
2454+
bucket: "my-bucket",
2455+
object: "\xc3\x28",
2456+
wantSuccess: false,
2457+
},
2458+
{
2459+
name: "object name .",
2460+
bucket: "my-bucket",
2461+
object: ".",
2462+
wantSuccess: false,
2463+
},
2464+
} {
2465+
t.Run(c.name, func(r *testing.T) {
2466+
b := &BucketHandle{name: c.bucket}
2467+
err := b.Object(c.object).validate()
2468+
if c.wantSuccess && err != nil {
2469+
r.Errorf("want success, got error %v", err)
2470+
}
2471+
if !c.wantSuccess && err == nil {
2472+
r.Errorf("want error, got nil")
2473+
}
2474+
})
2475+
}
2476+
}
2477+
24272478
// isZeroValue reports whether v is the zero value for its type
24282479
// It errors if the argument is unknown
24292480
func isZeroValue(v reflect.Value) (bool, error) {

0 commit comments

Comments
 (0)