include/punk/punk_cookie.h

filter: Statementstmt 100% 56 / 56 Branchbran 75.0% 77 / 104 (1 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.7% 133 / 160 (1 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/future.xs ↑ Summary include/punk/punk_wshandshake.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 175 static void pk_cookie_encode(pTHX_ SV *out, const char *v, STRLEN vl) {
21 static const char hex[] = "0123456789ABCDEF";
22 STRLEN i;
23 17282 ▶ for (i = 0; i < vl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < vl; i++) { 17107 175 17282
24 17107 unsigned char c = (unsigned char)v[i];
25 /*
26 # uncoverable condition right count:2 class:ignore_covered_err
27 # uncoverable condition right count:4 class:ignore_covered_err
28 # uncoverable condition left count:10 class:ignore_covered_err */
29 17107 ▶ if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
Branch: 4 branches
BranchTrueFalseTotal
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 14441 2666 17107
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 7137 7304 14441
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 7029 2774 9803
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 0 7029 7029
30 2774 ▶ || (c >= '0' && c <= '9')
Branch: 2 branches
BranchTrueFalseTotal
|| (c >= '0' && c <= '9') 2509 265 2774
|| (c >= '0' && c <= '9') 109 2400 2509
31 374 ▶ || c == '-' || c == '_' || c == '.' || c == '~')
Branch: 4 branches
BranchTrueFalseTotal
|| c == '-' || c == '_' || c == '.' || c == '~') 252 122 374
|| c == '-' || c == '_' || c == '.' || c == '~') 144 108 252
|| c == '-' || c == '_' || c == '.' || c == '~') 2 142 144
|| c == '-' || c == '_' || c == '.' || c == '~') 0 2 2
32 17105 sv_catpvn(out, (const char *)&c, 1);
33 else {
34 char e[3];
35 2 e[0] = '%'; e[1] = hex[c >> 4]; e[2] = hex[c & 0x0f];
36 2 sv_catpvn(out, e, 3);
37 }
38 }
39 175 }
40  
41 /* is `v` safe to write into a Set-Cookie verbatim? */
42 973 static int pk_cookie_safe(pTHX_ SV *v) {
43 STRLEN l, i;
44 973 const char *p = SvPV_const(v, l);
45 4743 ▶ for (i = 0; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) { 3783 960 4743
46 3783 unsigned char c = (unsigned char)p[i];
47 3783 ▶ if (c < 0x20 || c == 0x7f || c == ';') return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (c < 0x20 || c == 0x7f || c == ';') return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 3777 6 3783
if (c < 0x20 || c == 0x7f || c == ';') return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 3777 0 3777
if (c < 0x20 || c == 0x7f || c == ';') return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 7 3770 3777
48 }
49 960 return 1;
50 }
51  
52 540 static void pk_cookie_check(pTHX_ const char *what, SV *v) {
53 540 ▶ if (!pk_cookie_safe(aTHX_ v))
Branch: 1 branch
BranchTrueFalseTotal
if (!pk_cookie_safe(aTHX_ v)) 7 533 540
54 7 croak("Punk: a cookie %s may not contain ';' or a control "
55 "character", what);
56 533 }
57  
58 /* `secure_default` decides the Secure attribute when the options do not say:
59 * the session passes whether the request arrived over https. */
60 196 static SV *pk_build_cookie_ex(pTHX_ SV *name, SV *value, HV *opts,
61 int secure_default) {
62 SV *out;
63 STRLEN nl;
64 196 const char *n = SvPV_const(name, nl);
65 196 ▶ int deleting = !(value && SvOK(value)); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
int deleting = !(value && SvOK(value)); /* # uncoverable condition left count:1 class:ignore_covered_err */ 196 0 196
int deleting = !(value && SvOK(value)); /* # uncoverable condition left count:1 class:ignore_covered_err */ 12 184 196
66 SV **o;
67 static const char *const attrs[] = { "path", "domain", "samesite", NULL };
68 int i;
69 196 ▶ if (!nl) croak("Punk: a cookie needs a name");
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) croak("Punk: a cookie needs a name"); 1 195 196
70 195 pk_cookie_check(aTHX_ "name", name);
71 193 ▶ if (memchr(n, '=', nl))
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(n, '=', nl)) 1 192 193
72 1 croak("Punk: a cookie name may not contain '='");
73 757 ▶ for (i = 0; opts && attrs[i]; i++) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; opts && attrs[i]; i++) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 757 0 757
for (i = 0; opts && attrs[i]; i++) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 570 187 757
74 570 o = hv_fetch(opts, attrs[i], (I32)strlen(attrs[i]), 0);
75 /*
76 # uncoverable condition right count:2 class:ignore_covered_err
77 # uncoverable condition right count:3 class:ignore_covered_err */
78 570 ▶ 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); 345 225 570
if (o && *o && SvOK(*o)) pk_cookie_check(aTHX_ attrs[i], *o); 345 0 345
if (o && *o && SvOK(*o)) pk_cookie_check(aTHX_ attrs[i], *o); 345 0 345
79 }
80 187 out = newSVpvs("");
81 187 sv_catpvn(out, n, nl);
82 187 sv_catpvs(out, "=");
83 187 ▶ if (!deleting) {
Branch: 1 branch
BranchTrueFalseTotal
if (!deleting) { 175 12 187
84 STRLEN vl;
85 175 const char *v = SvPV_const(value, vl);
86 175 pk_cookie_encode(aTHX_ out, v, vl);
87 }
88 187 ▶ o = opts ? hv_fetchs(opts, "path", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
o = opts ? hv_fetchs(opts, "path", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 187 0 187
89 187 sv_catpvs(out, "; Path=");
90 /*
91 # uncoverable condition right count:2 class:ignore_covered_err
92 # uncoverable condition right count:3 class:ignore_covered_err */
93 187 ▶ 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, "/"); 170 17 187
if (o && *o && SvOK(*o)) sv_catsv(out, *o); else sv_catpvs(out, "/"); 170 0 170
if (o && *o && SvOK(*o)) sv_catsv(out, *o); else sv_catpvs(out, "/"); 170 0 170
94  
95 187 ▶ o = opts ? hv_fetchs(opts, "domain", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
o = opts ? hv_fetchs(opts, "domain", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 187 0 187
96 /*
97 # uncoverable branch true
98 # uncoverable condition left count:1 class:ignore_covered_err
99 # uncoverable condition left count:2 class:ignore_covered_err
100 # uncoverable condition right count:2 class:ignore_covered_err
101 # uncoverable condition left count:3 class:ignore_covered_err
102 # uncoverable condition right count:3 class:ignore_covered_err */
103 187 ▶ 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 187 187
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
104  
105 187 ▶ if (deleting)
Branch: 1 branch
BranchTrueFalseTotal
if (deleting) 12 175 187
106 12 sv_catpvs(out, "; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT");
107 else {
108 175 ▶ o = opts ? hv_fetchs(opts, "max_age", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
o = opts ? hv_fetchs(opts, "max_age", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 175 0 175
109 /*
110 # uncoverable condition right count:2 class:ignore_covered_err
111 # uncoverable condition right count:3 class:ignore_covered_err */
112 175 ▶ if (o && *o && SvOK(*o))
Branch: 3 branches
BranchTrueFalseTotal
if (o && *o && SvOK(*o)) 33 142 175
if (o && *o && SvOK(*o)) 33 0 33
if (o && *o && SvOK(*o)) 33 0 33
113 33 sv_catpvf(out, "; Max-Age=%" IVdf, (IV)SvIV(*o));
114 }
115  
116 187 ▶ o = opts ? hv_fetchs(opts, "samesite", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
o = opts ? hv_fetchs(opts, "samesite", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 187 0 187
117 /*
118 # uncoverable condition right count:2 class:ignore_covered_err
119 # uncoverable condition right count:3 class:ignore_covered_err */
120 187 ▶ 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); } 170 17 187
if (o && *o && SvOK(*o)) { sv_catpvs(out, "; SameSite="); sv_catsv(out, *o); } 170 0 170
if (o && *o && SvOK(*o)) { sv_catpvs(out, "; SameSite="); sv_catsv(out, *o); } 170 0 170
121  
122 187 ▶ o = opts ? hv_fetchs(opts, "secure", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
o = opts ? hv_fetchs(opts, "secure", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 187 0 187
123 /*
124 # uncoverable condition right count:2 class:ignore_covered_err
125 # uncoverable condition right count:3 class:ignore_covered_err */
126 187 ▶ if ((o && *o && SvOK(*o)) ? SvTRUE(*o) : secure_default)
Branch: 4 branches
BranchTrueFalseTotal
if ((o && *o && SvOK(*o)) ? SvTRUE(*o) : secure_default) 3 184 187
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 181 187
127 6 sv_catpvs(out, "; Secure");
128  
129 187 ▶ o = opts ? hv_fetchs(opts, "httponly", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
o = opts ? hv_fetchs(opts, "httponly", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 187 0 187
130 187 ▶ if (o && *o && SvTRUE(*o)) sv_catpvs(out, "; HttpOnly"); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (o && *o && SvTRUE(*o)) sv_catpvs(out, "; HttpOnly"); /* # uncoverable condition right count:2 class:ignore_covered_err */ 170 17 187
if (o && *o && SvTRUE(*o)) sv_catpvs(out, "; HttpOnly"); /* # uncoverable condition right count:2 class:ignore_covered_err */ 170 0 170
if (o && *o && SvTRUE(*o)) sv_catpvs(out, "; HttpOnly"); /* # uncoverable condition right count:2 class:ignore_covered_err */ 147 23 170
131  
132 187 return out;
133 }
134  
135 27 static SV *pk_build_cookie(pTHX_ SV *name, SV *value, HV *opts) {
136 27 return pk_build_cookie_ex(aTHX_ name, value, opts, 0);
137 }
138  
139 #endif /* PUNK_COOKIE_H */
« xs/future.xs ↑ Summary include/punk/punk_wshandshake.h »