Skip to content

Commit

Permalink
Cover cases when we have multiple percent encoded octet sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
santileira committed Jun 19, 2024
1 parent 7b621ca commit 4c9c61c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 8 additions & 6 deletions baggage/baggage.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,26 +308,28 @@ func parseMember(member string) (Member, error) {
if !utf8.ValidString(value) {
// Handle invalid UTF-8 sequences
// Replace them with a replacement code point or handle them as needed
invalidSeq := findInvalidUTF8Sequence(value)
invalidSequences := findInvalidUTF8Sequences(value)
// Replace invalid sequence with a replacement code point
value = strings.ReplaceAll(value, invalidSeq, "�")
for _, is := range invalidSequences {
value = strings.ReplaceAll(value, is, "�")
}
}

return Member{key: key, value: value, properties: props, hasData: true}, nil
}

func findInvalidUTF8Sequence(input string) string {
invalidSequence := ""
func findInvalidUTF8Sequences(input string) []string {
var invalidSequences []string
for i := 0; i < len(input); i++ {
r, size := utf8.DecodeRuneInString(input[i:])

if r == utf8.RuneError && size == 1 {
// RuneError indicates an invalid UTF-8 sequence
invalidSequence += input[i : i+size]
invalidSequences = append(invalidSequences, input[i:i+size])
}
}

return invalidSequence
return invalidSequences
}

// validate ensures m conforms to the W3C Baggage specification.
Expand Down
6 changes: 6 additions & 0 deletions baggage/baggage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,12 @@ func TestBaggageParseValue(t *testing.T) {
valueWant: "aa�cc",
valueWantSize: 7,
},
{
name: "multiple percent encoded octet sequences don't match UTF-8 encoding scheme",
in: "k=aa%ffcc%fedd%fa",
valueWant: "aa�cc�dd�",
valueWantSize: 15,
},
{
name: "raw value",
in: "k=aacc",
Expand Down

0 comments on commit 4c9c61c

Please sign in to comment.