Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix metadata header value sanitization #3581

Merged
merged 3 commits into from
Nov 5, 2024
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
9 changes: 8 additions & 1 deletion common/component/azure/blobstorage/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func SanitizeMetadata(log logger.Logger, metadata map[string]string) map[string]
n = 0
newVal := make([]byte, len(val))
for i := range len(val) {
if val[i] > 127 || val[i] == 0 {
if val[i] > 127 || (isCTL(val[i]) && !isLWS(val[i])) {
continue
}
newVal[n] = val[i]
Expand All @@ -118,3 +118,10 @@ func SanitizeMetadata(log logger.Logger, metadata map[string]string) map[string]

return res
}

func isLWS(b byte) bool { return b == ' ' || b == '\t' }

func isCTL(b byte) bool {
const del = 0x7f // a CTL
return b < ' ' || b == del
}
48 changes: 48 additions & 0 deletions common/component/azure/blobstorage/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestSanitizeRequestMetadata(t *testing.T) {
"somecustomfield": "some-custom-value",
"specialfield": "special:valueÜ",
"not-allowed:": "not-allowed",
"ctr-characters": string([]byte{72, 20, 1, 0, 101, 120}),
}
meta := SanitizeMetadata(log, m)
_ = assert.NotNil(t, meta["somecustomfield"]) &&
Expand All @@ -68,5 +69,52 @@ func TestSanitizeRequestMetadata(t *testing.T) {
assert.Equal(t, "special:value", *meta["specialfield"])
_ = assert.NotNil(t, meta["notallowed"]) &&
assert.Equal(t, "not-allowed", *meta["notallowed"])
_ = assert.NotNil(t, meta["ctrcharacters"]) &&
assert.Equal(t, string([]byte{72, 101, 120}), *meta["ctrcharacters"])
})
}

func TestIsLWS(t *testing.T) {
// Test cases for isLWS
tests := []struct {
input byte
expected bool
}{
{' ', true}, // Space character, should return true
{'\t', true}, // Tab character, should return true
{'A', false}, // Non-LWS character, should return false
{'1', false}, // Non-LWS character, should return false
{'\n', false}, // Newline, a CTL but not LWS, should return false
{0x7F, false}, // DEL character, a CTL but not LWS, should return false
}

for _, tt := range tests {
t.Run("Testing for LWS", func(t *testing.T) {
result := isLWS(tt.input)
assert.Equal(t, tt.expected, result, "input: %v", tt.input)
})
}
}

func TestIsCTL(t *testing.T) {
// Test cases for isCTL
tests := []struct {
input byte
expected bool
}{
{0x00, true}, // NUL, a control character
{0x1F, true}, // US (Unit Separator), a control character
{'\n', true}, // Newline, a control character
{0x7F, true}, // DEL, a control character
{'A', false}, // Non-CTL character
{'1', false}, // Non-CTL character
{' ', false}, // Space is not a CTL (although LWS)
}

for _, tt := range tests {
t.Run("Testing for CTL characters", func(t *testing.T) {
result := isCTL(tt.input)
assert.Equal(t, tt.expected, result, "input: %v", tt.input)
})
}
}
Loading