include/psaml/psaml_flow.h

filter: Statementstmt 94.1% 113 / 120 Branchbran 55.5% 60 / 108 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.8% 173 / 228 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/response.xs ↑ Summary include/psaml/psaml_compat.h »
1 #ifndef PSAML_FLOW_H
2 #define PSAML_FLOW_H
3  
4 /* The flow cookie: what is remembered between sending an AuthnRequest and
5 * receiving the Response.
6 *
7 * WHY IT IS NOT THE SESSION, which is the whole reason this file exists.
8 * The POST to the assertion consumer comes from the provider's origin. It
9 * is a cross-site POST, and a cookie with SameSite=Lax IS NOT SENT on
10 * one: Lax covers top-level navigations with safe methods only. Punk's
11 * session cookie is Lax, as it should be, so at the moment the assertion
12 * arrives the session is not there and a flow record kept in it is
13 * unreachable. Every login would fail `in_response_to`, and only in
14 * browsers new enough to default to Lax, which by now is all of them.
15 * The industry spent 2020 learning this.
16 *
17 * So the record lives in a cookie of the plugin's own, SameSite=None,
18 * which browsers only send with Secure, which is why phase 4 croaks
19 * unless `host` is https.
20 *
21 * NOT ENCRYPTED, signed only. There is nothing in a record that was not
22 * already in the AuthnRequest the browser carried in a URL a moment ago.
23 * The signature is not for secrecy, it is so the record cannot be
24 * written by anyone but us.
25 *
26 * Must be included after psaml_boot.h. */
27  
28 #include "psaml_abi.h"
29 #include "psaml_b64.h"
30 #include "psaml_clos.h"
31  
32 #define PSAML_FLOW_COOKIE "_saml_flow"
33 #define PSAML_FLOW_KEEP 5
34  
35 /* ---- the serialised form ----------------------------------------------- */
36  
37 /* Records are `id|idp|to|issued` joined by tabs and newline-separated,
38 * base64url'd, then `.` and the MAC. The fields cannot contain a tab or a
39 * newline: an id is hex, a provider name is a bareword, and `to` has been
40 * through safe_path. Anything that did contain one would be a record this
41 * dist did not write, and the MAC catches that first. */
42  
43 72 PERL_STATIC_INLINE SV *psaml_flow_mac(pTHX_ SV *payload, SV *secret) {
44 STRLEN pl, sl;
45 72 ▶ const unsigned char *pp = (const unsigned char *)SvPVbyte(payload, pl);
Branch: 1 branch
BranchTrueFalseTotal
const unsigned char *pp = (const unsigned char *)SvPVbyte(payload, pl); 72 0 72
46 72 const unsigned char *sp = (const unsigned char *)SvPVbyte(secret, sl);
47 72 return psaml_jws(aTHX)->hmac_sha256(aTHX_ sp, sl, pp, pl);
48 }
49  
50 /* The signing key is the FIRST of a `secret` list, and every one of them
51 * verifies. That is what lets a key be rotated with both live: deploy the
52 * new one at the front, and the cookies signed by the old one keep
53 * working until they expire. */
54 72 PERL_STATIC_INLINE SV *psaml_secret_at(pTHX_ SV *secret, SSize_t i) {
55 72 ▶ if (psaml_is_array(aTHX_ secret)) {
Branch: 1 branch
BranchTrueFalseTotal
if (psaml_is_array(aTHX_ secret)) { 0 72 72
56 0 AV *av = (AV *)SvRV(secret);
57 0 SV **e = av_fetch(av, i, 0);
58 0 ▶ return (e && *e && SvOK(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvOK(*e)) ? *e : NULL; 0 0 0
return (e && *e && SvOK(*e)) ? *e : NULL; 0 0 0
return (e && *e && SvOK(*e)) ? *e : NULL; 0 0 0
59 }
60 72 ▶ return i == 0 ? secret : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return i == 0 ? secret : NULL; 72 0 72
61 }
62  
63 59 PERL_STATIC_INLINE SV *psaml_flow_serialise(pTHX_ AV *records, SV *secret) {
64 59 SV *payload = sv_2mortal(newSVpvs(""));
65 SV *b64, *mac, *out;
66 59 SSize_t i, n = av_len(records) + 1;
67 59 SV *key = psaml_secret_at(aTHX_ secret, 0);
68  
69 83 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 24 59 83
70 24 SV **e = av_fetch(records, i, 0);
71 HV *r;
72 SV *v;
73 24 ▶ if (!e || !*e || !SvROK(*e)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!e || !*e || !SvROK(*e)) continue; 24 0 24
if (!e || !*e || !SvROK(*e)) continue; 24 0 24
if (!e || !*e || !SvROK(*e)) continue; 0 24 24
74 24 r = (HV *)SvRV(*e);
75 24 ▶ if (SvCUR(payload)) sv_catpvs(payload, "\n");
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(payload)) sv_catpvs(payload, "\n"); 1 23 24
76 24 ▶ v = psaml_hget(aTHX_ r, "id"); if (v) sv_catsv(payload, v);
Branch: 1 branch
BranchTrueFalseTotal
v = psaml_hget(aTHX_ r, "id"); if (v) sv_catsv(payload, v); 24 0 24
77 24 sv_catpvs(payload, "\t");
78 24 ▶ v = psaml_hget(aTHX_ r, "idp"); if (v) sv_catsv(payload, v);
Branch: 1 branch
BranchTrueFalseTotal
v = psaml_hget(aTHX_ r, "idp"); if (v) sv_catsv(payload, v); 24 0 24
79 24 sv_catpvs(payload, "\t");
80 24 ▶ v = psaml_hget(aTHX_ r, "to"); if (v) sv_catsv(payload, v);
Branch: 1 branch
BranchTrueFalseTotal
v = psaml_hget(aTHX_ r, "to"); if (v) sv_catsv(payload, v); 24 0 24
81 24 sv_catpvs(payload, "\t");
82 24 v = psaml_hget(aTHX_ r, "issued");
83 24 ▶ sv_catpvf(payload, "%" IVdf, v ? SvIV(v) : 0);
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvf(payload, "%" IVdf, v ? SvIV(v) : 0); 24 0 24
84 }
85  
86 {
87 STRLEN pl;
88 59 const unsigned char *pp = (const unsigned char *)SvPVbyte(payload, pl);
89 59 b64 = sv_2mortal(psaml_jws(aTHX)->b64url(aTHX_ pp, pl));
90 }
91 59 ▶ if (!key) croak("%s: the flow cookie has no secret to sign with",
Branch: 1 branch
BranchTrueFalseTotal
if (!key) croak("%s: the flow cookie has no secret to sign with", 0 59 59
92 PSAML_WHO);
93 59 mac = psaml_flow_mac(aTHX_ b64, key);
94 59 ▶ if (!mac) croak("%s: the flow cookie MAC failed", PSAML_WHO);
Branch: 1 branch
BranchTrueFalseTotal
if (!mac) croak("%s: the flow cookie MAC failed", PSAML_WHO); 0 59 59
95 59 sv_2mortal(mac);
96  
97 59 out = newSVsv(b64);
98 59 sv_catpvs(out, ".");
99 {
100 STRLEN ml;
101 59 const unsigned char *mp = (const unsigned char *)SvPVbyte(mac, ml);
102 59 SV *m64 = sv_2mortal(psaml_jws(aTHX)->b64url(aTHX_ mp, ml));
103 59 sv_catsv(out, m64);
104 }
105 59 return out;
106 }
107  
108 /* Parse and verify. Returns a +1 AV of records, EMPTY when the cookie is
109 * absent, malformed, or does not verify.
110 *
111 * A tampered cookie reads as no cookie rather than an exception. A
112 * browser holding a stale record from a previous deployment, or a cookie
113 * somebody edited, should get `unsolicited` and a "sign in again" page,
114 * not a 500 and a stack trace in the log. */
115 59 PERL_STATIC_INLINE AV *psaml_flow_parse(pTHX_ SV *cookie, SV *secret,
116 IV now, IV ttl) {
117 59 AV *out = newAV();
118 const jws_abi *J;
119 STRLEN cl;
120 const char *cp;
121 const char *dot;
122 SV *b64, *given, *want;
123 SSize_t k;
124 59 int ok = 0;
125  
126 59 ▶ if (!cookie || !SvOK(cookie) || !SvCUR(cookie)) return out;
Branch: 3 branches
BranchTrueFalseTotal
if (!cookie || !SvOK(cookie) || !SvCUR(cookie)) return out; 13 46 59
if (!cookie || !SvOK(cookie) || !SvCUR(cookie)) return out; 13 0 13
if (!cookie || !SvOK(cookie) || !SvCUR(cookie)) return out; 0 13 13
127 13 J = psaml_jws(aTHX);
128 13 cp = SvPVbyte(cookie, cl);
129 13 dot = (const char *)memchr(cp, '.', cl);
130 13 ▶ if (!dot) return out;
Branch: 1 branch
BranchTrueFalseTotal
if (!dot) return out; 0 13 13
131  
132 13 b64 = sv_2mortal(newSVpvn(cp, (STRLEN)(dot - cp)));
133 13 given = sv_2mortal(J->b64url_decode(aTHX_ dot + 1,
134 cl - (STRLEN)(dot - cp) - 1));
135 13 ▶ if (!given) return out;
Branch: 1 branch
BranchTrueFalseTotal
if (!given) return out; 0 13 13
136  
137 /* every configured secret verifies; the first signs. ct_eq because a
138 * timing difference here is a byte-at-a-time forgery of the MAC. */
139 26 ▶ for (k = 0; !ok; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; !ok; k++) { 13 13 26
140 13 SV *key = psaml_secret_at(aTHX_ secret, k);
141 13 ▶ if (!key) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!key) break; 0 13 13
142 13 want = psaml_flow_mac(aTHX_ b64, key);
143 13 ▶ if (!want) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!want) continue; 0 13 13
144 13 sv_2mortal(want);
145 {
146 STRLEN al, bl;
147 13 ▶ const unsigned char *ap = (const unsigned char *)SvPVbyte(want, al);
Branch: 1 branch
BranchTrueFalseTotal
const unsigned char *ap = (const unsigned char *)SvPVbyte(want, al); 13 0 13
148 13 const unsigned char *bp = (const unsigned char *)SvPVbyte(given, bl);
149 13 ok = J->ct_eq(aTHX_ ap, al, bp, bl);
150 }
151 }
152 13 ▶ if (!ok) return out;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return out; 0 13 13
153  
154 {
155 13 SV *plain = sv_2mortal(J->b64url_decode(aTHX_ SvPVX(b64), SvCUR(b64)));
156 STRLEN pl;
157 const char *pp;
158 const char *line, *end;
159 13 ▶ if (!plain) return out;
Branch: 1 branch
BranchTrueFalseTotal
if (!plain) return out; 0 13 13
160 13 pp = SvPVbyte(plain, pl);
161 13 line = pp;
162 13 end = pp + pl;
163 13 ▶ while (line < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (line < end) { 12 1 13
164 12 const char *nl = (const char *)memchr(line, '\n', (STRLEN)(end - line));
165 12 ▶ const char *stop = nl ? nl : end;
Branch: 1 branch
BranchTrueFalseTotal
const char *stop = nl ? nl : end; 0 12 12
166 const char *f[4];
167 STRLEN fl[4];
168 12 int nf = 0;
169 12 const char *s = line;
170 48 ▶ while (nf < 4 && s <= stop) {
Branch: 2 branches
BranchTrueFalseTotal
while (nf < 4 && s <= stop) { 48 0 48
while (nf < 4 && s <= stop) { 48 0 48
171 48 const char *tab = (const char *)memchr(s, '\t', (STRLEN)(stop - s));
172 48 ▶ const char *fe = (nf == 3 || !tab) ? stop : tab;
Branch: 2 branches
BranchTrueFalseTotal
const char *fe = (nf == 3 || !tab) ? stop : tab; 36 12 48
const char *fe = (nf == 3 || !tab) ? stop : tab; 0 36 36
173 48 f[nf] = s;
174 48 fl[nf] = (STRLEN)(fe - s);
175 48 nf++;
176 48 ▶ if (fe == stop) break;
Branch: 1 branch
BranchTrueFalseTotal
if (fe == stop) break; 12 36 48
177 36 s = fe + 1;
178 }
179 12 ▶ if (nf == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (nf == 4) { 12 0 12
180 12 IV issued = 0;
181 STRLEN i;
182 132 ▶ for (i = 0; i < fl[3]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < fl[3]; i++) 120 12 132
183 120 ▶ if (f[3][i] >= '0' && f[3][i] <= '9')
Branch: 2 branches
BranchTrueFalseTotal
if (f[3][i] >= '0' && f[3][i] <= '9') 120 0 120
if (f[3][i] >= '0' && f[3][i] <= '9') 120 0 120
184 120 issued = issued * 10 + (f[3][i] - '0');
185 /* one older than flow_ttl is dropped on read as well as on
186 * write, so a cookie that outlived a deploy cannot resurrect a
187 * flow the browser has forgotten about */
188 12 ▶ if (!ttl || issued + ttl > now) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ttl || issued + ttl > now) { 12 0 12
if (!ttl || issued + ttl > now) { 12 0 12
189 12 HV *r = newHV();
190 12 (void)hv_stores(r, "id", newSVpvn(f[0], fl[0]));
191 12 (void)hv_stores(r, "idp", newSVpvn(f[1], fl[1]));
192 12 (void)hv_stores(r, "to", newSVpvn(f[2], fl[2]));
193 12 (void)hv_stores(r, "issued", newSViv(issued));
194 12 av_push(out, newRV_noinc((SV *)r));
195 }
196 }
197 12 ▶ if (!nl) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) break; 12 0 12
198 0 line = nl + 1;
199 }
200 }
201 13 return out;
202 }
203  
204 /* Add a record, keeping the newest PSAML_FLOW_KEEP.
205 *
206 * Several coexist because a user opens two tabs and starts two logins,
207 * and the second must not evict the first. Five is enough for that and
208 * small enough that the cookie stays well inside every browser's
209 * per-cookie limit. */
210 23 PERL_STATIC_INLINE void psaml_flow_add(pTHX_ AV *records, SV *rec) {
211 23 av_push(records, rec);
212 23 ▶ while (av_len(records) + 1 > PSAML_FLOW_KEEP) {
Branch: 1 branch
BranchTrueFalseTotal
while (av_len(records) + 1 > PSAML_FLOW_KEEP) { 0 23 23
213 0 SV *old = av_shift(records);
214 0 SvREFCNT_dec(old);
215 }
216 23 }
217  
218 /* Remove the record with this id and return it (+1), or NULL.
219 *
220 * The ACS calls this and writes the cookie back BEFORE verifying
221 * anything: one flow answers once, whichever way the verification goes.
222 * A record left in place on failure would let an attacker retry a
223 * tampered assertion against the same flow until something got through. */
224 20 PERL_STATIC_INLINE SV *psaml_flow_take(pTHX_ AV *records, SV *id) {
225 20 SSize_t i, n = av_len(records) + 1;
226 20 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 11 9 20
227 11 SV **e = av_fetch(records, i, 0);
228 HV *r;
229 SV *rid;
230 11 ▶ if (!e || !*e || !SvROK(*e)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!e || !*e || !SvROK(*e)) continue; 11 0 11
if (!e || !*e || !SvROK(*e)) continue; 11 0 11
if (!e || !*e || !SvROK(*e)) continue; 0 11 11
231 11 r = (HV *)SvRV(*e);
232 11 rid = psaml_hget(aTHX_ r, "id");
233 11 ▶ if (rid && sv_eq(rid, id)) {
Branch: 2 branches
BranchTrueFalseTotal
if (rid && sv_eq(rid, id)) { 11 0 11
if (rid && sv_eq(rid, id)) { 11 0 11
234 /* +1 first, then G_DISCARD.
235 *
236 * av_delete WITHOUT G_DISCARD hands the element back already
237 * mortalised: it takes the array's reference and puts it on the
238 * temps stack. Incrementing as well and then mortalising in the
239 * caller decrements it twice, which surfaces as "Attempt to free
240 * unreferenced scalar" one request later. G_DISCARD drops the
241 * array's reference and mortalises nothing, so this returns a
242 * clean +1 the caller owns. */
243 11 SV *found = SvREFCNT_inc_simple_NN(*e);
244 11 (void)av_delete(records, i, G_DISCARD);
245 11 return found;
246 }
247 }
248 9 return NULL;
249 }
250  
251 /* The Set-Cookie value.
252 *
253 * Every attribute here is load-bearing:
254 * SameSite=None the only value a browser sends on the cross-site POST
255 * from the provider. Lax is not sent; Strict is not sent.
256 * Secure which None REQUIRES, or the browser drops the cookie
257 * silently. This is why host must be https.
258 * Path=<mount> so it travels on the two requests that need it, and no
259 * other request in the application carries it.
260 * HttpOnly nothing in the page needs it. */
261 59 PERL_STATIC_INLINE SV *psaml_flow_cookie(pTHX_ SV *value, SV *mount, IV ttl) {
262 59 SV *out = newSVpvs(PSAML_FLOW_COOKIE "=");
263 59 ▶ if (value && SvOK(value)) sv_catsv(out, value);
Branch: 2 branches
BranchTrueFalseTotal
if (value && SvOK(value)) sv_catsv(out, value); 59 0 59
if (value && SvOK(value)) sv_catsv(out, value); 59 0 59
264 59 sv_catpvs(out, "; Path=");
265 59 ▶ if (mount && SvOK(mount) && SvCUR(mount)) sv_catsv(out, mount);
Branch: 3 branches
BranchTrueFalseTotal
if (mount && SvOK(mount) && SvCUR(mount)) sv_catsv(out, mount); 59 0 59
if (mount && SvOK(mount) && SvCUR(mount)) sv_catsv(out, mount); 59 0 59
if (mount && SvOK(mount) && SvCUR(mount)) sv_catsv(out, mount); 59 0 59
266 0 else sv_catpvs(out, "/");
267 59 sv_catpvf(out, "; Max-Age=%" IVdf, ttl);
268 59 sv_catpvs(out, "; HttpOnly; Secure; SameSite=None");
269 59 return out;
270 }
271  
272 #endif /* PSAML_FLOW_H */
« xs/response.xs ↑ Summary include/psaml/psaml_compat.h »