Files
bryanpedini 99fa5de747 feat(auth): add argon2id passwords, sessions, rate limiter, authorization and audit builders
- feat(auth): add argon2id passwords, sessions, rate limiter, authorization and audit builders
- feat(cli): add check and audit commands
- feat(api): add api framework, login, page routes and account, user, delegation and audit handlers
- test(auth): cover passwords, sessions, rate limiter, authorization and audit builders
- test(api): cover the api framework, sessions, client origin, headers and cross-origin refusal
- test(api): cover account, user, delegation and audit handlers and the authorization matrix
- test(ui): cover login, logout, page routes and the page authorization matrix
2026-11-19 15:46:12 +01:00

174 lines
4.2 KiB
Go

// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2026 Bryan Joshua Pedini
package main
import (
"strconv"
"testing"
"time"
)
func newTestLimiter(max int) (*loginLimiter, *testClock) {
c := &testClock{now: webNow}
return newLoginLimiter(c.Now, max), c
}
func TestLimiterBlocksAfterFiveFailuresPerUsername(t *testing.T) {
l, c := newTestLimiter(100)
for i := range 4 {
l.fail("alice", "192.0.2."+strconv.Itoa(i))
if !l.allow("alice", "192.0.2.99") {
t.Fatalf("blocked after %d failures", i+1)
}
}
l.fail("alice", "192.0.2.4")
if l.allow("alice", "192.0.2.99") {
t.Fatal("not blocked after five failures")
}
if !l.allow("bob", "192.0.2.99") {
t.Fatal("other username blocked")
}
c.advance(loginBlockDuration - time.Second)
if l.allow("alice", "192.0.2.99") {
t.Fatal("block shorter than 15 minutes")
}
l.fail("alice", "192.0.2.99")
c.advance(time.Second)
if !l.allow("alice", "192.0.2.99") {
t.Fatal("block longer than 15 minutes or extended by failures while blocked")
}
}
func TestLimiterBlocksPerOrigin(t *testing.T) {
l, _ := newTestLimiter(100)
for i := range 5 {
l.fail("user"+strconv.Itoa(i), "198.51.100.7")
}
if l.allow("fresh", "198.51.100.7") {
t.Fatal("origin not blocked")
}
if !l.allow("fresh", "198.51.100.8") {
t.Fatal("other origin blocked")
}
}
func TestLimiterWindow(t *testing.T) {
l, c := newTestLimiter(100)
for range 4 {
l.fail("alice", "192.0.2.1")
}
c.advance(loginFailureWindow)
l.fail("alice", "192.0.2.1")
if !l.allow("alice", "192.0.2.1") {
t.Fatal("failures older than the window counted")
}
c.advance(time.Minute)
for range 3 {
l.fail("alice", "192.0.2.2")
}
if !l.allow("alice", "192.0.2.3") {
t.Fatal("blocked after four failures in the window")
}
l.fail("alice", "192.0.2.2")
if l.allow("alice", "192.0.2.3") {
t.Fatal("five failures in the window not blocked")
}
}
func TestLimiterSuccessResetsUsername(t *testing.T) {
l, _ := newTestLimiter(100)
for range 4 {
l.fail("alice", "192.0.2.1")
}
l.succeed("alice")
for range 4 {
l.fail("alice", "192.0.2.2")
}
if !l.allow("alice", "192.0.2.3") {
t.Fatal("success did not reset the username counter")
}
l.fail("alice", "192.0.2.1")
if l.allow("bob", "192.0.2.1") {
t.Fatal("success reset the origin counter")
}
}
func TestLimiterBoundedMemory(t *testing.T) {
l, c := newTestLimiter(10)
for i := range 1000 {
l.fail("u"+strconv.Itoa(i), "192.0.2.1")
c.advance(time.Millisecond)
}
if len(l.users.entries) > 10 || len(l.origins.entries) > 10 {
t.Fatal(len(l.users.entries), len(l.origins.entries))
}
}
func TestLimiterCannotEvictBlockedKey(t *testing.T) {
l, c := newTestLimiter(3)
for range 5 {
l.fail("victim", "192.0.2.1")
}
if l.allow("victim", "192.0.2.200") {
t.Fatal("victim not blocked")
}
for i := range 50 {
l.fail("noise"+strconv.Itoa(i), "203.0.113."+strconv.Itoa(i))
c.advance(time.Second)
}
if l.allow("victim", "192.0.2.200") {
t.Fatal("flooding freed a blocked username")
}
if l.allow("x", "192.0.2.1") {
t.Fatal("flooding freed a blocked origin")
}
if len(l.users.entries) > 3 || len(l.origins.entries) > 3 {
t.Fatal("table grew")
}
}
func TestLimiterFullOfBlockedKeys(t *testing.T) {
l, c := newTestLimiter(2)
for _, u := range []string{"a", "b"} {
for range 5 {
l.fail(u, "192.0.2."+u)
}
}
if l.allow("c", "192.0.2.c") {
t.Fatal("new key admitted while the table holds only blocked keys")
}
l.fail("c", "192.0.2.c")
if _, ok := l.users.entries["c"]; ok {
t.Fatal("new key stored past the bound")
}
c.advance(loginBlockDuration)
if !l.allow("c", "192.0.2.c") {
t.Fatal("expired blocks not pruned")
}
l.fail("c", "192.0.2.c")
if len(l.users.entries) != 1 {
t.Fatal(len(l.users.entries))
}
}
func TestLimiterEvictsOldestUnblocked(t *testing.T) {
tbl := newLimitTable(2)
tbl.fail("old", webNow)
tbl.fail("new", webNow.Add(time.Second))
tbl.fail("third", webNow.Add(2*time.Second))
if _, ok := tbl.entries["old"]; ok {
t.Fatal("oldest entry kept")
}
if _, ok := tbl.entries["new"]; !ok {
t.Fatal("newer entry evicted")
}
if !tbl.allows("fourth", webNow.Add(3*time.Second)) {
t.Fatal("unblocked entries can make room")
}
empty := &limitEntry{}
if !empty.lastFailure().IsZero() {
t.Fatal("last failure of empty entry")
}
}