xs/session.xs

filter: Statementstmt 90.3% 75 / 83 Branchbran 57.4% 54 / 94 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 72.8% 129 / 177 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/app.xs ↑ Summary include/punk/pk_abi_impl.h »
1 MODULE = Punk PACKAGE = Punk::Session
2  
3 PROTOTYPES: DISABLE
4  
5 # _seal($app, \%session): the signed cookie a session hashref would ride in,
6 # as a (name, value) pair - Punk::Test::login_as mints a session with it so
7 # tests reach guarded pages without driving a login flow. The encoding and
8 # the signer are the write-back's own, so what this seals, ps_load verifies.
9 void
10 _seal(app, data)
11 SV *app
12 SV *data
13 PPCODE:
14 {
15 3 HV *cfg = NULL;
16 SV **s;
17 STRLEN nl, kl;
18 const char *name, *key;
19 SV *payload, *sealed;
20 3 ▶ if (SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) { 3 0 3
if (SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) { 3 0 3
21 3 s = hv_fetchs((HV *)SvRV(app), "session", 0);
22 3 ▶ if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 3 0 3
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 3 0 3
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 3 0 3
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 3 0 3
23 3 cfg = (HV *)SvRV(*s);
24 }
25 3 ▶ if (!cfg)
Branch: 1 branch
BranchTrueFalseTotal
if (!cfg) 0 3 3
26 0 croak("Punk::Session: _seal needs an app with a session keyword");
27 3 name = ps_cfg_str(aTHX_ cfg, "cookie", "punk.sid", &nl);
28 3 key = ps_cfg_str(aTHX_ cfg, "secret", "", &kl);
29 3 ▶ if (!kl)
Branch: 1 branch
BranchTrueFalseTotal
if (!kl) 0 3 3
30 0 croak("Punk::Session: _seal found no session secret");
31 3 payload = sv_2mortal(ps_encode(aTHX_ data));
32 3 sealed = pk_session_sign(aTHX_ payload, key, kl);
33 3 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 3 3
34 3 PUSHs(sv_2mortal(newSVpvn(name, nl)));
35 3 PUSHs(sv_2mortal(sealed));
36 }
37  
38 PROTOTYPES: DISABLE
39  
40 # _new_id(): one session id - 128 bits of CSPRNG, hex. Internal, and exposed
41 # because the properties that matter (the shape, and that two forked workers
42 # never draw the same one) are properties of the GENERATOR, testable before
43 # anything reads a session out of a store.
44 SV *
45 _new_id()
46 CODE:
47 20008 RETVAL = ps_new_id(aTHX);
48 OUTPUT:
49 RETVAL
50  
51 # _seal_id($app, $id): the cookie an id rides in, as a (name, value) pair -
52 # the same shape _seal returns, through the same signer. Punk::Test mints one
53 # with it, and so does t/, so what this seals is what the reader verifies.
54 void
55 _seal_id(app, id)
56 SV *app
57 SV *id
58 PPCODE:
59 {
60 8 HV *cfg = NULL;
61 SV **s;
62 STRLEN nl, kl, il;
63 const char *name, *key, *idp;
64 8 ▶ if (SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) { 8 0 8
if (SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) { 8 0 8
65 8 s = hv_fetchs((HV *)SvRV(app), "session", 0);
66 8 ▶ if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 7 1 8
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 7 0 7
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 7 0 7
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 7 0 7
67 7 cfg = (HV *)SvRV(*s);
68 }
69 8 ▶ if (!cfg)
Branch: 1 branch
BranchTrueFalseTotal
if (!cfg) 1 7 8
70 1 croak("Punk::Session: _seal_id needs an app with a session keyword");
71 14 ▶ idp = SvOK(id) ? SvPV_const(id, il) : (il = 0, "");
Branch: 1 branch
BranchTrueFalseTotal
idp = SvOK(id) ? SvPV_const(id, il) : (il = 0, ""); 7 0 7
72 7 ▶ if (!ps_id_ok(idp, il))
Branch: 1 branch
BranchTrueFalseTotal
if (!ps_id_ok(idp, il)) 3 4 7
73 3 croak("Punk::Session: _seal_id needs a %d-character hex id",
74 PK_SESSION_ID_HEX);
75 4 name = ps_cfg_str(aTHX_ cfg, "cookie", "punk.sid", &nl);
76 4 key = ps_cfg_str(aTHX_ cfg, "secret", "", &kl);
77 4 ▶ if (!kl)
Branch: 1 branch
BranchTrueFalseTotal
if (!kl) 0 4 4
78 0 croak("Punk::Session: _seal_id found no session secret");
79 4 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 4 4
80 4 PUSHs(sv_2mortal(newSVpvn(name, nl)));
81 4 PUSHs(sv_2mortal(ps_id_seal(aTHX_ id, key, kl,
82 ps_cfg_iv(aTHX_ cfg, "max_age", 0))));
83 }
84  
85 # _seal_session($app, \%session): a cookie a request will arrive with, whether
86 # the application keeps its sessions in the cookie or in a store.
87 #
88 # Punk::Test::login_as signs a user straight into the jar, and it did that by
89 # sealing the session INTO the cookie - which is no session at all to an
90 # application with a store, so every suite using login_as would have broken the
91 # day its application gained one. So the store case is handled here rather than
92 # in each suite: mint an id, write the session where the application will look
93 # for it, and seal the id.
94 void
95 _seal_session(app, data)
96 SV *app
97 SV *data
98 PPCODE:
99 {
100 4 HV *cfg = NULL, *apph;
101 SV **s, *store;
102 STRLEN nl, kl;
103 const char *name, *key;
104 4 ▶ if (!(SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) 4 0 4
if (!(SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) 0 4 4
105 0 croak("Punk::Session: _seal_session needs an app");
106 4 apph = (HV *)SvRV(app);
107 4 s = hv_fetchs(apph, "session", 0);
108 4 ▶ if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 4 0 4
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 4 0 4
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 4 0 4
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 4 0 4
109 4 cfg = (HV *)SvRV(*s);
110 4 ▶ if (!cfg)
Branch: 1 branch
BranchTrueFalseTotal
if (!cfg) 0 4 4
111 0 croak("Punk::Session: _seal_session needs an app with a session "
112 "keyword");
113 4 name = ps_cfg_str(aTHX_ cfg, "cookie", "punk.sid", &nl);
114 4 key = ps_cfg_str(aTHX_ cfg, "secret", "", &kl);
115 4 ▶ if (!kl)
Branch: 1 branch
BranchTrueFalseTotal
if (!kl) 0 4 4
116 0 croak("Punk::Session: _seal_session found no session secret");
117 4 store = ps_cfg_store(aTHX_ cfg);
118 /* A store is asked for on the keyword and resolved at to_app, so
119 * between those two moments the config says "server-side" and there is
120 * nowhere to write. Sealing into the cookie would be silently wrong -
121 * a cookie the compiled application will not recognise - so say which
122 * mistake it is instead. */
123 4 ▶ if (!store && hv_exists(cfg, "store", 5))
Branch: 2 branches
BranchTrueFalseTotal
if (!store && hv_exists(cfg, "store", 5)) 3 1 4
if (!store && hv_exists(cfg, "store", 5)) 1 2 3
124 1 croak("Punk::Session: this application has `session store => ...` "
125 "but has not been compiled yet - call to_app before sealing "
126 "a session, or the cookie names a store that does not exist "
127 "yet");
128 3 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 3 3
129 3 PUSHs(sv_2mortal(newSVpvn(name, nl)));
130 3 ▶ if (store) {
Branch: 1 branch
BranchTrueFalseTotal
if (store) { 1 2 3
131 1 IV ttl = ps_cfg_iv(aTHX_ cfg, "max_age", 0);
132 1 SV *id = sv_2mortal(ps_new_id(aTHX));
133 SV *argv[3], *ok;
134 HV *payload;
135 1 ▶ if (ttl <= 0) ttl = PK_SESSION_MAX_LIFETIME;
Branch: 1 branch
BranchTrueFalseTotal
if (ttl <= 0) ttl = PK_SESSION_MAX_LIFETIME; 1 0 1
136 /* stamped like any other write, so a sealed session slides too */
137 1 ▶ payload = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
payload = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) 1 0 1
138 2 ▶ ? newHVhv((HV *)SvRV(data)) : newHV();
Branch: 1 branch
BranchTrueFalseTotal
? newHVhv((HV *)SvRV(data)) : newHV(); 1 0 1
139 1 (void)hv_store(payload, PK_SESSION_AT, PK_SESSION_AT_LEN,
140 newSViv((IV)time(NULL)), 0);
141 1 argv[0] = id;
142 1 argv[1] = sv_2mortal(ps_encode(aTHX_
143 sv_2mortal(newRV_noinc((SV *)payload))));
144 1 argv[2] = sv_2mortal(newSViv(ttl));
145 1 ok = pcx_call_meth(aTHX_ store, "set", argv, 3, 1);
146 1 ▶ if (ok) sv_2mortal(ok);
Branch: 1 branch
BranchTrueFalseTotal
if (ok) sv_2mortal(ok); 1 0 1
147 1 ▶ if (!(ok && SvTRUE(ok)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(ok && SvTRUE(ok))) 1 0 1
if (!(ok && SvTRUE(ok))) 0 1 1
148 0 croak("Punk::Session: the session store refused the write");
149 1 PUSHs(sv_2mortal(ps_id_seal(aTHX_ id, key, kl, ttl)));
150 }
151 else {
152 2 PUSHs(sv_2mortal(pk_session_sign(aTHX_
153 sv_2mortal(ps_encode(aTHX_ data)), key, kl)));
154 }
155 }
156  
157 # _unseal_id($app, $value): the id inside a cookie value, or undef for a bad
158 # signature, an expired stamp or an id this server would never have minted.
159 # One answer for all three: which it was is a distinction useful only to
160 # somebody probing.
161 SV *
162 _unseal_id(app, value)
163 SV *app
164 SV *value
165 CODE:
166 {
167 12 HV *cfg = NULL;
168 SV **s;
169 STRLEN kl, vl;
170 const char *key;
171 12 SV *id = NULL;
172 12 ▶ if (SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) { 12 0 12
if (SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) { 12 0 12
173 12 s = hv_fetchs((HV *)SvRV(app), "session", 0);
174 12 ▶ if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 12 0 12
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 12 0 12
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 12 0 12
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 12 0 12
175 12 cfg = (HV *)SvRV(*s);
176 }
177 12 ▶ if (!cfg)
Branch: 1 branch
BranchTrueFalseTotal
if (!cfg) 0 12 12
178 0 croak("Punk::Session: _unseal_id needs an app with a session "
179 "keyword");
180 12 key = ps_cfg_str(aTHX_ cfg, "secret", "", &kl);
181 12 ▶ if (SvOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(value)) { 12 0 12
182 12 const char *v = SvPV_const(value, vl);
183 12 id = ps_id_unseal(aTHX_ v, vl, key, kl);
184 }
185 12 ▶ RETVAL = id ? id : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = id ? id : newSV(0); 8 4 12
186 }
187 OUTPUT:
188 RETVAL
189  
190 # The after-dispatch write-back installed when the `session` keyword is used
191 # (punk_session.h). It adds a signed Set-Cookie to the finished triplet when the
192 # session changed, or a deletion cookie when it was expired. lib/Punk/Session.pm
193 # is documentation.
194 SV *
195 _writeback(c, resp)
196 SV *c
197 SV *resp
198 CODE:
199 397 ps_writeback(aTHX_ c, resp);
200 397 RETVAL = newSVsv(resp);
201 OUTPUT:
202 RETVAL
« xs/app.xs ↑ Summary include/punk/pk_abi_impl.h »