oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "bytes"
5 "encoding/base64"
6 "encoding/json"
7 "math"
8 "net/http"
9 "net/url"
10 "time"
11
12 "amadan.net/oficina/home/connect"
13 "amadan.net/oficina/home/handoff"
14 "amadan.net/rastrillo/rastrillo"
15)
16
17const homeReturnCookie = "__Host-oficina-docs-home-return"
18const homeReturnLimit = 4096
19const returnLifetime = int64(600)
20
21// localLinkCompletePath is where a local sign-in lands (rastrillo/auth's
22// SignedInPath), connect's path with Home and Docs' own without.
23const localLinkCompletePath = connect.LocalCompletePath
24
25// returnMemory is the page a sign-in began from (spec §9.4), in a sealed,
26// ten-minute cookie: Docs' own, beside connect's, because connect lands a
27// local sign-in on a fixed path and keeps its memory to itself
28// (home_connect.go). Standalone Docs uses it too (signin_return.go).
29type returnMemory struct {
30 sealer handoff.Sealer
31 origin, host string
32 now func() time.Time
33}
34
35func newReturnMemory(sealer handoff.Sealer, origin string) *returnMemory {
36 u, err := url.Parse(origin)
37 if err != nil || u.Host == "" || u.User != nil || u.Path != "" || u.RawQuery != "" || u.Fragment != "" || sealer == nil {
38 return nil
39 }
40 return &returnMemory{sealer: sealer, origin: origin, host: u.Host, now: time.Now}
41}
42
43func localNavigationCookie(w http.ResponseWriter, name, value string, age int) {
44 http.SetCookie(w, &http.Cookie{Name: name, Value: value, Path: "/", Secure: true, HttpOnly: true, SameSite: http.SameSiteLaxMode, MaxAge: age})
45}
46
47func (n *returnMemory) guard(w http.ResponseWriter, r *http.Request, path string) bool {
48 w.Header().Set("Cache-Control", "no-store")
49 if r.Method != http.MethodGet {
50 w.Header().Set("Allow", http.MethodGet)
51 http.Error(w, rastrillo.T(r, "docs.error.generic"), http.StatusMethodNotAllowed)
52 return false
53 }
54 if r.Host != n.host || r.URL.Path != path || r.URL.RawQuery != "" {
55 http.Redirect(w, r, "/", http.StatusSeeOther)
56 return false
57 }
58 return true
59}
60
61type homeReturnIntent struct {
62 Path string `json:"path"`
63 Issued int64 `json:"issued"`
64 Expires int64 `json:"expires"`
65}
66
67// The remembered page authorizes nothing: not linking, and not access to
68// the document it names.
69func (n *returnMemory) rememberReturn(w http.ResponseWriter, path string) bool {
70 if !handoff.SafePath(path) {
71 path = "/"
72 }
73 now := n.now().Unix()
74 if now <= 0 || now > math.MaxInt64-returnLifetime {
75 return false
76 }
77 raw, err := json.Marshal(homeReturnIntent{path, now, now + returnLifetime})
78 if err != nil {
79 return false
80 }
81 sealed, err := n.sealer.Seal("docs.home-return.v1:"+n.origin, raw)
82 if err != nil {
83 return false
84 }
85 value := base64.RawURLEncoding.EncodeToString(sealed)
86 if len(value) > homeReturnLimit {
87 return false
88 }
89 localNavigationCookie(w, homeReturnCookie, value, int(returnLifetime))
90 return true
91}
92
93func (n *returnMemory) returnPath(r *http.Request) string {
94 value := ""
95 if cookieCount(r, homeReturnCookie) != 1 {
96 return "/"
97 }
98 cookie, err := r.Cookie(homeReturnCookie)
99 if err != nil {
100 return "/"
101 }
102 value = cookie.Value
103 if value == "" || len(value) > homeReturnLimit {
104 return "/"
105 }
106 sealed, err := base64.RawURLEncoding.Strict().DecodeString(value)
107 if err != nil || base64.RawURLEncoding.EncodeToString(sealed) != value {
108 return "/"
109 }
110 raw, err := n.sealer.Open("docs.home-return.v1:"+n.origin, sealed)
111 if err != nil || len(raw) > homeReturnLimit {
112 return "/"
113 }
114 var intent homeReturnIntent
115 if json.Unmarshal(raw, &intent) != nil {
116 return "/"
117 }
118 canonical, _ := json.Marshal(intent)
119 now := n.now().Unix()
120 if !bytes.Equal(raw, canonical) || intent.Issued <= 0 || intent.Issued > now || intent.Expires <= now || intent.Expires <= intent.Issued || intent.Expires-intent.Issued > returnLifetime || !handoff.SafePath(intent.Path) {
121 return "/"
122 }
123 return intent.Path
124}
125