From 8b1ba6dbb8b7586c94564dce46410ddfb4b03959 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 24 Nov 2026 11:30:19 +0000 Subject: [PATCH 1/3] fix(api): panel username always wins over a chosen SFTP username A user could choose another person's email as their SFTP username before that person signed up, which blocked that person's own SFTP login. Login now matches panel usernames first, and a chosen name that another account later takes as its panel username stops working until the user chooses again. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Claude --- api/internal/auth/sftp_login_name.go | 28 ++++++++++++++++------- api/internal/auth/sftp_login_name_test.go | 24 +++++++++++++++++++ api/specs.md | 2 +- specs/040-sftp-access/contracts/api.md | 2 +- specs/040-sftp-access/spec.md | 2 +- 5 files changed, 47 insertions(+), 11 deletions(-) diff --git a/api/internal/auth/sftp_login_name.go b/api/internal/auth/sftp_login_name.go index f72ada2ff..2b663bf8f 100644 --- a/api/internal/auth/sftp_login_name.go +++ b/api/internal/auth/sftp_login_name.go @@ -50,7 +50,8 @@ func nameClaims(ctx context.Context, q queryer, name string, userID int64) (int, return n, nil } -// SFTPLoginName returns the name userID logs in with over SFTP: the chosen name when set; otherwise the +// SFTPLoginName returns the name userID logs in with over SFTP: the chosen name when set and no other +// account has it as its panel username; otherwise the // display name when it is a valid login name and no other account holds it as a username, chosen name or // display name; otherwise the panel username. custom reports whether the name was chosen. func (s *SessionStore) SFTPLoginName(ctx context.Context, userID int64) (name string, custom bool, err error) { @@ -65,7 +66,17 @@ func (s *SessionStore) SFTPLoginName(ctx context.Context, userID int64) (name st return "", false, fmt.Errorf("sftp login name: %w", err) } if chosen.Valid { - return chosen.String, true, nil + // A panel username always wins at login, so a chosen name that another account later took as its + // panel username no longer logs in; the default applies until the user chooses again. + var taken int + if err := s.db.DB.QueryRowContext(ctx, + `SELECT COUNT(*) FROM users WHERE id <> ? AND username = ?`, userID, chosen.String, + ).Scan(&taken); err != nil { + return "", false, fmt.Errorf("sftp login name: chosen: %w", err) + } + if taken == 0 { + return chosen.String, true, nil + } } if display.Valid && display.String != username && ValidSFTPLoginName(display.String) { n, err := nameClaims(ctx, s.db.DB, display.String, userID) @@ -114,25 +125,26 @@ func (s *SessionStore) SetSFTPLoginName(ctx context.Context, userID int64, name return nil } -// ResolveSFTPLoginName maps the name typed at the SFTP login to a panel username. A chosen SFTP login name -// matches first, then a panel username (it always works), then a display name that is the account's +// ResolveSFTPLoginName maps the name typed at the SFTP login to a panel username. A panel username matches +// first (it always works, even when another account chose the same name before this account existed), +// then a chosen SFTP login name, then a display name that is the account's // default login name (see SFTPLoginName). An unknown name comes back unchanged, and the lookup that // follows refuses it like any other unknown user. The credential is still checked against that account. func (s *SessionStore) ResolveSFTPLoginName(ctx context.Context, name string) (string, error) { var username string - err := s.db.DB.QueryRowContext(ctx, `SELECT username FROM users WHERE sftp_username = ?`, name).Scan(&username) + err := s.db.DB.QueryRowContext(ctx, `SELECT username FROM users WHERE username = ?`, name).Scan(&username) if err == nil { return username, nil } if !errors.Is(err, sql.ErrNoRows) { - return "", fmt.Errorf("resolve sftp login name: %w", err) + return "", fmt.Errorf("resolve sftp login name: username: %w", err) } - err = s.db.DB.QueryRowContext(ctx, `SELECT username FROM users WHERE username = ?`, name).Scan(&username) + err = s.db.DB.QueryRowContext(ctx, `SELECT username FROM users WHERE sftp_username = ?`, name).Scan(&username) if err == nil { return username, nil } if !errors.Is(err, sql.ErrNoRows) { - return "", fmt.Errorf("resolve sftp login name: username: %w", err) + return "", fmt.Errorf("resolve sftp login name: %w", err) } if !ValidSFTPLoginName(name) { return name, nil diff --git a/api/internal/auth/sftp_login_name_test.go b/api/internal/auth/sftp_login_name_test.go index e45efa944..10e6f00bf 100644 --- a/api/internal/auth/sftp_login_name_test.go +++ b/api/internal/auth/sftp_login_name_test.go @@ -127,3 +127,27 @@ func TestSFTPLoginName_DatabaseErrors(t *testing.T) { } } } + +// A panel username always wins: an account created later with the name another user chose gets its own +// login back, and the user who chose the name falls back to the default until they choose again. +func TestSFTPLoginName_LaterUsernameWins(t *testing.T) { + ctx := context.Background() + s, _ := sftpFault(ctx, t) + early := addNameUser(ctx, t, s, "early@example.com", "earlyname") + if err := s.SetSFTPLoginName(ctx, early, "late@example.com"); err != nil { + t.Fatalf("set: %v", err) + } + if got, _ := s.ResolveSFTPLoginName(ctx, "late@example.com"); got != "early@example.com" { + t.Fatalf("resolve before the account exists = %q", got) + } + addNameUser(ctx, t, s, "late@example.com", "latename") + if got, err := s.ResolveSFTPLoginName(ctx, "late@example.com"); err != nil || got != "late@example.com" { + t.Fatalf("resolve after the account exists = %q, %v; want the new account", got, err) + } + if got, custom, err := s.SFTPLoginName(ctx, early); err != nil || got != "earlyname" || custom { + t.Fatalf("early user's login name = %q, %v, %v; want earlyname default", got, custom, err) + } + if got, _ := s.ResolveSFTPLoginName(ctx, "earlyname"); got != "early@example.com" { + t.Fatalf("early user's default does not resolve: %q", got) + } +} diff --git a/api/specs.md b/api/specs.md index e84fe3dff..8a2eec96e 100644 --- a/api/specs.md +++ b/api/specs.md @@ -352,7 +352,7 @@ trust assumptions, rotation behavior and surfaces outside this protocol. - `POST /clusters/{name}/sftp-token`: mint a `gpsftps_` service token, returned once (201). At most two active per cluster; a third returns 409 `server.sftp.token_limit_reached`. Only the digest is stored. - `DELETE /clusters/{name}/sftp-token/{id}`: revoke (204). Tokens of other clusters are not found. -**Endpoint-facing routes** (`sftp_login.go`, mounted under `/sftp/v1` outside the protected group, authenticated by the service token or the session token the endpoint holds, never by a dashboard cookie): `POST /sftp/v1/login` resolves username and credential to a session; `POST /sftp/v1/sessions/check` re-validates open sessions (revocation and user disable end them within 60 s, SC-006); `POST /sftp/v1/sessions/end`; `GET /sftp/v1/servers` lists the servers the session can read. Login accepts the panel username as the part before the last `+` (local accounts and SSO accounts named by email address alike): non-empty valid UTF-8, at most 254 runes, no whitespace, control characters or `/`; the name is matched against chosen SFTP usernames (`users.sftp_username`, set with `PUT /users/me/sftp/login-name`), then panel usernames, then display names that are their account's default SFTP username (a display name no other account holds as username, SFTP username or display name); the user lookup then decides whether the account exists and the credential is checked against that account. The username rate-limit budget keys on that full name. Every refusal gives the endpoint and the audit trail one generic `server.sftp.login_failed`, and the API logs the cause at info level with the username and calling cluster. Every file operation after login goes to the agent through the normal file routes as the signed-in user, with the same RBAC checks as the Files tab, and is audited with `source: sftp`. +**Endpoint-facing routes** (`sftp_login.go`, mounted under `/sftp/v1` outside the protected group, authenticated by the service token or the session token the endpoint holds, never by a dashboard cookie): `POST /sftp/v1/login` resolves username and credential to a session; `POST /sftp/v1/sessions/check` re-validates open sessions (revocation and user disable end them within 60 s, SC-006); `POST /sftp/v1/sessions/end`; `GET /sftp/v1/servers` lists the servers the session can read. Login accepts the panel username as the part before the last `+` (local accounts and SSO accounts named by email address alike): non-empty valid UTF-8, at most 254 runes, no whitespace, control characters or `/`; the name is matched against panel usernames, then chosen SFTP usernames (`users.sftp_username`, set with `PUT /users/me/sftp/login-name`; a chosen name that another account later took as its panel username stops working and the default applies), then display names that are their account's default SFTP username (a display name no other account holds as username, SFTP username or display name); the user lookup then decides whether the account exists and the credential is checked against that account. The username rate-limit budget keys on that full name. Every refusal gives the endpoint and the audit trail one generic `server.sftp.login_failed`, and the API logs the cause at info level with the username and calling cluster. Every file operation after login goes to the agent through the normal file routes as the signed-in user, with the same RBAC checks as the Files tab, and is audited with `source: sftp`. **Permissions:** reading needs `servers:read` (or ownership or collaborator access); writing, renaming and deleting need `servers:write` (or the same ownership or collaborator access). The data-root and server folders are read-only over SFTP. diff --git a/specs/040-sftp-access/contracts/api.md b/specs/040-sftp-access/contracts/api.md index f5ba91f3e..5139d5127 100644 --- a/specs/040-sftp-access/contracts/api.md +++ b/specs/040-sftp-access/contracts/api.md @@ -11,7 +11,7 @@ Request: `{"username": "alice" | "alice+survival" | "alice+mods/survival", "pass Response 200: `{"sessionToken": "gpsftp_…", "expiresAt": "…", "user": "alice", "target": null | {"namespace": "mods", "server": "survival"}}`. Response 401: generic. Response 429: rate limited (same limiter keys as `/auth/login`: username and source address). -The API splits `username` at the last `+` (R4). The part before it is matched, in order, against chosen SFTP usernames, panel usernames, and display names that are their account's default SFTP username (FR-005a), so SSO users need not type their email address. A target login also asks the file-access check (research R8) whether the user may list the target; failure is the same 401. +The API splits `username` at the last `+` (R4). The part before it is matched, in order, against panel usernames, chosen SFTP usernames, and display names that are their account's default SFTP username (FR-005a), so SSO users need not type their email address. A target login also asks the file-access check (research R8) whether the user may list the target; failure is the same 401. ### `POST /sftp/v1/sessions/check` diff --git a/specs/040-sftp-access/spec.md b/specs/040-sftp-access/spec.md index e75f1e377..5c4daaa76 100644 --- a/specs/040-sftp-access/spec.md +++ b/specs/040-sftp-access/spec.md @@ -132,7 +132,7 @@ A user stops a server, or it falls asleep when idle, and then opens its folder o **Authentication** -- **FR-005a**: Each user MUST have one SFTP username (valgul, 2026-11-24). By default it is their display name when that name is a valid login name (no space, control character, `/` or `+`, at most 64 characters) and no other account has it as username, SFTP username or display name; otherwise it is their panel username (for SSO, usually the email address). The user MUST be able to choose another SFTP username in Settings, SFTP, and clear the choice to return to the default. A chosen name another account holds as username, SFTP username or display name MUST be refused. The panel username MUST keep working as a login name. +- **FR-005a**: Each user MUST have one SFTP username (valgul, 2026-11-24). By default it is their display name when that name is a valid login name (no space, control character, `/` or `+`, at most 64 characters) and no other account has it as username, SFTP username or display name; otherwise it is their panel username (for SSO, usually the email address). The user MUST be able to choose another SFTP username in Settings, SFTP, and clear the choice to return to the default. A chosen name another account holds as username, SFTP username or display name MUST be refused. The panel username MUST keep working as a login name and wins over a chosen name: if another account later takes a chosen name as its panel username, the chosen name stops working and the default applies. - **FR-005**: Users MUST authenticate with an SFTP credential tied to their Gameplane user, which works for local and SSO users alike: an SSH public key or a generated SFTP password (OD-2). The dashboard login password MUST NOT be accepted. - **FR-006**: Users MUST be able to add, list and remove their own SFTP SSH public keys in their profile; admins with `users:manage` MUST be able to list and remove any user's. - **FR-006a**: Each user MUST be able to have at most one generated SFTP password. Gameplane generates it, shows it exactly once, and stores only a hash. It never expires. The user can only regenerate it (the old one stops working at once) or revoke it; admins with `users:manage` can revoke any user's. From 25c4ef2289b9c125578343b326d1aa895e560019 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 24 Nov 2026 11:36:44 +0000 Subject: [PATCH 2/3] fix(api): fold the SFTP username lookups into single queries Folds the chosen-name check into the account lookup and the panel-username and chosen-name matches into one ordered query, so each path has one database error branch (changed-lines coverage gate). Co-Authored-By: Claude Opus 5.5 Signed-off-by: Claude --- api/internal/auth/sftp_login_name.go | 39 ++++++++++++---------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/api/internal/auth/sftp_login_name.go b/api/internal/auth/sftp_login_name.go index 2b663bf8f..38c2837db 100644 --- a/api/internal/auth/sftp_login_name.go +++ b/api/internal/auth/sftp_login_name.go @@ -59,24 +59,20 @@ func (s *SessionStore) SFTPLoginName(ctx context.Context, userID int64) (name st username string chosen sql.NullString display sql.NullString + taken int ) - if err := s.db.DB.QueryRowContext(ctx, - `SELECT username, sftp_username, display_name FROM users WHERE id = ?`, userID, - ).Scan(&username, &chosen, &display); err != nil { + // taken counts other accounts whose panel username is the chosen name: a panel username always wins + // at login, so such a chosen name no longer logs in and the default applies until the user chooses + // again. + if err := s.db.DB.QueryRowContext(ctx, ` + SELECT u.username, u.sftp_username, u.display_name, + (SELECT COUNT(*) FROM users o WHERE o.id <> u.id AND o.username = u.sftp_username) + FROM users u WHERE u.id = ?`, userID, + ).Scan(&username, &chosen, &display, &taken); err != nil { return "", false, fmt.Errorf("sftp login name: %w", err) } - if chosen.Valid { - // A panel username always wins at login, so a chosen name that another account later took as its - // panel username no longer logs in; the default applies until the user chooses again. - var taken int - if err := s.db.DB.QueryRowContext(ctx, - `SELECT COUNT(*) FROM users WHERE id <> ? AND username = ?`, userID, chosen.String, - ).Scan(&taken); err != nil { - return "", false, fmt.Errorf("sftp login name: chosen: %w", err) - } - if taken == 0 { - return chosen.String, true, nil - } + if chosen.Valid && taken == 0 { + return chosen.String, true, nil } if display.Valid && display.String != username && ValidSFTPLoginName(display.String) { n, err := nameClaims(ctx, s.db.DB, display.String, userID) @@ -132,14 +128,11 @@ func (s *SessionStore) SetSFTPLoginName(ctx context.Context, userID int64, name // follows refuses it like any other unknown user. The credential is still checked against that account. func (s *SessionStore) ResolveSFTPLoginName(ctx context.Context, name string) (string, error) { var username string - err := s.db.DB.QueryRowContext(ctx, `SELECT username FROM users WHERE username = ?`, name).Scan(&username) - if err == nil { - return username, nil - } - if !errors.Is(err, sql.ErrNoRows) { - return "", fmt.Errorf("resolve sftp login name: username: %w", err) - } - err = s.db.DB.QueryRowContext(ctx, `SELECT username FROM users WHERE sftp_username = ?`, name).Scan(&username) + err := s.db.DB.QueryRowContext(ctx, ` + SELECT username, 0 AS prio FROM users WHERE username = ? + UNION ALL + SELECT username, 1 AS prio FROM users WHERE sftp_username = ? + ORDER BY prio LIMIT 1`, name, name).Scan(&username, new(int)) if err == nil { return username, nil } From 777d591508972042d0794ba8b25cd729b0ebb497 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 24 Nov 2026 11:37:41 +0000 Subject: [PATCH 3/3] docs(changelog): SFTP panel username wins over a chosen name Co-Authored-By: Claude Opus 5.5 Signed-off-by: Claude --- changelog/unreleased/1096-sftp-panel-username-wins.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelog/unreleased/1096-sftp-panel-username-wins.md diff --git a/changelog/unreleased/1096-sftp-panel-username-wins.md b/changelog/unreleased/1096-sftp-panel-username-wins.md new file mode 100644 index 000000000..e0e02ae6f --- /dev/null +++ b/changelog/unreleased/1096-sftp-panel-username-wins.md @@ -0,0 +1,3 @@ +# SFTP login: a panel username always wins over a chosen SFTP username + +At SFTP login a panel username now matches before a chosen SFTP username, so nobody can block another person's login by choosing their email address as an SFTP username before that person's account exists. If another account later takes a chosen SFTP username as its panel username, the user who chose it falls back to their default SFTP username until they choose a new one under **Settings, SFTP**. [PR #1096](https://github.com/GameplanePanel/Gameplane/pull/1096)