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
Changes from 1 commit
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
Loading