~bigbes/sr-ht-spec

ref: 8219ede1804c144de2a2f2e42f3f476bff4b622a sr-ht-spec/authn/cookie_test.go -rw-r--r-- 2.9 KiB
8219ede1 — Eugene Blikh feat(web,service): the owner mints and revokes agent tokens in a browser 13 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package authn

import (
	"testing"

	"sourcecraft.dev/bigbes/sr-ht-core/crypto"
)

func TestUsernameFromRequest_ValidCookieRoundTrips(t *testing.T) {
	got := UsernameFromRequest(request(sealCookie(t, "bigbes"), nil))
	if got != "bigbes" {
		t.Fatalf("username = %q, want %q", got, "bigbes")
	}
}

func TestUsernameFromRequest_StripsTilde(t *testing.T) {
	got := UsernameFromRequest(request(sealCookie(t, "~bigbes"), nil))
	if got != "bigbes" {
		t.Fatalf("username = %q, want %q", got, "bigbes")
	}
}

// A cookie whose ciphertext has been altered must fail the Fernet HMAC and read
// as anonymous — not as an error, and certainly not as an identity.
func TestUsernameFromRequest_TamperedCookieIsAnonymous(t *testing.T) {
	got := UsernameFromRequest(request(tamper(t, sealCookie(t, "bigbes")), nil))
	if got != "" {
		t.Fatalf("tampered cookie yielded %q, want anonymous", got)
	}
}

func TestUsernameFromRequest_AbsentCookieIsAnonymous(t *testing.T) {
	if got := UsernameFromRequest(request("", nil)); got != "" {
		t.Fatalf("absent cookie yielded %q, want anonymous", got)
	}
}

func TestUsernameFromRequest_GarbageIsAnonymous(t *testing.T) {
	for name, value := range map[string]string{
		"not base64":       "not-a-valid-fernet-token",
		"empty":            "",
		"truncated fernet": sealCookie(t, "bigbes")[:10],
	} {
		t.Run(name, func(t *testing.T) {
			if got := UsernameFromCookie(value); got != "" {
				t.Fatalf("garbage cookie yielded %q, want anonymous", got)
			}
		})
	}
}

// A cookie sealed under a key we no longer hold — the shape of both a rotated
// network key and an outright forgery. It must expire the session, not the
// request.
func TestUsernameFromCookie_ForeignKeyIsAnonymous(t *testing.T) {
	value := sealCookieWithKey(t, &rotatedKey, "bigbes")
	if got := UsernameFromCookie(value); got != "" {
		t.Fatalf("cookie under a foreign key yielded %q, want anonymous", got)
	}
	// Sanity: the same payload under the live key does resolve, so the test
	// above is proving the key check and not a broken helper.
	if got := UsernameFromCookie(sealCookie(t, "bigbes")); got != "bigbes" {
		t.Fatalf("control cookie yielded %q, want %q", got, "bigbes")
	}
}

func TestUsernameFromCookie_NonJSONPayloadIsAnonymous(t *testing.T) {
	value := string(crypto.Encrypt([]byte("plain text, well sealed")))
	if got := UsernameFromCookie(value); got != "" {
		t.Fatalf("non-JSON payload yielded %q, want anonymous", got)
	}
}

// A well-sealed cookie can still carry a name we must refuse to treat as an
// identity: empty, or something that would not survive being used as a path
// segment or a log field.
func TestUsernameFromCookie_UnusableNameIsAnonymous(t *testing.T) {
	for _, name := range []string{
		"",
		"..",
		"../../etc/passwd",
		"has space",
		"Uppercase",
		"-leading-dash",
	} {
		t.Run(name, func(t *testing.T) {
			if got := UsernameFromCookie(sealCookie(t, name)); got != "" {
				t.Fatalf("cookie name %q yielded %q, want anonymous", name, got)
			}
		})
	}
}