agent: trim space when parsing X-Nomad-Token header (#16469)

Our auth token parsing code trims space around the `Authorization` header but
not around `X-Nomad-Token`. When using the UI, it's easy to accidentally
introduce a leading or trailing space, which results in spurious authentication
errors. Trim the space at the HTTP server.
This commit is contained in:
Tim Gross
2023-03-14 08:57:53 -04:00
committed by GitHub
parent a42a33fa6b
commit c70bbd14ba
3 changed files with 5 additions and 2 deletions

3
.changelog/16469.txt Normal file
View File

@@ -0,0 +1,3 @@
```release-note:improvement
agent: trim leading and trailing spaces when parsing `X-Nomad-Token` header
```

View File

@@ -969,7 +969,7 @@ func parseInt(req *http.Request, field string) (*int, error) {
// parseToken is used to parse the X-Nomad-Token param
func (s *HTTPServer) parseToken(req *http.Request, token *string) {
if other := req.Header.Get("X-Nomad-Token"); other != "" {
*token = other
*token = strings.TrimSpace(other)
return
}

View File

@@ -552,7 +552,7 @@ func TestParseToken(t *testing.T) {
{
Name: "Parses token from X-Nomad-Token",
HeaderKey: "X-Nomad-Token",
HeaderValue: "foobar",
HeaderValue: " foobar",
ExpectedToken: "foobar",
},
{