fleet/server/contexts/token/token_test.go
jacobshandling 0eb8d432bf
Safely split incoming request headers, remove support for token presence in request body (#39427)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issues:**
- Prevents unbounded split length exploits similar to
https://nvd.nist.gov/vuln/detail/CVE-2025-30204
- Also removes parsing of request body for token, see
https://github.com/fleetdm/fleet/issues/39659
- @iansltx I figured since this PR updates the code blocks in question,
makes sense to [remove the body parsing
here](https://github.com/fleetdm/fleet/pull/39427/changes#diff-83b0d73af21e81cf2c5ed4448718d0760543699fe6e36e401372467befea29edL30-L33),
and clean up the [related dead
code](c1e3e89b5f/frontend/services/entities/installers.ts (L13))
in a follow-up

See https://fleetdm.slack.com/archives/C019WG4GH0A/p1770322925865209

- [x] Added/updated automated tests
- [x] QA'd all new/changed functionality manually
2026-02-18 08:50:04 -08:00

103 lines
2.5 KiB
Go

package token
import (
"io"
"net/http"
"strings"
"testing"
)
func TestFromHTTPRequest(t *testing.T) {
tests := []struct {
name string
r *http.Request
want Token
}{
{
name: "no auth",
want: "",
r: &http.Request{},
}, {
name: "empty auth",
r: &http.Request{
Header: map[string][]string{
"Authorization": {""},
},
},
want: "",
}, {
name: "BEARER no data",
r: &http.Request{
Header: map[string][]string{
"Authorization": {"BEARER"},
"Content-Type": {"application/x-www-form-urlencoded"},
},
Method: http.MethodPost,
Body: io.NopCloser(strings.NewReader("token=bar")),
},
want: "",
}, {
name: "BEARER foobar",
r: &http.Request{
Header: map[string][]string{
"Authorization": {"BEARER foobar"},
"Content-Type": {"application/x-www-form-urlencoded"},
},
Method: http.MethodPost,
Body: io.NopCloser(strings.NewReader("token=bar")),
},
want: "foobar",
}, {
name: "from body",
r: &http.Request{
Header: map[string][]string{
"Authorization": {"FOOBAR foobar"},
"Content-Type": {"application/x-www-form-urlencoded"},
},
Method: http.MethodPost,
Body: io.NopCloser(strings.NewReader("token=bar")),
},
want: "", // no longer support parsing token from request body
}, {
name: "BEARER with 3 parts",
r: &http.Request{
Header: map[string][]string{
"Authorization": {"BEARER foobar extra"},
"Content-Type": {"application/x-www-form-urlencoded"},
},
Method: http.MethodPost,
Body: io.NopCloser(strings.NewReader("token=bar")),
},
want: "",
}, {
name: "BEARER with multiple extra parts",
r: &http.Request{
Header: map[string][]string{
"Authorization": {"BEARER foobar extra parts here"},
"Content-Type": {"application/x-www-form-urlencoded"},
},
Method: http.MethodPost,
Body: io.NopCloser(strings.NewReader("token=bar")),
},
want: "",
}, {
name: "bearer lowercase with extra parts",
r: &http.Request{
Header: map[string][]string{
"Authorization": {"bearer foobar extra"},
"Content-Type": {"application/x-www-form-urlencoded"},
},
Method: http.MethodPost,
Body: io.NopCloser(strings.NewReader("token=bar")),
},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := FromHTTPRequest(tt.r); got != tt.want {
t.Errorf("FromHTTPRequest() = %v, want %v", got, tt.want)
}
})
}
}