include/punk/punk_csrf.h

filter: Statementstmt 93.2% 193 / 207 Branchbran 60.1% 184 / 306 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.4% 377 / 513 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/cache.xs ↑ Summary include/punk/punk_model.h »
1 /* punk_csrf.h - single-use CSRF tokens, in C.
2 *
3 * The token lives in the session (punk_session.h), which is a signed cookie:
4 * mutating it dirties the session, and the after-dispatch write-back re-signs
5 * the cookie for free. A successful check replaces the token, so a token is
6 * good for exactly one unsafe request.
7 *
8 * What that does and does not give you, precisely: the server keeps no record
9 * of spent tokens - there is no server-side session store - so this is
10 * freshness, not one-time enforcement against a replayed *whole request*
11 * (old cookie and old token together). For CSRF that is the right trade: the
12 * attacker's whole problem is that they cannot read the token cross-origin,
13 * and anyone who can present the session cookie already has the session.
14 *
15 * Include after punk_session.h (ps_cfg / ps_load / ps_stash) and
16 * punk_cookie.h (pk_build_cookie).
17 */
18  
19 #ifndef PUNK_CSRF_H
20 #define PUNK_CSRF_H
21  
22 #ifdef PUNK_HAVE_GETENTROPY
23 # ifdef PUNK_GETENTROPY_SYS_RANDOM
24 # include <sys/random.h>
25 # else
26 # include <unistd.h>
27 # endif
28 #endif
29  
30 /* the session key the token sits under - namespaced so an application's own
31 * session data can never collide with it */
32 #define PC_SESSION_KEY "punk.csrf"
33 #define PC_STASH_DIRTY "punk.csrf.set"
34  
35 /* ---- randomness ----------------------------------------------------------- */
36  
37 /* Fill out with n cryptographically random bytes. getentropy where the build
38 * probe found it, otherwise a cached /dev/urandom descriptor. Croaks rather
39 * than falling back to anything weaker: a guessable CSRF token is worse than a
40 * server that will not start. */
41 static int pk_urandom_fd = -1;
42  
43 51 static void pk_random_bytes(pTHX_ unsigned char *out, size_t n) {
44 #ifdef PUNK_HAVE_GETENTROPY
45 51 size_t off = 0;
46 102 ▶ while (off < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (off < n) { 51 51 102
47 51 size_t want = n - off;
48 51 ▶ if (want > 256) want = 256; /* getentropy's documented maximum */
Branch: 1 branch
BranchTrueFalseTotal
if (want > 256) want = 256; /* getentropy's documented maximum */ 0 51 51
49 51 ▶ if (getentropy(out + off, want) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (getentropy(out + off, want) != 0) 0 51 51
50 0 croak("Punk::CSRF: getentropy failed: %s", Strerror(errno));
51 51 off += want;
52 }
53 #else
54 size_t off = 0;
55 if (pk_urandom_fd < 0) {
56 pk_urandom_fd = PerlLIO_open3("/dev/urandom", O_RDONLY, 0);
57 if (pk_urandom_fd < 0)
58 croak("Punk::CSRF: cannot open /dev/urandom: %s", Strerror(errno));
59 }
60 while (off < n) {
61 SSize_t got = PerlLIO_read(pk_urandom_fd, out + off, n - off);
62 if (got <= 0) croak("Punk::CSRF: short read from /dev/urandom");
63 off += (size_t)got;
64 }
65 #endif
66 51 }
67  
68 /* 32 random bytes as base64url, no padding - 43 characters. */
69 23 static SV *pcf_mint(pTHX) {
70 unsigned char raw[32];
71 23 pk_random_bytes(aTHX_ raw, sizeof raw);
72 23 return pk_b64url(aTHX_ raw, sizeof raw);
73 }
74  
75 /* Constant time, and length-independent in the branch it takes: a token
76 * comparison that returned early would leak the matching prefix. */
77 15 static int pk_ct_eq(const char *a, STRLEN alen, const char *b, STRLEN blen) {
78 15 volatile unsigned char diff = (unsigned char)((alen ^ blen) != 0);
79 STRLEN i;
80 15 ▶ if (!alen || !blen) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!alen || !blen) return 0; 15 0 15
if (!alen || !blen) return 0; 0 15 15
81 660 ▶ for (i = 0; i < alen; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < alen; i++) 645 15 660
82 645 diff |= (unsigned char)(a[i] ^ b[i % blen]);
83 15 return diff == 0;
84 }
85  
86 /* Escape for an HTML attribute. The token is base64url and needs none of
87 * this, but the field name is configurable and lands in the same markup. */
88 8 static void pcf_attr_escape(pTHX_ SV *out, const char *p, STRLEN len) {
89 8 punk_html_escape(aTHX_ out, p, len);
90 8 }
91  
92 /* ---- configuration -------------------------------------------------------- */
93  
94 /* the csrf config the keyword froze on the app, or NULL when it is not on */
95 202 static HV *pcf_cfg(pTHX_ SV *c) {
96 202 SV *app = pcx_get(aTHX_ pcx_av(aTHX_ c), PCX_APP);
97 SV **s;
98 202 ▶ if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; 202 0 202
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; 202 0 202
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; 0 202 202
99 202 s = hv_fetchs((HV *)SvRV(app), K_CSRF, 0);
100 202 ▶ return (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
return (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 202 0 202
return (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 202 0 202
return (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 202 0 202
101 404 ▶ ? (HV *)SvRV(*s) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(*s) : NULL; 202 0 202
102 }
103  
104 41 static IV pcf_cfg_iv(pTHX_ HV *cfg, const char *k, IV def) {
105 41 SV **e = hv_fetch(cfg, k, (I32)strlen(k), 0);
106 41 ▶ return (e && *e && SvOK(*e)) ? SvIV(*e) : def;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvOK(*e)) ? SvIV(*e) : def; 41 0 41
return (e && *e && SvOK(*e)) ? SvIV(*e) : def; 41 0 41
return (e && *e && SvOK(*e)) ? SvIV(*e) : def; 41 0 41
107 }
108  
109 /* ---- the token in the session --------------------------------------------- */
110  
111 /* The session hashref (+1 from ps_load, mortalised here for the caller). */
112 97 static HV *pcf_session(pTHX_ SV *c) {
113 97 SV *rv = ps_load(aTHX_ c);
114 97 sv_2mortal(rv);
115 97 ▶ return (SvROK(rv) && SvTYPE(SvRV(rv)) == SVt_PVHV) ? (HV *)SvRV(rv) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (SvROK(rv) && SvTYPE(SvRV(rv)) == SVt_PVHV) ? (HV *)SvRV(rv) : NULL; 97 0 97
return (SvROK(rv) && SvTYPE(SvRV(rv)) == SVt_PVHV) ? (HV *)SvRV(rv) : NULL; 97 0 97
116 }
117  
118 /* Mark the token changed, so the mirror cookie is written for this response. */
119 23 static void pcf_mark_dirty(pTHX_ SV *c) {
120 23 HV *stash = ps_stash(aTHX_ pcx_av(aTHX_ c));
121 23 (void)hv_stores(stash, PC_STASH_DIRTY, newSViv(1));
122 23 }
123  
124 /* The current token, minting one when there is none. `keep` tokens are held
125 * as a list; the shipped default is 1, which is the strict single-token
126 * behaviour - the list exists so widening it later is configuration rather
127 * than a redesign. Element 0 is always the newest. */
128 43 static SV *pcf_token(pTHX_ SV *c, int mint) {
129 43 HV *sess = pcf_session(aTHX_ c);
130 SV **slot;
131 AV *toks;
132 43 ▶ if (!sess) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!sess) return NULL; 0 43 43
133  
134 43 slot = hv_fetchs(sess, PC_SESSION_KEY, 0);
135 43 ▶ if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) {
Branch: 4 branches
BranchTrueFalseTotal
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) { 31 12 43
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) { 31 0 31
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) { 31 0 31
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) { 31 0 31
136 31 toks = (AV *)SvRV(*slot);
137 31 ▶ if (av_len(toks) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(toks) >= 0) { 31 0 31
138 31 SV **t = av_fetch(toks, 0, 0);
139 31 ▶ if (t && *t && SvOK(*t)) return *t; /* borrowed */
Branch: 3 branches
BranchTrueFalseTotal
if (t && *t && SvOK(*t)) return *t; /* borrowed */ 31 0 31
if (t && *t && SvOK(*t)) return *t; /* borrowed */ 31 0 31
if (t && *t && SvOK(*t)) return *t; /* borrowed */ 31 0 31
140 }
141 }
142 else {
143 12 ▶ if (!mint) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!mint) return NULL; 0 12 12
144 12 toks = newAV();
145 12 (void)hv_stores(sess, PC_SESSION_KEY, newRV_noinc((SV *)toks));
146 }
147 12 ▶ if (!mint) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!mint) return NULL; 0 12 12
148 12 av_unshift(toks, 1);
149 12 (void)av_store(toks, 0, pcf_mint(aTHX));
150 12 pcf_mark_dirty(aTHX_ c);
151 {
152 12 SV **t = av_fetch(toks, 0, 0);
153 12 ▶ return (t && *t) ? *t : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (t && *t) ? *t : NULL; 12 0 12
return (t && *t) ? *t : NULL; 12 0 12
154 }
155 }
156  
157 /* Spend `given`: drop it from the list and put a fresh one at the head. The
158 * drop is what makes the token single-use - the same value never validates
159 * twice, because the session it was checked against no longer holds it. */
160 11 static void pcf_rotate(pTHX_ SV *c, SV *given, IV keep) {
161 11 HV *sess = pcf_session(aTHX_ c);
162 SV **slot;
163 AV *toks, *next;
164 SSize_t i, n;
165 STRLEN gl;
166 const char *g;
167 11 ▶ if (!sess) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!sess) return; 0 11 11
168 11 slot = hv_fetchs(sess, PC_SESSION_KEY, 0);
169 11 ▶ if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) 11 0 11
if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) 11 0 11
if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) 11 0 11
if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) 0 11 11
170 0 return;
171 11 toks = (AV *)SvRV(*slot);
172 11 g = SvPV_const(given, gl);
173  
174 11 next = newAV();
175 11 av_push(next, pcf_mint(aTHX));
176 11 n = av_len(toks) + 1;
177 11 ▶ for (i = 0; i < n && av_len(next) + 1 < keep; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n && av_len(next) + 1 < keep; i++) { 11 0 11
for (i = 0; i < n && av_len(next) + 1 < keep; i++) { 0 11 11
178 0 SV **t = av_fetch(toks, i, 0);
179 STRLEN tl;
180 const char *tv;
181 0 ▶ if (!(t && *t && SvOK(*t))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(t && *t && SvOK(*t))) continue; 0 0 0
if (!(t && *t && SvOK(*t))) continue; 0 0 0
if (!(t && *t && SvOK(*t))) continue; 0 0 0
182 0 tv = SvPV_const(*t, tl);
183 0 ▶ if (tl == gl && memEQ(tv, g, tl)) continue; /* the one just spent */
Branch: 2 branches
BranchTrueFalseTotal
if (tl == gl && memEQ(tv, g, tl)) continue; /* the one just spent */ 0 0 0
if (tl == gl && memEQ(tv, g, tl)) continue; /* the one just spent */ 0 0 0
184 0 av_push(next, newSVsv(*t));
185 }
186 11 (void)hv_stores(sess, PC_SESSION_KEY, newRV_noinc((SV *)next));
187 11 pcf_mark_dirty(aTHX_ c);
188 }
189  
190 /* Does any live token match? */
191 43 static int pcf_accepts(pTHX_ SV *c, SV *given) {
192 43 HV *sess = pcf_session(aTHX_ c);
193 SV **slot;
194 AV *toks;
195 SSize_t i, n;
196 STRLEN gl;
197 const char *g;
198 43 ▶ if (!sess || !given || !SvOK(given)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!sess || !given || !SvOK(given)) return 0; 43 0 43
if (!sess || !given || !SvOK(given)) return 0; 15 28 43
if (!sess || !given || !SvOK(given)) return 0; 0 15 15
199 15 g = SvPV_const(given, gl);
200 15 ▶ if (!gl) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!gl) return 0; 0 15 15
201 15 slot = hv_fetchs(sess, PC_SESSION_KEY, 0);
202 15 ▶ if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) 15 0 15
if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) 15 0 15
if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) 15 0 15
if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) 0 15 15
203 0 return 0;
204 15 toks = (AV *)SvRV(*slot);
205 15 n = av_len(toks) + 1;
206 19 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 15 4 19
207 15 SV **t = av_fetch(toks, i, 0);
208 STRLEN tl;
209 const char *tv;
210 15 ▶ if (!(t && *t && SvOK(*t))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(t && *t && SvOK(*t))) continue; 15 0 15
if (!(t && *t && SvOK(*t))) continue; 15 0 15
if (!(t && *t && SvOK(*t))) continue; 0 15 15
211 15 tv = SvPV_const(*t, tl);
212 15 ▶ if (pk_ct_eq(tv, tl, g, gl)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (pk_ct_eq(tv, tl, g, gl)) return 1; 11 4 15
213 }
214 4 return 0;
215 }
216  
217 /* ---- reading what the client sent ----------------------------------------- */
218  
219 292 static SV *pcf_env_str(pTHX_ SV *c, const char *key) {
220 292 SV *env = pcx_get(aTHX_ pcx_av(aTHX_ c), PCX_ENV);
221 SV **e;
222 292 ▶ if (!(env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV)) return NULL; 292 0 292
if (!(env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV)) return NULL; 292 0 292
if (!(env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV)) return NULL; 0 292 292
223 292 e = hv_fetch((HV *)SvRV(env), key, (I32)strlen(key), 0);
224 292 ▶ return (e && *e && SvOK(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvOK(*e)) ? *e : NULL; 205 87 292
return (e && *e && SvOK(*e)) ? *e : NULL; 205 0 205
return (e && *e && SvOK(*e)) ? *e : NULL; 205 0 205
225 }
226  
227 /* The submitted token: the header first, then the form field.
228 *
229 * The field is only looked at for urlencoded bodies under max_body, and this
230 * is not fussiness - pq_body reads the whole body into memory on first touch,
231 * so reaching for the field on a multipart upload would pull the upload into
232 * memory before the mount's own max_body_size check could refuse it. Uploads
233 * send the header. */
234 43 static SV *pcf_submitted(pTHX_ SV *c, HV *cfg) {
235 STRLEN l;
236 43 const char *hdr = ps_cfg_str(aTHX_ cfg, "header", "X-CSRF-Token", &l);
237 SV *v;
238 { /* X-CSRF-Token -> HTTP_X_CSRF_TOKEN */
239 char env[128];
240 STRLEN i;
241 56 ▶ if (l > sizeof(env) - 6) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (l > sizeof(env) - 6) return NULL; 0 43 43
242 43 memcpy(env, "HTTP_", 5);
243 554 ▶ for (i = 0; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) { 511 43 554
244 511 char ch = hdr[i];
245 511 ▶ env[5 + i] = (ch == '-') ? '_' : (char)toUPPER((U8)ch);
Branch: 2 branches
BranchTrueFalseTotal
env[5 + i] = (ch == '-') ? '_' : (char)toUPPER((U8)ch); 426 85 511
env[5 + i] = (ch == '-') ? '_' : (char)toUPPER((U8)ch); 172 254 426
246 }
247 43 env[5 + l] = '\0';
248 43 v = pcf_env_str(aTHX_ c, env);
249 43 ▶ if (v && SvCUR(v)) return v;
Branch: 2 branches
BranchTrueFalseTotal
if (v && SvCUR(v)) return v; 13 30 43
if (v && SvCUR(v)) return v; 13 0 13
250 }
251  
252 {
253 30 SV *ct = pcf_env_str(aTHX_ c, "CONTENT_TYPE");
254 30 SV *cl = pcf_env_str(aTHX_ c, "CONTENT_LENGTH");
255 30 IV max = pcf_cfg_iv(aTHX_ cfg, "max_body", 65536);
256 const char *field;
257 STRLEN fl;
258 SV *req, *form, **e;
259 30 ▶ if (!ct) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!ct) return NULL; 8 22 30
260 22 ▶ if (!strnEQ(SvPVX(ct), "application/x-www-form-urlencoded", 33))
Branch: 1 branch
BranchTrueFalseTotal
if (!strnEQ(SvPVX(ct), "application/x-www-form-urlencoded", 33)) 3 19 22
261 3 return NULL;
262 19 ▶ if (cl && SvIV(cl) > max) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (cl && SvIV(cl) > max) return NULL; 19 0 19
if (cl && SvIV(cl) > max) return NULL; 0 19 19
263  
264 19 field = ps_cfg_str(aTHX_ cfg, "field", "_csrf", &fl);
265 /* The form table and nothing else. `param` looks in the query string
266 * first, and a token accepted from there is a token in access logs
267 * and Referer headers. */
268 19 req = pcx_force(aTHX_ pcx_av(aTHX_ c), PCX_REQ, "Punk::Request",
269 pcx_get(aTHX_ pcx_av(aTHX_ c), PCX_ENV));
270 19 form = pcx_call_meth(aTHX_ req, "form", NULL, 0, 1);
271 19 ▶ if (form) sv_2mortal(form);
Branch: 1 branch
BranchTrueFalseTotal
if (form) sv_2mortal(form); 19 0 19
272 19 ▶ if (!(form && SvROK(form) && SvTYPE(SvRV(form)) == SVt_PVHV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(form && SvROK(form) && SvTYPE(SvRV(form)) == SVt_PVHV)) 19 0 19
if (!(form && SvROK(form) && SvTYPE(SvRV(form)) == SVt_PVHV)) 19 0 19
if (!(form && SvROK(form) && SvTYPE(SvRV(form)) == SVt_PVHV)) 0 19 19
273 0 return NULL;
274 19 e = hv_fetch((HV *)SvRV(form), field, (I32)fl, 0);
275 19 ▶ if (!(e && *e && SvOK(*e) && !SvROK(*e) && SvCUR(*e))) return NULL;
Branch: 5 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e) && !SvROK(*e) && SvCUR(*e))) return NULL; 2 17 19
if (!(e && *e && SvOK(*e) && !SvROK(*e) && SvCUR(*e))) return NULL; 2 0 2
if (!(e && *e && SvOK(*e) && !SvROK(*e) && SvCUR(*e))) return NULL; 2 0 2
if (!(e && *e && SvOK(*e) && !SvROK(*e) && SvCUR(*e))) return NULL; 2 0 2
if (!(e && *e && SvOK(*e) && !SvROK(*e) && SvCUR(*e))) return NULL; 0 2 2
276 2 return sv_2mortal(newSVsv(*e));
277 }
278 }
279  
280 /* ---- the check ------------------------------------------------------------ */
281  
282 89 static int pcf_safe_method(const char *m, STRLEN l) {
283 37 ▶ return (l == 3 && memEQ(m, "GET", 3))
Branch: 1 branch
BranchTrueFalseTotal
return (l == 3 && memEQ(m, "GET", 3)) 1 36 37
284 53 ▶ || (l == 4 && memEQ(m, "HEAD", 4))
Branch: 2 branches
BranchTrueFalseTotal
|| (l == 4 && memEQ(m, "HEAD", 4)) 52 1 53
|| (l == 4 && memEQ(m, "HEAD", 4)) 51 1 52
285 52 ▶ || (l == 7 && memEQ(m, "OPTIONS", 7))
Branch: 2 branches
BranchTrueFalseTotal
|| (l == 7 && memEQ(m, "OPTIONS", 7)) 0 52 52
|| (l == 7 && memEQ(m, "OPTIONS", 7)) 0 0 0
286 178 ▶ || (l == 5 && memEQ(m, "TRACE", 5));
Branch: 3 branches
BranchTrueFalseTotal
|| (l == 5 && memEQ(m, "TRACE", 5)); 37 52 89
|| (l == 5 && memEQ(m, "TRACE", 5)); 0 52 52
|| (l == 5 && memEQ(m, "TRACE", 5)); 0 0 0
287 }
288  
289 48 static int pcf_exempt(pTHX_ HV *cfg, SV *path) {
290 48 SV **e = hv_fetchs(cfg, "exempt", 0);
291 AV *list;
292 SSize_t i, n;
293 STRLEN pl;
294 const char *p;
295 48 ▶ if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) return 0; 8 40 48
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) return 0; 8 0 8
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) return 0; 8 0 8
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) return 0; 0 8 8
296 8 ▶ if (!(path && SvOK(path))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(path && SvOK(path))) return 0; 8 0 8
if (!(path && SvOK(path))) return 0; 0 8 8
297 8 list = (AV *)SvRV(*e);
298 8 p = SvPV_const(path, pl);
299 8 n = av_len(list) + 1;
300 14 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 11 3 14
301 11 SV **x = av_fetch(list, i, 0);
302 STRLEN xl;
303 const char *xv;
304 11 ▶ if (!(x && *x && SvOK(*x))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(x && *x && SvOK(*x))) continue; 11 0 11
if (!(x && *x && SvOK(*x))) continue; 11 0 11
if (!(x && *x && SvOK(*x))) continue; 0 11 11
305 11 xv = SvPV_const(*x, xl);
306 /* on a segment boundary: '/hooks' covers /hooks and /hooks/x and
307 * not /hooksadmin. A prefix written with its own trailing slash
308 * already ends on one. */
309 11 ▶ if (xl && xv[xl - 1] == '/') {
Branch: 2 branches
BranchTrueFalseTotal
if (xl && xv[xl - 1] == '/') { 11 0 11
if (xl && xv[xl - 1] == '/') { 5 6 11
310 8 ▶ if (xl <= pl && memEQ(p, xv, xl)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (xl <= pl && memEQ(p, xv, xl)) return 1; 3 2 5
if (xl <= pl && memEQ(p, xv, xl)) return 1; 2 1 3
311 }
312 6 ▶ else if (ps_under(p, pl, xv, xl)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (ps_under(p, pl, xv, xl)) return 1; 3 3 6
313 }
314 3 return 0;
315 }
316  
317 /* Is this Authorization header one a script had to attach? Bearer and the
318 * like are; Basic, Digest, Negotiate and NTLM are not - a browser that has
319 * the credentials sends them by itself on a cross-site request, exactly as it
320 * does a cookie, so they exempt nothing. */
321 52 static int pcf_explicit_auth(pTHX_ SV *auth) {
322 static const char *const ambient[] = { "basic", "digest", "negotiate",
323 "ntlm", NULL };
324 52 STRLEN l, sl = 0;
325 const char *p;
326 int i;
327 52 ▶ if (!auth) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!auth) return 0; 41 11 52
328 11 p = SvPV_const(auth, l);
329 14 ▶ while (l && (*p == ' ' || *p == '\t')) { p++; l--; }
Branch: 3 branches
BranchTrueFalseTotal
while (l && (*p == ' ' || *p == '\t')) { p++; l--; } 13 1 14
while (l && (*p == ' ' || *p == '\t')) { p++; l--; } 3 10 13
while (l && (*p == ' ' || *p == '\t')) { p++; l--; } 0 10 10
330 67 ▶ while (sl < l && p[sl] != ' ' && p[sl] != '\t') sl++;
Branch: 3 branches
BranchTrueFalseTotal
while (sl < l && p[sl] != ' ' && p[sl] != '\t') sl++; 65 2 67
while (sl < l && p[sl] != ' ' && p[sl] != '\t') sl++; 56 9 65
while (sl < l && p[sl] != ' ' && p[sl] != '\t') sl++; 56 0 56
331 11 ▶ if (!sl || sl == l) return 0; /* no scheme, or no credentials */
Branch: 2 branches
BranchTrueFalseTotal
if (!sl || sl == l) return 0; /* no scheme, or no credentials */ 10 1 11
if (!sl || sl == l) return 0; /* no scheme, or no credentials */ 1 9 10
332 31 ▶ for (i = 0; ambient[i]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; ambient[i]; i++) 27 4 31
333 27 ▶ if (strlen(ambient[i]) == sl && foldEQ(p, ambient[i], (I32)sl))
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(ambient[i]) == sl && foldEQ(p, ambient[i], (I32)sl)) 9 18 27
if (strlen(ambient[i]) == sl && foldEQ(p, ambient[i], (I32)sl)) 5 4 9
334 5 return 0;
335 4 return 1;
336 }
337  
338 /* The 403, in the shape everything else in Punk answers errors with. */
339 32 static SV *pcf_refuse(pTHX_ SV *c, HV *cfg) {
340 32 SV **h = hv_fetchs(cfg, "on_error", 0);
341 32 ▶ if (h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVCV) {
Branch: 4 branches
BranchTrueFalseTotal
if (h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVCV) { 2 30 32
if (h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVCV) { 2 0 2
if (h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVCV) { 2 0 2
if (h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVCV) { 2 0 2
342 2 int died = 0;
343 2 SV *r = pcx_call2(aTHX_ *h, c, &PL_sv_undef, &died);
344 2 ▶ if (!died && r) return r;
Branch: 2 branches
BranchTrueFalseTotal
if (!died && r) return r; 2 0 2
if (!died && r) return r; 2 0 2
345 0 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 0 0 0
346 }
347 30 return punk_triplet(aTHX_ 403,
348 sv_2mortal(newSVpvs("application/json")),
349 newSVpvs("{\"errors\":[{\"message\":\"invalid csrf token\"}]}"),
350 NULL);
351 }
352  
353 /* The before-dispatch hook: a reference return short-circuits the request, so
354 * a refusal answers before any guard or handler runs. */
355 89 static SV *pcf_check(pTHX_ SV *c) {
356 89 HV *cfg = pcf_cfg(aTHX_ c);
357 SV *method, *given;
358 STRLEN ml;
359 const char *m;
360 89 ▶ if (!cfg) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!cfg) return NULL; 0 89 89
361  
362 89 method = pcf_env_str(aTHX_ c, "REQUEST_METHOD");
363 178 ▶ m = method ? SvPV_const(method, ml) : "GET";
Branch: 1 branch
BranchTrueFalseTotal
m = method ? SvPV_const(method, ml) : "GET"; 89 0 89
364 89 ▶ if (!method) ml = 3;
Branch: 1 branch
BranchTrueFalseTotal
if (!method) ml = 3; 0 89 89
365 89 ▶ if (pcf_safe_method(m, ml)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (pcf_safe_method(m, ml)) return NULL; 37 52 89
366  
367 /* An Extended CONNECT (RFC 8441 on HTTP/2, RFC 9220 on HTTP/3) is the
368 * websocket upgrade a multiplexed transport has instead of a GET, and
369 * punk_serve.h routes it as that GET. The GET was never a CSRF target,
370 * so nor is this: the handshake's own origin check is what stands
371 * between a page elsewhere and an authenticated socket, on either
372 * transport. A browser opens every websocket this way once the server
373 * has advertised the setting, so refusing it here refused them all. */
374 52 ▶ if (ml == 7 && memEQ(m, "CONNECT", 7)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ml == 7 && memEQ(m, "CONNECT", 7)) { 0 52 52
if (ml == 7 && memEQ(m, "CONNECT", 7)) { 0 0 0
375 0 SV *cp = pcf_env_str(aTHX_ c, "psgix.connect_protocol");
376 0 ▶ if (cp) {
Branch: 1 branch
BranchTrueFalseTotal
if (cp) { 0 0 0
377 STRLEN pl;
378 0 const char *pv = SvPV_const(cp, pl);
379 0 ▶ if (pl == 9 && memEQ(pv, "websocket", 9)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (pl == 9 && memEQ(pv, "websocket", 9)) return NULL; 0 0 0
if (pl == 9 && memEQ(pv, "websocket", 9)) return NULL; 0 0 0
380 }
381 }
382  
383 /* Not riding on cookies, so not a CSRF target. */
384 52 ▶ if (pcf_explicit_auth(aTHX_ pcf_env_str(aTHX_ c, "HTTP_AUTHORIZATION")))
Branch: 1 branch
BranchTrueFalseTotal
if (pcf_explicit_auth(aTHX_ pcf_env_str(aTHX_ c, "HTTP_AUTHORIZATION"))) 4 48 52
385 4 return NULL;
386 48 ▶ if (pcf_exempt(aTHX_ cfg, pcf_env_str(aTHX_ c, "PATH_INFO"))) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (pcf_exempt(aTHX_ cfg, pcf_env_str(aTHX_ c, "PATH_INFO"))) return NULL; 5 43 48
387  
388 43 given = pcf_submitted(aTHX_ c, cfg);
389 43 ▶ if (!pcf_accepts(aTHX_ c, given)) return pcf_refuse(aTHX_ c, cfg);
Branch: 1 branch
BranchTrueFalseTotal
if (!pcf_accepts(aTHX_ c, given)) return pcf_refuse(aTHX_ c, cfg); 32 11 43
390  
391 11 pcf_rotate(aTHX_ c, given, pcf_cfg_iv(aTHX_ cfg, "keep", 1));
392 11 return NULL;
393 }
394  
395 /* ---- the mirror cookie ---------------------------------------------------- */
396  
397 /* The token also goes out in a cookie the browser will let script read, which
398 * is how fetch and Open::API::UI's try-it-out get hold of it. It is not the
399 * authority - the session is - so a forged one proves nothing. */
400 89 static void pcf_writeback(pTHX_ SV *c, SV *resp) {
401 89 HV *cfg = pcf_cfg(aTHX_ c);
402 AV *av, *r, *headers;
403 HV *stash, *ckopts;
404 SV **hp, **dirty;
405 SV *tok, *name;
406 STRLEN nl;
407 const char *cookie_name;
408 155 ▶ if (!cfg) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cfg) return; 0 89 89
409 89 av = pcx_av(aTHX_ c);
410 {
411 89 SV *st = pcx_get(aTHX_ av, PCX_STASH);
412 89 ▶ if (!(st && SvROK(st))) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!(st && SvROK(st))) return; 79 10 89
if (!(st && SvROK(st))) return; 0 79 79
413 79 stash = (HV *)SvRV(st);
414 }
415 79 dirty = hv_fetchs(stash, PC_STASH_DIRTY, 0);
416 79 ▶ if (!(dirty && *dirty && SvTRUE(*dirty))) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!(dirty && *dirty && SvTRUE(*dirty))) return; 23 56 79
if (!(dirty && *dirty && SvTRUE(*dirty))) return; 23 0 23
if (!(dirty && *dirty && SvTRUE(*dirty))) return; 0 23 23
417 23 ▶ if (!(SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) return; 23 0 23
if (!(SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) return; 0 23 23
418 23 r = (AV *)SvRV(resp);
419 23 hp = av_fetch(r, 1, 0);
420 23 ▶ if (!(hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV)) return;
Branch: 4 branches
BranchTrueFalseTotal
if (!(hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV)) return; 23 0 23
if (!(hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV)) return; 23 0 23
if (!(hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV)) return; 23 0 23
if (!(hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV)) return; 0 23 23
421 23 headers = (AV *)SvRV(*hp);
422  
423 23 tok = pcf_token(aTHX_ c, 0);
424 23 ▶ if (!tok) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!tok) return; 0 23 23
425  
426 /* the session's cookie attributes, minus HttpOnly - script has to read it */
427 23 ckopts = (HV *)sv_2mortal((SV *)newHV());
428 {
429 23 HV *scfg = ps_cfg(aTHX_ c);
430 static const char *const copy[] = { "path", "domain", "secure",
431 "samesite", "max_age", NULL };
432 int i;
433 138 ▶ for (i = 0; scfg && copy[i]; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; scfg && copy[i]; i++) { 138 0 138
for (i = 0; scfg && copy[i]; i++) { 115 23 138
434 115 SV **e = hv_fetch(scfg, copy[i], (I32)strlen(copy[i]), 0);
435 115 ▶ if (e && *e && SvOK(*e))
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) 48 67 115
if (e && *e && SvOK(*e)) 48 0 48
if (e && *e && SvOK(*e)) 48 0 48
436 48 (void)hv_store(ckopts, copy[i], (I32)strlen(copy[i]),
437 newSVsv(*e), 0);
438 }
439 23 (void)hv_stores(ckopts, "httponly", newSViv(0));
440 }
441  
442 23 cookie_name = ps_cfg_str(aTHX_ cfg, "cookie", "csrf", &nl);
443 23 name = sv_2mortal(newSVpvn(cookie_name, nl));
444 23 av_push(headers, newSVpvs("Set-Cookie"));
445 23 av_push(headers, pk_build_cookie_ex(aTHX_ name, tok, ckopts,
446 ps_secure_default(aTHX_ c)));
447 }
448  
449 #endif /* PUNK_CSRF_H */
« xs/cache.xs ↑ Summary include/punk/punk_model.h »