security: reject unsafe usernames at account creation

The hub auto-created an account from the raw SSH login name for any new
key, skipping SanitizeUsername. Names containing '/', '..', spaces or
shell metacharacters were accepted, and the per-user directory
users/<name> could resolve outside users/.

store.EnsureUser now refuses to insert a name that fails
ValidUsername (^[a-z0-9][a-z0-9-]{1,31}$), covering every creation path;
the hub prints a clear error. The root IRC password helper applies the
same rule to its argv.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Anthony Ettinger 2026-11-20 02:38:31 +00:00
parent 87cfa61b1f
commit 0910e54f66
5 changed files with 71 additions and 1 deletions

View file

@ -497,6 +497,10 @@ func (a *app) teaHandler(s ssh.Session) (tea.Model, []tea.ProgramOption) {
if err == nil && !found {
su, err = a.st.EnsureUser(username, string(auth.KindFor(username)), fp)
}
if errors.Is(err, store.ErrInvalidUsername) {
wish.Fatalln(s, "usernames are 2-32 lowercase letters, digits or dashes. Try ssh join@"+a.host)
return nil, nil
}
if errors.Is(err, store.ErrKeyMismatch) {
wish.Fatalln(s, "that username is registered with a different SSH key.")
return nil, nil

View file

@ -345,6 +345,32 @@ type DomainMap struct {
// ErrKeyMismatch means a username is already registered with another key.
var ErrKeyMismatch = errors.New("username registered with a different key")
// ErrInvalidUsername is returned when a new account name is not a safe handle.
var ErrInvalidUsername = errors.New("invalid username: use 2-32 lowercase letters, digits or dashes, starting with a letter or digit")
// ValidUsername reports whether name is safe to create an account under.
// The name becomes a filesystem path (users/<name>, the pod home), a forge
// and mail account and an argv element of root helpers, so it is held to
// ^[a-z0-9][a-z0-9-]{1,31}$: no "/", "..", spaces, shell metacharacters or a
// leading "-". On 2026-11-19 an attacker logged in as
// "../../../../home/agentbbs/.ssh", "$(id)" and "-oproxycommand=zz"; the
// auto-signup path created all of them and the first wrote into ~/.ssh.
func ValidUsername(name string) bool {
if len(name) < 2 || len(name) > 32 {
return false
}
for i := 0; i < len(name); i++ {
c := name[i]
switch {
case c >= 'a' && c <= 'z', c >= '0' && c <= '9':
case c == '-' && i > 0:
default:
return false
}
}
return true
}
// ErrDomainTaken means a domain is already mapped to a different member.
var ErrDomainTaken = errors.New("domain already mapped to another account")
@ -560,6 +586,9 @@ func (s *sqliteStore) EnsureUser(name, kind, fp string) (User, error) {
u, err := scanUser(s.db.QueryRow(`SELECT `+userCols+` FROM users WHERE name = ?`, name))
switch {
case errors.Is(err, sql.ErrNoRows):
if !ValidUsername(name) {
return User{}, ErrInvalidUsername
}
res, err := s.db.Exec(`INSERT INTO users (name, kind, pubkey_fp) VALUES (?,?,?)`, name, kind, fp)
if err != nil {
return User{}, err

View file

@ -1,6 +1,7 @@
package store
import (
"errors"
"path/filepath"
"testing"
)
@ -164,3 +165,32 @@ func TestRecentChatsAll(t *testing.T) {
t.Fatalf("unexpected first chat: %+v", chats[0])
}
}
func TestValidUsername(t *testing.T) {
good := []string{"chovy", "agent-eogops", "member-1mdutzu9", "iji825404gmailcom", "z3n", "ab"}
bad := []string{"", "a", "-oproxycommand=zz", "$(id)", "`id`", "a b", "x;id>/tmp/p",
"../../../../etc", "x/../../opt/agentbbs/.git", "../..", "./run", "Chovy", "a_b", "a.b",
"abcdefghijklmnopqrstuvwxyz0123456"}
for _, n := range good {
if !ValidUsername(n) {
t.Errorf("ValidUsername(%q) = false, want true", n)
}
}
for _, n := range bad {
if ValidUsername(n) {
t.Errorf("ValidUsername(%q) = true, want false", n)
}
}
}
func TestEnsureUserRejectsUnsafeNames(t *testing.T) {
st := openTest(t)
for _, n := range []string{"../../../../home/agentbbs/.ssh", "$(id)", "-oproxycommand=zz"} {
if _, err := st.EnsureUser(n, "member", "SHA256:x"+n); !errors.Is(err, ErrInvalidUsername) {
t.Errorf("EnsureUser(%q) err = %v, want ErrInvalidUsername", n, err)
}
}
if _, err := st.EnsureUser("okname", "member", "SHA256:ok"); err != nil {
t.Fatalf("EnsureUser(okname): %v", err)
}
}

Binary file not shown.

View file

@ -21,7 +21,7 @@
#
# Run as root on the BBS box. The member must already be a BBS member; this only
# sets the secret — membership itself is still gated by /irc-auth.
import sys, os, json, glob, secrets, hashlib, pwd, grp, subprocess
import sys, os, re, json, glob, secrets, hashlib, pwd, grp, subprocess
PASSWD_FILE = os.environ.get("ERGO_IRC_PASSWD", "/var/lib/ergo/irc-passwd")
LOUNGE_USERS = os.environ.get("AGENTBBS_LOUNGE_USERS", "/var/lib/thelounge/users")
@ -35,6 +35,9 @@ LOUNGE_RESET_CMD = os.environ.get(
"AGENTBBS_LOUNGE_RESET_CMD", "docker exec -i thelounge thelounge reset"
)
ITERS = 200_000
# Same rule as store.ValidUsername. This runs as root via sudo with a member
# name in argv, and the name is joined into file paths and a docker command.
VALID_MEMBER = re.compile(r"^[a-z0-9][a-z0-9-]{1,31}$")
def hash_pw(pw):
@ -134,6 +137,7 @@ def main(argv):
store = load_store()
if argv[0] == "--all":
members = sorted(os.path.basename(p)[:-5] for p in glob.glob(os.path.join(LOUNGE_USERS, "*.json")))
members = [m for m in members if VALID_MEMBER.match(m)]
done = []
for m in members:
if m not in store:
@ -146,6 +150,9 @@ def main(argv):
print(f"provisioned {len(done)} member(s) that were missing a password")
return 0
member = argv[0]
if not VALID_MEMBER.match(member):
print(f"refusing unsafe member name {member!r}", file=sys.stderr)
return 2
from_stdin = len(argv) > 1 and argv[1] == "-"
if from_stdin:
pw = sys.stdin.readline().rstrip("\n")