mirror of
https://github.com/profullstack/agentbbs.git
synced 2026-11-29 15:31:53 +00:00
security: reject unsafe usernames at account creation (#171)
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:
parent
87cfa61b1f
commit
388360012c
5 changed files with 71 additions and 1 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue