mirror of
https://github.com/profullstack/agentbbs.git
synced 2026-12-01 17:24:48 +00:00
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:
parent
87cfa61b1f
commit
0910e54f66
5 changed files with 71 additions and 1 deletions
|
|
@ -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