Skip to content

Commit

Permalink
fix: enable HttpOnly and Secure using TLS
Browse files Browse the repository at this point in the history
When TLS is enabled, set the HttpOnly and
Secure flags when a cookie is created.

closes: #24522
  • Loading branch information
davidby-influx committed Dec 20, 2023
1 parent 081f951 commit e5b864a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions session/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (h *SessionHandler) handleSignin(w http.ResponseWriter, r *http.Request) {
return
}

encodeCookieSession(w, s)
encodeCookieSession(w, s, (r != nil) && (r.TLS != nil))
w.WriteHeader(http.StatusNoContent)
}

Expand Down Expand Up @@ -163,7 +163,7 @@ func decodeSignoutRequest(ctx context.Context, r *http.Request) (*signoutRequest

const cookieSessionName = "influxdb-oss-session"

func encodeCookieSession(w http.ResponseWriter, s *influxdb.Session) {
func encodeCookieSession(w http.ResponseWriter, s *influxdb.Session, tlsEnabled bool) {
// We only need the session cookie for accesses to "/api/...", so limit
// it to that using "Path".
//
Expand Down Expand Up @@ -208,6 +208,8 @@ func encodeCookieSession(w http.ResponseWriter, s *influxdb.Session) {
Path: "/api/", // since UI doesn't need it, limit cookie usage to API requests
Expires: s.ExpiresAt,
SameSite: http.SameSiteStrictMode,
HttpOnly: tlsEnabled,
Secure: tlsEnabled,
}

http.SetCookie(w, c)
Expand Down
2 changes: 1 addition & 1 deletion session/http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestSessionHandler_handleSignin(t *testing.T) {
password: "supersecret",
},
wants: wants{
cookie: "influxdb-oss-session=abc123xyz; Path=/api/; Expires=Thu, 26 Sep 2030 00:00:00 GMT; SameSite=Strict",
cookie: "influxdb-oss-session=abc123xyz; Path=/api/; Expires=Thu, 26 Sep 2030 00:00:00 GMT; Secure; SameSite=Strict",
code: http.StatusNoContent,
},
},
Expand Down

0 comments on commit e5b864a

Please sign in to comment.