Skip to content
This repository was archived by the owner on Jan 27, 2021. It is now read-only.

Commit a4a31db

Browse files
authored
Merge pull request #38 from owncloud/fix/defensive-code
Defensive Code on Type Assertion
2 parents e2082c1 + ea65b50 commit a4a31db

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Bugfix: Fix runtime error when type asserting on nil value
2+
3+
Fixed the case where an account UUID present in the context is nil, and type asserting it as a string would produce a runtime error.
4+
5+
https://github.com/owncloud/ocis-settings/pull/38
6+
https://github.com/owncloud/ocis-settings/issues/37

pkg/service/v0/service.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,8 @@ func getFailsafeIdentifier(c context.Context, identifier *proto.Identifier) *pro
115115
identifier = &proto.Identifier{}
116116
}
117117
if identifier.AccountUuid == "me" {
118-
ownAccountUUID := c.Value(middleware.UUIDKey).(string)
119-
if len(ownAccountUUID) > 0 {
118+
if ownAccountUUID, ok := c.Value(middleware.UUIDKey).(string); ok {
120119
identifier.AccountUuid = ownAccountUUID
121-
} else {
122-
// might be valid for the request not having an AccountUuid in the identifier.
123-
// but clear it, instead of passing on `me`.
124-
identifier.AccountUuid = ""
125120
}
126121
}
127122
identifier.AccountUuid = strings.ToLower(identifier.AccountUuid)

pkg/service/v0/service_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package svc
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/owncloud/ocis-pkg/v2/middleware"
8+
"github.com/owncloud/ocis-settings/pkg/proto/v0"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
var (
13+
ctxWithUUID = context.WithValue(context.Background(), middleware.UUIDKey, "61445573-4dbe-4d56-88dc-88ab47aceba7")
14+
ctxWithEmptyUUID = context.WithValue(context.Background(), middleware.UUIDKey, "")
15+
emptyCtx = context.Background()
16+
17+
scenarios = []struct {
18+
name string
19+
identifier *proto.Identifier
20+
ctx context.Context
21+
expect *proto.Identifier
22+
}{
23+
{
24+
name: "context with UUID; identifier = 'me'",
25+
ctx: ctxWithUUID,
26+
identifier: &proto.Identifier{
27+
AccountUuid: "me",
28+
},
29+
expect: &proto.Identifier{
30+
AccountUuid: ctxWithUUID.Value(middleware.UUIDKey).(string),
31+
},
32+
},
33+
{
34+
name: "context without UUID; identifier = 'me'",
35+
ctx: ctxWithEmptyUUID,
36+
identifier: &proto.Identifier{
37+
AccountUuid: "me",
38+
},
39+
expect: &proto.Identifier{
40+
AccountUuid: "",
41+
},
42+
},
43+
{
44+
name: "context with UUID; identifier not 'me'",
45+
ctx: ctxWithUUID,
46+
identifier: &proto.Identifier{},
47+
expect: &proto.Identifier{
48+
AccountUuid: "",
49+
},
50+
},
51+
}
52+
)
53+
54+
func TestGetFailsafeIdentifier(t *testing.T) {
55+
for _, s := range scenarios {
56+
scenario := s
57+
t.Run(scenario.name, func(t *testing.T) {
58+
got := getFailsafeIdentifier(scenario.ctx, scenario.identifier)
59+
assert.NotPanics(t, func() {
60+
getFailsafeIdentifier(emptyCtx, scenario.identifier)
61+
})
62+
assert.Equal(t, scenario.expect, got)
63+
})
64+
}
65+
}

0 commit comments

Comments
 (0)