diff --git a/cmd/agentbbs/main.go b/cmd/agentbbs/main.go index b4aaf05..bdc4f6c 100644 --- a/cmd/agentbbs/main.go +++ b/cmd/agentbbs/main.go @@ -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 diff --git a/internal/store/store.go b/internal/store/store.go index f403dd7..94deb37 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -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/, 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 diff --git a/internal/store/store_admin_test.go b/internal/store/store_admin_test.go index 62ba4c5..fe32652 100644 --- a/internal/store/store_admin_test.go +++ b/internal/store/store_admin_test.go @@ -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) + } +} diff --git a/scripts/__pycache__/set-irc-password.cpython-312.pyc b/scripts/__pycache__/set-irc-password.cpython-312.pyc new file mode 100644 index 0000000..2b62554 Binary files /dev/null and b/scripts/__pycache__/set-irc-password.cpython-312.pyc differ diff --git a/scripts/set-irc-password.sh b/scripts/set-irc-password.sh index 79e1d15..d9ed278 100644 --- a/scripts/set-irc-password.sh +++ b/scripts/set-irc-password.sh @@ -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")