Decide request TLS in one place, per request (closes #269)
All checks were successful
check / check (push) Successful in 3m16s
All checks were successful
check / check (push) Successful in 3m16s
Two places decided whether a request was TLS, by two different means, and they disagreed. The session cookie's Secure attribute was fixed at startup from !Config.IsDev(). "dev" is the environment when WEBHOOKER_ENVIRONMENT is unset, so a deployment terminating TLS at a proxy without also setting the environment shipped the authentication cookie with no Secure attribute -- on the same response as a CSRF cookie that had one. It failed silently: everything kept working, so nothing prompted anyone to look. The CSRF middleware's per-request check compared X-Forwarded-Proto with == "https" exactly, so "HTTPS", "https, http" and "https,https" all took the plaintext path. Uppercase is legal for a case-insensitive token and the comma forms are what a proxy chained behind another proxy emits by appending rather than replacing. On that path gorilla/csrf stops enforcing the strict Referer check on a site that genuinely is HTTPS. Both now go through internal/reqtls.IsTLS, which folds case and takes the leftmost comma-separated element -- the hop nearest the client, and so the one a cookie's Secure attribute is about. A third package is needed because internal/middleware already imports internal/session, so session cannot import middleware back. Per-request beat a startup warning for the session cookie because it turned out to need no restructuring: gorilla/sessions gives every session its own copy of the store's Options and renders the cookie from that copy, and every session-cookie write here already goes through Session.Save or Session.Regenerate, both of which hold the request. The store's template Secure becomes true so that a write path added later which forgets to track the transport fails visibly instead of silently dropping Secure. The flag tracks the transport in both directions rather than latching on. Secure over plaintext is discarded by the browser without an error, which would make a plain-HTTP local run impossible to log into -- and would also void the deletion cookies in Destroy and Regenerate, leaving a session the user just tried to end still live. A third site that makes this decision, internal/handlers' BaseURL construction, assigns the raw header straight into the URL scheme. It is left alone here and filed separately.
This commit is contained in:
@@ -2,6 +2,7 @@ package session_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -73,7 +74,7 @@ func testSessionWithClock(
|
||||
t.Helper()
|
||||
|
||||
key := testKey()
|
||||
store := session.NewStore(key, false)
|
||||
store := session.NewStore(key)
|
||||
|
||||
cfg := &config.Config{
|
||||
Environment: config.EnvironmentDev,
|
||||
@@ -880,3 +881,264 @@ func TestDestroy_ThenSave_DeletesCookie(t *testing.T) {
|
||||
"destroyed session cookie should have negative MaxAge",
|
||||
)
|
||||
}
|
||||
|
||||
// --- Secure Attribute / Transport Tests ---
|
||||
|
||||
// transportCase describes one client-facing transport and the Secure
|
||||
// attribute the session cookie must carry for it.
|
||||
type transportCase struct {
|
||||
name string
|
||||
tls bool
|
||||
header string
|
||||
want bool
|
||||
why string
|
||||
}
|
||||
|
||||
// transportCases enumerates the transports the session cookie has to
|
||||
// get right. Every https spelling here is one a real proxy emits.
|
||||
func transportCases() []transportCase {
|
||||
return []transportCase{
|
||||
{
|
||||
name: "direct TLS",
|
||||
tls: true,
|
||||
want: true,
|
||||
why: "this process terminated TLS itself",
|
||||
},
|
||||
{
|
||||
name: "proxy reports https",
|
||||
header: "https",
|
||||
want: true,
|
||||
why: "the ordinary reverse-proxy deployment",
|
||||
},
|
||||
{
|
||||
name: "proxy reports HTTPS",
|
||||
header: "HTTPS",
|
||||
want: true,
|
||||
why: "the header value is a case-insensitive token",
|
||||
},
|
||||
{
|
||||
name: "appended chain https, http",
|
||||
header: "https, http",
|
||||
want: true,
|
||||
why: "the leftmost hop is the browser's connection",
|
||||
},
|
||||
{
|
||||
name: "appended chain https,https",
|
||||
header: "https,https",
|
||||
want: true,
|
||||
why: "two TLS hops, no space after the comma",
|
||||
},
|
||||
{
|
||||
name: "trailing space",
|
||||
header: "https ",
|
||||
want: true,
|
||||
why: "whitespace is not part of the token",
|
||||
},
|
||||
{
|
||||
name: "proxy reports http",
|
||||
header: "http",
|
||||
want: false,
|
||||
why: "the negative control: Secure over plaintext is " +
|
||||
"dropped by the browser without a word",
|
||||
},
|
||||
{
|
||||
name: "plaintext, no proxy",
|
||||
want: false,
|
||||
why: "a plain local run must stay loggable-in",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// transportRequest builds a request carrying the case's transport.
|
||||
func (tc transportCase) request(t *testing.T) *http.Request {
|
||||
t.Helper()
|
||||
|
||||
r := httptest.NewRequestWithContext(
|
||||
context.Background(), http.MethodGet,
|
||||
"http://example.com/", nil,
|
||||
)
|
||||
|
||||
if tc.tls {
|
||||
r.TLS = &tls.ConnectionState{}
|
||||
}
|
||||
|
||||
if tc.header != "" {
|
||||
r.Header.Set("X-Forwarded-Proto", tc.header)
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// sessionCookieFrom returns the session cookie from a response, or
|
||||
// fails the test if there is none.
|
||||
func sessionCookieFrom(
|
||||
t *testing.T,
|
||||
w *httptest.ResponseRecorder,
|
||||
) *http.Cookie {
|
||||
t.Helper()
|
||||
|
||||
for _, c := range w.Result().Cookies() {
|
||||
if c.Name == session.SessionName {
|
||||
return c
|
||||
}
|
||||
}
|
||||
|
||||
require.FailNow(t, "no session cookie in response")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// TestSave_SecureFollowsRequestTransport is the regression test for
|
||||
// the defect this replaces: Secure was fixed at startup from the
|
||||
// configured environment, and "dev" is the environment when
|
||||
// WEBHOOKER_ENVIRONMENT is unset. A deployment behind a TLS proxy in
|
||||
// that DEFAULT posture shipped the authentication cookie with no
|
||||
// Secure attribute and said nothing about it.
|
||||
//
|
||||
// testSession builds its config with EnvironmentDev precisely so that
|
||||
// the https cases below fail against the old startup-fixed behaviour.
|
||||
func TestSave_SecureFollowsRequestTransport(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, tc := range transportCases() {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
s := testSession(t)
|
||||
r := tc.request(t)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
sess, err := s.Get(r)
|
||||
require.NoError(t, err)
|
||||
|
||||
s.SetUser(sess, "user-1", "alice")
|
||||
require.NoError(t, s.Save(r, w, sess))
|
||||
|
||||
assert.Equal(
|
||||
t, tc.want, sessionCookieFrom(t, w).Secure,
|
||||
"session cookie Secure for %q: %s",
|
||||
tc.name, tc.why,
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestSave_SecureTracksTransportBothWays pins that the flag is not
|
||||
// latched. One store serves every request, so a Secure cookie set for
|
||||
// a proxied request must not leak into a later plaintext response --
|
||||
// the browser would silently discard that one, and a local run would
|
||||
// become impossible to log into.
|
||||
func TestSave_SecureTracksTransportBothWays(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
s := testSession(t)
|
||||
|
||||
secureReq := httptest.NewRequestWithContext(
|
||||
context.Background(), http.MethodGet,
|
||||
"http://example.com/", nil,
|
||||
)
|
||||
secureReq.Header.Set("X-Forwarded-Proto", "https")
|
||||
|
||||
secureW := httptest.NewRecorder()
|
||||
|
||||
secureSess, err := s.Get(secureReq)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, s.Save(secureReq, secureW, secureSess))
|
||||
require.True(
|
||||
t, sessionCookieFrom(t, secureW).Secure,
|
||||
"proxied request should produce a Secure cookie",
|
||||
)
|
||||
|
||||
plainReq := httptest.NewRequestWithContext(
|
||||
context.Background(), http.MethodGet,
|
||||
"http://example.com/", nil,
|
||||
)
|
||||
plainW := httptest.NewRecorder()
|
||||
|
||||
plainSess, err := s.Get(plainReq)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, s.Save(plainReq, plainW, plainSess))
|
||||
|
||||
assert.False(
|
||||
t, sessionCookieFrom(t, plainW).Secure,
|
||||
"a later plaintext request must not inherit Secure from "+
|
||||
"the earlier proxied one",
|
||||
)
|
||||
}
|
||||
|
||||
// TestDestroy_DeletionCookieFollowsTransport covers the trap in the
|
||||
// deletion path. The store's template Secure is true, so a logout over
|
||||
// plaintext that failed to track the transport would emit a Secure
|
||||
// deletion cookie -- which the browser drops, leaving the session the
|
||||
// user just tried to end still sitting in the jar.
|
||||
func TestDestroy_DeletionCookieFollowsTransport(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
s := testSession(t)
|
||||
|
||||
r := httptest.NewRequestWithContext(
|
||||
context.Background(), http.MethodGet,
|
||||
"http://example.com/", nil,
|
||||
)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
sess, err := s.Get(r)
|
||||
require.NoError(t, err)
|
||||
|
||||
s.Destroy(sess)
|
||||
require.NoError(t, s.Save(r, w, sess))
|
||||
|
||||
cookie := sessionCookieFrom(t, w)
|
||||
|
||||
require.Negative(
|
||||
t, cookie.MaxAge,
|
||||
"Destroy then Save should emit a deletion cookie",
|
||||
)
|
||||
assert.False(
|
||||
t, cookie.Secure,
|
||||
"a deletion cookie sent over plaintext must not be Secure, "+
|
||||
"or the browser discards it and the session survives",
|
||||
)
|
||||
}
|
||||
|
||||
// TestRegenerate_BothCookiesFollowTransport covers the login path.
|
||||
// Regenerate writes two cookies -- a deletion for the pre-login
|
||||
// session and the new authenticated one -- and both have to match the
|
||||
// transport or one of them is silently dropped.
|
||||
func TestRegenerate_BothCookiesFollowTransport(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, tc := range transportCases() {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
s := testSession(t)
|
||||
r := tc.request(t)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
oldSess, err := s.Get(r)
|
||||
require.NoError(t, err)
|
||||
|
||||
newSess, err := s.Regenerate(r, w, oldSess)
|
||||
require.NoError(t, err)
|
||||
|
||||
s.SetUser(newSess, "user-1", "alice")
|
||||
require.NoError(t, s.Save(r, w, newSess))
|
||||
|
||||
cookies := w.Result().Cookies()
|
||||
require.Len(
|
||||
t, cookies, 2,
|
||||
"Regenerate then Save writes a deletion cookie "+
|
||||
"and a replacement",
|
||||
)
|
||||
|
||||
for _, c := range cookies {
|
||||
assert.Equal(
|
||||
t, tc.want, c.Secure,
|
||||
"cookie %d Secure for %q: %s",
|
||||
c.MaxAge, tc.name, tc.why,
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user