From 8eecad1237967f3d0af3983af1007ac5e43ff084 Mon Sep 17 00:00:00 2001 From: Josue Giron Date: Sat, 21 Nov 2026 20:17:43 +0000 Subject: [PATCH] fix(async): quote the key in the get not-found error Co-Authored-By: Claude Opus 5.5 --- README.md | 1 + async/async_container.go | 13 +++++----- async/async_container_test.go | 48 +++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a945c37..c72604d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/async/async_container.go b/async/async_container.go index 0fcc912..7e95d50 100644 --- a/async/async_container.go +++ b/async/async_container.go @@ -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 { @@ -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") diff --git a/async/async_container_test.go b/async/async_container_test.go index b678406..04a14b5 100644 --- a/async/async_container_test.go +++ b/async/async_container_test.go @@ -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) + } + }) + } +}