Every key needed an operator with a shell. This adds the login system that made that necessary, which is the thing I argued against building until it was asked for — so it is built as if it will be attacked, because a public login form is. Signing up produces an account that can do nothing. Approval is what makes it work and stays on the CLI: having a shell on the control plane is what makes someone an administrator, and that is a smaller thing to get right than roles and an admin UI that mints credentials. The details that matter: Passwords are argon2id, where a slow hash is correct — the input is chosen by a human and has a dictionary, which is the opposite of an API key. Both kinds of authentication now live in this codebase and they are in separate packages so neither set of assumptions leaks into the other. Length is the only rule: composition rules push people towards Password1! and cost more entropy than they buy. Signup never says whether an address already has an account, and sign-in answers the same way for a wrong password and a missing account — verifying against a dummy hash when there is no user, so a stopwatch cannot enumerate accounts. Account state is checked after the password, not before, so guessing an address reveals nothing about it. Guessing is bounded per address rather than per IP: an attacker has many addresses to come from and one to guess against, and limiting by IP locks out an office behind one NAT. Sessions are server-side, so signing out and suspending take effect immediately rather than whenever a token expires; suspension is checked in the same query that resolves the session, because a second round trip is a check somebody eventually forgets. Every state-changing form carries a CSRF token verified against a hash in the session — a cookie authenticates the browser, not the page that asked. Key ownership is enforced in the UPDATE rather than by reading first, so a guessed key id from another account changes nothing. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
8d35faf6f8
commit
216fc9bb03
22 files changed
+2263
-39
No files matched your search
@@ -0,0 +1,319 @@
|
||||
package e2e
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/config"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||||
)
|
||||
|
||||
// browser is a client that keeps cookies and does not follow redirects, so a
|
||||
// test can see them.
|
||||
type browser struct {
|
||||
t *testing.T
|
||||
base string
|
||||
c *http.Client
|
||||
}
|
||||
|
||||
func newBrowser(t *testing.T, base string) *browser {
|
||||
t.Helper()
|
||||
jar, err := cookiejar.New(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return &browser{t: t, base: base, c: &http.Client{
|
||||
Jar: jar,
|
||||
CheckRedirect: func(*http.Request, []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
}}
|
||||
}
|
||||
|
||||
func (b *browser) get(path string) (int, string) {
|
||||
b.t.Helper()
|
||||
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, b.base+path, nil)
|
||||
resp, err := b.c.Do(req)
|
||||
if err != nil {
|
||||
b.t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return resp.StatusCode, string(body)
|
||||
}
|
||||
|
||||
func (b *browser) post(path string, form url.Values) (int, string, string) {
|
||||
b.t.Helper()
|
||||
req, _ := http.NewRequestWithContext(context.Background(), http.MethodPost,
|
||||
b.base+path, strings.NewReader(form.Encode()))
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
resp, err := b.c.Do(req)
|
||||
if err != nil {
|
||||
b.t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return resp.StatusCode, string(body), resp.Header.Get("Location")
|
||||
}
|
||||
|
||||
// csrf pulls the token the server put in the page's forms.
|
||||
func csrf(t *testing.T, body string) string {
|
||||
t.Helper()
|
||||
const marker = `name="csrf" value="`
|
||||
i := strings.Index(body, marker)
|
||||
if i < 0 {
|
||||
t.Fatal("no csrf token in the page")
|
||||
}
|
||||
rest := body[i+len(marker):]
|
||||
return rest[:strings.IndexByte(rest, '"')]
|
||||
}
|
||||
|
||||
func signupHarness(t *testing.T) (*harness, string) {
|
||||
t.Helper()
|
||||
path, _ := provision(t)
|
||||
h := start(t, 2, func(c *config.Server) {
|
||||
c.Auth = config.Auth{
|
||||
Enabled: true, Store: path, SignupOpen: true,
|
||||
CacheTTL: 50 * time.Millisecond,
|
||||
}
|
||||
})
|
||||
return h, path
|
||||
}
|
||||
|
||||
const password = "a long enough passphrase"
|
||||
|
||||
func TestAnAccountIsUselessUntilItIsApproved(t *testing.T) {
|
||||
// The whole reason a public signup form is safe: it produces something that
|
||||
// cannot do anything.
|
||||
h, path := signupHarness(t)
|
||||
b := newBrowser(t, h.router.URL)
|
||||
|
||||
code, body, _ := b.post("/signup", url.Values{
|
||||
"email": {"ayse@example.com"}, "name": {"Ayse"}, "password": {password},
|
||||
})
|
||||
if code != http.StatusOK || !strings.Contains(body, "waiting for approval") {
|
||||
t.Fatalf("signup returned %d: %s", code, body)
|
||||
}
|
||||
|
||||
code, body, _ = b.post("/login", url.Values{
|
||||
"email": {"ayse@example.com"}, "password": {password},
|
||||
})
|
||||
if code != http.StatusUnauthorized || !strings.Contains(body, "waiting for approval") {
|
||||
t.Fatalf("a pending account signed in: %d", code)
|
||||
}
|
||||
|
||||
// The profile page is not reachable either.
|
||||
if code, _ := b.get("/profile"); code != http.StatusSeeOther {
|
||||
t.Errorf("an unauthenticated visit to /profile returned %d", code)
|
||||
}
|
||||
|
||||
// Approve, and it all works.
|
||||
withStore(t, path, func(s *store.SQLite) {
|
||||
users, err := s.Users(context.Background(), store.UserPending)
|
||||
if err != nil || len(users) != 1 {
|
||||
t.Fatalf("pending users: %v %v", users, err)
|
||||
}
|
||||
if err := s.ApproveUser(context.Background(), users[0].ID, "proj", "test", time.Now()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
|
||||
code, _, loc := b.post("/login", url.Values{
|
||||
"email": {"ayse@example.com"}, "password": {password},
|
||||
})
|
||||
if code != http.StatusSeeOther || loc != "/profile" {
|
||||
t.Fatalf("sign-in after approval returned %d to %q", code, loc)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAUserIssuesTheirOwnKeyAndItWorks(t *testing.T) {
|
||||
h, path := signupHarness(t)
|
||||
b := newBrowser(t, h.router.URL)
|
||||
signUpAndApprove(t, b, path, "ayse@example.com")
|
||||
|
||||
_, profile := b.get("/profile")
|
||||
token := csrf(t, profile)
|
||||
|
||||
code, body, _ := b.post("/profile/keys", url.Values{
|
||||
"csrf": {token}, "name": {"laptop"},
|
||||
})
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("creating a key returned %d", code)
|
||||
}
|
||||
key := extractKey(t, body)
|
||||
|
||||
// The key the person made for themselves is a real key.
|
||||
if code, msg := h.call(t, key, testModel); code != http.StatusOK {
|
||||
t.Fatalf("a self-issued key does not work: %d %s", code, msg)
|
||||
}
|
||||
|
||||
// And it is listed afterwards, without the secret.
|
||||
_, profile = b.get("/profile")
|
||||
if !strings.Contains(profile, "laptop") {
|
||||
t.Error("the key is not listed")
|
||||
}
|
||||
if strings.Contains(profile, key) {
|
||||
t.Error("the profile page shows the key again; it is meant to be shown once")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAUserCannotRevokeSomeoneElsesKey(t *testing.T) {
|
||||
// The ownership check is in the statement rather than a prior read, so a
|
||||
// guessed id from another account changes nothing.
|
||||
h, path := signupHarness(t)
|
||||
|
||||
ayse := newBrowser(t, h.router.URL)
|
||||
signUpAndApprove(t, ayse, path, "ayse@example.com")
|
||||
_, p1 := ayse.get("/profile")
|
||||
_, body, _ := ayse.post("/profile/keys", url.Values{"csrf": {csrf(t, p1)}, "name": {"ayse-key"}})
|
||||
ayseKey := extractKey(t, body)
|
||||
|
||||
var ayseKeyID string
|
||||
withStore(t, path, func(s *store.SQLite) {
|
||||
users, _ := s.Users(context.Background(), store.UserActive)
|
||||
for _, u := range users {
|
||||
if u.Email == "ayse@example.com" {
|
||||
keys, _ := s.KeysForUser(context.Background(), u.ID)
|
||||
if len(keys) != 1 {
|
||||
t.Fatalf("ayse has %d keys", len(keys))
|
||||
}
|
||||
ayseKeyID = keys[0].ID
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
mehmet := newBrowser(t, h.router.URL)
|
||||
signUpAndApprove(t, mehmet, path, "mehmet@example.com")
|
||||
_, p2 := mehmet.get("/profile")
|
||||
|
||||
if code, _, _ := mehmet.post("/profile/keys/"+ayseKeyID+"/revoke",
|
||||
url.Values{"csrf": {csrf(t, p2)}}); code != http.StatusSeeOther {
|
||||
t.Fatalf("status %d", code)
|
||||
}
|
||||
// It must still work.
|
||||
if code, msg := h.call(t, ayseKey, testModel); code != http.StatusOK {
|
||||
t.Errorf("someone else revoked this key: %d %s", code, msg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAFormWithoutItsTokenIsRefused(t *testing.T) {
|
||||
// A cookie authenticates the browser, not the page that asked. Without this,
|
||||
// any site the person visits can post here and have the browser attach the
|
||||
// session for it.
|
||||
h, path := signupHarness(t)
|
||||
b := newBrowser(t, h.router.URL)
|
||||
signUpAndApprove(t, b, path, "ayse@example.com")
|
||||
|
||||
for _, bad := range []url.Values{
|
||||
{"name": {"x"}},
|
||||
{"csrf": {""}, "name": {"x"}},
|
||||
{"csrf": {"forged"}, "name": {"x"}},
|
||||
} {
|
||||
if code, _, _ := b.post("/profile/keys", bad); code != http.StatusBadRequest {
|
||||
t.Errorf("a form with csrf=%q was accepted: %d", bad.Get("csrf"), code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSigningOutEndsTheSession(t *testing.T) {
|
||||
h, path := signupHarness(t)
|
||||
b := newBrowser(t, h.router.URL)
|
||||
signUpAndApprove(t, b, path, "ayse@example.com")
|
||||
|
||||
_, profile := b.get("/profile")
|
||||
if _, _, _ = b.post("/logout", url.Values{"csrf": {csrf(t, profile)}}); false {
|
||||
t.Fatal("unreachable")
|
||||
}
|
||||
if code, _ := b.get("/profile"); code != http.StatusSeeOther {
|
||||
t.Errorf("the profile was still reachable after signing out: %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSuspensionClosesOpenSessions(t *testing.T) {
|
||||
h, path := signupHarness(t)
|
||||
b := newBrowser(t, h.router.URL)
|
||||
id := signUpAndApprove(t, b, path, "ayse@example.com")
|
||||
|
||||
if code, _ := b.get("/profile"); code != http.StatusOK {
|
||||
t.Fatalf("profile before suspension: %d", code)
|
||||
}
|
||||
withStore(t, path, func(s *store.SQLite) {
|
||||
if err := s.SetUserState(context.Background(), id, store.UserSuspended); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
if code, _ := b.get("/profile"); code != http.StatusSeeOther {
|
||||
t.Errorf("a suspended account kept its session: %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignupCanBeClosed(t *testing.T) {
|
||||
path, _ := provision(t)
|
||||
h := start(t, 2, func(c *config.Server) {
|
||||
c.Auth = config.Auth{Enabled: true, Store: path, SignupOpen: false}
|
||||
})
|
||||
b := newBrowser(t, h.router.URL)
|
||||
|
||||
if code, body := b.get("/signup"); code == http.StatusOK && strings.Contains(body, "<form") {
|
||||
t.Errorf("the signup form is served although signup is closed: %d", code)
|
||||
}
|
||||
// The status the mux gives depends on which catch-all the path falls
|
||||
// through to, which is not the point. What matters is that no account
|
||||
// appears.
|
||||
if code, _, _ := b.post("/signup", url.Values{
|
||||
"email": {"a@example.com"}, "password": {password},
|
||||
}); code == http.StatusOK || code == http.StatusSeeOther {
|
||||
t.Errorf("signup accepted a post although it is closed: %d", code)
|
||||
}
|
||||
withStore(t, path, func(s *store.SQLite) {
|
||||
users, err := s.Users(context.Background(), "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(users) != 0 {
|
||||
t.Errorf("%d accounts exist although signup is closed", len(users))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// signUpAndApprove runs the operator's half and returns the new user's id.
|
||||
func signUpAndApprove(t *testing.T, b *browser, path, email string) string {
|
||||
t.Helper()
|
||||
if code, _, _ := b.post("/signup", url.Values{
|
||||
"email": {email}, "name": {email}, "password": {password},
|
||||
}); code != http.StatusOK {
|
||||
t.Fatalf("signup returned %d", code)
|
||||
}
|
||||
|
||||
var id string
|
||||
withStore(t, path, func(s *store.SQLite) {
|
||||
users, err := s.Users(context.Background(), store.UserPending)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, u := range users {
|
||||
if u.Email == email {
|
||||
id = u.ID
|
||||
}
|
||||
}
|
||||
if id == "" {
|
||||
t.Fatalf("no pending account for %s", email)
|
||||
}
|
||||
if err := s.ApproveUser(context.Background(), id, "proj", "test", time.Now()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
|
||||
if code, _, _ := b.post("/login", url.Values{
|
||||
"email": {email}, "password": {password},
|
||||
}); code != http.StatusSeeOther {
|
||||
t.Fatalf("sign-in returned %d", code)
|
||||
}
|
||||
return id
|
||||
}
|
||||
Reference in new issue
Block a user