feat(server): ghost-session guard — JWTs for deleted users stop authenticating
The JWT middleware validated signature + expiry and fabricated the request user purely from token claims, so a signature-valid token for a DELETED user kept full API access until token expiry — observed live on hardware (a phone session survived the test suite's user-table recreation and kept serving its true empty sets; open handoff bug 1). An ensureUserExistsMiddleware already existed but was only wired on the frontend SSR group (frontend.go) — every /api group used bare createJWTMiddleware. The existence check now lives INSIDE createJWTMiddleware's SuccessHandler so every protected group (SSR and API alike) gets it: - After claim validation, GetUser by the token's user_id; a missing row clears the token cookie and answers 401 user_not_found for API calls (the app's TokenAuthenticator then re-logins with stored credentials) or redirects to /login?session=invalid for page requests — same shape-aware split the error handler already uses. - Cost: one primary-key lookup per authenticated request (~0.2ms local); instant revocation, no TTL semantics. The redundant SSR-only middleware is deleted (its redirect behavior is preserved inside the guard). The guard exposed two processing-issues tests that were unknowingly RELYING on ghost semantics: they nested a second setupTestServer whose bootstrap deleted the first setup's regular user, then asserted 403 from the dead token. De-nested: a createLibraryDirect helper inserts the library under the live setup's user, no second bootstrap. New TestDeletedUserTokenRejected pins the contract: token works while the user exists -> 401 user_not_found after deletion. Full suite: 219 groups, zero failures.
This commit is contained in:
1 parent
4f05a232fe
commit
9cd6356892
5 files changed
+125
-57
No files matched your search
@@ -657,6 +657,69 @@ func TestProgressWeb_EdgeCases(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// TestDeletedUserTokenRejected pins the ghost-session guard: a
|
||||
// signature-valid JWT for a user that has been DELETED must stop
|
||||
// authenticating (401 user_not_found), not serve a phantom session.
|
||||
// Before the guard, deleted users kept full API access until token
|
||||
// expiry (observed live on hardware 2026-10-09).
|
||||
func TestDeletedUserTokenRejected(t *testing.T) {
|
||||
setup := setupTestServer(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// Create a dedicated user, then log in as it to get a fresh token.
|
||||
createResp := doReq(t, "POST", setup.Server.URL+"/api/auth/register", map[string]interface{}{
|
||||
"email": "ghost-test@tests.bookhoard.internal",
|
||||
"username": "ghosttest",
|
||||
"password": "Ghost@Pass123!",
|
||||
}, setup.Token)
|
||||
defer createResp.Body.Close()
|
||||
if createResp.StatusCode != http.StatusCreated {
|
||||
t.Skipf("registration unavailable (status %d)", createResp.StatusCode)
|
||||
}
|
||||
|
||||
loginResp := doReq(t, "POST", setup.Server.URL+"/api/auth/login", map[string]interface{}{
|
||||
"login": "ghost-test@tests.bookhoard.internal",
|
||||
"password": "Ghost@Pass123!",
|
||||
}, "")
|
||||
defer loginResp.Body.Close()
|
||||
require.Equal(t, http.StatusOK, loginResp.StatusCode)
|
||||
var loginBody struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
}
|
||||
require.NoError(t, json.NewDecoder(loginResp.Body).Decode(&loginBody))
|
||||
require.NotEmpty(t, loginBody.AccessToken)
|
||||
|
||||
ghostToken := loginBody.AccessToken
|
||||
|
||||
// Sanity: the token works while the user exists (user-scoped endpoint
|
||||
// a freshly-registered regular user can access).
|
||||
okResp := doReq(t, "GET", setup.Server.URL+"/api/collections", nil, ghostToken)
|
||||
defer okResp.Body.Close()
|
||||
require.Equal(t, http.StatusOK, okResp.StatusCode, "token must work while the user exists")
|
||||
|
||||
// Delete the user directly via the DB for determinism (same query the
|
||||
// suite's own cleanup uses).
|
||||
ghostID := ""
|
||||
users, err := setup.DB.ListUsers(ctx)
|
||||
require.NoError(t, err)
|
||||
for _, u := range users {
|
||||
if u.Email == "ghost-test@tests.bookhoard.internal" {
|
||||
ghostID = uuid.UUID(u.ID.Bytes).String()
|
||||
require.NoError(t, setup.DB.DeleteUser(ctx, u.ID))
|
||||
}
|
||||
}
|
||||
require.NotEmpty(t, ghostID, "ghost user row must exist before deletion")
|
||||
|
||||
// The same token must now be rejected with 401 user_not_found.
|
||||
resp := doReq(t, "GET", setup.Server.URL+"/api/collections", nil, ghostToken)
|
||||
defer resp.Body.Close()
|
||||
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode, "ghost token must be rejected")
|
||||
|
||||
var errBody map[string]string
|
||||
require.NoError(t, json.NewDecoder(resp.Body).Decode(&errBody))
|
||||
assert.Equal(t, "user_not_found", errBody["error"])
|
||||
}
|
||||
|
||||
// TestProgressWeb_LocatorCoherence pins the locator coherence rule: a
|
||||
// percentage-only write that DECLARES no locator (ClearLocator) must clear
|
||||
// any inherited stale anchor instead of re-pairing the new percentage with
|
||||
|
||||
Reference in new issue
Block a user