| 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 |
55 |
| static void pk_random_bytes(pTHX_ unsigned char *out, size_t n) { |
| 44 |
|
| #ifdef PUNK_HAVE_GETENTROPY |
| 45 |
55 |
| size_t off = 0; |
| 46 |
110 |
▶ | while (off < n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (off < n) { |
55 |
55 |
110 |
|
| 47 |
55 |
| size_t want = n - off; |
| 48 |
|
| /* |
| 49 |
|
| # uncoverable branch true |
| 50 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 51 |
55 |
▶ | if (want > 256) want = 256; /* getentropy's documented maximum */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (want > 256) want = 256; /* getentropy's documented maximum */ |
0 |
55 |
55 |
|
| 52 |
|
| /* |
| 53 |
|
| # uncoverable branch true |
| 54 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 55 |
55 |
▶ | if (getentropy(out + off, want) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (getentropy(out + off, want) != 0) |
0 |
55 |
55 |
|
| 56 |
0 |
| croak("Punk::CSRF: getentropy failed: %s", Strerror(errno)); /* # uncoverable statement */ |
| 57 |
55 |
| off += want; |
| 58 |
|
| } |
| 59 |
|
| #else |
| 60 |
|
| size_t off = 0; |
| 61 |
|
| if (pk_urandom_fd < 0) { |
| 62 |
|
| pk_urandom_fd = PerlLIO_open3("/dev/urandom", O_RDONLY, 0); |
| 63 |
|
| if (pk_urandom_fd < 0) |
| 64 |
|
| croak("Punk::CSRF: cannot open /dev/urandom: %s", Strerror(errno)); |
| 65 |
|
| } |
| 66 |
|
| while (off < n) { |
| 67 |
|
| SSize_t got = PerlLIO_read(pk_urandom_fd, out + off, n - off); |
| 68 |
|
| if (got <= 0) croak("Punk::CSRF: short read from /dev/urandom"); |
| 69 |
|
| off += (size_t)got; |
| 70 |
|
| } |
| 71 |
|
| #endif |
| 72 |
55 |
| } |
| 73 |
|
| |
| 74 |
|
| /* 32 random bytes as base64url, no padding - 43 characters. */ |
| 75 |
23 |
| static SV *pcf_mint(pTHX) { |
| 76 |
|
| unsigned char raw[32]; |
| 77 |
23 |
| pk_random_bytes(aTHX_ raw, sizeof raw); |
| 78 |
23 |
| return pk_b64url(aTHX_ raw, sizeof raw); |
| 79 |
|
| } |
| 80 |
|
| |
| 81 |
|
| /* Constant time, and length-independent in the branch it takes: a token |
| 82 |
|
| * comparison that returned early would leak the matching prefix. */ |
| 83 |
15 |
| static int pk_ct_eq(const char *a, STRLEN alen, const char *b, STRLEN blen) { |
| 84 |
15 |
| volatile unsigned char diff = (unsigned char)((alen ^ blen) != 0); |
| 85 |
|
| STRLEN i; |
| 86 |
|
| /* |
| 87 |
|
| # uncoverable branch true |
| 88 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 89 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 90 |
15 |
▶ | if (!alen || !blen) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!alen || !blen) return 0; |
15 |
0 |
15 |
| if (!alen || !blen) return 0; |
0 |
15 |
15 |
|
| 91 |
660 |
▶ | for (i = 0; i < alen; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < alen; i++) |
645 |
15 |
660 |
|
| 92 |
645 |
| diff |= (unsigned char)(a[i] ^ b[i % blen]); |
| 93 |
15 |
| return diff == 0; |
| 94 |
|
| } |
| 95 |
|
| |
| 96 |
|
| /* Escape for an HTML attribute. The token is base64url and needs none of |
| 97 |
|
| * this, but the field name is configurable and lands in the same markup. */ |
| 98 |
8 |
| static void pcf_attr_escape(pTHX_ SV *out, const char *p, STRLEN len) { |
| 99 |
8 |
| punk_html_escape(aTHX_ out, p, len); |
| 100 |
8 |
| } |
| 101 |
|
| |
| 102 |
|
| /* ---- configuration -------------------------------------------------------- */ |
| 103 |
|
| |
| 104 |
|
| /* the csrf config the keyword froze on the app, or NULL when it is not on */ |
| 105 |
203 |
| static HV *pcf_cfg(pTHX_ SV *c) { |
| 106 |
203 |
| SV *app = pcx_get(aTHX_ pcx_av(aTHX_ c), PCX_APP); |
| 107 |
|
| SV **s; |
| 108 |
|
| /* |
| 109 |
|
| # uncoverable branch true |
| 110 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 111 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 112 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 113 |
203 |
▶ | if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; |
203 |
0 |
203 |
| if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; |
203 |
0 |
203 |
| if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; |
0 |
203 |
203 |
|
| 114 |
203 |
| s = hv_fetchs((HV *)SvRV(app), K_CSRF, 0); |
| 115 |
202 |
▶ | return (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 116 |
405 |
▶ | ? (HV *)SvRV(*s) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (HV *)SvRV(*s) : NULL; |
202 |
1 |
203 |
|
| 117 |
|
| } |
| 118 |
|
| |
| 119 |
41 |
| static IV pcf_cfg_iv(pTHX_ HV *cfg, const char *k, IV def) { |
| 120 |
41 |
| SV **e = hv_fetch(cfg, k, (I32)strlen(k), 0); |
| 121 |
|
| /* |
| 122 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 123 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 124 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 125 |
41 |
▶ | return (e && *e && SvOK(*e)) ? SvIV(*e) : def; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 126 |
|
| } |
| 127 |
|
| |
| 128 |
|
| /* ---- the token in the session --------------------------------------------- */ |
| 129 |
|
| |
| 130 |
|
| /* The session hashref (+1 from ps_load, mortalised here for the caller). */ |
| 131 |
97 |
| static HV *pcf_session(pTHX_ SV *c) { |
| 132 |
97 |
| SV *rv = ps_load(aTHX_ c); |
| 133 |
97 |
| sv_2mortal(rv); |
| 134 |
|
| /* |
| 135 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 136 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 137 |
97 |
▶ | return (SvROK(rv) && SvTYPE(SvRV(rv)) == SVt_PVHV) ? (HV *)SvRV(rv) : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 138 |
|
| } |
| 139 |
|
| |
| 140 |
|
| /* Mark the token changed, so the mirror cookie is written for this response. */ |
| 141 |
23 |
| static void pcf_mark_dirty(pTHX_ SV *c) { |
| 142 |
23 |
| HV *stash = ps_stash(aTHX_ pcx_av(aTHX_ c)); |
| 143 |
23 |
| (void)hv_stores(stash, PC_STASH_DIRTY, newSViv(1)); |
| 144 |
23 |
| } |
| 145 |
|
| |
| 146 |
|
| /* The current token, minting one when there is none. `keep` tokens are held |
| 147 |
|
| * as a list; the shipped default is 1, which is the strict single-token |
| 148 |
|
| * behaviour - the list exists so widening it later is configuration rather |
| 149 |
|
| * than a redesign. Element 0 is always the newest. */ |
| 150 |
43 |
| static SV *pcf_token(pTHX_ SV *c, int mint) { |
| 151 |
43 |
| HV *sess = pcf_session(aTHX_ c); |
| 152 |
|
| SV **slot; |
| 153 |
|
| AV *toks; |
| 154 |
|
| /* |
| 155 |
|
| # uncoverable branch true |
| 156 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 157 |
43 |
▶ | if (!sess) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sess) return NULL; |
0 |
43 |
43 |
|
| 158 |
|
| |
| 159 |
43 |
| slot = hv_fetchs(sess, PC_SESSION_KEY, 0); |
| 160 |
|
| /* |
| 161 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 162 |
|
| # uncoverable condition right count:3 class:ignore_covered_err |
| 163 |
|
| # uncoverable condition right count:4 class:ignore_covered_err */ |
| 164 |
43 |
▶ | if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV) { |
Branch: 4 branches
| Branch | True | False | Total |
| 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 |
|
| 165 |
31 |
| toks = (AV *)SvRV(*slot); |
| 166 |
|
| /* |
| 167 |
|
| # uncoverable branch false |
| 168 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 169 |
31 |
▶ | if (av_len(toks) >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(toks) >= 0) { |
31 |
0 |
31 |
|
| 170 |
31 |
| SV **t = av_fetch(toks, 0, 0); |
| 171 |
|
| /* |
| 172 |
|
| # uncoverable branch false |
| 173 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 174 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 175 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 176 |
31 |
▶ | if (t && *t && SvOK(*t)) return *t; /* borrowed */ |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 177 |
|
| } |
| 178 |
|
| } |
| 179 |
|
| else { |
| 180 |
|
| /* |
| 181 |
|
| # uncoverable branch true |
| 182 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 183 |
12 |
▶ | if (!mint) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!mint) return NULL; |
0 |
12 |
12 |
|
| 184 |
12 |
| toks = newAV(); |
| 185 |
12 |
| (void)hv_stores(sess, PC_SESSION_KEY, newRV_noinc((SV *)toks)); |
| 186 |
|
| } |
| 187 |
|
| /* |
| 188 |
|
| # uncoverable branch true |
| 189 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 190 |
12 |
▶ | if (!mint) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!mint) return NULL; |
0 |
12 |
12 |
|
| 191 |
12 |
| av_unshift(toks, 1); |
| 192 |
12 |
| (void)av_store(toks, 0, pcf_mint(aTHX)); |
| 193 |
12 |
| pcf_mark_dirty(aTHX_ c); |
| 194 |
|
| { |
| 195 |
12 |
| SV **t = av_fetch(toks, 0, 0); |
| 196 |
|
| /* |
| 197 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 198 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 199 |
12 |
▶ | return (t && *t) ? *t : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| return (t && *t) ? *t : NULL; |
12 |
0 |
12 |
| return (t && *t) ? *t : NULL; |
12 |
0 |
12 |
|
| 200 |
|
| } |
| 201 |
|
| } |
| 202 |
|
| |
| 203 |
|
| /* Spend `given`: drop it from the list and put a fresh one at the head. The |
| 204 |
|
| * drop is what makes the token single-use - the same value never validates |
| 205 |
|
| * twice, because the session it was checked against no longer holds it. */ |
| 206 |
11 |
| static void pcf_rotate(pTHX_ SV *c, SV *given, IV keep) { |
| 207 |
11 |
| HV *sess = pcf_session(aTHX_ c); |
| 208 |
|
| SV **slot; |
| 209 |
|
| AV *toks, *next; |
| 210 |
|
| SSize_t i, n; |
| 211 |
|
| STRLEN gl; |
| 212 |
|
| const char *g; |
| 213 |
|
| /* |
| 214 |
|
| # uncoverable branch true |
| 215 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 216 |
11 |
▶ | if (!sess) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sess) return; |
0 |
11 |
11 |
|
| 217 |
11 |
| slot = hv_fetchs(sess, PC_SESSION_KEY, 0); |
| 218 |
|
| /* |
| 219 |
|
| # uncoverable branch true |
| 220 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 221 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 222 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 223 |
|
| # uncoverable condition left count:4 class:ignore_covered_err */ |
| 224 |
11 |
▶ | if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) |
Branch: 4 branches
| Branch | True | False | Total |
| 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 |
|
| 225 |
0 |
| return; /* # uncoverable statement */ |
| 226 |
11 |
| toks = (AV *)SvRV(*slot); |
| 227 |
11 |
| g = SvPV_const(given, gl); |
| 228 |
|
| |
| 229 |
11 |
| next = newAV(); |
| 230 |
11 |
| av_push(next, pcf_mint(aTHX)); |
| 231 |
11 |
| n = av_len(toks) + 1; |
| 232 |
|
| /* |
| 233 |
|
| # uncoverable branch true |
| 234 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 235 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 236 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 237 |
11 |
▶ | for (i = 0; i < n && av_len(next) + 1 < keep; i++) { |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 238 |
0 |
| SV **t = av_fetch(toks, i, 0); /* # uncoverable statement */ |
| 239 |
|
| STRLEN tl; |
| 240 |
|
| const char *tv; |
| 241 |
|
| /* |
| 242 |
|
| # uncoverable statement |
| 243 |
|
| # uncoverable branch true |
| 244 |
|
| # uncoverable branch false |
| 245 |
|
| # uncoverable condition left count:1 |
| 246 |
|
| # uncoverable condition right count:1 |
| 247 |
|
| # uncoverable condition left count:2 |
| 248 |
|
| # uncoverable condition right count:2 |
| 249 |
|
| # uncoverable condition left count:3 |
| 250 |
|
| # uncoverable condition right count:3 */ |
| 251 |
0 |
▶ | if (!(t && *t && SvOK(*t))) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 252 |
0 |
| tv = SvPV_const(*t, tl); /* # uncoverable statement */ |
| 253 |
|
| /* |
| 254 |
|
| # uncoverable statement |
| 255 |
|
| # uncoverable branch true |
| 256 |
|
| # uncoverable branch false |
| 257 |
|
| # uncoverable condition left count:1 |
| 258 |
|
| # uncoverable condition right count:1 |
| 259 |
|
| # uncoverable condition left count:2 |
| 260 |
|
| # uncoverable condition right count:2 */ |
| 261 |
0 |
▶ | if (tl == gl && memEQ(tv, g, tl)) continue; /* the one just spent */ |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 262 |
0 |
| av_push(next, newSVsv(*t)); /* # uncoverable statement */ |
| 263 |
|
| } |
| 264 |
11 |
| (void)hv_stores(sess, PC_SESSION_KEY, newRV_noinc((SV *)next)); |
| 265 |
11 |
| pcf_mark_dirty(aTHX_ c); |
| 266 |
|
| } |
| 267 |
|
| |
| 268 |
|
| /* Does any live token match? */ |
| 269 |
43 |
| static int pcf_accepts(pTHX_ SV *c, SV *given) { |
| 270 |
43 |
| HV *sess = pcf_session(aTHX_ c); |
| 271 |
|
| SV **slot; |
| 272 |
|
| AV *toks; |
| 273 |
|
| SSize_t i, n; |
| 274 |
|
| STRLEN gl; |
| 275 |
|
| const char *g; |
| 276 |
|
| /* |
| 277 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 278 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 279 |
43 |
▶ | if (!sess || !given || !SvOK(given)) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 280 |
15 |
| g = SvPV_const(given, gl); |
| 281 |
|
| /* |
| 282 |
|
| # uncoverable branch true |
| 283 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 284 |
15 |
▶ | if (!gl) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!gl) return 0; |
0 |
15 |
15 |
|
| 285 |
15 |
| slot = hv_fetchs(sess, PC_SESSION_KEY, 0); |
| 286 |
|
| /* |
| 287 |
|
| # uncoverable branch true |
| 288 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 289 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 290 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 291 |
|
| # uncoverable condition left count:4 class:ignore_covered_err */ |
| 292 |
15 |
▶ | if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) |
Branch: 4 branches
| Branch | True | False | Total |
| 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 |
|
| 293 |
0 |
| return 0; /* # uncoverable statement */ |
| 294 |
15 |
| toks = (AV *)SvRV(*slot); |
| 295 |
15 |
| n = av_len(toks) + 1; |
| 296 |
19 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
15 |
4 |
19 |
|
| 297 |
15 |
| SV **t = av_fetch(toks, i, 0); |
| 298 |
|
| STRLEN tl; |
| 299 |
|
| const char *tv; |
| 300 |
|
| /* |
| 301 |
|
| # uncoverable branch true |
| 302 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 303 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 304 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 305 |
15 |
▶ | if (!(t && *t && SvOK(*t))) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 306 |
15 |
| tv = SvPV_const(*t, tl); |
| 307 |
15 |
▶ | if (pk_ct_eq(tv, tl, g, gl)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pk_ct_eq(tv, tl, g, gl)) return 1; |
11 |
4 |
15 |
|
| 308 |
|
| } |
| 309 |
4 |
| return 0; |
| 310 |
|
| } |
| 311 |
|
| |
| 312 |
|
| /* ---- reading what the client sent ----------------------------------------- */ |
| 313 |
|
| |
| 314 |
292 |
| static SV *pcf_env_str(pTHX_ SV *c, const char *key) { |
| 315 |
292 |
| SV *env = pcx_get(aTHX_ pcx_av(aTHX_ c), PCX_ENV); |
| 316 |
|
| SV **e; |
| 317 |
|
| /* |
| 318 |
|
| # uncoverable branch true |
| 319 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 320 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 321 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 322 |
292 |
▶ | if (!(env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV)) return NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 323 |
292 |
| e = hv_fetch((HV *)SvRV(env), key, (I32)strlen(key), 0); |
| 324 |
|
| /* |
| 325 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 326 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 327 |
292 |
▶ | return (e && *e && SvOK(*e)) ? *e : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 328 |
|
| } |
| 329 |
|
| |
| 330 |
|
| /* The submitted token: the header first, then the form field. |
| 331 |
|
| * |
| 332 |
|
| * The field is only looked at for urlencoded bodies under max_body, and this |
| 333 |
|
| * is not fussiness - pq_body reads the whole body into memory on first touch, |
| 334 |
|
| * so reaching for the field on a multipart upload would pull the upload into |
| 335 |
|
| * memory before the mount's own max_body_size check could refuse it. Uploads |
| 336 |
|
| * send the header. */ |
| 337 |
43 |
| static SV *pcf_submitted(pTHX_ SV *c, HV *cfg) { |
| 338 |
|
| STRLEN l; |
| 339 |
43 |
| const char *hdr = ps_cfg_str(aTHX_ cfg, "header", "X-CSRF-Token", &l); |
| 340 |
|
| SV *v; |
| 341 |
|
| { /* X-CSRF-Token -> HTTP_X_CSRF_TOKEN */ |
| 342 |
|
| char env[128]; |
| 343 |
|
| STRLEN i; |
| 344 |
|
| /* |
| 345 |
|
| # uncoverable branch true class:ignore_covered_err |
| 346 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 347 |
56 |
▶ | if (l > sizeof(env) - 6) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (l > sizeof(env) - 6) return NULL; |
0 |
43 |
43 |
|
| 348 |
43 |
| memcpy(env, "HTTP_", 5); |
| 349 |
554 |
▶ | for (i = 0; i < l; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < l; i++) { |
511 |
43 |
554 |
|
| 350 |
511 |
| char ch = hdr[i]; |
| 351 |
511 |
▶ | env[5 + i] = (ch == '-') ? '_' : (char)toUPPER((U8)ch); |
Branch: 2 branches
| Branch | True | False | Total |
| env[5 + i] = (ch == '-') ? '_' : (char)toUPPER((U8)ch); |
426 |
85 |
511 |
| env[5 + i] = (ch == '-') ? '_' : (char)toUPPER((U8)ch); |
172 |
254 |
426 |
|
| 352 |
|
| } |
| 353 |
43 |
| env[5 + l] = '\0'; |
| 354 |
43 |
| v = pcf_env_str(aTHX_ c, env); |
| 355 |
43 |
▶ | if (v && SvCUR(v)) return v; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (v && SvCUR(v)) return v; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
13 |
30 |
43 |
| if (v && SvCUR(v)) return v; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
13 |
0 |
13 |
|
| 356 |
|
| } |
| 357 |
|
| |
| 358 |
|
| { |
| 359 |
30 |
| SV *ct = pcf_env_str(aTHX_ c, "CONTENT_TYPE"); |
| 360 |
30 |
| SV *cl = pcf_env_str(aTHX_ c, "CONTENT_LENGTH"); |
| 361 |
30 |
| IV max = pcf_cfg_iv(aTHX_ cfg, "max_body", 65536); |
| 362 |
|
| const char *field; |
| 363 |
|
| STRLEN fl; |
| 364 |
|
| SV *req, *form, **e; |
| 365 |
30 |
▶ | if (!ct) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ct) return NULL; |
8 |
22 |
30 |
|
| 366 |
22 |
▶ | if (!strnEQ(SvPVX(ct), "application/x-www-form-urlencoded", 33)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!strnEQ(SvPVX(ct), "application/x-www-form-urlencoded", 33)) |
3 |
19 |
22 |
|
| 367 |
3 |
| return NULL; |
| 368 |
|
| /* |
| 369 |
|
| # uncoverable branch true |
| 370 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 371 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 372 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 373 |
19 |
▶ | if (cl && SvIV(cl) > max) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cl && SvIV(cl) > max) return NULL; |
19 |
0 |
19 |
| if (cl && SvIV(cl) > max) return NULL; |
0 |
19 |
19 |
|
| 374 |
|
| |
| 375 |
19 |
| field = ps_cfg_str(aTHX_ cfg, "field", "_csrf", &fl); |
| 376 |
|
| /* The form table and nothing else. `param` looks in the query string |
| 377 |
|
| * first, and a token accepted from there is a token in access logs |
| 378 |
|
| * and Referer headers. */ |
| 379 |
19 |
| req = pcx_force(aTHX_ pcx_av(aTHX_ c), PCX_REQ, "Punk::Request", |
| 380 |
|
| pcx_get(aTHX_ pcx_av(aTHX_ c), PCX_ENV)); |
| 381 |
19 |
| form = pcx_call_meth(aTHX_ req, "form", NULL, 0, 1); |
| 382 |
|
| /* |
| 383 |
|
| # uncoverable branch false |
| 384 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 385 |
19 |
▶ | if (form) sv_2mortal(form); |
Branch: 1 branch
| Branch | True | False | Total |
| if (form) sv_2mortal(form); |
19 |
0 |
19 |
|
| 386 |
|
| /* |
| 387 |
|
| # uncoverable branch true |
| 388 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 389 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 390 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 391 |
19 |
▶ | if (!(form && SvROK(form) && SvTYPE(SvRV(form)) == SVt_PVHV)) |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 392 |
0 |
| return NULL; /* # uncoverable statement */ |
| 393 |
19 |
| e = hv_fetch((HV *)SvRV(form), field, (I32)fl, 0); |
| 394 |
|
| /* |
| 395 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 396 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 397 |
|
| # uncoverable condition left count:4 class:ignore_covered_err |
| 398 |
|
| # uncoverable condition left count:5 class:ignore_covered_err */ |
| 399 |
19 |
▶ | if (!(e && *e && SvOK(*e) && !SvROK(*e) && SvCUR(*e))) return NULL; |
Branch: 5 branches
| Branch | True | False | Total |
| 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 |
|
| 400 |
2 |
| return sv_2mortal(newSVsv(*e)); |
| 401 |
|
| } |
| 402 |
|
| } |
| 403 |
|
| |
| 404 |
|
| /* ---- the check ------------------------------------------------------------ */ |
| 405 |
|
| |
| 406 |
89 |
| static int pcf_safe_method(const char *m, STRLEN l) { |
| 407 |
37 |
▶ | return (l == 3 && memEQ(m, "GET", 3)) |
Branch: 1 branch
| Branch | True | False | Total |
| return (l == 3 && memEQ(m, "GET", 3)) |
1 |
36 |
37 |
|
| 408 |
53 |
▶ | || (l == 4 && memEQ(m, "HEAD", 4)) |
Branch: 2 branches
| Branch | True | False | Total |
| || (l == 4 && memEQ(m, "HEAD", 4)) |
52 |
1 |
53 |
| || (l == 4 && memEQ(m, "HEAD", 4)) |
51 |
1 |
52 |
|
| 409 |
52 |
▶ | || (l == 7 && memEQ(m, "OPTIONS", 7)) |
Branch: 2 branches
| Branch | True | False | Total |
| || (l == 7 && memEQ(m, "OPTIONS", 7)) |
0 |
52 |
52 |
| || (l == 7 && memEQ(m, "OPTIONS", 7)) |
0 |
0 |
0 |
|
| 410 |
178 |
▶ | || (l == 5 && memEQ(m, "TRACE", 5)); |
Branch: 3 branches
| Branch | True | False | Total |
| || (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 |
|
| 411 |
|
| } |
| 412 |
|
| |
| 413 |
48 |
| static int pcf_exempt(pTHX_ HV *cfg, SV *path) { |
| 414 |
48 |
| SV **e = hv_fetchs(cfg, "exempt", 0); |
| 415 |
|
| AV *list; |
| 416 |
|
| SSize_t i, n; |
| 417 |
|
| STRLEN pl; |
| 418 |
|
| const char *p; |
| 419 |
|
| /* |
| 420 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 421 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 422 |
|
| # uncoverable condition left count:4 class:ignore_covered_err */ |
| 423 |
48 |
▶ | if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) return 0; |
Branch: 4 branches
| Branch | True | False | Total |
| 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 |
|
| 424 |
|
| /* |
| 425 |
|
| # uncoverable branch true |
| 426 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 427 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 428 |
8 |
▶ | if (!(path && SvOK(path))) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(path && SvOK(path))) return 0; |
8 |
0 |
8 |
| if (!(path && SvOK(path))) return 0; |
0 |
8 |
8 |
|
| 429 |
8 |
| list = (AV *)SvRV(*e); |
| 430 |
8 |
| p = SvPV_const(path, pl); |
| 431 |
8 |
| n = av_len(list) + 1; |
| 432 |
14 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
11 |
3 |
14 |
|
| 433 |
11 |
| SV **x = av_fetch(list, i, 0); |
| 434 |
|
| STRLEN xl; |
| 435 |
|
| const char *xv; |
| 436 |
|
| /* |
| 437 |
|
| # uncoverable branch true |
| 438 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 439 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 440 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 441 |
11 |
▶ | if (!(x && *x && SvOK(*x))) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 442 |
11 |
| xv = SvPV_const(*x, xl); |
| 443 |
|
| /* on a segment boundary: '/hooks' covers /hooks and /hooks/x and |
| 444 |
|
| * not /hooksadmin. A prefix written with its own trailing slash |
| 445 |
|
| * already ends on one. */ |
| 446 |
11 |
▶ | if (xl && xv[xl - 1] == '/') { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (xl && xv[xl - 1] == '/') { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
11 |
0 |
11 |
| if (xl && xv[xl - 1] == '/') { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
5 |
6 |
11 |
|
| 447 |
8 |
▶ | if (xl <= pl && memEQ(p, xv, xl)) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (xl <= pl && memEQ(p, xv, xl)) return 1; |
3 |
2 |
5 |
| if (xl <= pl && memEQ(p, xv, xl)) return 1; |
2 |
1 |
3 |
|
| 448 |
|
| } |
| 449 |
6 |
▶ | else if (ps_under(p, pl, xv, xl)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ps_under(p, pl, xv, xl)) return 1; |
3 |
3 |
6 |
|
| 450 |
|
| } |
| 451 |
3 |
| return 0; |
| 452 |
|
| } |
| 453 |
|
| |
| 454 |
|
| /* Is this Authorization header one a script had to attach? Bearer and the |
| 455 |
|
| * like are; Basic, Digest, Negotiate and NTLM are not - a browser that has |
| 456 |
|
| * the credentials sends them by itself on a cross-site request, exactly as it |
| 457 |
|
| * does a cookie, so they exempt nothing. */ |
| 458 |
52 |
| static int pcf_explicit_auth(pTHX_ SV *auth) { |
| 459 |
|
| static const char *const ambient[] = { "basic", "digest", "negotiate", |
| 460 |
|
| "ntlm", NULL }; |
| 461 |
52 |
| STRLEN l, sl = 0; |
| 462 |
|
| const char *p; |
| 463 |
|
| int i; |
| 464 |
52 |
▶ | if (!auth) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!auth) return 0; |
41 |
11 |
52 |
|
| 465 |
11 |
| p = SvPV_const(auth, l); |
| 466 |
14 |
▶ | while (l && (*p == ' ' || *p == '\t')) { p++; l--; } /* # uncoverable condition left count:3 class:ignore_covered_err */ |
Branch: 3 branches
| Branch | True | False | Total |
| while (l && (*p == ' ' || *p == '\t')) { p++; l--; } /* # uncoverable condition left count:3 class:ignore_covered_err */ |
13 |
1 |
14 |
| while (l && (*p == ' ' || *p == '\t')) { p++; l--; } /* # uncoverable condition left count:3 class:ignore_covered_err */ |
3 |
10 |
13 |
| while (l && (*p == ' ' || *p == '\t')) { p++; l--; } /* # uncoverable condition left count:3 class:ignore_covered_err */ |
0 |
10 |
10 |
|
| 467 |
67 |
▶ | while (sl < l && p[sl] != ' ' && p[sl] != '\t') sl++; /* # uncoverable condition right count:3 class:ignore_covered_err */ |
Branch: 3 branches
| Branch | True | False | Total |
| while (sl < l && p[sl] != ' ' && p[sl] != '\t') sl++; /* # uncoverable condition right count:3 class:ignore_covered_err */ |
65 |
2 |
67 |
| while (sl < l && p[sl] != ' ' && p[sl] != '\t') sl++; /* # uncoverable condition right count:3 class:ignore_covered_err */ |
56 |
9 |
65 |
| while (sl < l && p[sl] != ' ' && p[sl] != '\t') sl++; /* # uncoverable condition right count:3 class:ignore_covered_err */ |
56 |
0 |
56 |
|
| 468 |
11 |
▶ | if (!sl || sl == l) return 0; /* no scheme, or no credentials */ |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 469 |
31 |
▶ | for (i = 0; ambient[i]; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; ambient[i]; i++) |
27 |
4 |
31 |
|
| 470 |
27 |
▶ | if (strlen(ambient[i]) == sl && foldEQ(p, ambient[i], (I32)sl)) |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 471 |
5 |
| return 0; |
| 472 |
4 |
| return 1; |
| 473 |
|
| } |
| 474 |
|
| |
| 475 |
|
| /* The 403, in the shape everything else in Punk answers errors with. */ |
| 476 |
32 |
| static SV *pcf_refuse(pTHX_ SV *c, HV *cfg) { |
| 477 |
32 |
| SV **h = hv_fetchs(cfg, "on_error", 0); |
| 478 |
|
| /* |
| 479 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 480 |
|
| # uncoverable condition right count:3 class:ignore_covered_err |
| 481 |
|
| # uncoverable condition right count:4 class:ignore_covered_err */ |
| 482 |
32 |
▶ | if (h && *h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVCV) { |
Branch: 4 branches
| Branch | True | False | Total |
| 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 |
|
| 483 |
2 |
| int died = 0; |
| 484 |
2 |
| SV *r = pcx_call2(aTHX_ *h, c, &PL_sv_undef, &died); |
| 485 |
|
| /* |
| 486 |
|
| # uncoverable branch false |
| 487 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 488 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 489 |
2 |
▶ | if (!died && r) return r; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!died && r) return r; |
2 |
0 |
2 |
| if (!died && r) return r; |
2 |
0 |
2 |
|
| 490 |
|
| /* |
| 491 |
|
| # uncoverable statement |
| 492 |
|
| # uncoverable branch true |
| 493 |
|
| # uncoverable branch false |
| 494 |
|
| # uncoverable condition left count:1 |
| 495 |
|
| # uncoverable condition right count:1 */ |
| 496 |
0 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
0 |
0 |
0 |
|
| 497 |
|
| } |
| 498 |
30 |
| return punk_triplet(aTHX_ 403, |
| 499 |
|
| sv_2mortal(newSVpvs("application/json")), |
| 500 |
|
| newSVpvs("{\"errors\":[{\"message\":\"invalid csrf token\"}]}"), |
| 501 |
|
| NULL); |
| 502 |
|
| } |
| 503 |
|
| |
| 504 |
|
| /* The before-dispatch hook: a reference return short-circuits the request, so |
| 505 |
|
| * a refusal answers before any guard or handler runs. */ |
| 506 |
89 |
| static SV *pcf_check(pTHX_ SV *c) { |
| 507 |
89 |
| HV *cfg = pcf_cfg(aTHX_ c); |
| 508 |
|
| SV *method, *given; |
| 509 |
|
| STRLEN ml; |
| 510 |
|
| const char *m; |
| 511 |
|
| /* |
| 512 |
|
| # uncoverable branch true |
| 513 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 514 |
89 |
▶ | if (!cfg) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cfg) return NULL; |
0 |
89 |
89 |
|
| 515 |
|
| |
| 516 |
89 |
| method = pcf_env_str(aTHX_ c, "REQUEST_METHOD"); |
| 517 |
178 |
▶ | m = method ? SvPV_const(method, ml) : "GET"; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| m = method ? SvPV_const(method, ml) : "GET"; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
89 |
0 |
89 |
|
| 518 |
|
| /* |
| 519 |
|
| # uncoverable branch true |
| 520 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 521 |
89 |
▶ | if (!method) ml = 3; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!method) ml = 3; |
0 |
89 |
89 |
|
| 522 |
89 |
▶ | if (pcf_safe_method(m, ml)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pcf_safe_method(m, ml)) return NULL; |
37 |
52 |
89 |
|
| 523 |
|
| |
| 524 |
|
| /* An Extended CONNECT (RFC 8441 on HTTP/2, RFC 9220 on HTTP/3) is the |
| 525 |
|
| * websocket upgrade a multiplexed transport has instead of a GET, and |
| 526 |
|
| * punk_serve.h routes it as that GET. The GET was never a CSRF target, |
| 527 |
|
| * so nor is this: the handshake's own origin check is what stands |
| 528 |
|
| * between a page elsewhere and an authenticated socket, on either |
| 529 |
|
| * transport. A browser opens every websocket this way once the server |
| 530 |
|
| * has advertised the setting, so refusing it here refused them all. */ |
| 531 |
52 |
▶ | if (ml == 7 && memEQ(m, "CONNECT", 7)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (ml == 7 && memEQ(m, "CONNECT", 7)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
0 |
52 |
52 |
| if (ml == 7 && memEQ(m, "CONNECT", 7)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
0 |
0 |
0 |
|
| 532 |
0 |
| SV *cp = pcf_env_str(aTHX_ c, "psgix.connect_protocol"); |
| 533 |
|
| /* |
| 534 |
|
| # uncoverable branch false |
| 535 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 536 |
0 |
▶ | if (cp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cp) { |
0 |
0 |
0 |
|
| 537 |
|
| STRLEN pl; |
| 538 |
0 |
| const char *pv = SvPV_const(cp, pl); |
| 539 |
|
| /* |
| 540 |
|
| # uncoverable branch false |
| 541 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 542 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 543 |
0 |
▶ | if (pl == 9 && memEQ(pv, "websocket", 9)) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (pl == 9 && memEQ(pv, "websocket", 9)) return NULL; |
0 |
0 |
0 |
| if (pl == 9 && memEQ(pv, "websocket", 9)) return NULL; |
0 |
0 |
0 |
|
| 544 |
|
| } |
| 545 |
|
| } |
| 546 |
|
| |
| 547 |
|
| /* Not riding on cookies, so not a CSRF target. */ |
| 548 |
52 |
▶ | if (pcf_explicit_auth(aTHX_ pcf_env_str(aTHX_ c, "HTTP_AUTHORIZATION"))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (pcf_explicit_auth(aTHX_ pcf_env_str(aTHX_ c, "HTTP_AUTHORIZATION"))) |
4 |
48 |
52 |
|
| 549 |
4 |
| return NULL; |
| 550 |
48 |
▶ | if (pcf_exempt(aTHX_ cfg, pcf_env_str(aTHX_ c, "PATH_INFO"))) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pcf_exempt(aTHX_ cfg, pcf_env_str(aTHX_ c, "PATH_INFO"))) return NULL; |
5 |
43 |
48 |
|
| 551 |
|
| |
| 552 |
43 |
| given = pcf_submitted(aTHX_ c, cfg); |
| 553 |
43 |
▶ | if (!pcf_accepts(aTHX_ c, given)) return pcf_refuse(aTHX_ c, cfg); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pcf_accepts(aTHX_ c, given)) return pcf_refuse(aTHX_ c, cfg); |
32 |
11 |
43 |
|
| 554 |
|
| |
| 555 |
11 |
| pcf_rotate(aTHX_ c, given, pcf_cfg_iv(aTHX_ cfg, "keep", 1)); |
| 556 |
11 |
| return NULL; |
| 557 |
|
| } |
| 558 |
|
| |
| 559 |
|
| /* ---- the mirror cookie ---------------------------------------------------- */ |
| 560 |
|
| |
| 561 |
|
| /* The token also goes out in a cookie the browser will let script read, which |
| 562 |
|
| * is how fetch and Open::API::UI's try-it-out get hold of it. It is not the |
| 563 |
|
| * authority - the session is - so a forged one proves nothing. */ |
| 564 |
89 |
| static void pcf_writeback(pTHX_ SV *c, SV *resp) { |
| 565 |
89 |
| HV *cfg = pcf_cfg(aTHX_ c); |
| 566 |
|
| AV *av, *r, *headers; |
| 567 |
|
| HV *stash, *ckopts; |
| 568 |
|
| SV **hp, **dirty; |
| 569 |
|
| SV *tok, *name; |
| 570 |
|
| STRLEN nl; |
| 571 |
|
| const char *cookie_name; |
| 572 |
|
| /* |
| 573 |
|
| # uncoverable branch true class:ignore_covered_err |
| 574 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 575 |
155 |
▶ | if (!cfg) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cfg) return; |
0 |
89 |
89 |
|
| 576 |
89 |
| av = pcx_av(aTHX_ c); |
| 577 |
|
| { |
| 578 |
89 |
| SV *st = pcx_get(aTHX_ av, PCX_STASH); |
| 579 |
89 |
▶ | if (!(st && SvROK(st))) return; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(st && SvROK(st))) return; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
79 |
10 |
89 |
| if (!(st && SvROK(st))) return; /* # uncoverable condition left count:2 class:ignore_covered_err */ |
0 |
79 |
79 |
|
| 580 |
79 |
| stash = (HV *)SvRV(st); |
| 581 |
|
| } |
| 582 |
79 |
| dirty = hv_fetchs(stash, PC_STASH_DIRTY, 0); |
| 583 |
|
| /* |
| 584 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 585 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 586 |
79 |
▶ | if (!(dirty && *dirty && SvTRUE(*dirty))) return; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 587 |
|
| /* |
| 588 |
|
| # uncoverable branch true |
| 589 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 590 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 591 |
23 |
▶ | if (!(SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) return; |
23 |
0 |
23 |
| if (!(SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) return; |
0 |
23 |
23 |
|
| 592 |
23 |
| r = (AV *)SvRV(resp); |
| 593 |
23 |
| hp = av_fetch(r, 1, 0); |
| 594 |
|
| /* |
| 595 |
|
| # uncoverable branch true |
| 596 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 597 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 598 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 599 |
|
| # uncoverable condition left count:4 class:ignore_covered_err */ |
| 600 |
23 |
▶ | if (!(hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV)) return; |
Branch: 4 branches
| Branch | True | False | Total |
| 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 |
|
| 601 |
23 |
| headers = (AV *)SvRV(*hp); |
| 602 |
|
| |
| 603 |
23 |
| tok = pcf_token(aTHX_ c, 0); |
| 604 |
|
| /* |
| 605 |
|
| # uncoverable branch true |
| 606 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 607 |
23 |
▶ | if (!tok) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tok) return; |
0 |
23 |
23 |
|
| 608 |
|
| |
| 609 |
|
| /* the session's cookie attributes, minus HttpOnly - script has to read it */ |
| 610 |
23 |
| ckopts = (HV *)sv_2mortal((SV *)newHV()); |
| 611 |
|
| { |
| 612 |
23 |
| HV *scfg = ps_cfg(aTHX_ c); |
| 613 |
|
| static const char *const copy[] = { "path", "domain", "secure", |
| 614 |
|
| "samesite", "max_age", NULL }; |
| 615 |
|
| int i; |
| 616 |
138 |
▶ | for (i = 0; scfg && copy[i]; i++) { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; scfg && copy[i]; i++) { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
138 |
0 |
138 |
| for (i = 0; scfg && copy[i]; i++) { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
115 |
23 |
138 |
|
| 617 |
115 |
| SV **e = hv_fetch(scfg, copy[i], (I32)strlen(copy[i]), 0); |
| 618 |
|
| /* |
| 619 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 620 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 621 |
115 |
▶ | if (e && *e && SvOK(*e)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && *e && SvOK(*e)) |
48 |
67 |
115 |
| if (e && *e && SvOK(*e)) |
48 |
0 |
48 |
| if (e && *e && SvOK(*e)) |
48 |
0 |
48 |
|
| 622 |
48 |
| (void)hv_store(ckopts, copy[i], (I32)strlen(copy[i]), |
| 623 |
|
| newSVsv(*e), 0); |
| 624 |
|
| } |
| 625 |
23 |
| (void)hv_stores(ckopts, "httponly", newSViv(0)); |
| 626 |
|
| } |
| 627 |
|
| |
| 628 |
23 |
| cookie_name = ps_cfg_str(aTHX_ cfg, "cookie", "csrf", &nl); |
| 629 |
23 |
| name = sv_2mortal(newSVpvn(cookie_name, nl)); |
| 630 |
23 |
| av_push(headers, newSVpvs("Set-Cookie")); |
| 631 |
23 |
| av_push(headers, pk_build_cookie_ex(aTHX_ name, tok, ckopts, |
| 632 |
|
| ps_secure_default(aTHX_ c))); |
| 633 |
|
| } |
| 634 |
|
| |
| 635 |
|
| #endif /* PUNK_CSRF_H */ |