| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "encoding/base64" |
| 5 | "net/http" |
| 6 | "net/url" |
| 7 | "strings" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/home/handoff" |
| 11 | "amadan.net/rastrillo/rastrillo/assertion" |
| 12 | "amadan.net/rastrillo/rastrillo/sessions" |
| 13 | json "github.com/go-json-experiment/json" |
| 14 | "github.com/keymaildev/signin" |
| 15 | ) |
| 16 | |
| 17 | const assertionIntentCookie = "__Host-oficina-assertion-intent" |
| 18 | |
| 19 | type assertionIntent struct { |
| 20 | Consumer string `json:"consumer"` |
| 21 | Nonce string `json:"nonce"` |
| 22 | Expires int64 `json:"expires"` |
| 23 | MaxAge int `json:"max_age,omitzero"` |
| 24 | Subject string `json:"subject,omitzero"` |
| 25 | Started int64 `json:"started,omitzero"` |
| 26 | Previous string `json:"previous,omitzero"` |
| 27 | } |
| 28 | |
| 29 | func (a *App) handleAuthorize(w http.ResponseWriter, r *http.Request) { |
| 30 | w.Header().Set("Cache-Control", "no-store") |
| 31 | q, err := url.ParseQuery(r.URL.RawQuery) |
| 32 | maxAge := 0 |
| 33 | if values, present := q["max_age"]; present { |
| 34 | if len(values) != 1 || values[0] != "300" { |
| 35 | a.failAssertion(w, r, authorizeAge) |
| 36 | return |
| 37 | } |
| 38 | maxAge = 300 |
| 39 | } |
| 40 | wantFields := 2 |
| 41 | if maxAge != 0 { |
| 42 | wantFields++ |
| 43 | } |
| 44 | if err != nil || len(q) != wantFields || len(q["consumer"]) != 1 || len(q["nonce"]) != 1 || !handoff.ValidNonce(q.Get("nonce")) || a.consumers()[q.Get("consumer")] == "" { |
| 45 | a.failAssertion(w, r, authorizeRequest) |
| 46 | return |
| 47 | } |
| 48 | intent := assertionIntent{Consumer: q.Get("consumer"), Nonce: q.Get("nonce"), Expires: time.Now().Unix() + 600, MaxAge: maxAge} |
| 49 | if maxAge != 0 { |
| 50 | intent.Started = time.Now().Unix() |
| 51 | if c, err := r.Cookie(a.sessions.CookieName()); err == nil { |
| 52 | intent.Previous = sessions.HashToken(c.Value) |
| 53 | } |
| 54 | if session, ok := a.sessions.From(r); ok { |
| 55 | intent.Subject = session.Subject |
| 56 | } |
| 57 | } |
| 58 | b, err := json.Marshal(intent) |
| 59 | if err != nil { |
| 60 | a.failAssertion(w, r, authorizeEncode) |
| 61 | return |
| 62 | } |
| 63 | sealed, err := a.secrets.Sealer.Seal("assertion.intent", b) |
| 64 | if err != nil { |
| 65 | a.failAssertion(w, r, authorizeSeal) |
| 66 | return |
| 67 | } |
| 68 | http.SetCookie(w, &http.Cookie{Name: assertionIntentCookie, Value: base64.RawURLEncoding.EncodeToString(sealed), Path: "/", MaxAge: 600, Secure: true, HttpOnly: true, SameSite: http.SameSiteLaxMode}) |
| 69 | http.Redirect(w, r, "/authorize/resume", http.StatusSeeOther) |
| 70 | } |
| 71 | func (a *App) handleAssertion(w http.ResponseWriter, r *http.Request) { |
| 72 | w.Header().Set("Cache-Control", "no-store") |
| 73 | // no-referrer would turn the cross-origin form POST's Origin into null. |
| 74 | w.Header().Set("Referrer-Policy", "strict-origin-when-cross-origin") |
| 75 | if r.URL.RawQuery != "" { |
| 76 | a.failAssertion(w, r, assertionQuery) |
| 77 | return |
| 78 | } |
| 79 | if r.Method == http.MethodGet && !mintingNavigation(r) { |
| 80 | a.failAssertion(w, r, assertionNotNavigation) |
| 81 | return |
| 82 | } |
| 83 | now := time.Now() |
| 84 | intent, ok := a.readAssertionIntent(r, now) |
| 85 | if !ok { |
| 86 | a.failAssertion(w, r, assertionIntentInvalid) |
| 87 | return |
| 88 | } |
| 89 | // The operator confirm page's fresh intent carries a reserved consumer |
| 90 | // value that is never a consumer name; it can never mint an assertion. |
| 91 | target, ok := a.consumers()[intent.Consumer] |
| 92 | if !ok || intent.Consumer == operatorConfirmConsumer { |
| 93 | a.failAssertion(w, r, assertionConsumer) |
| 94 | return |
| 95 | } |
| 96 | tx, _, err := a.idTransaction(r.Context()) |
| 97 | if err != nil { |
| 98 | a.failAssertion(w, r, assertionSession) |
| 99 | return |
| 100 | } |
| 101 | defer tx.Rollback() |
| 102 | session, _, err := a.idSession(tx, r, now.Unix()) |
| 103 | if err != nil { |
| 104 | a.failAssertion(w, r, assertionSession) |
| 105 | return |
| 106 | } |
| 107 | authTime := session.AuthTime |
| 108 | if authTime.IsZero() { |
| 109 | authTime = session.At |
| 110 | } |
| 111 | if intent.Subject != "" && intent.Subject != session.Subject { |
| 112 | a.failAssertion(w, r, assertionSubject) |
| 113 | return |
| 114 | } |
| 115 | sameSession := false |
| 116 | if c, err := r.Cookie(a.sessions.CookieName()); err == nil { |
| 117 | sameSession = intent.Previous != "" && sessions.HashToken(c.Value) == intent.Previous |
| 118 | } |
| 119 | if intent.MaxAge != 0 && (sameSession || (intent.Started != 0 && authTime.Unix() < intent.Started) || !sessions.Fresh(session, time.Duration(intent.MaxAge)*time.Second, now) || authTime.After(now) || keymailWithoutGesture(session) || strings.Contains(session.Method, "recovery")) { |
| 120 | http.Redirect(w, r, "/signin?return_to=%2Fauthorize%2Fresume", http.StatusSeeOther) |
| 121 | return |
| 122 | } |
| 123 | // The one mint rule, shared with the native assert (native_assert.go). |
| 124 | if mintPolicy(intent.Consumer, session.Method) != nil { |
| 125 | a.failAssertion(w, r, assertionConsumer) |
| 126 | return |
| 127 | } |
| 128 | // Straight in (Paul, 2026-11-20; slice 1 spec, "Assertion and browser |
| 129 | // exchange" step 2): someone already signed in to id.* goes straight |
| 130 | // to the app. The GET mints, with no "Continue to the app" page, and |
| 131 | // is deliberately outside csrf.Protect's reach (a GET); only a |
| 132 | // top-level navigation mints (mintingNavigation). Everything it binds |
| 133 | // to is the victim's own: the intent cookie this browser was given by |
| 134 | // /authorize, this browser's id.* session, and the delivery, a form |
| 135 | // POST from this browser to the consumer, whose finish matches the |
| 136 | // nonce against that browser's own pending cookie, so no attacker |
| 137 | // identity can be planted. The accepted limits of Paul's ruling are |
| 138 | // wider than "signs the victim in to their own account" and are listed |
| 139 | // in the slice 1 spec (Assertion and browser exchange): any site can |
| 140 | // sign a victim in to any registered app with no click; any registered |
| 141 | // app learns a visitor's subject with no id.* interaction; a junk |
| 142 | // /authorize overwrites an in-flight intent; and session presence is |
| 143 | // visible to registered apps. A step-up or a link (max_age) never |
| 144 | // reaches here on a standing session: the check above sent it to the |
| 145 | // door for a new gesture. The POST, behind csrf.Protect, is kept for a |
| 146 | // Continue page still open in a tab. |
| 147 | id, err := assertion.RandomID() |
| 148 | if err != nil { |
| 149 | a.failAssertion(w, r, assertionRandom) |
| 150 | return |
| 151 | } |
| 152 | token, err := assertion.Sign(a.runtime.signer, assertion.Claims{Version: 1, Issuer: a.origins.Credential, Audience: target, Subject: session.Subject, KeyID: a.runtime.keyID, ID: id, Nonce: intent.Nonce, IssuedAt: now.Unix(), ExpiresAt: now.Unix() + 120, AuthTime: authTime.Unix()}) |
| 153 | if err != nil { |
| 154 | a.failAssertion(w, r, assertionSign) |
| 155 | return |
| 156 | } |
| 157 | if tx.Commit() != nil { |
| 158 | a.failAssertion(w, r, assertionSession) |
| 159 | return |
| 160 | } |
| 161 | http.SetCookie(w, &http.Cookie{Name: assertionIntentCookie, Value: "", Path: "/", MaxAge: -1, Secure: true, HttpOnly: true, SameSite: http.SameSiteLaxMode}) |
| 162 | // This page's form posts to one consumer; its form-action names that one |
| 163 | // origin and no other (team-provisioning-amendment.md §7). |
| 164 | a.setConsumerPolicy(w, target) |
| 165 | a.render(w, r, http.StatusOK, "assertion.html", map[string]any{"Action": target + handoff.CallbackPath, "Token": token}) |
| 166 | } |
| 167 | |
| 168 | // keymailWithoutGesture reports a session whose only factor is a Keymail |
| 169 | // approval that reported no gesture time. sessions.Fresh falls back to |
| 170 | // At for such a session, and At is when Home minted it, not when the |
| 171 | // person did anything: a standing Keymail inbox session would pass a |
| 172 | // step-up for being new. A second factor (Method "keymail+passkey", |
| 173 | // "keymail+recovery") sets AuthTime itself, so it is not caught here. |
| 174 | func keymailWithoutGesture(session sessions.Session) bool { |
| 175 | return session.Method == string(signin.MethodKeymail) && session.AuthTime.IsZero() |
| 176 | } |
| 177 | |
| 178 | // mintingNavigation reports whether a GET of /authorize/resume may mint: |
| 179 | // a top-level navigation, never a prefetch or prerender, and never a |
| 180 | // fetch, an image or a frame (defence in depth for straight in, Fable's |
| 181 | // review of 2026-11-20). A browser that sends no Sec-Fetch headers is |
| 182 | // judged on Sec-Purpose and Purpose alone; one that sends any must say |
| 183 | // navigate and document. |
| 184 | func mintingNavigation(r *http.Request) bool { |
| 185 | for _, name := range []string{"Sec-Purpose", "Purpose", "X-Purpose", "X-Moz"} { |
| 186 | for _, v := range r.Header.Values(name) { |
| 187 | v = strings.ToLower(v) |
| 188 | if strings.Contains(v, "prefetch") || strings.Contains(v, "prerender") || strings.Contains(v, "preview") { |
| 189 | return false |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | _, mode := r.Header["Sec-Fetch-Mode"] |
| 194 | _, dest := r.Header["Sec-Fetch-Dest"] |
| 195 | if mode || dest { |
| 196 | return r.Header.Get("Sec-Fetch-Mode") == "navigate" && r.Header.Get("Sec-Fetch-Dest") == "document" |
| 197 | } |
| 198 | return true |
| 199 | } |
| 200 | |