include/punk/punk_cookie.h

filter: Statementstmt 100% 56 / 56 Branchbran 74.0% 77 / 104 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.1% 133 / 160 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_serve.h ↑ Summary include/punk/punk_asset.h »
1 /* punk_cookie.h - build a Set-Cookie header value, in C.
2 *
3 * The shared builder behind $c->cookie(...) and the session write-back. The
4 * value is percent-encoded so it round-trips through Punk::Request's cookie
5 * reader (pq_decode); a base64url session value passes through unchanged.
6 * Options: path (default /), domain, max_age (seconds), samesite, secure,
7 * httponly. An undef value emits a deletion cookie (Max-Age=0, past Expires).
8 *
9 * The value is encoded; the name and the path, domain and samesite attributes
10 * are not, so they are refused outright when they carry a `;` or a control
11 * character. An attribute built from request input would otherwise be a way
12 * to append attributes, or a header.
13 */
14  
15 #ifndef PUNK_COOKIE_H
16 #define PUNK_COOKIE_H
17  
18 /* percent-encode everything but the URI unreserved set, so ';' ',' space and
19 * the rest cannot break the cookie and the reader decodes back exactly */
20 158 static void pk_cookie_encode(pTHX_ SV *out, const char *v, STRLEN vl) {
21 static const char hex[] = "0123456789ABCDEF";
22 STRLEN i;
23 15673 ▶ for (i = 0; i < vl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < vl; i++) { 15515 158 15673
24 15515 unsigned char c = (unsigned char)v[i];
25 15515 ▶ if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
Branch: 4 branches
BranchTrueFalseTotal
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 12999 2516 15515
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 6277 6722 12999
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 6183 2610 8793
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 0 6183 6183
26 2610 ▶ || (c >= '0' && c <= '9')
Branch: 2 branches
BranchTrueFalseTotal
|| (c >= '0' && c <= '9') 2382 228 2610
|| (c >= '0' && c <= '9') 95 2287 2382
27 323 ▶ || c == '-' || c == '_' || c == '.' || c == '~')
Branch: 4 branches
BranchTrueFalseTotal
|| c == '-' || c == '_' || c == '.' || c == '~') 221 102 323
|| c == '-' || c == '_' || c == '.' || c == '~') 127 94 221
|| c == '-' || c == '_' || c == '.' || c == '~') 2 125 127
|| c == '-' || c == '_' || c == '.' || c == '~') 0 2 2
28 15513 sv_catpvn(out, (const char *)&c, 1);
29 else {
30 char e[3];
31 2 e[0] = '%'; e[1] = hex[c >> 4]; e[2] = hex[c & 0x0f];
32 2 sv_catpvn(out, e, 3);
33 }
34 }
35 158 }
36  
37 /* is `v` safe to write into a Set-Cookie verbatim? */
38 826 static int pk_cookie_safe(pTHX_ SV *v) {
39 STRLEN l, i;
40 826 const char *p = SvPV_const(v, l);
41 4013 ▶ for (i = 0; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) { 3199 814 4013
42 3199 unsigned char c = (unsigned char)p[i];
43 3199 ▶ if (c < 0x20 || c == 0x7f || c == ';') return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (c < 0x20 || c == 0x7f || c == ';') return 0; 3193 6 3199
if (c < 0x20 || c == 0x7f || c == ';') return 0; 3193 0 3193
if (c < 0x20 || c == 0x7f || c == ';') return 0; 6 3187 3193
44 }
45 814 return 1;
46 }
47  
48 486 static void pk_cookie_check(pTHX_ const char *what, SV *v) {
49 486 ▶ if (!pk_cookie_safe(aTHX_ v))
Branch: 1 branch
BranchTrueFalseTotal
if (!pk_cookie_safe(aTHX_ v)) 7 479 486
50 7 croak("Punk: a cookie %s may not contain ';' or a control "
51 "character", what);
52 479 }
53  
54 /* `secure_default` decides the Secure attribute when the options do not say:
55 * the session passes whether the request arrived over https. */
56 178 static SV *pk_build_cookie_ex(pTHX_ SV *name, SV *value, HV *opts,
57 int secure_default) {
58 SV *out;
59 STRLEN nl;
60 178 const char *n = SvPV_const(name, nl);
61 178 ▶ int deleting = !(value && SvOK(value));
Branch: 2 branches
BranchTrueFalseTotal
int deleting = !(value && SvOK(value)); 178 0 178
int deleting = !(value && SvOK(value)); 11 167 178
62 SV **o;
63 static const char *const attrs[] = { "path", "domain", "samesite", NULL };
64 int i;
65 178 ▶ if (!nl) croak("Punk: a cookie needs a name");
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) croak("Punk: a cookie needs a name"); 1 177 178
66 177 pk_cookie_check(aTHX_ "name", name);
67 175 ▶ if (memchr(n, '=', nl))
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(n, '=', nl)) 1 174 175
68 1 croak("Punk: a cookie name may not contain '='");
69 685 ▶ for (i = 0; opts && attrs[i]; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; opts && attrs[i]; i++) { 685 0 685
for (i = 0; opts && attrs[i]; i++) { 516 169 685
70 516 o = hv_fetch(opts, attrs[i], (I32)strlen(attrs[i]), 0);
71 516 ▶ if (o && *o && SvOK(*o)) pk_cookie_check(aTHX_ attrs[i], *o);
Branch: 3 branches
BranchTrueFalseTotal
if (o && *o && SvOK(*o)) pk_cookie_check(aTHX_ attrs[i], *o); 309 207 516
if (o && *o && SvOK(*o)) pk_cookie_check(aTHX_ attrs[i], *o); 309 0 309
if (o && *o && SvOK(*o)) pk_cookie_check(aTHX_ attrs[i], *o); 309 0 309
72 }
73 169 out = newSVpvs("");
74 169 sv_catpvn(out, n, nl);
75 169 sv_catpvs(out, "=");
76 169 ▶ if (!deleting) {
Branch: 1 branch
BranchTrueFalseTotal
if (!deleting) { 158 11 169
77 STRLEN vl;
78 158 const char *v = SvPV_const(value, vl);
79 158 pk_cookie_encode(aTHX_ out, v, vl);
80 }
81 169 ▶ o = opts ? hv_fetchs(opts, "path", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
o = opts ? hv_fetchs(opts, "path", 0) : NULL; 169 0 169
82 169 sv_catpvs(out, "; Path=");
83 169 ▶ if (o && *o && SvOK(*o)) sv_catsv(out, *o); else sv_catpvs(out, "/");
Branch: 3 branches
BranchTrueFalseTotal
if (o && *o && SvOK(*o)) sv_catsv(out, *o); else sv_catpvs(out, "/"); 152 17 169
if (o && *o && SvOK(*o)) sv_catsv(out, *o); else sv_catpvs(out, "/"); 152 0 152
if (o && *o && SvOK(*o)) sv_catsv(out, *o); else sv_catpvs(out, "/"); 152 0 152
84  
85 169 ▶ o = opts ? hv_fetchs(opts, "domain", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
o = opts ? hv_fetchs(opts, "domain", 0) : NULL; 169 0 169
86 169 ▶ if (o && *o && SvOK(*o)) { sv_catpvs(out, "; Domain="); sv_catsv(out, *o); }
Branch: 3 branches
BranchTrueFalseTotal
if (o && *o && SvOK(*o)) { sv_catpvs(out, "; Domain="); sv_catsv(out, *o); } 0 169 169
if (o && *o && SvOK(*o)) { sv_catpvs(out, "; Domain="); sv_catsv(out, *o); } 0 0 0
if (o && *o && SvOK(*o)) { sv_catpvs(out, "; Domain="); sv_catsv(out, *o); } 0 0 0
87  
88 169 ▶ if (deleting)
Branch: 1 branch
BranchTrueFalseTotal
if (deleting) 11 158 169
89 11 sv_catpvs(out, "; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT");
90 else {
91 158 ▶ o = opts ? hv_fetchs(opts, "max_age", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
o = opts ? hv_fetchs(opts, "max_age", 0) : NULL; 158 0 158
92 158 ▶ if (o && *o && SvOK(*o))
Branch: 3 branches
BranchTrueFalseTotal
if (o && *o && SvOK(*o)) 25 133 158
if (o && *o && SvOK(*o)) 25 0 25
if (o && *o && SvOK(*o)) 25 0 25
93 25 sv_catpvf(out, "; Max-Age=%" IVdf, (IV)SvIV(*o));
94 }
95  
96 169 ▶ o = opts ? hv_fetchs(opts, "samesite", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
o = opts ? hv_fetchs(opts, "samesite", 0) : NULL; 169 0 169
97 169 ▶ if (o && *o && SvOK(*o)) { sv_catpvs(out, "; SameSite="); sv_catsv(out, *o); }
Branch: 3 branches
BranchTrueFalseTotal
if (o && *o && SvOK(*o)) { sv_catpvs(out, "; SameSite="); sv_catsv(out, *o); } 152 17 169
if (o && *o && SvOK(*o)) { sv_catpvs(out, "; SameSite="); sv_catsv(out, *o); } 152 0 152
if (o && *o && SvOK(*o)) { sv_catpvs(out, "; SameSite="); sv_catsv(out, *o); } 152 0 152
98  
99 169 ▶ o = opts ? hv_fetchs(opts, "secure", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
o = opts ? hv_fetchs(opts, "secure", 0) : NULL; 169 0 169
100 169 ▶ if ((o && *o && SvOK(*o)) ? SvTRUE(*o) : secure_default)
Branch: 4 branches
BranchTrueFalseTotal
if ((o && *o && SvOK(*o)) ? SvTRUE(*o) : secure_default) 3 166 169
if ((o && *o && SvOK(*o)) ? SvTRUE(*o) : secure_default) 3 0 3
if ((o && *o && SvOK(*o)) ? SvTRUE(*o) : secure_default) 3 0 3
if ((o && *o && SvOK(*o)) ? SvTRUE(*o) : secure_default) 6 163 169
101 6 sv_catpvs(out, "; Secure");
102  
103 169 ▶ o = opts ? hv_fetchs(opts, "httponly", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
o = opts ? hv_fetchs(opts, "httponly", 0) : NULL; 169 0 169
104 169 ▶ if (o && *o && SvTRUE(*o)) sv_catpvs(out, "; HttpOnly");
Branch: 3 branches
BranchTrueFalseTotal
if (o && *o && SvTRUE(*o)) sv_catpvs(out, "; HttpOnly"); 152 17 169
if (o && *o && SvTRUE(*o)) sv_catpvs(out, "; HttpOnly"); 152 0 152
if (o && *o && SvTRUE(*o)) sv_catpvs(out, "; HttpOnly"); 129 23 152
105  
106 169 return out;
107 }
108  
109 27 static SV *pk_build_cookie(pTHX_ SV *name, SV *value, HV *opts) {
110 27 return pk_build_cookie_ex(aTHX_ name, value, opts, 0);
111 }
112  
113 #endif /* PUNK_COOKIE_H */
« include/punk/punk_serve.h ↑ Summary include/punk/punk_asset.h »