include/punk/punk_session.h

filter: Statementstmt 97.5% 350 / 361 (2 uncoverable) Branchbran 76.8% 273 / 380 (31 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.9% 623 / 741 (33 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/sse.xs ↑ Summary include/punk/punk_frozen.h »
1 /* punk_session.h - signed cookie sessions, in C.
2 *
3 * The session is the handler's hashref, JSON-encoded (punk_frj) and carried in
4 * a cookie as base64url(payload).base64url(HMAC-SHA256(payload, key)) - the
5 * client can read it but cannot forge it. A bundled public-domain SHA-256 (as
6 * SHA-1 was bundled for the WS handshake) keeps the dist zero-dependency.
7 *
8 * Must be included after punk_context.h (frj / pcx_*), punk_cookie.h
9 * (pk_build_cookie) and punk_names.h.
10 */
11  
12 #ifndef PUNK_SESSION_H
13 #define PUNK_SESSION_H
14  
15 /* ---- SHA-256 (public domain) ---------------------------------------------- */
16  
17 #define PS_ROR(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
18  
19 static const U32 ps_sha256_k[64] = {
20 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
21 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
22 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
23 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
24 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
25 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
26 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
27 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
28 };
29  
30 /* The compression function over whole 64-byte blocks, folded into `h`. Split
31 * out so a caller that cannot hold its message in memory (punk_asset.h hashes
32 * a file in chunks) shares this loop rather than keeping a second copy of it
33 * in step. */
34 133834 static void ps_sha256_blocks(U32 h[8], const unsigned char *m, size_t nblocks) {
35 size_t b;
36 int i;
37 267882 ▶ for (b = 0; b < nblocks; b++) {
Branch: 1 branch
BranchTrueFalseTotal
for (b = 0; b < nblocks; b++) { 134048 133834 267882
38 134048 const unsigned char *p = m + b * 64;
39 U32 w[64], a, c0, c, d, e, f, g, hh, t1, t2;
40 2278816 ▶ for (i = 0; i < 16; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) 2144768 134048 2278816
41 2144768 w[i] = ((U32)p[4*i]<<24)|((U32)p[4*i+1]<<16)
42 2144768 | ((U32)p[4*i+2]<<8)|(U32)p[4*i+3];
43 6568352 ▶ for (i = 16; i < 64; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 16; i < 64; i++) { 6434304 134048 6568352
44 6434304 U32 s0 = PS_ROR(w[i-15],7) ^ PS_ROR(w[i-15],18) ^ (w[i-15]>>3);
45 6434304 U32 s1 = PS_ROR(w[i-2],17) ^ PS_ROR(w[i-2],19) ^ (w[i-2]>>10);
46 6434304 w[i] = w[i-16] + s0 + w[i-7] + s1;
47 }
48 134048 a=h[0];c0=h[1];c=h[2];d=h[3];e=h[4];f=h[5];g=h[6];hh=h[7];
49 8713120 ▶ for (i = 0; i < 64; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 64; i++) { 8579072 134048 8713120
50 8579072 U32 S1 = PS_ROR(e,6)^PS_ROR(e,11)^PS_ROR(e,25);
51 8579072 U32 ch = (e & f) ^ ((~e) & g);
52 8579072 U32 S0 = PS_ROR(a,2)^PS_ROR(a,13)^PS_ROR(a,22);
53 8579072 U32 maj = (a & c0) ^ (a & c) ^ (c0 & c);
54 8579072 t1 = hh + S1 + ch + ps_sha256_k[i] + w[i];
55 8579072 t2 = S0 + maj;
56 8579072 hh=g; g=f; f=e; e=d+t1; d=c; c=c0; c0=a; a=t1+t2;
57 }
58 134048 h[0]+=a;h[1]+=c0;h[2]+=c;h[3]+=d;h[4]+=e;h[5]+=f;h[6]+=g;h[7]+=hh;
59 }
60 133834 }
61  
62 static const U32 ps_sha256_iv[8] = { 0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,
63 0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19 };
64  
65 /* The final block(s): the 0x80 terminator, the zero padding and the 64-bit
66 * bit-count, folded in. `rem` is what is left of the message after the whole
67 * blocks, `total` its full byte length. */
68 66903 static void ps_sha256_final(U32 h[8], const unsigned char *rest, size_t rem,
69 size_t total, unsigned char out[32]) {
70 unsigned char tail[128];
71 66903 ▶ size_t tlen = (rem < 56) ? 64 : 128;
Branch: 1 branch
BranchTrueFalseTotal
size_t tlen = (rem < 56) ? 64 : 128; 66900 3 66903
72 66903 U32 hi = (U32)(total >> 29), lo = (U32)(total << 3);
73 int i;
74 66903 memset(tail, 0, sizeof tail);
75 /*
76 # uncoverable branch false
77 # uncoverable condition right count:1 class:ignore_covered_err */
78 66903 ▶ if (rem) memcpy(tail, rest, rem);
Branch: 1 branch
BranchTrueFalseTotal
if (rem) memcpy(tail, rest, rem); 66903 0 66903
79 66903 tail[rem] = 0x80;
80 66903 tail[tlen-1]=(unsigned char)(lo&0xff); tail[tlen-2]=(unsigned char)((lo>>8)&0xff);
81 66903 tail[tlen-3]=(unsigned char)((lo>>16)&0xff); tail[tlen-4]=(unsigned char)((lo>>24)&0xff);
82 66903 tail[tlen-5]=(unsigned char)(hi&0xff); tail[tlen-6]=(unsigned char)((hi>>8)&0xff);
83 66903 tail[tlen-7]=(unsigned char)((hi>>16)&0xff); tail[tlen-8]=(unsigned char)((hi>>24)&0xff);
84 66903 ps_sha256_blocks(h, tail, tlen / 64);
85 602127 ▶ for (i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) { 535224 66903 602127
86 535224 out[4*i] =(unsigned char)((h[i]>>24)&0xff);
87 535224 out[4*i+1]=(unsigned char)((h[i]>>16)&0xff);
88 535224 out[4*i+2]=(unsigned char)((h[i]>>8)&0xff);
89 535224 out[4*i+3]=(unsigned char)(h[i]&0xff);
90 }
91 66903 }
92  
93 66859 static void pk_sha256(const unsigned char *msg, size_t len, unsigned char out[32]) {
94 U32 h[8];
95 66859 size_t whole = len / 64;
96 66859 memcpy(h, ps_sha256_iv, sizeof h);
97 66859 ▶ if (whole) ps_sha256_blocks(h, msg, whole);
Branch: 1 branch
BranchTrueFalseTotal
if (whole) ps_sha256_blocks(h, msg, whole); 66727 132 66859
98 66859 ps_sha256_final(h, msg + whole * 64, len - whole * 64, len, out);
99 66859 }
100  
101 33363 static void pk_hmac_sha256(const unsigned char *key, size_t klen,
102 const unsigned char *msg, size_t mlen,
103 unsigned char out[32]) {
104 unsigned char k[64], ipad[64], opad[64], ihash[32], ob[96];
105 unsigned char *ibuf;
106 int i;
107 /*
108 # uncoverable branch true
109 # uncoverable condition left count:1 class:ignore_covered_err */
110 33363 ▶ if (klen > 64) { pk_sha256(key, klen, k); memset(k+32, 0, 32); }
Branch: 1 branch
BranchTrueFalseTotal
if (klen > 64) { pk_sha256(key, klen, k); memset(k+32, 0, 32); } 0 33363 33363
111 /*
112 # uncoverable branch false
113 # uncoverable condition right count:1 class:ignore_covered_err */
114 33363 ▶ else { memcpy(k, key, klen); if (klen < 64) memset(k+klen, 0, 64-klen); }
Branch: 1 branch
BranchTrueFalseTotal
else { memcpy(k, key, klen); if (klen < 64) memset(k+klen, 0, 64-klen); } 33363 0 33363
115 2168595 ▶ for (i = 0; i < 64; i++) { ipad[i] = k[i]^0x36; opad[i] = k[i]^0x5c; }
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 64; i++) { ipad[i] = k[i]^0x36; opad[i] = k[i]^0x5c; } 2135232 33363 2168595
116 33363 Newx(ibuf, 64 + mlen, unsigned char);
117 33363 memcpy(ibuf, ipad, 64);
118 /*
119 # uncoverable branch false
120 # uncoverable condition right count:1 class:ignore_covered_err */
121 33363 ▶ if (mlen) memcpy(ibuf+64, msg, mlen);
Branch: 1 branch
BranchTrueFalseTotal
if (mlen) memcpy(ibuf+64, msg, mlen); 33363 0 33363
122 33363 pk_sha256(ibuf, 64+mlen, ihash);
123 33363 Safefree(ibuf);
124 33363 memcpy(ob, opad, 64);
125 33363 memcpy(ob+64, ihash, 32);
126 33363 pk_sha256(ob, 96, out);
127 33363 }
128  
129 /* ---- base64url (no padding, cookie-safe) ---------------------------------- */
130  
131 static const char PS_B64U[] =
132 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
133  
134 5462 static SV *pk_b64url(pTHX_ const unsigned char *in, size_t len) {
135 5462 SV *out = newSVpvs("");
136 5462 size_t i = 0;
137 34291 ▶ for (; i + 3 <= len; i += 3) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i + 3 <= len; i += 3) { 28829 5462 34291
138 28829 U32 n = ((U32)in[i]<<16)|((U32)in[i+1]<<8)|in[i+2];
139 28829 char c[4] = { PS_B64U[(n>>18)&63], PS_B64U[(n>>12)&63],
140 28829 PS_B64U[(n>>6)&63], PS_B64U[n&63] };
141 28829 sv_catpvn(out, c, 4);
142 }
143 5462 ▶ if (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < len) { 4319 1143 5462
144 4319 int rem = (int)(len - i), cn = 2;
145 4319 U32 n = (U32)in[i]<<16;
146 char c[3];
147 4319 ▶ if (rem == 2) n |= (U32)in[i+1]<<8;
Branch: 1 branch
BranchTrueFalseTotal
if (rem == 2) n |= (U32)in[i+1]<<8; 512 3807 4319
148 4319 c[0] = PS_B64U[(n>>18)&63];
149 4319 c[1] = PS_B64U[(n>>12)&63];
150 4319 ▶ if (rem == 2) c[cn++] = PS_B64U[(n>>6)&63];
Branch: 1 branch
BranchTrueFalseTotal
if (rem == 2) c[cn++] = PS_B64U[(n>>6)&63]; 512 3807 4319
151 4319 sv_catpvn(out, c, cn);
152 }
153 5462 return out;
154 }
155  
156 15055 static int ps_b64u_val(unsigned char c) {
157 15055 ▶ if (c>='A'&&c<='Z') return c-'A';
Branch: 2 branches
BranchTrueFalseTotal
if (c>='A'&&c<='Z') return c-'A'; 12994 2061 15055
if (c>='A'&&c<='Z') return c-'A'; 6924 6070 12994
158 8131 ▶ if (c>='a'&&c<='z') return c-'a'+26; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (c>='a'&&c<='z') return c-'a'+26; /* # uncoverable condition right count:2 class:ignore_covered_err */ 6070 2061 8131
if (c>='a'&&c<='z') return c-'a'+26; /* # uncoverable condition right count:2 class:ignore_covered_err */ 6070 0 6070
159 /*
160 # uncoverable branch false
161 # uncoverable condition right count:1 class:ignore_covered_err
162 # uncoverable condition right count:2 class:ignore_covered_err */
163 2061 ▶ if (c>='0'&&c<='9') return c-'0'+52;
Branch: 2 branches
BranchTrueFalseTotal
if (c>='0'&&c<='9') return c-'0'+52; 2061 0 2061
if (c>='0'&&c<='9') return c-'0'+52; 2061 0 2061
164 /*
165 # uncoverable statement
166 # uncoverable branch true
167 # uncoverable branch false
168 # uncoverable condition left count:1
169 # uncoverable condition right count:1 */
170 0 ▶ if (c=='-') return 62;
Branch: 1 branch
BranchTrueFalseTotal
if (c=='-') return 62; 0 0 0
171 /*
172 # uncoverable statement
173 # uncoverable branch true
174 # uncoverable branch false
175 # uncoverable condition left count:1
176 # uncoverable condition right count:1 */
177 0 ▶ if (c=='_') return 63;
Branch: 1 branch
BranchTrueFalseTotal
if (c=='_') return 63; 0 0 0
178 0 return -1; /* # uncoverable statement */
179 }
180  
181 203 static SV *pk_b64url_decode(pTHX_ const char *in, size_t len) {
182 203 SV *out = newSVpvs("");
183 203 U32 acc = 0; int bits = 0; size_t i;
184 15258 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 15055 203 15258
185 15055 int v = ps_b64u_val((unsigned char)in[i]);
186 /*
187 # uncoverable branch true
188 # uncoverable condition left count:1 class:ignore_covered_err */
189 15055 ▶ if (v < 0) { SvREFCNT_dec(out); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (v < 0) { SvREFCNT_dec(out); return NULL; } 0 15055 15055
190 15055 acc = (acc << 6) | (U32)v; bits += 6;
191 15055 ▶ if (bits >= 8) { unsigned char b; bits -= 8; b = (acc >> bits) & 0xff; /* # uncoverable branch true count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
if (bits >= 8) { unsigned char b; bits -= 8; b = (acc >> bits) & 0xff; /* # uncoverable branch true count:1 class:ignore_covered_err */ 11241 3814 15055
192 11241 sv_catpvn(out, (char *)&b, 1); }
193 }
194 203 return out;
195 }
196  
197 /* ---- sign / verify -------------------------------------------------------- */
198  
199 /* payload -> "base64url(payload).base64url(HMAC(base64url(payload)))" (+1) */
200 152 static SV *pk_session_sign(pTHX_ SV *payload, const char *key, STRLEN klen) {
201 152 STRLEN pl; const char *p = SvPV_const(payload, pl);
202 unsigned char mac[32];
203 152 SV *b64p = pk_b64url(aTHX_ (const unsigned char *)p, pl);
204 SV *out;
205 152 pk_hmac_sha256((const unsigned char *)key, klen,
206 152 (const unsigned char *)SvPVX(b64p), SvCUR(b64p), mac);
207 152 out = newSVsv(b64p);
208 152 SvREFCNT_dec(b64p);
209 152 sv_catpvs(out, ".");
210 152 { SV *b64m = pk_b64url(aTHX_ mac, 32); sv_catsv(out, b64m); SvREFCNT_dec(b64m); }
211 152 return out;
212 }
213  
214 /* verify a cookie value; on a good signature return the decoded payload (+1),
215 * else NULL. Constant-time signature compare. */
216 215 static SV *pk_session_verify(pTHX_ const char *cookie, STRLEN clen,
217 const char *key, STRLEN klen) {
218 215 STRLEN i, dot = clen;
219 const char *b64p, *sig;
220 STRLEN b64pl, sigl;
221 unsigned char mac[32];
222 SV *expsig;
223 9433 ▶ for (i = clen; i > 0; i--) if (cookie[i-1] == '.') { dot = i-1; break; }
Branch: 2 branches
BranchTrueFalseTotal
for (i = clen; i > 0; i--) if (cookie[i-1] == '.') { dot = i-1; break; } 211 9218 9429
for (i = clen; i > 0; i--) if (cookie[i-1] == '.') { dot = i-1; break; } 9429 4 9433
224 215 ▶ if (dot >= clen) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (dot >= clen) return NULL; 4 211 215
225 211 b64p = cookie; b64pl = dot;
226 211 sig = cookie + dot + 1; sigl = clen - dot - 1;
227 211 pk_hmac_sha256((const unsigned char *)key, klen,
228 (const unsigned char *)b64p, b64pl, mac);
229 211 expsig = pk_b64url(aTHX_ mac, 32);
230 211 ▶ if (SvCUR(expsig) != sigl) { SvREFCNT_dec(expsig); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(expsig) != sigl) { SvREFCNT_dec(expsig); return NULL; } 3 208 211
231 {
232 208 const char *e = SvPVX(expsig);
233 208 unsigned char diff = 0;
234 STRLEN j;
235 9152 ▶ for (j = 0; j < sigl; j++) diff |= (unsigned char)(e[j] ^ sig[j]);
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < sigl; j++) diff |= (unsigned char)(e[j] ^ sig[j]); 8944 208 9152
236 208 SvREFCNT_dec(expsig);
237 208 ▶ if (diff) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (diff) return NULL; 5 203 208
238 }
239 203 return pk_b64url_decode(aTHX_ b64p, b64pl);
240 }
241  
242 /* ---- the session lifecycle ------------------------------------------------ *
243 * $c->session is a hashref cached in the stash under `punk.session` alongside
244 * the canonical serialization it loaded with (`punk.session.orig`); the after-
245 * dispatch write-back re-signs a Set-Cookie only when that serialization
246 * changed, or deletes the cookie when session_expire set `punk.session.expire`. */
247  
248 /* the session config the `session` keyword froze on the app, or NULL */
249 817 static HV *ps_cfg(pTHX_ SV *c) {
250 817 SV *app = pcx_get(aTHX_ pcx_av(aTHX_ c), PCX_APP);
251 SV **s;
252 /*
253 # uncoverable branch true
254 # uncoverable condition left count:1 class:ignore_covered_err
255 # uncoverable condition left count:2 class:ignore_covered_err
256 # uncoverable condition left count:3 class:ignore_covered_err */
257 817 ▶ 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; 817 0 817
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; 817 0 817
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; 0 817 817
258 817 s = hv_fetchs((HV *)SvRV(app), "session", 0);
259 814 ▶ return (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
return (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 814 0 814
return (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 814 0 814
return (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 814 0 814
260 1631 ▶ ? (HV *)SvRV(*s) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(*s) : NULL; 814 3 817
261 }
262  
263 1537 static const char *ps_cfg_str(pTHX_ HV *cfg, const char *k, const char *def,
264 STRLEN *len) {
265 1537 SV **e = hv_fetch(cfg, k, (I32)strlen(k), 0);
266 /*
267 # uncoverable branch false
268 # uncoverable condition right count:1 class:ignore_covered_err
269 # uncoverable condition right count:2 class:ignore_covered_err
270 # uncoverable condition right count:3 class:ignore_covered_err */
271 3074 ▶ if (e && *e && SvOK(*e)) return SvPV_const(*e, *len);
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) return SvPV_const(*e, *len); 1537 0 1537
if (e && *e && SvOK(*e)) return SvPV_const(*e, *len); 1537 0 1537
if (e && *e && SvOK(*e)) return SvPV_const(*e, *len); 1537 0 1537
272 0 *len = strlen(def); /* # uncoverable statement */
273 0 return def; /* # uncoverable statement */
274 }
275  
276 315 static IV ps_cfg_iv(pTHX_ HV *cfg, const char *k, IV def) {
277 315 SV **e = hv_fetch(cfg, k, (I32)strlen(k), 0);
278 /*
279 # uncoverable condition right count:2 class:ignore_covered_err
280 # uncoverable condition right count:3 class:ignore_covered_err */
281 315 ▶ return (e && *e && SvOK(*e)) ? SvIV(*e) : def;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvOK(*e)) ? SvIV(*e) : def; 63 252 315
return (e && *e && SvOK(*e)) ? SvIV(*e) : def; 63 0 63
return (e && *e && SvOK(*e)) ? SvIV(*e) : def; 63 0 63
282 }
283  
284 /* The absolute expiry stamped into the signed payload, under a reserved key
285 * the application never sees (the same trick flash plays with punk.flash).
286 *
287 * Without it `expires` was only the browser's Max-Age: a hint to a client
288 * that is free to ignore it. The signature carries no time, so a cookie
289 * captured once stayed valid for as long as the secret did, and session_expire
290 * only asked the browser to forget a value that still authenticated. Stamping
291 * the expiry inside the signature is what makes the lifetime the server's to
292 * decide.
293 *
294 * A session cookie with no `expires` is still bounded here, because "until the
295 * browser closes" is the client's promise and not a limit on the value. */
296 #define PK_SESSION_EXP "punk.exp"
297 #define PK_SESSION_EXP_LEN 8
298 #define PK_SESSION_MAX_LIFETIME (30 * 86400)
299  
300 1450 static HV *ps_stash(pTHX_ AV *av) {
301 1450 SV *st = pcx_get(aTHX_ av, PCX_STASH);
302 1450 ▶ if (!st) { st = newRV_noinc((SV *)newHV()); (void)av_store(av, PCX_STASH, st); }
Branch: 1 branch
BranchTrueFalseTotal
if (!st) { st = newRV_noinc((SV *)newHV()); (void)av_store(av, PCX_STASH, st); } 1099 351 1450
303 1450 return (HV *)SvRV(st);
304 }
305  
306 /* canonical JSON of an SV (+1) - stable across key order for the dirty check */
307 656 static SV *ps_encode(pTHX_ SV *data) {
308 frj_opts o;
309 656 Zero(&o, 1, frj_opts);
310 656 o.canonical = 1;
311 656 return punk_frj(aTHX)->encode(aTHX_ data, &o);
312 }
313  
314 /* "7d" / "1h" / "30m" / "3600" -> seconds */
315 39 static IV ps_parse_duration(pTHX_ SV *v) {
316 STRLEN l; const char *s;
317 39 IV n = 0; STRLEN i = 0;
318 39 ▶ if (SvIOK(v) || looks_like_number(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(v) || looks_like_number(v)) { 39 0 39
319 /* a bare number with no unit is seconds unless a unit follows */
320 }
321 39 s = SvPV_const(v, l);
322 83 ▶ while (i < l && s[i] >= '0' && s[i] <= '9') { n = n * 10 + (s[i] - '0'); i++; } /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
while (i < l && s[i] >= '0' && s[i] <= '9') { n = n * 10 + (s[i] - '0'); i++; } /* # uncoverable condition right count:2 class:ignore_covered_err */ 82 1 83
while (i < l && s[i] >= '0' && s[i] <= '9') { n = n * 10 + (s[i] - '0'); i++; } /* # uncoverable condition right count:2 class:ignore_covered_err */ 82 0 82
while (i < l && s[i] >= '0' && s[i] <= '9') { n = n * 10 + (s[i] - '0'); i++; } /* # uncoverable condition right count:2 class:ignore_covered_err */ 44 38 82
323 39 ▶ if (i < l) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < l) { 38 1 39
324 38 switch (s[i]) { /* # uncoverable branch true count:6 */
325 2 case 's': break;
326 1 case 'm': n *= 60; break;
327 13 case 'h': n *= 3600; break;
328 21 case 'd': n *= 86400; break;
329 1 case 'w': n *= 604800; break;
330 0 default: break; /* # uncoverable statement */
331 }
332 }
333 39 return n;
334 }
335  
336 /* The options `session` understands.
337 *
338 * It used to copy every pair it was given straight into the config, so a typo
339 * was silent: `htponly => 1` looked configured and was not. That was survivable
340 * while every option was a cookie attribute with a safe default. It stopped
341 * being survivable with `store`, where the typo's failure is "everything works
342 * and the store stays empty" - a cookie session, at the 4KB ceiling, wearing
343 * the configuration of a server-side one.
344 *
345 * `max_age` is here as well as `expires` because it is what the frozen config
346 * actually holds: reading one back and passing it in again should not croak. */
347 static const char *const PS_KEYS[] = {
348 "secret", "cookie", "expires", "max_age", "path", "domain", "secure",
349 "httponly", "samesite", "store", "allow_unshared", "sliding", "tier", NULL
350 };
351  
352 /* `cfg` is the half-built config to release first: this croak is catchable
353 * (a test, an eval around to_app), so leaking it on the way out would be a
354 * leak per attempt rather than one on a process that is about to die. */
355 255 static void ps_check_key(pTHX_ const char *k, STRLEN kl, HV *cfg) {
356 int i;
357 987 ▶ for (i = 0; PS_KEYS[i]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; PS_KEYS[i]; i++) 985 2 987
358 985 ▶ if (strlen(PS_KEYS[i]) == kl && memEQ(k, PS_KEYS[i], kl)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(PS_KEYS[i]) == kl && memEQ(k, PS_KEYS[i], kl)) return; 284 701 985
if (strlen(PS_KEYS[i]) == kl && memEQ(k, PS_KEYS[i], kl)) return; 253 31 284
359 /*
360 # uncoverable branch false
361 # uncoverable condition right count:1 class:ignore_covered_err */
362 2 ▶ if (cfg) SvREFCNT_dec((SV *)cfg);
Branch: 1 branch
BranchTrueFalseTotal
if (cfg) SvREFCNT_dec((SV *)cfg); 2 0 2
363 2 croak("Punk: `session` does not understand `%.*s`. It takes secret, "
364 "cookie, expires, path, domain, secure, httponly, samesite, store, "
365 "sliding, tier and allow_unshared", (int)kl, k);
366 }
367  
368 /* ---- the server-side store, when there is one ------------------------------ *
369 *
370 * `session store => ...` moves the payload off the client: the cookie carries a
371 * signed id and the session itself lives in a Punk::Cache. The seam is narrow
372 * on purpose - three method calls, get / set / delete - so any backend that
373 * satisfies that contract can hold a session, including one from outside this
374 * distribution.
375 *
376 * The id helpers are declared and not defined here. They need punk_entropy.h,
377 * which needs punk_csrf.h, which needs this file, so they live in
378 * punk_sessionstore.h and are reached from here by declaration. The compiler
379 * still checks the signatures; the ordering is the only thing being worked
380 * around. */
381 static SV *ps_new_id(pTHX);
382 static SV *ps_id_seal(pTHX_ SV *id, const char *key, STRLEN kl, IV ttl);
383 static SV *ps_id_unseal(pTHX_ const char *cv, STRLEN cvl,
384 const char *key, STRLEN kl);
385 /* the one read that has to know about the memory tier - see punk_sessionstore.h
386 * for why a session goes round it unless asked not to */
387 static SV *ps_store_get(pTHX_ SV *store, SV *key, int use_tier);
388  
389 /* The id this request arrived with, kept beside the session it named so the
390 * write-back knows whether the client already has it. */
391 #define PK_SESSION_SID "punk.session.id"
392 #define PK_SESSION_SID_LEN 15
393  
394 /* Set by session_rotate: the id to retire once the new one is written. */
395 #define PK_SESSION_ROT "punk.session.rotate"
396 #define PK_SESSION_ROT_LEN 19
397  
398 /* When the entry was last written, stamped inside the stored payload under a
399 * reserved key the application never sees - the same trick the cookie plays
400 * with punk.exp, and flash with punk.flash.
401 *
402 * It exists for the sliding expiry, which has to answer "how much of this
403 * session's life is left" and cannot ask the store: `get` returns bytes, and
404 * a contract that also reported an entry's expiry would be a sixth thing for
405 * every backend to implement. One integer in the payload answers it for all
406 * of them. */
407 #define PK_SESSION_AT "punk.at"
408 #define PK_SESSION_AT_LEN 7
409 #define PK_SESSION_AT_STASH "punk.session.at"
410 #define PK_SESSION_AT_STASH_LEN 15
411  
412 /* What a stored session may weigh.
413 *
414 * The cookie's ~4KB ceiling was the wire format's, and it goes: a basket in a
415 * session stops being a croak. Unbounded is not what replaces it, because a
416 * session an application can grow without limit is a way to fill somebody's
417 * store from a login form. A megabyte is far past any legitimate session and
418 * far below anything that threatens a store. */
419 #define PK_SESSION_STORE_MAX (1024 * 1024)
420  
421 /* the store the resolution put on the config at to_app, or NULL for a cookie
422 * session - which is every application that did not ask for one */
423 653 static SV *ps_cfg_store(pTHX_ HV *cfg) {
424 653 SV **s = hv_fetchs(cfg, "punk.store", 0);
425 /*
426 # uncoverable condition right count:2 class:ignore_covered_err
427 # uncoverable condition right count:3 class:ignore_covered_err */
428 653 ▶ return (s && *s && SvROK(*s)) ? *s : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (s && *s && SvROK(*s)) ? *s : NULL; 192 461 653
return (s && *s && SvROK(*s)) ? *s : NULL; 192 0 192
return (s && *s && SvROK(*s)) ? *s : NULL; 192 0 192
429 }
430  
431 /* The id this request arrived with (+1), unsealed from the cookie, or NULL.
432 *
433 * Separate from ps_load because a logout may never have read the session:
434 * `post '/logout' => sub { $_[0]->session_expire }` touches nothing, and the
435 * entry still has to be deleted. Revocation that depends on the handler
436 * having looked at the session first would be revocation that usually does
437 * not happen. */
438 /* The named request cookie's value (+1), or NULL: what Punk::Request::cookie
439 * answers (first value wins, decoded), read without building the request
440 * object and the two method frames. The request's own parsed table is used
441 * when it has one, and filled when the request exists but has not parsed
442 * yet, so a later $c->req->cookies finds the work done. */
443 403 static SV *ps_cookie_value(pTHX_ AV *av, const char *name, STRLEN nl) {
444 403 SV *req = pcx_get(aTHX_ av, PCX_REQ);
445 53 ▶ AV *reqav = (req && SvROK(req) && SvTYPE(SvRV(req)) == SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
AV *reqav = (req && SvROK(req) && SvTYPE(SvRV(req)) == SVt_PVAV) 53 0 53
AV *reqav = (req && SvROK(req) && SvTYPE(SvRV(req)) == SVt_PVAV) 53 0 53
446 456 ▶ ? (AV *)SvRV(req) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(req) : NULL; 53 350 403
447 403 HV *table = NULL;
448 SV **v;
449 403 ▶ if (reqav) {
Branch: 1 branch
BranchTrueFalseTotal
if (reqav) { 53 350 403
450 53 SV **ck = av_fetch(reqav, PQ_COOKIES, 0);
451 /*
452 # uncoverable branch true
453 # uncoverable condition left count:1 class:ignore_covered_err
454 # uncoverable condition left count:2 class:ignore_covered_err
455 # uncoverable condition right count:2 class:ignore_covered_err
456 # uncoverable condition left count:3 class:ignore_covered_err
457 # uncoverable condition right count:3 class:ignore_covered_err
458 # uncoverable condition left count:4 class:ignore_covered_err
459 # uncoverable condition right count:4 class:ignore_covered_err */
460 53 ▶ if (ck && *ck && SvROK(*ck) && SvTYPE(SvRV(*ck)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (ck && *ck && SvROK(*ck) && SvTYPE(SvRV(*ck)) == SVt_PVHV) 0 53 53
if (ck && *ck && SvROK(*ck) && SvTYPE(SvRV(*ck)) == SVt_PVHV) 0 0 0
if (ck && *ck && SvROK(*ck) && SvTYPE(SvRV(*ck)) == SVt_PVHV) 0 0 0
if (ck && *ck && SvROK(*ck) && SvTYPE(SvRV(*ck)) == SVt_PVHV) 0 0 0
461 0 table = (HV *)SvRV(*ck); /* # uncoverable statement */
462 }
463 /*
464 # uncoverable branch false
465 # uncoverable condition right count:1 class:ignore_covered_err */
466 403 ▶ if (!table) {
Branch: 1 branch
BranchTrueFalseTotal
if (!table) { 403 0 403
467 403 SV *envsv = pcx_get(aTHX_ av, PCX_ENV);
468 403 ▶ HV *env = (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
HV *env = (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) 403 0 403
HV *env = (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) 403 0 403
469 806 ▶ ? (HV *)SvRV(envsv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(envsv) : NULL; 403 0 403
470 403 ▶ SV **h = env ? hv_fetchs(env, "HTTP_COOKIE", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **h = env ? hv_fetchs(env, "HTTP_COOKIE", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 403 0 403
471 403 STRLEN hl = 0;
472 /*
473 # uncoverable condition right count:2 class:ignore_covered_err
474 # uncoverable condition right count:3 class:ignore_covered_err */
475 645 ▶ const char *hs = (h && *h && SvOK(*h)) ? SvPV_const(*h, hl) : "";
Branch: 3 branches
BranchTrueFalseTotal
const char *hs = (h && *h && SvOK(*h)) ? SvPV_const(*h, hl) : ""; 242 161 403
const char *hs = (h && *h && SvOK(*h)) ? SvPV_const(*h, hl) : ""; 242 0 242
const char *hs = (h && *h && SvOK(*h)) ? SvPV_const(*h, hl) : ""; 242 0 242
476 403 ▶ if (!hl) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!hl) return NULL; 161 242 403
477 242 table = pq_parse_cookies(aTHX_ hs, hl);
478 242 ▶ if (reqav) (void)pq_cached(aTHX_ reqav, PQ_COOKIES, table);
Branch: 1 branch
BranchTrueFalseTotal
if (reqav) (void)pq_cached(aTHX_ reqav, PQ_COOKIES, table); 23 219 242
479 219 else sv_2mortal((SV *)table);
480 }
481 242 v = hv_fetch(table, name, (I32)nl, 0);
482 /*
483 # uncoverable condition right count:1 class:ignore_covered_err
484 # uncoverable condition right count:2 class:ignore_covered_err */
485 242 ▶ return (v && *v) ? newSVsv(*v) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (v && *v) ? newSVsv(*v) : NULL; 242 0 242
return (v && *v) ? newSVsv(*v) : NULL; 242 0 242
486 }
487  
488 103 static SV *ps_req_id(pTHX_ SV *c, HV *cfg) {
489 103 AV *av = pcx_av(aTHX_ c);
490 STRLEN nl, kl, cvl;
491 103 const char *cookie_name = ps_cfg_str(aTHX_ cfg, "cookie", "punk.sid", &nl);
492 103 const char *key = ps_cfg_str(aTHX_ cfg, "secret", "", &kl);
493 103 SV *cval = ps_cookie_value(aTHX_ av, cookie_name, nl);
494 103 ▶ if (!cval) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!cval) return NULL; 45 58 103
495 58 sv_2mortal(cval);
496 /*
497 # uncoverable branch true
498 # uncoverable condition left count:1 class:ignore_covered_err
499 # uncoverable condition left count:2 class:ignore_covered_err */
500 58 ▶ if (!(SvOK(cval) && SvCUR(cval))) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvOK(cval) && SvCUR(cval))) return NULL; 58 0 58
if (!(SvOK(cval) && SvCUR(cval))) return NULL; 0 58 58
501 /* The SvPV goes in its OWN statement, and the length is read after it.
502 *
503 * `ps_id_unseal(aTHX_ SvPV_const(cval, cvl), cvl, ...)` is the same bug as
504 * a multi-eval Sv macro around POPs: the macro assigns cvl and the sibling
505 * argument reads it, and C does not say which happens first. clang went
506 * left to right and worked; gcc goes right to left, read cvl before it was
507 * ever assigned, and handed a garbage length to the verifier - which
508 * walked off the buffer. It crashed on every request carrying a cookie,
509 * on every non-threaded smoker, and on nothing here. */
510 {
511 58 const char *cv = SvPV_const(cval, cvl);
512 58 return ps_id_unseal(aTHX_ cv, cvl, key, kl);
513 }
514 }
515  
516 /* Give the session a new name at the write-back: the id to retire goes
517 * under PK_SESSION_ROT and the current one is forgotten. session_rotate and
518 * login both land here.
519 *
520 * When the stash holds no id the cookie is asked. A logout followed by a
521 * login never loaded the session, and a second rotation in one request has
522 * already forgotten the id: either way the entry the request named still has
523 * to go. */
524 43 static void ps_mark_rotate(pTHX_ SV *c, HV *cfg, HV *stash) {
525 SV **idp;
526 43 SV *old = NULL;
527 43 idp = hv_fetch(stash, PK_SESSION_SID, PK_SESSION_SID_LEN, 0);
528 /*
529 # uncoverable condition right count:2 class:ignore_covered_err
530 # uncoverable condition right count:3 class:ignore_covered_err */
531 43 ▶ if (idp && *idp && SvOK(*idp)) old = newSVsv(*idp);
Branch: 3 branches
BranchTrueFalseTotal
if (idp && *idp && SvOK(*idp)) old = newSVsv(*idp); 6 37 43
if (idp && *idp && SvOK(*idp)) old = newSVsv(*idp); 6 0 6
if (idp && *idp && SvOK(*idp)) old = newSVsv(*idp); 6 0 6
532 37 ▶ else if (cfg && ps_cfg_store(aTHX_ cfg)) old = ps_req_id(aTHX_ c, cfg); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (cfg && ps_cfg_store(aTHX_ cfg)) old = ps_req_id(aTHX_ c, cfg); /* # uncoverable condition right count:1 class:ignore_covered_err */ 37 0 37
else if (cfg && ps_cfg_store(aTHX_ cfg)) old = ps_req_id(aTHX_ c, cfg); /* # uncoverable condition right count:1 class:ignore_covered_err */ 9 28 37
533 43 ▶ (void)hv_store(stash, PK_SESSION_ROT, PK_SESSION_ROT_LEN,
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store(stash, PK_SESSION_ROT, PK_SESSION_ROT_LEN, 34 9 43
534 old ? old : newSV(0), 0);
535 43 (void)hv_delete(stash, PK_SESSION_SID, PK_SESSION_SID_LEN, G_DISCARD);
536 43 }
537  
538 /* Secure when the options do not say: on for a request that arrived over
539 * https, so a session cookie set there is never sent in the clear. */
540 335 static int ps_secure_default(pTHX_ SV *c) {
541 335 SV *env = pcx_get(aTHX_ pcx_av(aTHX_ c), PCX_ENV);
542 SV **e;
543 STRLEN l;
544 const char *v;
545 /*
546 # uncoverable branch true
547 # uncoverable condition left count:1 class:ignore_covered_err
548 # uncoverable condition left count:2 class:ignore_covered_err
549 # uncoverable condition left count:3 class:ignore_covered_err */
550 335 ▶ if (!(env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!(env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV)) return 0; 335 0 335
if (!(env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV)) return 0; 335 0 335
if (!(env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV)) return 0; 0 335 335
551 335 e = hv_fetchs((HV *)SvRV(env), "psgi.url_scheme", 0);
552 /*
553 # uncoverable condition left count:2 class:ignore_covered_err
554 # uncoverable condition left count:3 class:ignore_covered_err */
555 335 ▶ if (!(e && *e && SvOK(*e))) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) return 0; 203 132 335
if (!(e && *e && SvOK(*e))) return 0; 203 0 203
if (!(e && *e && SvOK(*e))) return 0; 0 203 203
556 203 v = SvPV_const(*e, l);
557 203 ▶ return l == 5 && memEQ(v, "https", 5); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
return l == 5 && memEQ(v, "https", 5); /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 198 203
return l == 5 && memEQ(v, "https", 5); /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 0 5
558 }
559  
560 /* $c->session: load (once) from the signed cookie and cache the hashref (+1) */
561 416 static SV *ps_load(pTHX_ SV *c) {
562 416 AV *av = pcx_av(aTHX_ c);
563 416 HV *stash = ps_stash(aTHX_ av);
564 416 SV **cached = hv_fetchs(stash, "punk.session", 0);
565 416 HV *cfg, *sess = NULL;
566 STRLEN nl, kl;
567 const char *cookie_name, *key;
568 SV *cval, *rv, *store;
569 /*
570 # uncoverable condition right count:2 class:ignore_covered_err
571 # uncoverable condition right count:3 class:ignore_covered_err */
572 416 ▶ if (cached && *cached && SvROK(*cached)) return newSVsv(*cached);
Branch: 3 branches
BranchTrueFalseTotal
if (cached && *cached && SvROK(*cached)) return newSVsv(*cached); 115 301 416
if (cached && *cached && SvROK(*cached)) return newSVsv(*cached); 115 0 115
if (cached && *cached && SvROK(*cached)) return newSVsv(*cached); 115 0 115
573 301 cfg = ps_cfg(aTHX_ c);
574 301 ▶ if (!cfg) croak("Punk: no session configured (add a `session` keyword)");
Branch: 1 branch
BranchTrueFalseTotal
if (!cfg) croak("Punk: no session configured (add a `session` keyword)"); 1 300 301
575 300 cookie_name = ps_cfg_str(aTHX_ cfg, "cookie", "punk.sid", &nl);
576 300 key = ps_cfg_str(aTHX_ cfg, "secret", "", &kl);
577 300 cval = ps_cookie_value(aTHX_ av, cookie_name, nl);
578 300 ▶ if (cval) sv_2mortal(cval);
Branch: 1 branch
BranchTrueFalseTotal
if (cval) sv_2mortal(cval); 184 116 300
579 300 store = ps_cfg_store(aTHX_ cfg);
580 300 ▶ if (store) {
Branch: 1 branch
BranchTrueFalseTotal
if (store) { 88 212 300
581 /* THE STORE ROUND TRIP HAPPENS HERE, and only here - which is why
582 * $c->session is loaded on demand rather than per request. A route
583 * that never asks for the session never pays for it: a static asset,
584 * a health check, an API route on a bearer token. The consequence to
585 * remember is the other way round - a request that never touches the
586 * session does not extend it either. */
587 /* A forged, tampered or expired cookie yields no id, and no id means
588 * no lookup: a guess never reaches the backend. */
589 88 SV *id = ps_req_id(aTHX_ c, cfg);
590 88 ▶ if (id) {
Branch: 1 branch
BranchTrueFalseTotal
if (id) { 47 41 88
591 SV *raw;
592 47 sv_2mortal(id);
593 47 raw = ps_store_get(aTHX_ store, id,
594 47 ps_cfg_iv(aTHX_ cfg, "tier", 0) ? 1 : 0);
595 47 ▶ if (raw) sv_2mortal(raw);
Branch: 1 branch
BranchTrueFalseTotal
if (raw) sv_2mortal(raw); 38 9 47
596 47 ▶ if (raw && SvOK(raw)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (raw && SvOK(raw)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 38 9 47
if (raw && SvOK(raw)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 38 0 38
597 STRLEN rl;
598 38 const char *rb = SvPV_const(raw, rl);
599 38 SV *decoded = punk_frj(aTHX)->decode(aTHX_ rb, rl, NULL);
600 /*
601 # uncoverable branch false
602 # uncoverable condition right count:1 class:ignore_covered_err */
603 38 ▶ if (decoded) sv_2mortal(decoded);
Branch: 1 branch
BranchTrueFalseTotal
if (decoded) sv_2mortal(decoded); 38 0 38
604 /*
605 # uncoverable branch false
606 # uncoverable condition right count:1 class:ignore_covered_err
607 # uncoverable condition right count:2 class:ignore_covered_err
608 # uncoverable condition right count:3 class:ignore_covered_err */
609 38 ▶ if (decoded && SvROK(decoded)
Branch: 2 branches
BranchTrueFalseTotal
if (decoded && SvROK(decoded) 38 0 38
if (decoded && SvROK(decoded) 38 0 38
610 38 ▶ && SvTYPE(SvRV(decoded)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(decoded)) == SVt_PVHV) { 38 0 38
611 SV **atp;
612 SV *rvs;
613 38 sess = newHVhv((HV *)SvRV(decoded));
614 /* the write stamp is ours, not the application's: it is
615 * lifted into the stash and stripped before anything sees
616 * the hash, so the dirty check compares like with like */
617 38 atp = hv_fetch(sess, PK_SESSION_AT, PK_SESSION_AT_LEN, 0);
618 /*
619 # uncoverable branch false
620 # uncoverable condition right count:1 class:ignore_covered_err
621 # uncoverable condition right count:2 class:ignore_covered_err
622 # uncoverable condition right count:3 class:ignore_covered_err */
623 38 ▶ if (atp && *atp && SvOK(*atp))
Branch: 3 branches
BranchTrueFalseTotal
if (atp && *atp && SvOK(*atp)) 38 0 38
if (atp && *atp && SvOK(*atp)) 38 0 38
if (atp && *atp && SvOK(*atp)) 38 0 38
624 38 (void)hv_store(stash, PK_SESSION_AT_STASH,
625 PK_SESSION_AT_STASH_LEN,
626 newSVsv(*atp), 0);
627 38 (void)hv_delete(sess, PK_SESSION_AT, PK_SESSION_AT_LEN,
628 G_DISCARD);
629 38 rvs = sv_2mortal(newRV_inc((SV *)sess));
630 38 (void)hv_stores(stash, "punk.session.orig",
631 ps_encode(aTHX_ rvs));
632 38 (void)hv_store(stash, PK_SESSION_SID, PK_SESSION_SID_LEN,
633 newSVsv(id), 0);
634 }
635 }
636 /* An id whose entry has gone - expired, evicted, or deleted by a
637 * logout - is NOT carried forward. The session starts empty and
638 * the write-back mints a fresh id, so a revoked id can never come
639 * back to life as the name of a new session. */
640 }
641 }
642 /*
643 # uncoverable condition right count:2 class:ignore_covered_err
644 # uncoverable condition right count:3 class:ignore_covered_err */
645 212 ▶ else if (cval && SvOK(cval) && SvCUR(cval)) {
Branch: 3 branches
BranchTrueFalseTotal
else if (cval && SvOK(cval) && SvCUR(cval)) { 135 77 212
else if (cval && SvOK(cval) && SvCUR(cval)) { 135 0 135
else if (cval && SvOK(cval) && SvCUR(cval)) { 135 0 135
646 135 STRLEN cvl; const char *cv = SvPV_const(cval, cvl);
647 135 SV *payload = pk_session_verify(aTHX_ cv, cvl, key, kl);
648 135 ▶ if (payload) {
Branch: 1 branch
BranchTrueFalseTotal
if (payload) { 134 1 135
649 SV *decoded;
650 134 sv_2mortal(payload);
651 134 decoded = punk_frj(aTHX)->decode(aTHX_ SvPVX(payload), SvCUR(payload), NULL);
652 /*
653 # uncoverable branch false
654 # uncoverable condition right count:1 class:ignore_covered_err */
655 134 ▶ if (decoded) sv_2mortal(decoded);
Branch: 1 branch
BranchTrueFalseTotal
if (decoded) sv_2mortal(decoded); 134 0 134
656 /*
657 # uncoverable branch false
658 # uncoverable condition right count:1 class:ignore_covered_err
659 # uncoverable condition right count:2 class:ignore_covered_err
660 # uncoverable condition right count:3 class:ignore_covered_err */
661 134 ▶ if (decoded && SvROK(decoded) && SvTYPE(SvRV(decoded)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (decoded && SvROK(decoded) && SvTYPE(SvRV(decoded)) == SVt_PVHV) { 134 0 134
if (decoded && SvROK(decoded) && SvTYPE(SvRV(decoded)) == SVt_PVHV) { 134 0 134
if (decoded && SvROK(decoded) && SvTYPE(SvRV(decoded)) == SVt_PVHV) { 134 0 134
662 134 HV *dh = (HV *)SvRV(decoded);
663 134 SV **ep = hv_fetch(dh, PK_SESSION_EXP, PK_SESSION_EXP_LEN, 0);
664 /* A payload past its stamped expiry is not a session. It is
665 * left to fall through to the empty one below, so an expired
666 * cookie logs the user out rather than half-loading. */
667 /*
668 # uncoverable condition left count:2 class:ignore_covered_err
669 # uncoverable condition left count:3 class:ignore_covered_err */
670 134 ▶ if (!ep || !*ep || !SvOK(*ep) || SvIV(*ep) > (IV)time(NULL)) {
Branch: 4 branches
BranchTrueFalseTotal
if (!ep || !*ep || !SvOK(*ep) || SvIV(*ep) > (IV)time(NULL)) { 132 2 134
if (!ep || !*ep || !SvOK(*ep) || SvIV(*ep) > (IV)time(NULL)) { 132 0 132
if (!ep || !*ep || !SvOK(*ep) || SvIV(*ep) > (IV)time(NULL)) { 132 0 132
if (!ep || !*ep || !SvOK(*ep) || SvIV(*ep) > (IV)time(NULL)) { 130 2 132
671 SV *rvs;
672 132 sess = newHVhv(dh); /* a fresh copy */
673 /* the expiry is ours, not the application's: strip it
674 * before anything sees the hash, and take the dirty-check
675 * baseline from the stripped copy so it compares like
676 * with like against what the write-back encodes */
677 132 (void)hv_delete(sess, PK_SESSION_EXP, PK_SESSION_EXP_LEN,
678 G_DISCARD);
679 132 rvs = sv_2mortal(newRV_inc((SV *)sess));
680 132 (void)hv_stores(stash, "punk.session.orig",
681 ps_encode(aTHX_ rvs));
682 }
683 }
684 }
685 }
686 300 ▶ if (!sess) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sess) { 130 170 300
687 130 sess = newHV();
688 130 (void)hv_stores(stash, "punk.session.orig", newSVpvs(""));
689 }
690 300 rv = newRV_noinc((SV *)sess);
691 300 (void)hv_stores(stash, "punk.session", rv); /* the stash owns rv */
692 300 return newRV_inc((SV *)sess); /* a second ref, +1 */
693 }
694  
695 /* The store half of the write-back: put the session where it lives, and hand
696 * back a Set-Cookie (+1) only when the client has something new to be told -
697 * which is on creation, and otherwise never.
698 *
699 * That asymmetry is the point. On a cookie session every write is a
700 * Set-Cookie, because the cookie IS the session. Here an ordinary write
701 * touches the store and leaves the response headers alone: the id in the
702 * client's cookie still names the same session, whatever the session now
703 * contains. */
704 88 static SV *ps_store_writeback(pTHX_ HV *cfg, HV *stash, SV *store,
705 SV *sess, SV *cname, int secure) {
706 88 SV **origp = hv_fetchs(stash, "punk.session.orig", 0);
707 88 SV **idp = hv_fetch(stash, PK_SESSION_SID, PK_SESSION_SID_LEN, 0);
708 88 SV **rotp = hv_fetch(stash, PK_SESSION_ROT, PK_SESSION_ROT_LEN, 0);
709 88 SV *cur = sv_2mortal(ps_encode(aTHX_ sess));
710 88 IV ttl = ps_cfg_iv(aTHX_ cfg, "max_age", 0);
711 88 IV now = (IV)time(NULL);
712 STRLEN kl;
713 const char *key;
714 SV *id, *argv[3], *ok;
715 88 ▶ int fresh, rotating = (rotp && *rotp) ? 1 : 0, sliding = 0; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
int fresh, rotating = (rotp && *rotp) ? 1 : 0, sliding = 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 12 76 88
int fresh, rotating = (rotp && *rotp) ? 1 : 0, sliding = 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 12 0 12
716  
717 88 ▶ if (ttl <= 0) ttl = PK_SESSION_MAX_LIFETIME;
Branch: 1 branch
BranchTrueFalseTotal
if (ttl <= 0) ttl = PK_SESSION_MAX_LIFETIME; 54 34 88
718  
719 /* A session nobody named and nobody wrote to is not a session. Loading
720 * one is enough to reach here - a guard or a csrf check on a cookieless
721 * request does it - and writing it would hand every anonymous denial a
722 * store entry with the full lifetime, which is a way to fill the store,
723 * or to evict the real sessions from one with a budget. */
724 /*
725 # uncoverable condition left count:3 class:ignore_covered_err
726 # uncoverable condition left count:4 class:ignore_covered_err */
727 88 ▶ if (!rotating && !(idp && *idp && SvOK(*idp))
Branch: 4 branches
BranchTrueFalseTotal
if (!rotating && !(idp && *idp && SvOK(*idp)) 76 12 88
if (!rotating && !(idp && *idp && SvOK(*idp)) 32 44 76
if (!rotating && !(idp && *idp && SvOK(*idp)) 32 0 32
if (!rotating && !(idp && *idp && SvOK(*idp)) 0 32 32
728 44 ▶ && HvUSEDKEYS((HV *)SvRV(sess)) == 0)
Branch: 2 branches
BranchTrueFalseTotal
&& HvUSEDKEYS((HV *)SvRV(sess)) == 0) 0 44 44
&& HvUSEDKEYS((HV *)SvRV(sess)) == 0) 21 23 44
729 21 return NULL;
730  
731 /* Unchanged is the common case, and change detection stops being an
732 * optimisation here: it is what keeps a read-mostly application off the
733 * store's write path entirely.
734 *
735 * Two things override it. A rotation has to write even when the session
736 * is identical, because the point is that it now lives under a different
737 * name. And a sliding expiry has to write to move the expiry - but only
738 * once the session is far enough through its life to need it, or sliding
739 * would be a store write on every request, which is the cost the read
740 * path was designed to avoid. */
741 67 ▶ if (origp && *origp && sv_eq(cur, *origp) && !rotating) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 4 branches
BranchTrueFalseTotal
if (origp && *origp && sv_eq(cur, *origp) && !rotating) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 66 1 67
if (origp && *origp && sv_eq(cur, *origp) && !rotating) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 66 0 66
if (origp && *origp && sv_eq(cur, *origp) && !rotating) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 30 36 66
if (origp && *origp && sv_eq(cur, *origp) && !rotating) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 28 2 30
742 SV **atp;
743 28 ▶ if (!ps_cfg_iv(aTHX_ cfg, "sliding", 0)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!ps_cfg_iv(aTHX_ cfg, "sliding", 0)) return NULL; 26 2 28
744 2 atp = hv_fetch(stash, PK_SESSION_AT_STASH, PK_SESSION_AT_STASH_LEN, 0);
745 /*
746 # uncoverable branch true
747 # uncoverable condition left count:1 class:ignore_covered_err
748 # uncoverable condition left count:2 class:ignore_covered_err
749 # uncoverable condition left count:3 class:ignore_covered_err */
750 2 ▶ if (!(atp && *atp && SvOK(*atp))) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(atp && *atp && SvOK(*atp))) return NULL; 2 0 2
if (!(atp && *atp && SvOK(*atp))) return NULL; 2 0 2
if (!(atp && *atp && SvOK(*atp))) return NULL; 0 2 2
751 2 ▶ if (now - SvIV(*atp) <= ttl / 2) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (now - SvIV(*atp) <= ttl / 2) return NULL; 1 1 2
752 1 sliding = 1;
753 }
754  
755 40 key = ps_cfg_str(aTHX_ cfg, "secret", "", &kl);
756 /*
757 # uncoverable branch true
758 # uncoverable condition left count:1 class:ignore_covered_err */
759 40 ▶ if (!kl) {
Branch: 1 branch
BranchTrueFalseTotal
if (!kl) { 0 40 40
760 /* checked before the store is touched, not after: a session written
761 * under an id that can never be sealed into a cookie is an orphan */
762 0 warn("Punk: session has no secret; not saved - the id would ride in " /* # uncoverable statement */
763 "a cookie signed with an empty key, which anyone can forge");
764 0 return NULL; /* # uncoverable statement */
765 }
766  
767 40 ▶ if (SvCUR(cur) > PK_SESSION_STORE_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(cur) > PK_SESSION_STORE_MAX) { 1 39 40
768 /* the write-back is a trapped after-hook, so croaking would vanish */
769 1 warn("Punk: session is %d bytes, over the %d-byte limit; not saved - "
770 "a session is a name for what a user is doing, not a place to "
771 "keep what they are doing it to",
772 (int)SvCUR(cur), (int)PK_SESSION_STORE_MAX);
773 1 return NULL;
774 }
775  
776 /* A rotation always gets a new name; otherwise the one the request
777 * arrived with, if it had one.
778 *
779 * A COPY of that one, not the stash's SV. Storing the id back into the
780 * stash below frees whatever was under the key, so holding the stash's
781 * own SV here left `id` dangling and the seal below reading freed memory.
782 * It surfaced as a missing Set-Cookie rather than a crash, because this
783 * runs on a trapped after-hook: the croak vanished with the response
784 * going out perfectly formed and one header short. */
785 /*
786 # uncoverable condition left count:3 class:ignore_covered_err
787 # uncoverable condition left count:4 class:ignore_covered_err */
788 39 ▶ fresh = rotating || !(idp && *idp && SvOK(*idp));
Branch: 4 branches
BranchTrueFalseTotal
fresh = rotating || !(idp && *idp && SvOK(*idp)); 27 12 39
fresh = rotating || !(idp && *idp && SvOK(*idp)); 5 22 27
fresh = rotating || !(idp && *idp && SvOK(*idp)); 5 0 5
fresh = rotating || !(idp && *idp && SvOK(*idp)); 0 5 5
789 39 ▶ id = sv_2mortal(fresh ? ps_new_id(aTHX) : newSVsv(*idp));
Branch: 1 branch
BranchTrueFalseTotal
id = sv_2mortal(fresh ? ps_new_id(aTHX) : newSVsv(*idp)); 34 5 39
790  
791 /* What goes into the store is the session plus the write stamp - never
792 * the hash the application holds, which keeps its own contents and keeps
793 * the dirty check comparing stripped serializations. */
794 {
795 39 HV *payload = newHVhv((HV *)SvRV(sess));
796 SV *prv;
797 39 (void)hv_store(payload, PK_SESSION_AT, PK_SESSION_AT_LEN,
798 newSViv(now), 0);
799 39 prv = sv_2mortal(newRV_noinc((SV *)payload));
800 39 argv[1] = sv_2mortal(ps_encode(aTHX_ prv));
801 }
802 39 argv[0] = id;
803 39 argv[2] = sv_2mortal(newSViv(ttl));
804 39 ok = pcx_call_meth(aTHX_ store, "set", argv, 3, 1);
805 /*
806 # uncoverable branch false
807 # uncoverable condition right count:1 class:ignore_covered_err */
808 39 ▶ if (ok) sv_2mortal(ok);
Branch: 1 branch
BranchTrueFalseTotal
if (ok) sv_2mortal(ok); 39 0 39
809 39 ▶ if (!(ok && SvTRUE(ok))) { /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(ok && SvTRUE(ok))) { /* # uncoverable condition left count:1 class:ignore_covered_err */ 39 0 39
if (!(ok && SvTRUE(ok))) { /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 38 39
810 /* A store may refuse: the file store refuses a value too large for its
811 * budget rather than evicting everything else to fit, and a backend
812 * from outside this distribution may refuse for reasons of its own.
813 * Say so. A refusal that returns quietly is a login that appeared to
814 * work and a user who is not logged in. */
815 1 warn("Punk: the session store refused the write; the session was NOT "
816 "saved - check the store's budget and its `refused` count");
817 1 return NULL;
818 }
819  
820 /* The old entry goes AFTER the new one is safely written, and the order
821 * matters in one direction only. A failed delete leaves an id naming the
822 * session as it was BEFORE this request - which in the attack this
823 * defends against is the attacker's own empty session, and which expires
824 * on its own TTL. A failed write after a delete would be a user logged
825 * out at the moment they logged in. */
826 38 ▶ if (rotating && SvOK(*rotp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (rotating && SvOK(*rotp)) { 12 26 38
if (rotating && SvOK(*rotp)) { 6 6 12
827 6 SV *old = *rotp;
828 6 SV *d = pcx_call_meth(aTHX_ store, "delete", &old, 1, 1);
829 /*
830 # uncoverable branch false
831 # uncoverable condition right count:1 class:ignore_covered_err */
832 6 ▶ if (d) SvREFCNT_dec(d);
Branch: 1 branch
BranchTrueFalseTotal
if (d) SvREFCNT_dec(d); 6 0 6
833 }
834 38 (void)hv_delete(stash, PK_SESSION_ROT, PK_SESSION_ROT_LEN, G_DISCARD);
835 38 (void)hv_store(stash, PK_SESSION_SID, PK_SESSION_SID_LEN, newSVsv(id), 0);
836  
837 /* Nothing to tell the client unless the id changed - or the expiry did,
838 * because a cookie that runs out while its entry lives is a logout the
839 * sliding expiry exists to prevent. */
840 38 ▶ if (!fresh && !sliding) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!fresh && !sliding) return NULL; 5 33 38
if (!fresh && !sliding) return NULL; 4 1 5
841 34 return pk_build_cookie_ex(aTHX_ cname,
842 sv_2mortal(ps_id_seal(aTHX_ id, key, kl, ttl)),
843 cfg, secure);
844 }
845  
846 /* the after-dispatch write-back: add a Set-Cookie to the triplet when the
847 * session changed, or a deletion cookie when it was expired */
848 430 static void ps_writeback(pTHX_ SV *c, SV *resp) {
849 430 AV *av = pcx_av(aTHX_ c);
850 430 HV *cfg = ps_cfg(aTHX_ c);
851 430 SV *st = pcx_get(aTHX_ av, PCX_STASH);
852 HV *stash;
853 SV **sessp, **origp, **expp, **hp;
854 AV *r, *headers;
855 STRLEN nl;
856 const char *cookie_name;
857 430 SV *cname, *store, *setcookie = NULL;
858 int secure;
859 /*
860 # uncoverable condition left count:1 class:ignore_covered_err
861 # uncoverable condition left count:3 class:ignore_covered_err */
862 603 ▶ if (!cfg || !st || !SvROK(st)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!cfg || !st || !SvROK(st)) return; 430 0 430
if (!cfg || !st || !SvROK(st)) return; 319 111 430
if (!cfg || !st || !SvROK(st)) return; 0 319 319
863 319 stash = (HV *)SvRV(st);
864 319 sessp = hv_fetchs(stash, "punk.session", 0);
865 /*
866 # uncoverable condition left count:2 class:ignore_covered_err
867 # uncoverable condition left count:3 class:ignore_covered_err */
868 319 ▶ if (!(sessp && *sessp && SvROK(*sessp))) return; /* never accessed */
Branch: 3 branches
BranchTrueFalseTotal
if (!(sessp && *sessp && SvROK(*sessp))) return; /* never accessed */ 312 7 319
if (!(sessp && *sessp && SvROK(*sessp))) return; /* never accessed */ 312 0 312
if (!(sessp && *sessp && SvROK(*sessp))) return; /* never accessed */ 0 312 312
869 /*
870 # uncoverable branch true
871 # uncoverable condition left count:1 class:ignore_covered_err
872 # uncoverable condition left count:2 class:ignore_covered_err */
873 312 ▶ if (!(SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) return; 312 0 312
if (!(SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV)) return; 0 312 312
874 312 r = (AV *)SvRV(resp);
875 312 hp = av_fetch(r, 1, 0);
876 /*
877 # uncoverable branch true
878 # uncoverable condition left count:1 class:ignore_covered_err
879 # uncoverable condition left count:2 class:ignore_covered_err
880 # uncoverable condition left count:3 class:ignore_covered_err
881 # uncoverable condition left count:4 class:ignore_covered_err */
882 312 ▶ 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; 312 0 312
if (!(hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV)) return; 312 0 312
if (!(hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV)) return; 312 0 312
if (!(hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV)) return; 0 312 312
883 312 headers = (AV *)SvRV(*hp);
884 312 cookie_name = ps_cfg_str(aTHX_ cfg, "cookie", "punk.sid", &nl);
885 312 cname = sv_2mortal(newSVpvn(cookie_name, nl));
886 312 expp = hv_fetchs(stash, "punk.session.expire", 0);
887 312 store = ps_cfg_store(aTHX_ cfg);
888 312 secure = ps_secure_default(aTHX_ c);
889 /*
890 # uncoverable condition right count:2 class:ignore_covered_err
891 # uncoverable condition right count:3 class:ignore_covered_err */
892 312 ▶ if (expp && *expp && SvTRUE(*expp)) {
Branch: 3 branches
BranchTrueFalseTotal
if (expp && *expp && SvTRUE(*expp)) { 11 301 312
if (expp && *expp && SvTRUE(*expp)) { 11 0 11
if (expp && *expp && SvTRUE(*expp)) { 11 0 11
893 /* Logging out of a stored session is a DELETE, and that is the whole
894 * difference. A signed cookie is good until it expires no matter what
895 * the server thinks, so session_expire could only ever ask the browser
896 * in front of it to forget - never reach a copy somebody had already
897 * taken. With an entry to remove, the stolen cookie is dead on its
898 * next request.
899 *
900 * The id comes from the cookie rather than the stash, because a
901 * logout route usually never touched $c->session at all. */
902 11 ▶ if (store) {
Branch: 1 branch
BranchTrueFalseTotal
if (store) { 6 5 11
903 6 SV *id = ps_req_id(aTHX_ c, cfg);
904 /*
905 # uncoverable branch false
906 # uncoverable condition right count:1 class:ignore_covered_err */
907 6 ▶ if (id) {
Branch: 1 branch
BranchTrueFalseTotal
if (id) { 6 0 6
908 SV *d;
909 6 sv_2mortal(id);
910 6 d = pcx_call_meth(aTHX_ store, "delete", &id, 1, 1);
911 /*
912 # uncoverable branch false
913 # uncoverable condition right count:1 class:ignore_covered_err */
914 6 ▶ if (d) SvREFCNT_dec(d);
Branch: 1 branch
BranchTrueFalseTotal
if (d) SvREFCNT_dec(d); 6 0 6
915 }
916 }
917 11 setcookie = pk_build_cookie_ex(aTHX_ cname, &PL_sv_undef, cfg, secure);
918 }
919 301 ▶ else if (store) {
Branch: 1 branch
BranchTrueFalseTotal
else if (store) { 88 213 301
920 88 setcookie = ps_store_writeback(aTHX_ cfg, stash, store, *sessp, cname,
921 secure);
922 88 ▶ if (!setcookie) return; /* stored, or refused, or unchanged */
Branch: 1 branch
BranchTrueFalseTotal
if (!setcookie) return; /* stored, or refused, or unchanged */ 54 34 88
923 }
924 else {
925 213 SV *cur = sv_2mortal(ps_encode(aTHX_ *sessp));
926 STRLEN kl; const char *key;
927 SV *signed_val;
928 213 origp = hv_fetchs(stash, "punk.session.orig", 0);
929 238 ▶ if (origp && *origp && sv_eq(cur, *origp)) return; /* unchanged */ /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (origp && *origp && sv_eq(cur, *origp)) return; /* unchanged */ /* # uncoverable condition right count:2 class:ignore_covered_err */ 212 1 213
if (origp && *origp && sv_eq(cur, *origp)) return; /* unchanged */ /* # uncoverable condition right count:2 class:ignore_covered_err */ 212 0 212
if (origp && *origp && sv_eq(cur, *origp)) return; /* unchanged */ /* # uncoverable condition right count:2 class:ignore_covered_err */ 87 125 212
930 /* nothing came in and nothing was put in: no cookie to send */
931 126 ▶ if (origp && *origp && !SvCUR(*origp) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (origp && *origp && !SvCUR(*origp) /* # uncoverable condition right count:2 class:ignore_covered_err */ 125 1 126
if (origp && *origp && !SvCUR(*origp) /* # uncoverable condition right count:2 class:ignore_covered_err */ 125 0 125
if (origp && *origp && !SvCUR(*origp) /* # uncoverable condition right count:2 class:ignore_covered_err */ 80 45 125
932 80 ▶ && HvUSEDKEYS((HV *)SvRV(*sessp)) == 0)
Branch: 2 branches
BranchTrueFalseTotal
&& HvUSEDKEYS((HV *)SvRV(*sessp)) == 0) 0 80 80
&& HvUSEDKEYS((HV *)SvRV(*sessp)) == 0) 24 56 80
933 24 return;
934 102 ▶ if (SvCUR(cur) > 4000) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(cur) > 4000) { 1 101 102
935 /* the write-back is a trapped after-hook, so croaking would vanish;
936 * warn and drop the cookie rather than silently truncate it */
937 1 warn("Punk: session is too large for a signed cookie (%d bytes); "
938 "not saved - a server-side store is needed", (int)SvCUR(cur));
939 1 return;
940 }
941 /* Sign an expiry-stamped copy, not the hash itself: the application
942 * keeps the session it was handed, and the dirty check above stays on
943 * the stripped serialization, so a request that changed nothing still
944 * writes no cookie. The stamp refreshes every time the session is
945 * written, which is what makes `expires` a lifetime rather than a
946 * countdown from first login. */
947 {
948 101 HV *payload = newHVhv((HV *)SvRV(*sessp));
949 101 IV ttl = ps_cfg_iv(aTHX_ cfg, "max_age", 0);
950 SV *prv;
951 101 ▶ if (ttl <= 0) ttl = PK_SESSION_MAX_LIFETIME;
Branch: 1 branch
BranchTrueFalseTotal
if (ttl <= 0) ttl = PK_SESSION_MAX_LIFETIME; 90 11 101
952 101 (void)hv_store(payload, PK_SESSION_EXP, PK_SESSION_EXP_LEN,
953 newSViv((IV)time(NULL) + ttl), 0);
954 101 prv = sv_2mortal(newRV_noinc((SV *)payload));
955 101 cur = sv_2mortal(ps_encode(aTHX_ prv));
956 }
957 101 key = ps_cfg_str(aTHX_ cfg, "secret", "", &kl);
958 /*
959 # uncoverable branch true
960 # uncoverable condition left count:1 class:ignore_covered_err */
961 101 ▶ if (!kl) {
Branch: 1 branch
BranchTrueFalseTotal
if (!kl) { 0 101 101
962 /* the `session` keyword refuses an empty secret at boot, so this
963 * is only reachable by writing the config by hand. Signing with a
964 * zero-length key is a forgeable cookie, so drop it here too -
965 * same shape as the oversize case, since croaking in a trapped
966 * after-hook would vanish */
967 0 warn("Punk: session has no secret; not saved - a cookie signed " /* # uncoverable statement */
968 "with an empty key can be forged by anyone");
969 0 return; /* # uncoverable statement */
970 }
971 101 signed_val = sv_2mortal(pk_session_sign(aTHX_ cur, key, kl));
972 101 setcookie = pk_build_cookie_ex(aTHX_ cname, signed_val, cfg, secure);
973 }
974 146 av_push(headers, newSVpvs("Set-Cookie"));
975 146 av_push(headers, setcookie); /* +1, list takes it */
976 }
977  
978 #endif /* PUNK_SESSION_H */
« xs/sse.xs ↑ Summary include/punk/punk_frozen.h »