oficina / home Public

Clone
git clone https://amadan.net/oficina/home

Plain git — no account needed to clone.

Download

Download this file

1package home
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "testing"
7
8 "amadan.net/oficina/estilo.git/sky"
9 "amadan.net/oficina/estilo.git/theme"
10 "github.com/go-chi/chi/v5"
11)
12
13// sharedPublicPaths are served on BOTH Home origins: the design tokens, the
14// brand marks, Calma's font and sky, and the front page's stylesheet and fonts. They are listed
15// once because two copies of this list would drift, and a route that
16// quietly stopped being shared — or quietly started — is exactly what the
17// two tests below exist to catch.
18var sharedPublicPaths = []string{
19 "/healthz", "/static/tokens.css", "/static/theme.css",
20 "/static/brand/icon.svg", "/static/brand/icon-32.png", "/static/brand/apple-touch-icon.png", "/static/brand/brand.css", "/static/brand/LICENSE.lucide", "/static/brand/docs.svg", "/static/brand/sheets.svg", "/static/brand/slides.svg", "/static/brand/calendar.svg", "/static/brand/calls.svg", "/static/brand/memoria.svg",
21 "/static/brand/fichas.svg", "/static/brand/mail.svg", "/static/brand/files.svg",
22 // Calma: the sky and its stylesheet, Home's rules for placing it, and
23 // estilo's retired v1 Onest files, still served for old pages.
24 "/static/estilo/fonts/v1/*", sky.Prefix + "*", "/static/estilo/sky.css", "/static/calma.css",
25 // The hand-off pages' reveal rule and its script-off override: both
26 // origins draw a hop page.
27 "/static/handoff.css", "/static/handoff-noscript.css",
28 "/static/busy.css", "/static/busy.js",
29 "/static/site/home.css", "/static/site/apps.css",
30 // estilo's shared chrome: base links, menus, the bar and the door.
31 "/static/estilo/base.css", "/static/estilo/menu.css", "/static/estilo/chrome.css",
32}
33
34// examplePath is a real file under a wildcard route, so fetching it
35// proves the handler is mounted rather than merely that chi has a pattern.
36func examplePath(route string) string {
37 switch route {
38 case "/static/estilo/fonts/v1/*":
39 return theme.FontPrefix + "onest-latin.woff2"
40 case sky.Prefix + "*":
41 for _, layer := range sky.For(sky.SuiteSeed).Layers {
42 if layer.Src != "" { // the placeholder is inline, with no URL
43 return layer.Src
44 }
45 }
46 }
47 return route
48}
49
50func sharedPublic(route string) bool {
51 for _, p := range sharedPublicPaths {
52 if p == route {
53 return true
54 }
55 }
56 return false
57}
58
59func TestEveryCredentialRouteRefusesDirectoryHost(t *testing.T) {
60 app := newTestApp(t)
61 router := app.Router()
62 routes := router.(chi.Routes)
63 count := 0
64 err := chi.Walk(routes, func(method, route string, _ http.Handler, _ ...func(http.Handler) http.Handler) error {
65 // "/" is the front page, which both origins answer (the credential
66 // host redirects rather than rendering — see the test below).
67 switch {
68 case route == "/", route == "/help/open-source", route == "/static/credits.css", sharedPublic(route):
69 return nil
70 }
71 count++
72 req := httptest.NewRequest(method, "https://"+directoryHost+route, nil)
73 req.Header.Set("Origin", Origin)
74 rec := httptest.NewRecorder()
75 router.ServeHTTP(rec, req)
76 if rec.Code != http.StatusNotFound || len(rec.Result().Cookies()) != 0 || rec.Body.Len() != 0 {
77 t.Errorf("%s %s on directory: status=%d cookies=%d body=%q", method, route, rec.Code, len(rec.Result().Cookies()), rec.Body.String())
78 }
79 return nil
80 })
81 if err != nil || count < 19 {
82 t.Fatalf("route walk err=%v credential routes=%d", err, count)
83 }
84 if _, ok := app.mailer.last(); ok {
85 t.Fatal("directory request sent mail")
86 }
87}
88
89func TestDirectoryContentNeverServedOnCredentialHost(t *testing.T) {
90 app := newTestApp(t)
91 req := httptest.NewRequest(http.MethodGet, Origin+"/?team=untrusted", nil)
92 rec := httptest.NewRecorder()
93 app.Router().ServeHTTP(rec, req)
94 if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != directoryOrigin+"/" || rec.Body.Len() != 0 {
95 t.Fatalf("credential root: status=%d location=%q body=%q", rec.Code, rec.Header().Get("Location"), rec.Body.String())
96 }
97}
98
99func TestUnknownHostsCannotReachRoutes(t *testing.T) {
100 app := newTestApp(t)
101 router := app.Router()
102 hosts := []string{"", "example.com", "oficina.works.attacker.invalid", "id.oficina.works.attacker.invalid", "id.oficina.works.", "id.oficina.works:443", "oficina.works:443", "localhost"}
103 err := chi.Walk(router.(chi.Routes), func(method, route string, _ http.Handler, _ ...func(http.Handler) http.Handler) error {
104 for _, host := range hosts {
105 req := httptest.NewRequest(method, route, nil)
106 req.Host = host
107 req.Header.Set("Origin", Origin)
108 req.Header.Set("X-Forwarded-Host", credentialHost)
109 req.Header.Set("Forwarded", "host="+credentialHost)
110 rec := httptest.NewRecorder()
111 router.ServeHTTP(rec, req)
112 if rec.Code != http.StatusNotFound || len(rec.Result().Cookies()) != 0 || rec.Body.Len() != 0 {
113 t.Errorf("%s %s host=%q status=%d", method, route, host, rec.Code)
114 }
115 }
116 return nil
117 })
118 if err != nil {
119 t.Fatal(err)
120 }
121}
122
123func TestDirectoryIsNotWithinCredentialRP(t *testing.T) {
124 app := newTestApp(t)
125 if credentialHost == directoryHost || app.credentialHost == directoryHost || Origin == directoryOrigin {
126 t.Fatal("directory overlaps the credential origin or relying party")
127 }
128}
129
130func TestSharedRoutesOnBothOrigins(t *testing.T) {
131 app := newTestApp(t)
132 for _, host := range []string{credentialHost, directoryHost} {
133 for _, path := range sharedPublicPaths {
134 path = examplePath(path)
135 req := httptest.NewRequest(http.MethodGet, "https://"+host+path, nil)
136 rec := httptest.NewRecorder()
137 app.Router().ServeHTTP(rec, req)
138 if rec.Code != http.StatusOK || len(rec.Result().Cookies()) != 0 {
139 t.Errorf("host=%s path=%s status=%d cookies=%d", host, path, rec.Code, len(rec.Result().Cookies()))
140 }
141 }
142 }
143}
144
145func TestDirectoryCredentialPostRefusedBeforeCSRF(t *testing.T) {
146 app := newTestApp(t)
147 req := httptest.NewRequest(http.MethodPost, directoryOrigin+"/signin", nil)
148 rec := httptest.NewRecorder()
149 app.Router().ServeHTTP(rec, req)
150 if rec.Code != http.StatusNotFound || rec.Body.Len() != 0 {
151 t.Fatalf("directory credential POST status=%d body=%q", rec.Code, rec.Body.String())
152 }
153}
154