include/punk/punk_sessionstore.h

filter: Statementstmt 96.4% 110 / 114 Branchbran 71.1% 101 / 142 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.4% 211 / 256 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_log.h ↑ Summary include/punk/punk_url.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 20041 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 20041 pk_ent_take(aTHX_ raw, sizeof raw);
63 340697 ▶ for (i = 0; i < PK_SESSION_ID_BYTES; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < PK_SESSION_ID_BYTES; i++) { 320656 20041 340697
64 320656 out[2 * i] = hex[raw[i] >> 4];
65 320656 out[2 * i + 1] = hex[raw[i] & 0x0f];
66 }
67 20041 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 62 static int ps_id_ok(const char *s, STRLEN len) {
87 STRLEN i;
88 62 ▶ if (len != PK_SESSION_ID_HEX) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len != PK_SESSION_ID_HEX) return 0; 1 61 62
89 1949 ▶ for (i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) 1890 59 1949
90 1890 ▶ 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'))) 1889 1 1890
if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) 728 1161 1889
if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) 728 1 729
if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) 1 727 728
91 2 return 0;
92 59 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 37 static SV *ps_id_seal(pTHX_ SV *id, const char *key, STRLEN kl, IV ttl) {
117 37 HV *payload = newHV();
118 SV *prv, *json;
119 37 ▶ if (ttl <= 0) ttl = PK_SESSION_MAX_LIFETIME;
Branch: 1 branch
BranchTrueFalseTotal
if (ttl <= 0) ttl = PK_SESSION_MAX_LIFETIME; 0 37 37
120 37 (void)hv_store(payload, PK_SESSION_ID, PK_SESSION_ID_LEN, newSVsv(id), 0);
121 37 (void)hv_store(payload, PK_SESSION_EXP, PK_SESSION_EXP_LEN,
122 newSViv((IV)time(NULL) + ttl), 0);
123 37 prv = sv_2mortal(newRV_noinc((SV *)payload));
124 37 json = sv_2mortal(ps_encode(aTHX_ prv));
125 37 return pk_session_sign(aTHX_ json, key, kl);
126 }
127  
128 /* the id inside a cookie value (+1), or NULL for anything that is not one:
129 * a bad signature, a payload past its stamped expiry, or an id whose shape
130 * says it was not minted here. A caller does not need to know which - all
131 * three mean "no session", and telling them apart is a distinction useful
132 * only to somebody probing. */
133 65 static SV *ps_id_unseal(pTHX_ const char *cv, STRLEN cvl,
134 const char *key, STRLEN kl) {
135 65 SV *payload, *decoded, *id = NULL;
136 65 ▶ if (!(cv && cvl && kl)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(cv && cvl && kl)) return NULL; 65 0 65
if (!(cv && cvl && kl)) return NULL; 64 1 65
if (!(cv && cvl && kl)) return NULL; 0 64 64
137 64 payload = pk_session_verify(aTHX_ cv, cvl, key, kl);
138 64 ▶ if (!payload) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!payload) return NULL; 7 57 64
139 57 sv_2mortal(payload);
140 57 decoded = punk_frj(aTHX)->decode(aTHX_ SvPVX(payload), SvCUR(payload), NULL);
141 57 ▶ if (!decoded) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!decoded) return NULL; 0 57 57
142 57 sv_2mortal(decoded);
143 57 ▶ if (SvROK(decoded) && SvTYPE(SvRV(decoded)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(decoded) && SvTYPE(SvRV(decoded)) == SVt_PVHV) { 57 0 57
if (SvROK(decoded) && SvTYPE(SvRV(decoded)) == SVt_PVHV) { 57 0 57
144 57 HV *dh = (HV *)SvRV(decoded);
145 57 SV **ep = hv_fetch(dh, PK_SESSION_EXP, PK_SESSION_EXP_LEN, 0);
146 57 SV **ip = hv_fetch(dh, PK_SESSION_ID, PK_SESSION_ID_LEN, 0);
147 57 ▶ 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; 57 0 57
if (ep && *ep && SvOK(*ep) && SvIV(*ep) <= (IV)time(NULL)) return NULL; 57 0 57
if (ep && *ep && SvOK(*ep) && SvIV(*ep) <= (IV)time(NULL)) return NULL; 57 0 57
if (ep && *ep && SvOK(*ep) && SvIV(*ep) <= (IV)time(NULL)) return NULL; 1 56 57
148 56 ▶ if (ip && *ip && SvOK(*ip)) {
Branch: 3 branches
BranchTrueFalseTotal
if (ip && *ip && SvOK(*ip)) { 55 1 56
if (ip && *ip && SvOK(*ip)) { 55 0 55
if (ip && *ip && SvOK(*ip)) { 55 0 55
149 STRLEN l;
150 55 const char *s = SvPV_const(*ip, l);
151 55 ▶ if (ps_id_ok(s, l)) id = newSVpvn(s, l);
Branch: 1 branch
BranchTrueFalseTotal
if (ps_id_ok(s, l)) id = newSVpvn(s, l); 55 0 55
152 }
153 }
154 56 return id; /* +1, or NULL */
155 }
156  
157 /* ---- the memory tier, and why a session goes round it ---------------------- *
158 *
159 * `Punk::Cache`'s `memory` option puts a per-worker copy in front of a shared
160 * store. For a cache that is the right trade. For a session it is a bound on
161 * HOW LONG A REVOKED SESSION KEEPS WORKING - a worker that missed the
162 * invalidation answers from its own copy for up to `memory_ttl`, and the copy
163 * it answers with is the session before the logout.
164 *
165 * Be accurate about the other direction while here, because the loose version
166 * of this argument is wrong: a tier MISS falls through to the backend, so a
167 * tier can never manufacture a missing session. The hazard is staleness in one
168 * direction only, and it is authentication staleness.
169 *
170 * So a session read goes straight to the backend by default, the way
171 * Punk::Plugin::Idempotency reaches past the tier for its own keys. An
172 * application's `memory => '64M'` stays honoured for everything else.
173 *
174 * WRITES are not routed around it. `set` and `delete` on the front already do
175 * the right thing in both modes: the backend changes, this worker's tier copy
176 * is dropped, and an invalidation is published so the other workers drop
177 * theirs. Reaching past that would leave the local copy stale, which is the
178 * one thing worse than reading through it. */
179 #define PK_SESSION_TIER_MAX 5 /* seconds a revocation may lag */
180  
181 43 static SV *ps_store_get(pTHX_ SV *store, SV *key, int use_tier) {
182 43 punk_cachefront *f = pkc_of(aTHX_ store);
183 6 return use_tier ? pkc_read(aTHX_ f, key)
184 49 ▶ : pkc_be_get(aTHX_ f, key, NULL);
Branch: 1 branch
BranchTrueFalseTotal
: pkc_be_get(aTHX_ f, key, NULL); 6 37 43
185 }
186  
187 /* The stores the `cache` keyword built, or NULL when there are none. */
188 15 static HV *ps_built_stores(pTHX_ HV *h) {
189 15 SV **b = hv_fetchs(h, "cache", 0);
190 13 ▶ return (b && *b && SvROK(*b) && SvTYPE(SvRV(*b)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
return (b && *b && SvROK(*b) && SvTYPE(SvRV(*b)) == SVt_PVHV) 13 0 13
return (b && *b && SvROK(*b) && SvTYPE(SvRV(*b)) == SVt_PVHV) 13 0 13
return (b && *b && SvROK(*b) && SvTYPE(SvRV(*b)) == SVt_PVHV) 13 0 13
191 28 ▶ ? (HV *)SvRV(*b) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(*b) : NULL; 13 2 15
192 }
193  
194 /* Punk::Cache->_from_spec($backend, \%opts, 'session') - the same constructor
195 * the `cache` keyword's own stores go through, so a session store is built by
196 * one code path and not by a second parser that drifts from it. */
197 27 static SV *ps_store_build(pTHX_ SV *spec, SV *opts) {
198 SV *argv[3];
199 SV *store;
200 27 ▶ argv[0] = spec ? spec : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
argv[0] = spec ? spec : &PL_sv_undef; 27 0 27
201 27 ▶ argv[1] = opts ? opts : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
argv[1] = opts ? opts : &PL_sv_undef; 26 1 27
202 27 argv[2] = sv_2mortal(newSVpvs(PK_SESSION_STORE_NAME));
203 27 (void)pk_require_once(aTHX_ "Punk::Cache", TRUE);
204 27 store = pcx_call_meth(aTHX_ sv_2mortal(newSVpvs("Punk::Cache")),
205 "_from_spec", argv, 3, 1);
206 27 ▶ if (!(store && SvOK(store))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(store && SvOK(store))) { 27 0 27
if (!(store && SvOK(store))) { 0 27 27
207 0 ▶ if (store) SvREFCNT_dec(store);
Branch: 1 branch
BranchTrueFalseTotal
if (store) SvREFCNT_dec(store); 0 0 0
208 0 croak("Punk: `session store` could not build a store");
209 }
210 27 return store; /* +1 */
211 }
212  
213 /* Whether a session written here can be read from another worker.
214 *
215 * The question is asked of the BACKEND, never of the front's `stats`. The
216 * front reports `shared` as "the backend is shared AND there is no tier",
217 * which is the right answer to its own question - a tiered store needs
218 * invalidation traffic - and the wrong answer to this one: a tiered file store
219 * is perfectly capable of holding a session, and refusing it at boot would be
220 * a bug fixed by deleting the check. */
221 38 static int ps_store_shared(pTHX_ SV *store) {
222 38 SV *backend = pcx_call_meth(aTHX_ store, "backend", NULL, 0, 1);
223 38 int shared = 0;
224 38 ▶ if (backend) {
Branch: 1 branch
BranchTrueFalseTotal
if (backend) { 38 0 38
225 38 ▶ if (SvOK(backend) && pkc_can(aTHX_ backend, "is_shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(backend) && pkc_can(aTHX_ backend, "is_shared")) { 38 0 38
if (SvOK(backend) && pkc_can(aTHX_ backend, "is_shared")) { 38 0 38
226 38 SV *r = pcx_call_meth(aTHX_ backend, "is_shared", NULL, 0, 1);
227 38 ▶ if (r) {
Branch: 1 branch
BranchTrueFalseTotal
if (r) { 38 0 38
228 38 shared = SvTRUE(r) ? 1 : 0;
229 38 SvREFCNT_dec(r);
230 }
231 }
232 38 SvREFCNT_dec(backend);
233 }
234 38 return shared;
235 }
236  
237 /* A store the `cache` keyword declared, by name.
238 *
239 * `cache` is the reserved spelling for the default store, because "the cache"
240 * is what an application with one store calls it and `default` - the name it
241 * is actually built under - is an internal detail nobody should have to know.
242 * A store genuinely declared under that name is refused rather than guessed
243 * at, naming both readings. */
244 15 static SV *ps_store_named(pTHX_ HV *h, SV *spec) {
245 STRLEN nl;
246 15 const char *name = SvPV_const(spec, nl);
247 15 HV *stores = ps_built_stores(aTHX_ h);
248 15 ▶ int is_the_default = (nl == 5 && memEQ(name, "cache", 5));
Branch: 2 branches
BranchTrueFalseTotal
int is_the_default = (nl == 5 && memEQ(name, "cache", 5)); 12 3 15
int is_the_default = (nl == 5 && memEQ(name, "cache", 5)); 12 0 12
249 SV **slot;
250  
251 /* `store:` in a YAML config with nothing after it. Absent means the cookie
252 * session, which is a decision; empty is a line somebody meant to fill in,
253 * and reading it as "the cookie" is the silent failure this whole check
254 * exists to prevent. */
255 15 ▶ if (!nl)
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) 1 14 15
256 1 croak("Punk: `session store` is empty - name a store, or leave the "
257 "option out entirely for the cookie session");
258  
259 14 ▶ if (!stores)
Branch: 1 branch
BranchTrueFalseTotal
if (!stores) 1 13 14
260 1 croak("Punk: `session store => '%s'` needs a store to point at, and "
261 "this application declares none - add a `cache` keyword",
262 name);
263  
264 13 ▶ if (is_the_default) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_the_default) { 11 2 13
265 11 ▶ if (hv_exists(stores, "cache", 5))
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(stores, "cache", 5)) 1 10 11
266 1 croak("Punk: `session store => 'cache'` is the DEFAULT store, but "
267 "this application also declares one named `cache` - rename "
268 "that store, since the two readings cannot both be right");
269 10 slot = hv_fetch(stores, K_DEFAULT, sizeof(K_DEFAULT) - 1, 0);
270 10 ▶ if (!(slot && *slot && SvROK(*slot)))
Branch: 3 branches
BranchTrueFalseTotal
if (!(slot && *slot && SvROK(*slot))) 10 0 10
if (!(slot && *slot && SvROK(*slot))) 10 0 10
if (!(slot && *slot && SvROK(*slot))) 0 10 10
271 0 croak("Punk: `session store => 'cache'` is the default store, and "
272 "this application declares only named ones - either add "
273 "`cache 'file', dir => ...` or name the store the session "
274 "should use");
275 }
276 else {
277 2 slot = hv_fetch(stores, name, (I32)nl, 0);
278 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
279 1 croak("Punk: `session store => '%s'` names a store that was never "
280 "declared - add `cache %s => { backend => ... }`, or use "
281 "`store => 'cache'` for the default one. A session store "
282 "that silently never hits looks like a login page that "
283 "forgets everybody", name, name);
284 }
285 11 return newSVsv(*slot); /* +1 */
286 }
287  
288 /* Resolve `session store => ...` into a handle on the session config.
289 *
290 * Runs at to_app, and it has to run AFTER the `cache` keyword's stores are
291 * built: it resolves names out of that hash, so a resolution that goes first
292 * finds an empty one and every declared store looks like a typo. */
293 671 static void ps_store_resolve(pTHX_ HV *h) {
294 671 SV **sc = hv_fetchs(h, "session", 0);
295 HV *cfg;
296 SV **sp;
297 SV *spec, *store;
298  
299 671 ▶ 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; 92 579 671
if (!(sc && *sc && SvROK(*sc) && SvTYPE(SvRV(*sc)) == SVt_PVHV)) return; 92 0 92
if (!(sc && *sc && SvROK(*sc) && SvTYPE(SvRV(*sc)) == SVt_PVHV)) return; 92 0 92
if (!(sc && *sc && SvROK(*sc) && SvTYPE(SvRV(*sc)) == SVt_PVHV)) return; 0 92 92
300 92 cfg = (HV *)SvRV(*sc);
301 92 sp = hv_fetchs(cfg, "store", 0);
302 92 ▶ 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 */ 42 50 92
if (!(sp && *sp && SvOK(*sp))) return; /* no store: the cookie, as before */ 42 0 42
if (!(sp && *sp && SvOK(*sp))) return; /* no store: the cookie, as before */ 0 42 42
303 42 spec = *sp;
304  
305 /* The four forms, told apart by what the value IS rather than by reading a
306 * string and guessing. A rule that inspects the string to decide whether
307 * it names a declared store or a class is a rule that eventually guesses
308 * wrong on somebody's store named `file`.
309 *
310 * BLESSED FIRST. Nearly every object is a hashref underneath, so a test
311 * for SVt_PVHV alone claims the object form as a spec, finds no `backend`
312 * key in it and quietly builds a file store instead of using the store it
313 * was handed - a backend swapped out under an application that named one.
314 * The `cache` keyword shipped that exact bug once already. */
315 42 ▶ if (SvROK(spec) && SvOBJECT(SvRV(spec))) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(spec) && SvOBJECT(SvRV(spec))) { 27 15 42
if (SvROK(spec) && SvOBJECT(SvRV(spec))) { 1 26 27
316 /* a ready-made object: _from_spec checks the five methods on it the
317 * same way it checks any other backend */
318 1 store = ps_store_build(aTHX_ spec, NULL);
319 }
320 67 ▶ else if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
else if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVHV) { 26 15 41
else if (SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVHV) { 26 0 26
321 /* a plain hashref: built here, in the shape `cache` already takes for
322 * a named store, so there is nothing new to learn */
323 26 HV *opts = (HV *)SvRV(spec);
324 26 SV **b = hv_fetchs(opts, "backend", 0);
325 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
326 }
327 15 ▶ else if (SvROK(spec)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvROK(spec)) { 0 15 15
328 0 croak("Punk: `session store` takes the name of a store, a hashref "
329 "describing one, or a store object - not a %s reference",
330 sv_reftype(SvRV(spec), 0));
331 }
332 else {
333 15 store = ps_store_named(aTHX_ h, spec);
334 }
335 38 sv_2mortal(store);
336  
337 /* A store that is not shared between workers cannot hold a session. Not
338 * "is slower" and not "is eventually consistent": the session written on
339 * worker A is ABSENT on worker B, so the user is logged out on whichever
340 * request the pool sends elsewhere, at random, forever.
341 *
342 * allow_unshared is how somebody says there is only one process - the test
343 * suite, a dev server, a single-worker deployment - which is a thing only
344 * the person deploying knows. */
345 38 ▶ 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 36 38
if (!ps_store_shared(aTHX_ store) && !ps_cfg_iv(aTHX_ cfg, "allow_unshared", 0)) 1 1 2
346 1 croak("Punk: the session store reports that it is not shared between "
347 "workers, so a session written on one worker would be missing "
348 "on the next - a logout at random, once per request. Use a "
349 "shared store (the `file` one is), or `allow_unshared => 1` if "
350 "this process is the only one");
351  
352 /* The tier, if the application opted into one for its sessions. The
353 * number is what the operator is prepared to have a revocation lag by,
354 * and both halves of it are checked here rather than discovered when
355 * somebody logs out and stays logged in. */
356 {
357 37 IV tier = ps_cfg_iv(aTHX_ cfg, "tier", 0);
358 37 ▶ if (tier) {
Branch: 1 branch
BranchTrueFalseTotal
if (tier) { 5 32 37
359 5 punk_cachefront *f = pkc_of(aTHX_ store);
360 5 ▶ if (tier < 0 || tier > PK_SESSION_TIER_MAX)
Branch: 2 branches
BranchTrueFalseTotal
if (tier < 0 || tier > PK_SESSION_TIER_MAX) 5 0 5
if (tier < 0 || tier > PK_SESSION_TIER_MAX) 1 4 5
361 1 croak("Punk: `session tier => %" IVdf "` is how many seconds a "
362 "revoked session may keep working on a worker that "
363 "missed the invalidation, and %d is the most this will "
364 "accept - a logout that lags longer than that is not a "
365 "logout", tier, PK_SESSION_TIER_MAX);
366 4 ▶ if (!f->tier)
Branch: 1 branch
BranchTrueFalseTotal
if (!f->tier) 1 3 4
367 1 croak("Punk: `session tier => %" IVdf "` asks to read sessions "
368 "through a memory tier, and this store has none - add "
369 "`memory => '8M'` to the cache, or drop the option",
370 tier);
371 3 ▶ if (f->tier_ttl > (double)tier)
Branch: 1 branch
BranchTrueFalseTotal
if (f->tier_ttl > (double)tier) 1 2 3
372 1 croak("Punk: `session tier => %" IVdf "` accepts a %" IVdf
373 "-second lag on a revocation, but the store's "
374 "memory_ttl is %d seconds - lower memory_ttl, or say "
375 "what you actually accept", tier, tier,
376 (int)f->tier_ttl);
377 }
378 }
379  
380 34 (void)hv_store(cfg, PK_SESSION_STORE, PK_SESSION_STORE_LEN,
381 SvREFCNT_inc(store), 0);
382 }
383  
384 #endif /* PUNK_SESSIONSTORE_H */
« include/punk/punk_log.h ↑ Summary include/punk/punk_url.h »