Skip to content
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Recent changes to consider if the library already has consumers:
- `info.Info.Get` returns `nil` when the map itself is `nil`.
- `info.Info` now provides `Lookup*` helpers for explicit contracts.
- `async.Container` now exposes `TrySet` to avoid panics on invalid keys.
- `async.Container.Get` quotes the key in its not-found error (`key ("missing")`), so control characters in untrusted keys are escaped.
- `txml.XMLString.Decode` now decodes directly into the caller-provided destination pointer.

## License
Expand Down
13 changes: 7 additions & 6 deletions async/async_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,9 @@ func (c *Container) Get(key interface{}, value interface{}) error {
c.mu.RLock()
defer c.mu.RUnlock()

// content:
if c.content == nil {
return fmt.Errorf("container: item not found, key (%v)", key)
}

content, ok := c.content[key]
if !ok {
return fmt.Errorf("container: item not found, key (%v)", key)
return notFoundError(key)
}

if err := assignValue(value, content); err != nil {
Expand All @@ -77,6 +72,12 @@ func (c *Container) Get(key interface{}, value interface{}) error {
return nil
}

// notFoundError quotes the key so that a key from untrusted input cannot
// inject line breaks or other control characters into logged error text.
func notFoundError(key interface{}) error {
return fmt.Errorf("container: item not found, key (%q)", fmt.Sprint(key))
}

func validateKey(key interface{}) error {
if key == nil {
return fmt.Errorf("container: key cannot be nil")
Expand Down
48 changes: 48 additions & 0 deletions async/async_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,51 @@ func FuzzContainerGetSliceIntoArray(f *testing.F) {
}
})
}

func TestContainerGetMissingKeyQuotesKey(t *testing.T) {
populated := &Container{}
populated.Set("present", "value", nil)

tests := []struct {
name string
container *Container
key interface{}
want string
}{
{
name: "line break in key, empty container",
container: &Container{},
key: "a\nlevel=ERROR msg=forged",
want: `container: item not found, key ("a\nlevel=ERROR msg=forged")`,
},
{
name: "line break in key, other keys stored",
container: populated,
key: "a\nlevel=ERROR msg=forged",
want: `container: item not found, key ("a\nlevel=ERROR msg=forged")`,
},
{
name: "carriage return and escape sequence in key",
container: populated,
key: "a\r\x1b[31mforged",
want: `container: item not found, key ("a\r\x1b[31mforged")`,
},
{
name: "integer key",
container: populated,
key: 65,
want: `container: item not found, key ("65")`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got string
err := tt.container.Get(tt.key, &got)

if err == nil || err.Error() != tt.want {
t.Fatalf("Get() error = %v, want %s", err, tt.want)
}
})
}
}
Loading