include/punk/punk_sessionstore.h

filter: Statementstmt 99.1% 112 / 114 (1 uncoverable) Branchbran 78.8% 104 / 142 (10 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.8% 216 / 256 (11 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_log.h ↑ Summary include/punk/punk_dbq.h »
1 /* punk_sessionstore.h - where a server-side session lives.
2 *
3 * `session store => ...` moves the payload off the client: the cookie carries
4 * an id, the session itself sits in a store. This file is the seam - it turns
5 * whatever the keyword was given into a Punk::Cache handle at to_app, and
6 * refuses at boot the configurations that would fail as a random logout at
7 * three in the morning.
8 *
9 * The store contract is Punk::Cache's, deliberately, and there is no second
10 * one: a Redis or DBI backend implements get/set/delete/clear/stats because it
11 * already had to, so one module serves `cache`, Idempotency and this. What
12 * that reuse costs is that a cache is allowed to FORGET, and a forgotten
13 * session is somebody logged out - which is why the documentation says to give
14 * sessions their own store, sized to hold every live one.
15 *
16 * Must be included after punk_cachefront.h (pkc_can) and punk_session.h
17 * (ps_cfg_iv).
18 */
19  
20 #ifndef PUNK_SESSIONSTORE_H
21 #define PUNK_SESSIONSTORE_H
22  
23 /* The resolved handle, on the session config under a reserved key. Reserved
24 * rather than replacing `store`: resolving is then idempotent, and the spec
25 * stays visible for anything that wants to report what was asked for. */
26 #define PK_SESSION_STORE "punk.store"
27 #define PK_SESSION_STORE_LEN 10
28  
29 /* The name a privately built store is registered under - its own invalidation
30 * topic, separate from any cache the application declared. */
31 #define PK_SESSION_STORE_NAME "session"
32  
33 /* ---- the id ---------------------------------------------------------------- *
34 *
35 * With a cookie session, forging one means forging an HMAC. With a server-side
36 * session it means GUESSING AN ID, so the id is the whole authentication and
37 * everything here is downstream of that sentence.
38 *
39 * 128 bits from a CSPRNG, and nothing derived from anything the user supplies:
40 * an id that encodes the account is an id an attacker can construct.
41 *
42 * The bytes come from pk_ent_take (punk_entropy.h) rather than a fresh draw,
43 * for both of the reasons that helper exists. `getentropy` costs the same for
44 * 16 bytes as for 256, so drawing per id is 17x the cost for nothing; and a
45 * buffer filled before `fork` hands every worker the same bytes - measured
46 * while building RequestId at 767 duplicates in 8000 across four workers, and
47 * it looks perfectly random the whole time. A duplicate request id is a
48 * nuisance. A duplicate session id is one user handed another user's session,
49 * which is the worst bug this distribution could ship, so the pid guard is the
50 * load-bearing half and the reason not to hand-roll a draw here. */
51  
52 #define PK_SESSION_ID "punk.id"
53 #define PK_SESSION_ID_LEN 7
54 #define PK_SESSION_ID_BYTES 16 /* 128 bits */
55 #define PK_SESSION_ID_HEX (PK_SESSION_ID_BYTES * 2)
56  
57 102051 static SV *ps_new_id(pTHX) {
58 static const char hex[] = "0123456789abcdef";
59 unsigned char raw[PK_SESSION_ID_BYTES];
60 char out[PK_SESSION_ID_HEX];
61 int i;
62 102051 pk_ent_take(aTHX_ raw, sizeof raw);
63 1734867 ▶ for (i = 0; i < PK_SESSION_ID_BYTES; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < PK_SESSION_ID_BYTES; i++) { 1632816 102051 1734867
64 1632816 out[2 * i] = hex[raw[i] >> 4];
65 1632816 out[2 * i + 1] = hex[raw[i] & 0x0f];
66 }
67 102051 return newSVpvn(out, sizeof out); /* +1 */
68 }
69  
70 /* The shape, checked before an id is ever used as a store key.
71 *
72 * The signature has already rejected anything this server did not mint, so
73 * this is not what keeps a guess out - it is what keeps a session written by
74 * hand, or by a later bug, from reaching a backend as a key of some other
75 * shape. Thirty-two byte comparisons, once per request.
76 *
77 * Not constant time, deliberately, and worth writing down because the next
78 * reader will wonder: an id is never compared against a stored secret here.
79 * It is a key, and the comparison that decides anything happens inside the
80 * backend - the file store compares the key it read against the key it was
81 * asked for, so a hash collision is a miss rather than a wrong answer. That
82 * compare is not constant time either, and making every backend promise it
83 * would be a sixth thing on the contract. What protects the keyspace is the
84 * signature, which IS constant time (pk_ct_eq), and which an attacker has to
85 * beat before any of this is reached. */
86 67 static int ps_id_ok(const char *s, STRLEN len) {
87 STRLEN i;
88 67 ▶ if (len != PK_SESSION_ID_HEX) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len != PK_SESSION_ID_HEX) return 0; 1 66 67
89 2114 ▶ for (i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) 2050 64 2114
90 2050 ▶ if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f')))
Branch: 4 branches
BranchTrueFalseTotal
if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) 2049 1 2050
if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) 779 1270 2049
if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) 779 1 780
if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) 1 778 779
91 2 return 0;
92 64 return 1;
93 }
94  
95 /* ---- the id in the cookie -------------------------------------------------- *
96 *
97 * The cookie still carries a SIGNED payload, and the payload is still JSON
98 * with the expiry stamped inside it. Only its contents shrink: one reserved
99 * key holding the id, where the whole session used to be.
100 *
101 * Keeping the format buys three things at the cost of about sixty bytes on the
102 * wire. The signer, the verifier and the expiry check are the ones already in
103 * service, so there is no second crypto path to keep in step. The 0.19 expiry
104 * stamp keeps applying, so a cookie past its lifetime is refused rather than
105 * looked up. And only what goes INTO the payload changes.
106 *
107 * The signature is not protecting a secret any more - an id is not one - so it
108 * would be easy to argue away. It stays because an unsigned id turns the store
109 * into an ORACLE: anyone could send a guess and make the server do a lookup
110 * for it, which is a cheap denial of service against a network store, a way to
111 * litter a file store with lock files, and a timing channel that distinguishes
112 * a hit from a miss. One HMAC refuses every guess before it costs a round
113 * trip. */
114  
115 /* the cookie value for an id (+1): {"punk.id":..., "punk.exp":...}, signed */
116 39 static SV *ps_id_seal(pTHX_ SV *id, const char *key, STRLEN kl, IV ttl) {
117 39 HV *payload = newHV();
118 SV *prv, *json;
119 /*
120 # uncoverable branch true
121 # uncoverable condition left count:1 class:ignore_covered_err */
122 39 ▶ if (ttl <= 0) ttl = PK_SESSION_MAX_LIFETIME;
Branch: 1 branch
BranchTrueFalseTotal
if (ttl <= 0) ttl = PK_SESSION_MAX_LIFETIME; 0 39 39
123 39 (void)hv_store(payload, PK_SESSION_ID, PK_SESSION_ID_LEN, newSVsv(id), 0);
124 39 (void)hv_store(payload, PK_SESSION_EXP, PK_SESSION_EXP_LEN,
125 newSViv((IV)time(NULL) + ttl), 0);
126 39 prv = sv_2mortal(newRV_noinc((SV *)payload));
127 39 json = sv_2mortal(ps_encode(aTHX_ prv));
128 39 return pk_session_sign(aTHX_ json, key, kl);
129 }
130  
131 /* the id inside a cookie value (+1), or NULL for anything that is not one:
132 * a bad signature, a payload past its stamped expiry, or an id whose shape
133 * says it was not minted here. A caller does not need to know which - all
134 * three mean "no session", and telling them apart is a distinction useful
135 * only to somebody probing. */
136 70 static SV *ps_id_unseal(pTHX_ const char *cv, STRLEN cvl,
137 const char *key, STRLEN kl) {
138 70 SV *payload, *decoded, *id = NULL;
139 /*
140 # uncoverable condition left count:1 class:ignore_covered_err
141 # uncoverable condition left count:3 class:ignore_covered_err */
142 70 ▶ if (!(cv && cvl && kl)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(cv && cvl && kl)) return NULL; 70 0 70
if (!(cv && cvl && kl)) return NULL; 69 1 70
if (!(cv && cvl && kl)) return NULL; 0 69 69
143 69 payload = pk_session_verify(aTHX_ cv, cvl, key, kl);
144 69 ▶ if (!payload) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!payload) return NULL; 7 62 69
145 62 sv_2mortal(payload);
146 62 decoded = punk_frj(aTHX)->decode(aTHX_ SvPVX(payload), SvCUR(payload), NULL);
147 /*
148 # uncoverable branch true
149 # uncoverable condition left count:1 class:ignore_covered_err */
150 62 ▶ if (!decoded) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!decoded) return NULL; 0 62 62
151 62 sv_2mortal(decoded);
152 /*
153 # uncoverable branch false
154 # uncoverable condition right count:1 class:ignore_covered_err
155 # uncoverable condition right count:2 class:ignore_covered_err */
156 62 ▶ if (SvROK(decoded) && SvTYPE(SvRV(decoded)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(decoded) && SvTYPE(SvRV(decoded)) == SVt_PVHV) { 62 0 62
if (SvROK(decoded) && SvTYPE(SvRV(decoded)) == SVt_PVHV) { 62 0 62
157 62 HV *dh = (HV *)SvRV(decoded);
158 62 SV **ep = hv_fetch(dh, PK_SESSION_EXP, PK_SESSION_EXP_LEN, 0);
159 62 SV **ip = hv_fetch(dh, PK_SESSION_ID, PK_SESSION_ID_LEN, 0);
160 /*
161 # uncoverable condition right count:1 class:ignore_covered_err
162 # uncoverable condition right count:2 class:ignore_covered_err
163 # uncoverable condition right count:3 class:ignore_covered_err */
164 62 ▶ if (ep && *ep && SvOK(*ep) && SvIV(*ep) <= (IV)time(NULL)) return NULL;
Branch: 4 branches
BranchTrueFalseTotal
if (ep && *ep && SvOK(*ep) && SvIV(*ep) <= (IV)time(NULL)) return NULL; 62 0 62
if (ep && *ep && SvOK(*ep) && SvIV(*ep) <= (IV)time(NULL)) return NULL; 62 0 62
if (ep && *ep && SvOK(*ep) && SvIV(*ep) <= (IV)time(NULL)) return NULL; 62 0 62
if (ep && *ep && SvOK(*ep) && SvIV(*ep) <= (IV)time(NULL)) return NULL; 1 61 62
165 /*
166 # uncoverable condition right count:2 class:ignore_covered_err
167 # uncoverable condition right count:3 class:ignore_covered_err */
168 61 ▶ if (ip && *ip && SvOK(*ip)) {
Branch: 3 branches
BranchTrueFalseTotal
if (ip && *ip && SvOK(*ip)) { 60 1 61
if (ip && *ip && SvOK(*ip)) { 60 0 60
if (ip && *ip && SvOK(*ip)) { 60 0 60
169 STRLEN l;
170 60 const char *s = SvPV_const(*ip, l);
171 /*
172 # uncoverable branch false
173 # uncoverable condition right count:1 class:ignore_covered_err */
174 60 ▶ if (ps_id_ok(s, l)) id = newSVpvn(s, l);
Branch: 1 branch
BranchTrueFalseTotal
if (ps_id_ok(s, l)) id = newSVpvn(s, l); 60 0 60
175 }
176 }
177 61 return id; /* +1, or NULL */
178 }
179  
180 /* ---- the memory tier, and why a session goes round it ---------------------- *
181 *
182 * `Punk::Cache`'s `memory` option puts a per-worker copy in front of a shared
183 * store. For a cache that is the right trade. For a session it is a bound on
184 * HOW LONG A REVOKED SESSION KEEPS WORKING - a worker that missed the
185 * invalidation answers from its own copy for up to `memory_ttl`, and the copy
186 * it answers with is the session before the logout.
187 *
188 * Be accurate about the other direction while here, because the loose version
189 * of this argument is wrong: a tier MISS falls through to the backend, so a
190 * tier can never manufacture a missing session. The hazard is staleness in one
191 * direction only, and it is authentication staleness.
192 *
193 * So a session read goes straight to the backend by default, the way
194 * Punk::Plugin::Idempotency reaches past the tier for its own keys. An
195 * application's `memory => '64M'` stays honoured for everything else.
196 *
197 * WRITES are not routed around it. `set` and `delete` on the front already do
198 * the right thing in both modes: the backend changes, this worker's tier copy
199 * is dropped, and an invalidation is published so the other workers drop
200 * theirs. Reaching past that would leave the local copy stale, which is the
201 * one thing worse than reading through it. */
202 #define PK_SESSION_TIER_MAX 5 /* seconds a revocation may lag */
203  
204 47 static SV *ps_store_get(pTHX_ SV *store, SV *key, int use_tier) {
205 47 punk_cachefront *f = pkc_of(aTHX_ store);
206 6 return use_tier ? pkc_read(aTHX_ f, key)
207 53 ▶ : pkc_be_get(aTHX_ f, key, NULL);
Branch: 1 branch
BranchTrueFalseTotal
: pkc_be_get(aTHX_ f, key, NULL); 6 41 47
208 }
209  
210 /* The stores the `cache` keyword built, or NULL when there are none. */
211 25 static HV *ps_built_stores(pTHX_ HV *h) {
212 25 SV **b = hv_fetchs(h, "cache", 0);
213 22 ▶ return (b && *b && SvROK(*b) && SvTYPE(SvRV(*b)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
return (b && *b && SvROK(*b) && SvTYPE(SvRV(*b)) == SVt_PVHV) 22 0 22
return (b && *b && SvROK(*b) && SvTYPE(SvRV(*b)) == SVt_PVHV) 22 0 22
return (b && *b && SvROK(*b) && SvTYPE(SvRV(*b)) == SVt_PVHV) 22 0 22
214 47 ▶ ? (HV *)SvRV(*b) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(*b) : NULL; 22 3 25
215 }
216  
217 /* Punk::Cache->_from_spec($backend, \%opts, 'session') - the same constructor
218 * the `cache` keyword's own stores go through, so a session store is built by
219 * one code path and not by a second parser that drifts from it. */
220 27 static SV *ps_store_build(pTHX_ SV *spec, SV *opts) {
221 SV *argv[3];
222 SV *store;
223 27 ▶ argv[0] = spec ? spec : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
argv[0] = spec ? spec : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */ 27 0 27
224 27 ▶ argv[1] = opts ? opts : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
argv[1] = opts ? opts : &PL_sv_undef; 26 1 27
225 27 argv[2] = sv_2mortal(newSVpvs(PK_SESSION_STORE_NAME));
226 27 (void)pk_require_once(aTHX_ "Punk::Cache", TRUE);
227 27 store = pcx_call_meth(aTHX_ sv_2mortal(newSVpvs("Punk::Cache")),
228 "_from_spec", argv, 3, 1);
229 /*
230 # uncoverable branch true
231 # uncoverable condition left count:1 class:ignore_covered_err
232 # uncoverable condition left count:2 class:ignore_covered_err */
233 27 ▶ if (!(store && SvOK(store))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(store && SvOK(store))) { 27 0 27
if (!(store && SvOK(store))) { 0 27 27
234 /*
235 # uncoverable statement
236 # uncoverable branch true
237 # uncoverable branch false
238 # uncoverable condition left count:1
239 # uncoverable condition right count:1 */
240 0 ▶ if (store) SvREFCNT_dec(store);
Branch: 1 branch
BranchTrueFalseTotal
if (store) SvREFCNT_dec(store); 0 0 0
241 0 croak("Punk: `session store` could not build a store"); /* # uncoverable statement */
242 }
243 27 return store; /* +1 */
244 }
245  
246 /* Whether a session written here can be read from another worker.
247 *
248 * The question is asked of the BACKEND, never of the front's `stats`. The
249 * front reports `shared` as "the backend is shared AND there is no tier",
250 * which is the right answer to its own question - a tiered store needs
251 * invalidation traffic - and the wrong answer to this one: a tiered file store
252 * is perfectly capable of holding a session, and refusing it at boot would be
253 * a bug fixed by deleting the check. */
254 45 static int ps_store_shared(pTHX_ SV *store) {
255 45 SV *backend = pcx_call_meth(aTHX_ store, "backend", NULL, 0, 1);
256 45 int shared = 0;
257 /*
258 # uncoverable branch false
259 # uncoverable condition right count:1 class:ignore_covered_err */
260 45 ▶ if (backend) {
Branch: 1 branch
BranchTrueFalseTotal
if (backend) { 45 0 45
261 /*
262 # uncoverable branch false
263 # uncoverable condition right count:1 class:ignore_covered_err
264 # uncoverable condition right count:2 class:ignore_covered_err */
265 45 ▶ if (SvOK(backend) && pkc_can(aTHX_ backend, "is_shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(backend) && pkc_can(aTHX_ backend, "is_shared")) { 45 0 45
if (SvOK(backend) && pkc_can(aTHX_ backend, "is_shared")) { 45 0 45
266 45 SV *r = pcx_call_meth(aTHX_ backend, "is_shared", NULL, 0, 1);
267 /*
268 # uncoverable branch false
269 # uncoverable condition right count:1 class:ignore_covered_err */
270 45 ▶ if (r) {
Branch: 1 branch
BranchTrueFalseTotal
if (r) { 45 0 45
271 45 shared = SvTRUE(r) ? 1 : 0;
272 45 SvREFCNT_dec(r);
273 }
274 }
275 45 SvREFCNT_dec(backend);
276 }
277 45 return shared;
278 }
279  
280 /* A store the `cache` keyword declared, by name.
281 *
282 * `cache` is the reserved spelling for the default store, because "the cache"
283 * is what an application with one store calls it and `default` - the name it
284 * is actually built under - is an internal detail nobody should have to know.
285 * A store genuinely declared under that name is refused rather than guessed
286 * at, naming both readings. */
287 25 static SV *ps_store_named(pTHX_ HV *h, SV *spec) {
288 STRLEN nl;
289 25 const char *name = SvPV_const(spec, nl);
290 25 HV *stores = ps_built_stores(aTHX_ h);
291 25 ▶ int is_the_default = (nl == 5 && memEQ(name, "cache", 5)); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
int is_the_default = (nl == 5 && memEQ(name, "cache", 5)); /* # uncoverable condition right count:2 class:ignore_covered_err */ 22 3 25
int is_the_default = (nl == 5 && memEQ(name, "cache", 5)); /* # uncoverable condition right count:2 class:ignore_covered_err */ 22 0 22
292 SV **slot;
293  
294 /* `store:` in a YAML config with nothing after it. Absent means the cookie
295 * session, which is a decision; empty is a line somebody meant to fill in,
296 * and reading it as "the cookie" is the silent failure this whole check
297 * exists to prevent. */
298 25 ▶ if (!nl)
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) 1 24 25
299 1 croak("Punk: `session store` is empty - name a store, or leave the "
300 "option out entirely for the cookie session");
301  
302 24 ▶ if (!stores)
Branch: 1 branch
BranchTrueFalseTotal
if (!stores) 2 22 24
303 2 croak("Punk: `session store => '%s'` needs a store to point at, and "
304 "this application declares none - add a `cache` keyword",
305 name);
306  
307 22 ▶ if (is_the_default) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_the_default) { 20 2 22
308 20 ▶ if (hv_exists(stores, "cache", 5))
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(stores, "cache", 5)) 2 18 20
309 2 croak("Punk: `session store => 'cache'` is the DEFAULT store, but "
310 "this application also declares one named `cache` - rename "
311 "that store, since the two readings cannot both be right");
312 18 slot = hv_fetch(stores, K_DEFAULT, sizeof(K_DEFAULT) - 1, 0);
313 /*
314 # uncoverable condition left count:2 class:ignore_covered_err
315 # uncoverable condition left count:3 class:ignore_covered_err */
316 18 ▶ if (!(slot && *slot && SvROK(*slot)))
Branch: 3 branches
BranchTrueFalseTotal
if (!(slot && *slot && SvROK(*slot))) 17 1 18
if (!(slot && *slot && SvROK(*slot))) 17 0 17
if (!(slot && *slot && SvROK(*slot))) 0 17 17
317 1 croak("Punk: `session store => 'cache'` is the default store, and "
318 "this application declares only named ones - either add "
319 "`cache 'file', dir => ...` or name the store the session "
320 "should use");
321 }
322 else {
323 2 slot = hv_fetch(stores, name, (I32)nl, 0);
324 /*
325 # uncoverable condition left count:2 class:ignore_covered_err
326 # uncoverable condition left count:3 class:ignore_covered_err */
327 2 ▶ if (!(slot && *slot && SvROK(*slot)))
Branch: 3 branches
BranchTrueFalseTotal
if (!(slot && *slot && SvROK(*slot))) 1 1 2
if (!(slot && *slot && SvROK(*slot))) 1 0 1
if (!(slot && *slot && SvROK(*slot))) 0 1 1
328 1 croak("Punk: `session store => '%s'` names a store that was never "
329 "declared - add `cache %s => { backend => ... }`, or use "
330 "`store => 'cache'` for the default one. A session store "
331 "that silently never hits looks like a login page that "
332 "forgets everybody", name, name);
333 }
334 18 return newSVsv(*slot); /* +1 */
335 }
336  
337 /* Resolve `session store => ...` into a handle on the session config.
338 *
339 * Runs at to_app, and it has to run AFTER the `cache` keyword's stores are
340 * built: it resolves names out of that hash, so a resolution that goes first
341 * finds an empty one and every declared store looks like a typo. */
342 893 static void ps_store_resolve(pTHX_ HV *h) {
343 893 SV **sc = hv_fetchs(h, "session", 0);
344 HV *cfg;
345 SV **sp;
346 SV *spec, *store;
347  
348 /*
349 # uncoverable condition left count:2 class:ignore_covered_err
350 # uncoverable condition left count:3 class:ignore_covered_err
351 # uncoverable condition left count:4 class:ignore_covered_err */
352 893 ▶ if (!(sc && *sc && SvROK(*sc) && SvTYPE(SvRV(*sc)) == SVt_PVHV)) return;
Branch: 4 branches
BranchTrueFalseTotal
if (!(sc && *sc && SvROK(*sc) && SvTYPE(SvRV(*sc)) == SVt_PVHV)) return; 117 776 893
if (!(sc && *sc && SvROK(*sc) && SvTYPE(SvRV(*sc)) == SVt_PVHV)) return; 117 0 117
if (!(sc && *sc && SvROK(*sc) && SvTYPE(SvRV(*sc)) == SVt_PVHV)) return; 117 0 117
if (!(sc && *sc && SvROK(*sc) && SvTYPE(SvRV(*sc)) == SVt_PVHV)) return; 0 117 117
353 117 cfg = (HV *)SvRV(*sc);
354 117 sp = hv_fetchs(cfg, "store", 0);
355 /*
356 # uncoverable condition left count:2 class:ignore_covered_err
357 # uncoverable condition left count:3 class:ignore_covered_err */
358 117 ▶ if (!(sp && *sp && SvOK(*sp))) return; /* no store: the cookie, as before */
Branch: 3 branches
BranchTrueFalseTotal
if (!(sp && *sp && SvOK(*sp))) return; /* no store: the cookie, as before */ 53 64 117
if (!(sp && *sp && SvOK(*sp))) return; /* no store: the cookie, as before */ 53 0 53
if (!(sp && *sp && SvOK(*sp))) return; /* no store: the cookie, as before */ 0 53 53
359 53 spec = *sp;
360  
361 /* The four forms, told apart by what the value IS rather than by reading a
362 * string and guessing. A rule that inspects the string to decide whether
363 * it names a declared store or a class is a rule that eventually guesses
364 * wrong on somebody's store named `file`.
365 *
366 * BLESSED FIRST. Nearly every object is a hashref underneath, so a test
367 * for SVt_PVHV alone claims the object form as a spec, finds no `backend`
368 * key in it and quietly builds a file store instead of using the store it
369 * was handed - a backend swapped out under an application that named one.
370 * The `cache` keyword shipped that exact bug once already. */
371 53 ▶ if (SvROK(spec) && SvOBJECT(SvRV(spec))) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(spec) && SvOBJECT(SvRV(spec))) { 28 25 53
if (SvROK(spec) && SvOBJECT(SvRV(spec))) { 1 27 28
372 /* a ready-made object: _from_spec checks the five methods on it the
373 * same way it checks any other backend */
374 1 store = ps_store_build(aTHX_ spec, NULL);
375 }
376 78 ▶ else if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
else if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVHV) { 27 25 52
else if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVHV) { 26 1 27
377 /* a plain hashref: built here, in the shape `cache` already takes for
378 * a named store, so there is nothing new to learn */
379 26 HV *opts = (HV *)SvRV(spec);
380 26 SV **b = hv_fetchs(opts, "backend", 0);
381 /*
382 # uncoverable condition right count:1 class:ignore_covered_err
383 # uncoverable condition right count:2 class:ignore_covered_err
384 # uncoverable condition right count:3 class:ignore_covered_err */
385 26 ▶ store = ps_store_build(aTHX_ (b && *b && SvOK(*b)) ? *b : NULL, spec);
Branch: 3 branches
BranchTrueFalseTotal
store = ps_store_build(aTHX_ (b && *b && SvOK(*b)) ? *b : NULL, spec); 26 0 26
store = ps_store_build(aTHX_ (b && *b && SvOK(*b)) ? *b : NULL, spec); 26 0 26
store = ps_store_build(aTHX_ (b && *b && SvOK(*b)) ? *b : NULL, spec); 26 0 26
386 }
387 26 ▶ else if (SvROK(spec)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvROK(spec)) { 1 25 26
388 1 croak("Punk: `session store` takes the name of a store, a hashref "
389 "describing one, or a store object - not a %s reference",
390 sv_reftype(SvRV(spec), 0));
391 }
392 else {
393 25 store = ps_store_named(aTHX_ h, spec);
394 }
395 45 sv_2mortal(store);
396  
397 /* A store that is not shared between workers cannot hold a session. Not
398 * "is slower" and not "is eventually consistent": the session written on
399 * worker A is ABSENT on worker B, so the user is logged out on whichever
400 * request the pool sends elsewhere, at random, forever.
401 *
402 * allow_unshared is how somebody says there is only one process - the test
403 * suite, a dev server, a single-worker deployment - which is a thing only
404 * the person deploying knows. */
405 45 ▶ if (!ps_store_shared(aTHX_ store) && !ps_cfg_iv(aTHX_ cfg, "allow_unshared", 0))
Branch: 2 branches
BranchTrueFalseTotal
if (!ps_store_shared(aTHX_ store) && !ps_cfg_iv(aTHX_ cfg, "allow_unshared", 0)) 2 43 45
if (!ps_store_shared(aTHX_ store) && !ps_cfg_iv(aTHX_ cfg, "allow_unshared", 0)) 1 1 2
406 1 croak("Punk: the session store reports that it is not shared between "
407 "workers, so a session written on one worker would be missing "
408 "on the next - a logout at random, once per request. Use a "
409 "shared store (the `file` one is), or `allow_unshared => 1` if "
410 "this process is the only one");
411  
412 /* The tier, if the application opted into one for its sessions. The
413 * number is what the operator is prepared to have a revocation lag by,
414 * and both halves of it are checked here rather than discovered when
415 * somebody logs out and stays logged in. */
416 {
417 44 IV tier = ps_cfg_iv(aTHX_ cfg, "tier", 0);
418 44 ▶ if (tier) {
Branch: 1 branch
BranchTrueFalseTotal
if (tier) { 5 39 44
419 5 punk_cachefront *f = pkc_of(aTHX_ store);
420 5 ▶ if (tier < 0 || tier > PK_SESSION_TIER_MAX) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (tier < 0 || tier > PK_SESSION_TIER_MAX) /* # uncoverable condition left count:1 class:ignore_covered_err */ 5 0 5
if (tier < 0 || tier > PK_SESSION_TIER_MAX) /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 4 5
421 1 croak("Punk: `session tier => %" IVdf "` is how many seconds a "
422 "revoked session may keep working on a worker that "
423 "missed the invalidation, and %d is the most this will "
424 "accept - a logout that lags longer than that is not a "
425 "logout", tier, PK_SESSION_TIER_MAX);
426 4 ▶ if (!f->tier)
Branch: 1 branch
BranchTrueFalseTotal
if (!f->tier) 1 3 4
427 1 croak("Punk: `session tier => %" IVdf "` asks to read sessions "
428 "through a memory tier, and this store has none - add "
429 "`memory => '8M'` to the cache, or drop the option",
430 tier);
431 3 ▶ if (f->tier_ttl > (double)tier)
Branch: 1 branch
BranchTrueFalseTotal
if (f->tier_ttl > (double)tier) 1 2 3
432 1 croak("Punk: `session tier => %" IVdf "` accepts a %" IVdf
433 "-second lag on a revocation, but the store's "
434 "memory_ttl is %d seconds - lower memory_ttl, or say "
435 "what you actually accept", tier, tier,
436 (int)f->tier_ttl);
437 }
438 }
439  
440 41 (void)hv_store(cfg, PK_SESSION_STORE, PK_SESSION_STORE_LEN,
441 SvREFCNT_inc(store), 0);
442 }
443  
444 #endif /* PUNK_SESSIONSTORE_H */
« include/punk/punk_log.h ↑ Summary include/punk/punk_dbq.h »