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

Accept referrer if origin is not available #1739

Merged
merged 2 commits into from
Jun 6, 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
18 changes: 12 additions & 6 deletions ee/localserver/krypto-ec-middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,22 +220,27 @@ func (e *kryptoEcMiddleware) Wrap(next http.Handler) http.Handler {
}

// Check if the origin is in the allowed list. See https://github.com/kolide/k2/issues/9634
origin := r.Header.Get("Origin")
// When loading images, the origin may not be set, but the referer will. We can accept that instead.
if origin == "" {
origin = strings.TrimSuffix(r.Header.Get("Referer"), "/")
}
if len(cmdReq.AllowedOrigins) > 0 {
allowed := false
for _, ao := range cmdReq.AllowedOrigins {
if strings.EqualFold(ao, r.Header.Get("Origin")) {
if strings.EqualFold(ao, origin) {
allowed = true
break
}
}

if !allowed {
span.SetAttributes(attribute.String("origin", r.Header.Get("Origin")))
traces.SetError(span, fmt.Errorf("origin %s is not allowed", r.Header.Get("Origin")))
span.SetAttributes(attribute.String("origin", origin))
traces.SetError(span, fmt.Errorf("origin %s is not allowed", origin))
e.slogger.Log(r.Context(), slog.LevelError,
"origin is not allowed",
"allowlist", cmdReq.AllowedOrigins,
"origin", r.Header.Get("Origin"),
"origin", origin,
)

w.WriteHeader(http.StatusUnauthorized)
Expand All @@ -245,12 +250,12 @@ func (e *kryptoEcMiddleware) Wrap(next http.Handler) http.Handler {

e.slogger.Log(r.Context(), slog.LevelDebug,
"origin matches allowlist",
"origin", r.Header.Get("Origin"),
"origin", origin,
)
} else {
e.slogger.Log(r.Context(), slog.LevelDebug,
"origin is allowed by default, no allowlist",
"origin", r.Header.Get("Origin"),
"origin", origin,
)
}

Expand Down Expand Up @@ -281,6 +286,7 @@ func (e *kryptoEcMiddleware) Wrap(next http.Handler) http.Handler {
}

newReq.Header.Set("Origin", r.Header.Get("Origin"))
newReq.Header.Set("Referer", r.Header.Get("Referer"))

// setting the newReq context to the current request context
// allows the trace to continue to the inner request,
Expand Down
19 changes: 14 additions & 5 deletions ee/localserver/krypto-ec-middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,12 @@ func Test_AllowedOrigin(t *testing.T) {
challengeData := []byte(ulid.New())

var tests = []struct {
name string
requestOrigin string
allowedOrigins []string
logStr string
expectedStatus int
name string
requestOrigin string
requestReferrer string
allowedOrigins []string
logStr string
expectedStatus int
}{
{
name: "no allowed specified",
Expand Down Expand Up @@ -277,6 +278,13 @@ func Test_AllowedOrigin(t *testing.T) {
expectedStatus: http.StatusOK,
logStr: "origin matches allowlist",
},
{
name: "no allowed specified origin, but acceptable referer is present",
allowedOrigins: []string{"https://auth.example.com", "https://login.example.com"},
requestReferrer: "https://auth.example.com",
expectedStatus: http.StatusOK,
logStr: "origin matches allowlist",
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -320,6 +328,7 @@ func Test_AllowedOrigin(t *testing.T) {

req := makeGetRequest(t, encodedChallenge)
req.Header.Set("origin", tt.requestOrigin)
req.Header.Set("referer", tt.requestReferrer)

rr := httptest.NewRecorder()
h.ServeHTTP(rr, req)
Expand Down
Loading