| 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 |
▶ | # uncoverable branch false |
Branch: 2 branches
| Branch | True | False | Total |
| # uncoverable branch false |
3 |
0 |
3 |
| # uncoverable branch false |
3 |
0 |
3 |
|
| 21 |
3 |
| # uncoverable condition right count:1 class:ignore_covered_err |
| 22 |
3 |
▶ | # uncoverable condition right count:2 class:ignore_covered_err |
Branch: 4 branches
| Branch | True | False | Total |
| # uncoverable condition right count:2 class:ignore_covered_err |
3 |
0 |
3 |
| # uncoverable condition right count:2 class:ignore_covered_err |
3 |
0 |
3 |
| # uncoverable condition right count:2 class:ignore_covered_err |
3 |
0 |
3 |
| # uncoverable condition right count:2 class:ignore_covered_err |
3 |
0 |
3 |
|
| 23 |
3 |
| if (SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) { |
| 24 |
|
| s = hv_fetchs((HV *)SvRV(app), "session", 0); |
| 25 |
3 |
▶ | # uncoverable branch false |
Branch: 1 branch
| Branch | True | False | Total |
| # uncoverable branch false |
0 |
3 |
3 |
|
| 26 |
0 |
| # uncoverable condition right count:1 class:ignore_covered_err |
| 27 |
3 |
| # uncoverable condition right count:2 class:ignore_covered_err |
| 28 |
3 |
| # uncoverable condition right count:3 class:ignore_covered_err |
| 29 |
3 |
▶ | # uncoverable condition right count:4 class:ignore_covered_err |
Branch: 1 branch
| Branch | True | False | Total |
| # uncoverable condition right count:4 class:ignore_covered_err |
0 |
3 |
3 |
|
| 30 |
0 |
| if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) |
| 31 |
3 |
| cfg = (HV *)SvRV(*s); |
| 32 |
3 |
| } |
| 33 |
3 |
▶ | # uncoverable branch true |
Branch: 1 branch
| Branch | True | False | Total |
| # uncoverable branch true |
0 |
3 |
3 |
|
| 34 |
3 |
| # uncoverable condition left count:1 class:ignore_covered_err |
| 35 |
3 |
| if (!cfg) |
| 36 |
|
| croak("Punk::Session: _seal needs an app with a session keyword"); /* # uncoverable statement */ |
| 37 |
|
| name = ps_cfg_str(aTHX_ cfg, "cookie", "punk.sid", &nl); |
| 38 |
|
| key = ps_cfg_str(aTHX_ cfg, "secret", "", &kl); |
| 39 |
|
| # uncoverable branch true |
| 40 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 41 |
|
| if (!kl) |
| 42 |
|
| croak("Punk::Session: _seal found no session secret"); /* # uncoverable statement */ |
| 43 |
|
| payload = sv_2mortal(ps_encode(aTHX_ data)); |
| 44 |
|
| sealed = pk_session_sign(aTHX_ payload, key, kl); |
| 45 |
|
| EXTEND(SP, 2); |
| 46 |
|
| PUSHs(sv_2mortal(newSVpvn(name, nl))); |
| 47 |
|
| PUSHs(sv_2mortal(sealed)); |
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
|
| PROTOTYPES: DISABLE |
| 51 |
|
| |
| 52 |
|
| # _new_id(): one session id - 128 bits of CSPRNG, hex. Internal, and exposed |
| 53 |
|
| # because the properties that matter (the shape, and that two forked workers |
| 54 |
|
| # never draw the same one) are properties of the GENERATOR, testable before |
| 55 |
|
| # anything reads a session out of a store. |
| 56 |
|
| SV * |
| 57 |
|
| _new_id() |
| 58 |
|
| CODE: |
| 59 |
102016 |
| RETVAL = ps_new_id(aTHX); |
| 60 |
|
| OUTPUT: |
| 61 |
|
| RETVAL |
| 62 |
|
| |
| 63 |
|
| # _seal_id($app, $id): the cookie an id rides in, as a (name, value) pair - |
| 64 |
|
| # the same shape _seal returns, through the same signer. Punk::Test mints one |
| 65 |
|
| # with it, and so does t/, so what this seals is what the reader verifies. |
| 66 |
|
| void |
| 67 |
|
| _seal_id(app, id) |
| 68 |
|
| SV *app |
| 69 |
|
| SV *id |
| 70 |
|
| PPCODE: |
| 71 |
|
| { |
| 72 |
8 |
| HV *cfg = NULL; |
| 73 |
|
| SV **s; |
| 74 |
|
| STRLEN nl, kl, il; |
| 75 |
|
| const char *name, *key, *idp; |
| 76 |
8 |
▶ | # uncoverable branch false |
Branch: 2 branches
| Branch | True | False | Total |
| # uncoverable branch false |
8 |
0 |
8 |
| # uncoverable branch false |
8 |
0 |
8 |
|
| 77 |
8 |
| # uncoverable condition right count:1 class:ignore_covered_err |
| 78 |
8 |
▶ | # uncoverable condition right count:2 class:ignore_covered_err |
Branch: 4 branches
| Branch | True | False | Total |
| # uncoverable condition right count:2 class:ignore_covered_err |
7 |
1 |
8 |
| # uncoverable condition right count:2 class:ignore_covered_err |
7 |
0 |
7 |
| # uncoverable condition right count:2 class:ignore_covered_err |
7 |
0 |
7 |
| # uncoverable condition right count:2 class:ignore_covered_err |
7 |
0 |
7 |
|
| 79 |
7 |
| if (SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) { |
| 80 |
|
| s = hv_fetchs((HV *)SvRV(app), "session", 0); |
| 81 |
8 |
▶ | # uncoverable condition right count:2 class:ignore_covered_err |
Branch: 1 branch
| Branch | True | False | Total |
| # uncoverable condition right count:2 class:ignore_covered_err |
1 |
7 |
8 |
|
| 82 |
1 |
| # uncoverable condition right count:3 class:ignore_covered_err |
| 83 |
14 |
▶ | # uncoverable condition right count:4 class:ignore_covered_err |
Branch: 1 branch
| Branch | True | False | Total |
| # uncoverable condition right count:4 class:ignore_covered_err |
7 |
0 |
7 |
|
| 84 |
7 |
▶ | if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) |
Branch: 1 branch
| Branch | True | False | Total |
| if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) |
3 |
4 |
7 |
|
| 85 |
3 |
| cfg = (HV *)SvRV(*s); |
| 86 |
|
| } |
| 87 |
4 |
| if (!cfg) |
| 88 |
4 |
| croak("Punk::Session: _seal_id needs an app with a session keyword"); |
| 89 |
4 |
▶ | idp = SvOK(id) ? SvPV_const(id, il) : (il = 0, ""); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| idp = SvOK(id) ? SvPV_const(id, il) : (il = 0, ""); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
0 |
4 |
4 |
|
| 90 |
0 |
| if (!ps_id_ok(idp, il)) |
| 91 |
4 |
▶ | croak("Punk::Session: _seal_id needs a %d-character hex id", |
Branch: 1 branch
| Branch | True | False | Total |
| croak("Punk::Session: _seal_id needs a %d-character hex id", |
0 |
4 |
4 |
|
| 92 |
4 |
| PK_SESSION_ID_HEX); |
| 93 |
4 |
| name = ps_cfg_str(aTHX_ cfg, "cookie", "punk.sid", &nl); |
| 94 |
|
| key = ps_cfg_str(aTHX_ cfg, "secret", "", &kl); |
| 95 |
|
| # uncoverable branch true |
| 96 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 97 |
|
| if (!kl) |
| 98 |
|
| croak("Punk::Session: _seal_id found no session secret"); /* # uncoverable statement */ |
| 99 |
|
| EXTEND(SP, 2); |
| 100 |
|
| PUSHs(sv_2mortal(newSVpvn(name, nl))); |
| 101 |
|
| PUSHs(sv_2mortal(ps_id_seal(aTHX_ id, key, kl, |
| 102 |
|
| ps_cfg_iv(aTHX_ cfg, "max_age", 0)))); |
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
|
| # _seal_session($app, \%session): a cookie a request will arrive with, whether |
| 106 |
|
| # the application keeps its sessions in the cookie or in a store. |
| 107 |
|
| # |
| 108 |
|
| # Punk::Test::login_as signs a user straight into the jar, and it did that by |
| 109 |
|
| # sealing the session INTO the cookie - which is no session at all to an |
| 110 |
|
| # application with a store, so every suite using login_as would have broken the |
| 111 |
|
| # day its application gained one. So the store case is handled here rather than |
| 112 |
|
| # in each suite: mint an id, write the session where the application will look |
| 113 |
|
| # for it, and seal the id. |
| 114 |
|
| void |
| 115 |
|
| _seal_session(app, data) |
| 116 |
|
| SV *app |
| 117 |
|
| SV *data |
| 118 |
|
| PPCODE: |
| 119 |
|
| { |
| 120 |
4 |
| HV *cfg = NULL, *apph; |
| 121 |
|
| SV **s, *store; |
| 122 |
|
| STRLEN nl, kl; |
| 123 |
|
| const char *name, *key; |
| 124 |
4 |
▶ | # uncoverable branch true |
Branch: 2 branches
| Branch | True | False | Total |
| # uncoverable branch true |
4 |
0 |
4 |
| # uncoverable branch true |
0 |
4 |
4 |
|
| 125 |
0 |
| # uncoverable condition left count:1 class:ignore_covered_err |
| 126 |
4 |
| # uncoverable condition left count:2 class:ignore_covered_err |
| 127 |
4 |
| if (!(SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) |
| 128 |
4 |
▶ | croak("Punk::Session: _seal_session needs an app"); /* # uncoverable statement */ |
Branch: 4 branches
| Branch | True | False | Total |
| croak("Punk::Session: _seal_session needs an app"); /* # uncoverable statement */ |
4 |
0 |
4 |
| croak("Punk::Session: _seal_session needs an app"); /* # uncoverable statement */ |
4 |
0 |
4 |
| croak("Punk::Session: _seal_session needs an app"); /* # uncoverable statement */ |
4 |
0 |
4 |
| croak("Punk::Session: _seal_session needs an app"); /* # uncoverable statement */ |
4 |
0 |
4 |
|
| 129 |
4 |
| apph = (HV *)SvRV(app); |
| 130 |
4 |
▶ | s = hv_fetchs(apph, "session", 0); |
Branch: 1 branch
| Branch | True | False | Total |
| s = hv_fetchs(apph, "session", 0); |
0 |
4 |
4 |
|
| 131 |
0 |
| # uncoverable branch false |
| 132 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 133 |
4 |
| # uncoverable condition right count:2 class:ignore_covered_err |
| 134 |
4 |
| # uncoverable condition right count:3 class:ignore_covered_err |
| 135 |
4 |
▶ | # uncoverable condition right count:4 class:ignore_covered_err |
Branch: 1 branch
| Branch | True | False | Total |
| # uncoverable condition right count:4 class:ignore_covered_err |
0 |
4 |
4 |
|
| 136 |
0 |
| if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) |
| 137 |
4 |
| cfg = (HV *)SvRV(*s); |
| 138 |
|
| # uncoverable branch true |
| 139 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 140 |
|
| if (!cfg) |
| 141 |
|
| croak("Punk::Session: _seal_session needs an app with a session " /* # uncoverable statement */ |
| 142 |
|
| "keyword"); |
| 143 |
4 |
▶ | name = ps_cfg_str(aTHX_ cfg, "cookie", "punk.sid", &nl); |
Branch: 2 branches
| Branch | True | False | Total |
| name = ps_cfg_str(aTHX_ cfg, "cookie", "punk.sid", &nl); |
3 |
1 |
4 |
| name = ps_cfg_str(aTHX_ cfg, "cookie", "punk.sid", &nl); |
1 |
2 |
3 |
|
| 144 |
1 |
| key = ps_cfg_str(aTHX_ cfg, "secret", "", &kl); |
| 145 |
|
| # uncoverable branch true |
| 146 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 147 |
|
| if (!kl) |
| 148 |
3 |
▶ | croak("Punk::Session: _seal_session found no session secret"); /* # uncoverable statement */ |
Branch: 1 branch
| Branch | True | False | Total |
| croak("Punk::Session: _seal_session found no session secret"); /* # uncoverable statement */ |
0 |
3 |
3 |
|
| 149 |
3 |
| store = ps_cfg_store(aTHX_ cfg); |
| 150 |
3 |
▶ | /* A store is asked for on the keyword and resolved at to_app, so |
Branch: 1 branch
| Branch | True | False | Total |
| /* A store is asked for on the keyword and resolved at to_app, so |
1 |
2 |
3 |
|
| 151 |
1 |
| * between those two moments the config says "server-side" and there is |
| 152 |
1 |
| * nowhere to write. Sealing into the cookie would be silently wrong - |
| 153 |
|
| * a cookie the compiled application will not recognise - so say which |
| 154 |
|
| * mistake it is instead. */ |
| 155 |
1 |
▶ | if (!store && hv_exists(cfg, "store", 5)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!store && hv_exists(cfg, "store", 5)) |
1 |
0 |
1 |
|
| 156 |
|
| croak("Punk::Session: this application has `session store => ...` " |
| 157 |
1 |
▶ | "but has not been compiled yet - call to_app before sealing " |
Branch: 1 branch
| Branch | True | False | Total |
| "but has not been compiled yet - call to_app before sealing " |
1 |
0 |
1 |
|
| 158 |
2 |
▶ | "a session, or the cookie names a store that does not exist " |
Branch: 1 branch
| Branch | True | False | Total |
| "a session, or the cookie names a store that does not exist " |
1 |
0 |
1 |
|
| 159 |
1 |
| "yet"); |
| 160 |
|
| EXTEND(SP, 2); |
| 161 |
1 |
| PUSHs(sv_2mortal(newSVpvn(name, nl))); |
| 162 |
1 |
| if (store) { |
| 163 |
|
| IV ttl = ps_cfg_iv(aTHX_ cfg, "max_age", 0); |
| 164 |
1 |
| SV *id = sv_2mortal(ps_new_id(aTHX)); |
| 165 |
1 |
| SV *argv[3], *ok; |
| 166 |
1 |
▶ | HV *payload; |
Branch: 1 branch
| Branch | True | False | Total |
| HV *payload; |
1 |
0 |
1 |
|
| 167 |
1 |
▶ | # uncoverable branch false |
Branch: 2 branches
| Branch | True | False | Total |
| # uncoverable branch false |
1 |
0 |
1 |
| # uncoverable branch false |
0 |
1 |
1 |
|
| 168 |
0 |
| # uncoverable condition right count:1 class:ignore_covered_err |
| 169 |
1 |
| if (ttl <= 0) ttl = PK_SESSION_MAX_LIFETIME; |
| 170 |
|
| /* stamped like any other write, so a sealed session slides too */ |
| 171 |
|
| payload = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) |
| 172 |
2 |
| ? newHVhv((HV *)SvRV(data)) : newHV(); |
| 173 |
|
| (void)hv_store(payload, PK_SESSION_AT, PK_SESSION_AT_LEN, |
| 174 |
|
| newSViv((IV)time(NULL)), 0); |
| 175 |
|
| argv[0] = id; |
| 176 |
|
| argv[1] = sv_2mortal(ps_encode(aTHX_ |
| 177 |
|
| sv_2mortal(newRV_noinc((SV *)payload)))); |
| 178 |
|
| argv[2] = sv_2mortal(newSViv(ttl)); |
| 179 |
|
| ok = pcx_call_meth(aTHX_ store, "set", argv, 3, 1); |
| 180 |
|
| # uncoverable branch false |
| 181 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 182 |
|
| if (ok) sv_2mortal(ok); |
| 183 |
|
| # uncoverable branch true |
| 184 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 185 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 186 |
|
| if (!(ok && SvTRUE(ok))) |
| 187 |
|
| croak("Punk::Session: the session store refused the write"); /* # uncoverable statement */ |
| 188 |
|
| PUSHs(sv_2mortal(ps_id_seal(aTHX_ id, key, kl, ttl))); |
| 189 |
|
| } |
| 190 |
|
| else { |
| 191 |
|
| PUSHs(sv_2mortal(pk_session_sign(aTHX_ |
| 192 |
|
| sv_2mortal(ps_encode(aTHX_ data)), key, kl))); |
| 193 |
|
| } |
| 194 |
|
| } |
| 195 |
|
| |
| 196 |
|
| # _unseal_id($app, $value): the id inside a cookie value, or undef for a bad |
| 197 |
|
| # signature, an expired stamp or an id this server would never have minted. |
| 198 |
|
| # One answer for all three: which it was is a distinction useful only to |
| 199 |
|
| # somebody probing. |
| 200 |
|
| SV * |
| 201 |
|
| _unseal_id(app, value) |
| 202 |
|
| SV *app |
| 203 |
|
| SV *value |
| 204 |
|
| CODE: |
| 205 |
|
| { |
| 206 |
12 |
| HV *cfg = NULL; |
| 207 |
|
| SV **s; |
| 208 |
|
| STRLEN kl, vl; |
| 209 |
|
| const char *key; |
| 210 |
12 |
| SV *id = NULL; |
| 211 |
12 |
▶ | # uncoverable branch false |
Branch: 2 branches
| Branch | True | False | Total |
| # uncoverable branch false |
12 |
0 |
12 |
| # uncoverable branch false |
12 |
0 |
12 |
|
| 212 |
12 |
| # uncoverable condition right count:1 class:ignore_covered_err |
| 213 |
12 |
▶ | # uncoverable condition right count:2 class:ignore_covered_err |
Branch: 4 branches
| Branch | True | False | Total |
| # uncoverable condition right count:2 class:ignore_covered_err |
12 |
0 |
12 |
| # uncoverable condition right count:2 class:ignore_covered_err |
12 |
0 |
12 |
| # uncoverable condition right count:2 class:ignore_covered_err |
12 |
0 |
12 |
| # uncoverable condition right count:2 class:ignore_covered_err |
12 |
0 |
12 |
|
| 214 |
12 |
| if (SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) { |
| 215 |
|
| s = hv_fetchs((HV *)SvRV(app), "session", 0); |
| 216 |
12 |
▶ | # uncoverable branch false |
Branch: 1 branch
| Branch | True | False | Total |
| # uncoverable branch false |
0 |
12 |
12 |
|
| 217 |
0 |
| # uncoverable condition right count:1 class:ignore_covered_err |
| 218 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 219 |
12 |
| # uncoverable condition right count:3 class:ignore_covered_err |
| 220 |
12 |
▶ | # uncoverable condition right count:4 class:ignore_covered_err |
Branch: 1 branch
| Branch | True | False | Total |
| # uncoverable condition right count:4 class:ignore_covered_err |
12 |
0 |
12 |
|
| 221 |
12 |
| if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) |
| 222 |
12 |
| cfg = (HV *)SvRV(*s); |
| 223 |
|
| } |
| 224 |
12 |
▶ | # uncoverable branch true |
Branch: 1 branch
| Branch | True | False | Total |
| # uncoverable branch true |
8 |
4 |
12 |
|
| 225 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 226 |
|
| if (!cfg) |
| 227 |
|
| croak("Punk::Session: _unseal_id needs an app with a session " /* # uncoverable statement */ |
| 228 |
|
| "keyword"); |
| 229 |
|
| key = ps_cfg_str(aTHX_ cfg, "secret", "", &kl); |
| 230 |
|
| # uncoverable branch false |
| 231 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 232 |
|
| if (SvOK(value)) { |
| 233 |
|
| const char *v = SvPV_const(value, vl); |
| 234 |
|
| id = ps_id_unseal(aTHX_ v, vl, key, kl); |
| 235 |
|
| } |
| 236 |
|
| RETVAL = id ? id : newSV(0); |
| 237 |
|
| } |
| 238 |
|
| OUTPUT: |
| 239 |
|
| RETVAL |
| 240 |
|
| |
| 241 |
|
| # The after-dispatch write-back installed when the `session` keyword is used |
| 242 |
|
| # (punk_session.h). It adds a signed Set-Cookie to the finished triplet when the |
| 243 |
|
| # session changed, or a deletion cookie when it was expired. lib/Punk/Session.pm |
| 244 |
|
| # is documentation. |
| 245 |
|
| SV * |
| 246 |
|
| _writeback(c, resp) |
| 247 |
|
| SV *c |
| 248 |
|
| SV *resp |
| 249 |
|
| CODE: |
| 250 |
430 |
| ps_writeback(aTHX_ c, resp); |
| 251 |
430 |
| RETVAL = newSVsv(resp); |
| 252 |
|
| OUTPUT: |
| 253 |
|
| RETVAL |