Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions api/internal/auth/sftp_login_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,28 @@ 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) {
var (
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 {
if chosen.Valid && taken == 0 {
return chosen.String, true, nil
}
if display.Valid && display.String != username && ValidSFTPLoginName(display.String) {
Expand Down Expand Up @@ -114,26 +121,24 @@ 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, 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
}
if !errors.Is(err, sql.ErrNoRows) {
return "", fmt.Errorf("resolve sftp login name: %w", err)
}
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)
}
if !ValidSFTPLoginName(name) {
return name, nil
}
Expand Down
24 changes: 24 additions & 0 deletions api/internal/auth/sftp_login_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion api/specs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 3 additions & 0 deletions changelog/unreleased/1096-sftp-panel-username-wins.md
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion specs/040-sftp-access/contracts/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
2 changes: 1 addition & 1 deletion specs/040-sftp-access/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down