Merge commit from fork

* fix(login): gate username change on the change-username step

handleChangeUsername renamed the account identified by authReq.UserID
without checking that the login flow had reached the change-username
step or that any factor had succeeded. Because authReq.UserID is bound
as soon as a login name is submitted (checkLoginName -> SetUserInfo),
an unauthenticated caller could start a flow, type a victim's login
name and then POST a new username to rename that account, including
instance admins.

Gate the handler on the current step being *domain.ChangeUsernameStep,
mirroring the other login handlers. That step is only produced in
nextSteps after the first factor and any required MFA have succeeded,
so the gate also proves the caller owns the account. The legitimate
forced-username-change flow is unaffected: the UI only ever renders
that form when the step is at index 0, and PossibleSteps is recomputed
on every request.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Aup17GXd4tQnXG36xr5UiZ

* test(login): add integration reproduction for GHSA-4hgj-wm6c-q7p2

Drive the unauthenticated-rename attack over raw HTTP against a running
instance, mirroring the sibling GHSA-738m external-login integration
test in the same package: authorize (V1) -> load login page (scrape
CSRF) -> POST the victim's login name (binds authReq.UserID, no factor)
-> POST a new username.

The test first proves the exploit precondition held - step 3 advanced
the flow to the victim's password step, so authReq.UserID was bound -
then asserts over gRPC that the victim's username is unchanged. The
precondition check guards against a false green: without it, a broken
upstream hop would make the unchanged-username assertion pass vacuously
even against unpatched code.

The unchanged-username poll and the sibling test's requireNeverUserByEmail
now share a small pollFor helper in the package.

Red -> green: fails against the unpatched handler (the rename lands),
passes against the fixed handler (the ChangeUsernameStep gate rejects
the POST before the command runs). Runs in the existing
nx test-integration job via the integration build tag.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Aup17GXd4tQnXG36xr5UiZ

---------

Co-authored-by: Claude Opus 4.8 <[email protected]>
This commit is contained in:
gayathri
2026-09-21 13:37:27 +02:00
committed by GitHub
co-authored by Claude Opus 4.8
parent 3ff4fbc7e4
commit 18a4f883f0
4 changed files with 326 additions and 7 deletions
@@ -245,11 +245,26 @@ func doPostForm(t *testing.T, c *http.Client, rawURL string, form url.Values) st
return string(b)
}
// requireNeverUserByEmail fails if a user with the given email appears within waitFor.
func requireNeverUserByEmail(t *testing.T, email string, waitFor, tick time.Duration) {
// pollFor runs check every tick until waitFor elapses (and once more at the deadline). check should
// require an invariant that must hold for the whole window; it fails the test on violation via
// require.*. It proves a negative - that a forbidden change never appears even under projection lag -
// without assert.Never, whose background goroutine can outlive the test and panic on CTX cancel.
func pollFor(t *testing.T, waitFor, tick time.Duration, check func()) {
t.Helper()
deadline := time.Now().Add(waitFor)
for {
check()
if !time.Now().Before(deadline) {
return
}
time.Sleep(tick)
}
}
// requireNeverUserByEmail fails if a user with the given email appears within waitFor.
func requireNeverUserByEmail(t *testing.T, email string, waitFor, tick time.Duration) {
t.Helper()
pollFor(t, waitFor, tick, func() {
resp, err := Instance.Client.UserV2.ListUsers(CTX, &user.ListUsersRequest{
Queries: []*user.SearchQuery{
{Query: &user.SearchQuery_EmailQuery{EmailQuery: &user.EmailQuery{EmailAddress: email}}},
@@ -258,9 +273,5 @@ func requireNeverUserByEmail(t *testing.T, email string, waitFor, tick time.Dura
require.NoError(t, err)
require.Empty(t, resp.GetResult(),
"a user was created for the attacker-forged external identity %q - account pre-hijack succeeded", email)
if !time.Now().Before(deadline) {
return
}
time.Sleep(tick)
}
})
}