Skip to content

Commit

Permalink
Trim leading or trailing space from trace ID
Browse files Browse the repository at this point in the history
Without this change, a leading or trailing space in the trace ID, as received
from the user input, causes a bad error message to the user.  Here we trim the
input to ensure that no leading or trailing space is present before attempting
to decode the string.
  • Loading branch information
zalegrala committed Aug 4, 2021
1 parent a0975ac commit c7b5f84
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"strings"

"github.com/gorilla/mux"
)
Expand Down Expand Up @@ -32,6 +33,10 @@ func ParseTraceID(r *http.Request) ([]byte, error) {
}

func hexStringToTraceID(id string) ([]byte, error) {
// the encoding/hex package does not handle non-hex characters.
// remove leading or trailing spaces if present.
id = strings.Trim(id, " ")

// the encoding/hex package does not like odd length strings.
// just append a bit here
if len(id)%2 == 1 {
Expand Down
4 changes: 4 additions & 0 deletions pkg/util/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func TestHexStringToTraceID(t *testing.T) {
id: "234567890abcdef", // odd length
expected: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef},
},
{
id: "1234567890abcdef ", // trailing space
expected: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef},
},
}

for _, tt := range tc {
Expand Down

0 comments on commit c7b5f84

Please sign in to comment.