include/punk/punk_csp.h

filter: Statementstmt 96.5% 222 / 230 Branchbran 63.6% 192 / 316 (25 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.4% 414 / 546 (25 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_obs.h ↑ Summary xs/stencil.xs »
1 #ifndef PUNK_CSP_H
2 #define PUNK_CSP_H
3  
4 /* Punk::Plugin::CSP - Content-Security-Policy with a per-request nonce.
5 *
6 * `headers` already sets a static CSP, and that is worth having. The policy
7 * that actually stops cross-site scripting is `script-src 'nonce-...'`, and a
8 * nonce is per request by definition: minted fresh, put in the header, and
9 * threaded into every <script> tag the templates emit.
10 *
11 * That thread is the whole plugin. A nonce in the header that no template
12 * carries blocks every script on the page - obvious, and someone fixes it. A
13 * nonce in the template that the header does not carry blocks nothing, looks
14 * perfectly fine, and nobody finds out. The second is why the two halves are
15 * tested against each other rather than separately.
16 *
17 * ---- the nonce ------------------------------------------------------------
18 *
19 * 16 bytes - 128 bits - as base64url without padding: 22 characters. The CSP
20 * grammar's base64-value accepts `-` and `_`, so base64url needs no
21 * translation, and it is a third shorter than hex for the same entropy on a
22 * header that goes out on every response.
23 *
24 * The requirement is UNPREDICTABILITY, not merely uniqueness, and that is a
25 * stricter thing: a unique but guessable nonce is one an attacker can put in
26 * their own injected <script nonce="...">, which is the entire attack the
27 * policy exists to stop. So a counter is disqualified here in a way it merely
28 * lost on taste for a request id, and so is anything derived from a clock, a
29 * pid or a request count.
30 *
31 * The bytes come from punk_entropy.h - `getentropy` where the build probe
32 * found it, a cached /dev/urandom descriptor otherwise, the same source
33 * punk_csrf.h already treats as cryptographic for a CSRF token. A nonce is a
34 * weaker requirement than that token, not a stronger one.
35 */
36  
37 #define PCSP_NONCE_BYTES 16
38 #define PCSP_ENV_KEY "punk.csp_nonce"
39  
40 /* This request's nonce, minted on first ask and kept in the env.
41 *
42 * The env rather than a C static, and not as a style choice: Punk dispatches
43 * asynchronously through Punk::Future, so a worker can have several requests
44 * in flight at once and a "current nonce" in a static would hand one
45 * request's nonce to another - invisibly, and only under load. For a nonce
46 * that is not an untidy log line, it is one user's page authorised by another
47 * user's value.
48 *
49 * The env rather than a context slot, because the header is written by
50 * phd_decorate, which sees the env and not the context.
51 */
52 3897 static SV *pcsp_nonce(pTHX_ HV *env) {
53 3897 SV **e = hv_fetchs(env, PCSP_ENV_KEY, 0);
54 unsigned char raw[PCSP_NONCE_BYTES];
55 SV *n;
56 /*
57 # uncoverable condition right count:2 class:ignore_covered_err
58 # uncoverable condition right count:3 class:ignore_covered_err */
59 3897 ▶ if (e && *e && SvOK(*e)) return *e;
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) return *e; 108 3789 3897
if (e && *e && SvOK(*e)) return *e; 108 0 108
if (e && *e && SvOK(*e)) return *e; 108 0 108
60 3789 pk_ent_take(aTHX_ raw, sizeof raw);
61 3789 n = pk_b64url(aTHX_ raw, sizeof raw);
62 3789 (void)hv_stores(env, PCSP_ENV_KEY, n);
63 3789 return n;
64 }
65  
66 /* Is CSP configured for the app this context belongs to?
67 *
68 * Read from the app hash rather than the compiled state because the view path
69 * has a context and not a state - and it has to be a per-app answer, since
70 * two Punk apps can share a process under Plack::Builder and only one of them
71 * may have asked for a policy. */
72 4338 static HV *pcsp_cfg_of(pTHX_ SV *c) {
73 AV *av;
74 SV *app, **e;
75 /*
76 # uncoverable branch true
77 # uncoverable condition left count:1 class:ignore_covered_err
78 # uncoverable condition left count:2 class:ignore_covered_err */
79 4338 ▶ if (!c || !SvROK(c)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!c || !SvROK(c)) return NULL; 4338 0 4338
if (!c || !SvROK(c)) return NULL; 0 4338 4338
80 4338 av = pcx_av(aTHX_ c);
81 /*
82 # uncoverable branch true
83 # uncoverable condition left count:1 class:ignore_covered_err */
84 4338 ▶ if (!av) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!av) return NULL; 0 4338 4338
85 4338 app = pcx_get(aTHX_ av, PCX_APP);
86 /*
87 # uncoverable branch true
88 # uncoverable condition left count:1 class:ignore_covered_err
89 # uncoverable condition left count:2 class:ignore_covered_err
90 # uncoverable condition left count:3 class:ignore_covered_err */
91 4338 ▶ if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; 4338 0 4338
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; 4338 0 4338
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; 0 4338 4338
92 4338 e = hv_fetchs((HV *)SvRV(app), K_CSP, 0);
93 192 ▶ return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 192 0 192
return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 192 0 192
return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) 192 0 192
94 4530 ▶ ? (HV *)SvRV(*e) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(*e) : NULL; 192 4146 4338
95 }
96  
97 /* Put this request's nonce into the variables a template is about to be
98 * rendered with, so `{% csp_nonce %}` works with nothing passed by the
99 * application.
100 *
101 * SET, not set-if-absent, and that is the whole reason this is safe: a
102 * handler that renders with a hashref it keeps between requests would
103 * otherwise carry the first request's nonce for ever, and a template nonce
104 * that does not match the header is the failure mode nobody notices - the
105 * page works, the policy is decorative.
106 */
107 2172 static void pcsp_bind_vars(pTHX_ SV *c, SV *data) {
108 2172 HV *cfg = pcsp_cfg_of(aTHX_ c);
109 AV *av;
110 SV *e;
111 2172 ▶ if (!cfg) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cfg) return; 2076 96 2172
112 /*
113 # uncoverable branch true
114 # uncoverable condition left count:1 class:ignore_covered_err
115 # uncoverable condition left count:2 class:ignore_covered_err
116 # uncoverable condition left count:3 class:ignore_covered_err */
117 96 ▶ if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; 96 0 96
if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; 96 0 96
if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; 0 96 96
118 96 av = pcx_av(aTHX_ c);
119 /*
120 # uncoverable branch true
121 # uncoverable condition left count:1 class:ignore_covered_err */
122 96 ▶ if (!av) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!av) return; 0 96 96
123 96 e = pcx_get(aTHX_ av, PCX_ENV);
124 /*
125 # uncoverable branch true
126 # uncoverable condition left count:1 class:ignore_covered_err
127 # uncoverable condition left count:2 class:ignore_covered_err
128 # uncoverable condition left count:3 class:ignore_covered_err */
129 96 ▶ if (!(e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV)) return; 96 0 96
if (!(e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV)) return; 96 0 96
if (!(e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV)) return; 0 96 96
130 96 (void)hv_stores((HV *)SvRV(data), "csp_nonce",
131 newSVsv(pcsp_nonce(aTHX_ (HV *)SvRV(e))));
132 }
133  
134 /* ---- the policy ---------------------------------------------------------- */
135  
136 /* The directives that are not negotiable, and why.
137 *
138 * `base-uri 'none'` is not optional and is the one people leave out: an
139 * injected <base href> rewrites every relative script URL, so a nonce on a
140 * relative <script src> would protect nothing at all.
141 *
142 * `object-src 'none'` closes the plugin-embedding bypasses a script-only
143 * policy leaves open.
144 *
145 * Both are overridable, because a policy nobody can adjust is a policy that
146 * gets removed rather than tuned - but they are present unless something is
147 * said, which is the direction that fails safe.
148 */
149 #define PCSP_DEF_DEFAULT "'self'"
150 #define PCSP_DEF_SCRIPT "'self'"
151 #define PCSP_DEF_OBJECT "'none'"
152 #define PCSP_DEF_BASE "'none'"
153  
154 /* A directive value from configuration is on its way into a response header.
155 *
156 * A CR or an LF in one splits the response - the same class as the header
157 * name check in Punk::Plugin::RequestId, and the same answer: croak at boot
158 * naming the directive, rather than sanitising into a shape nobody asked for
159 * and shipping a policy that is not the one that was written.
160 */
161 57 static void pcsp_check_value(pTHX_ const char *name, SV *v) {
162 STRLEN l, i;
163 const char *p;
164 /*
165 # uncoverable branch true
166 # uncoverable condition left count:1 class:ignore_covered_err
167 # uncoverable condition left count:2 class:ignore_covered_err */
168 57 ▶ if (!v || !SvOK(v)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!v || !SvOK(v)) return; 57 0 57
if (!v || !SvOK(v)) return; 0 57 57
169 57 p = SvPV_const(v, l);
170 809 ▶ for (i = 0; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) { 767 42 809
171 767 unsigned char ch = (unsigned char)p[i];
172 767 ▶ if (ch == '\r' || ch == '\n' || ch == '\0' || ch == ';') /* # uncoverable condition left count:3 class:ignore_covered_err */
Branch: 4 branches
BranchTrueFalseTotal
if (ch == '\r' || ch == '\n' || ch == '\0' || ch == ';') /* # uncoverable condition left count:3 class:ignore_covered_err */ 762 5 767
if (ch == '\r' || ch == '\n' || ch == '\0' || ch == ';') /* # uncoverable condition left count:3 class:ignore_covered_err */ 757 5 762
if (ch == '\r' || ch == '\n' || ch == '\0' || ch == ';') /* # uncoverable condition left count:3 class:ignore_covered_err */ 757 0 757
if (ch == '\r' || ch == '\n' || ch == '\0' || ch == ';') /* # uncoverable condition left count:3 class:ignore_covered_err */ 5 752 757
173 15 croak("Punk::Plugin::CSP: the %s directive contains a byte that "
174 "cannot appear in one (a newline, a NUL or a semicolon) - "
175 "give each directive its own option rather than joining "
176 "them", name);
177 }
178 }
179  
180 /* Append `name value` to the policy, with the separator when it is not the
181 * first. */
182 306 static void pcsp_add(pTHX_ SV *out, const char *name, const char *val,
183 STRLEN vlen) {
184 306 ▶ if (SvCUR(out)) sv_catpvs(out, "; ");
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(out)) sv_catpvs(out, "; "); 236 70 306
185 306 sv_catpv(out, name);
186 /*
187 # uncoverable branch false
188 # uncoverable condition right count:1 class:ignore_covered_err */
189 306 ▶ if (vlen) { sv_catpvs(out, " "); sv_catpvn(out, val, vlen); }
Branch: 1 branch
BranchTrueFalseTotal
if (vlen) { sv_catpvs(out, " "); sv_catpvn(out, val, vlen); } 306 0 306
190 306 }
191  
192 /* Build this request's policy.
193 *
194 * The nonce is spliced INTO script-src rather than appended to the finished
195 * string, so an application that configured its own script-src gets its
196 * sources and the nonce, in one directive, rather than a second script-src
197 * that browsers would ignore.
198 */
199 70 static SV *pcsp_policy_build(pTHX_ HV *cfg, const char *np, STRLEN nl) {
200 70 SV *out = newSVpvs("");
201 SV **v;
202  
203 70 ▶ v = cfg ? hv_fetchs(cfg, "default_src", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
v = cfg ? hv_fetchs(cfg, "default_src", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 70 0 70
204 0 ▶ { STRLEN l; const char *p = (v && *v && SvOK(*v)) /* # uncoverable statement class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
{ STRLEN l; const char *p = (v && *v && SvOK(*v)) /* # uncoverable statement class:ignore_covered_err */ 0 0 0
{ STRLEN l; const char *p = (v && *v && SvOK(*v)) /* # uncoverable statement class:ignore_covered_err */ 0 0 0
205 70 ▶ ? SvPV_const(*v, l) : (l = sizeof(PCSP_DEF_DEFAULT) - 1,
Branch: 1 branch
BranchTrueFalseTotal
? SvPV_const(*v, l) : (l = sizeof(PCSP_DEF_DEFAULT) - 1, 0 70 70
206 PCSP_DEF_DEFAULT);
207 /*
208 # uncoverable branch false
209 # uncoverable condition right count:1 class:ignore_covered_err */
210 70 ▶ if (l) pcsp_add(aTHX_ out, "default-src", p, l);
Branch: 1 branch
BranchTrueFalseTotal
if (l) pcsp_add(aTHX_ out, "default-src", p, l); 70 0 70
211 }
212  
213 { /* script-src: the configured sources, then this request's nonce */
214 70 SV *ss = newSVpvs("");
215 STRLEN l; const char *p;
216 70 ▶ v = cfg ? hv_fetchs(cfg, "script_src", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
v = cfg ? hv_fetchs(cfg, "script_src", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 70 0 70
217 30 ▶ p = (v && *v && SvOK(*v)) ? SvPV_const(*v, l)
Branch: 2 branches
BranchTrueFalseTotal
p = (v && *v && SvOK(*v)) ? SvPV_const(*v, l) 15 0 15
p = (v && *v && SvOK(*v)) ? SvPV_const(*v, l) 15 0 15
218 70 ▶ : (l = sizeof(PCSP_DEF_SCRIPT) - 1,
Branch: 1 branch
BranchTrueFalseTotal
: (l = sizeof(PCSP_DEF_SCRIPT) - 1, 15 55 70
219 PCSP_DEF_SCRIPT);
220 /*
221 # uncoverable branch false
222 # uncoverable condition right count:1 class:ignore_covered_err */
223 70 ▶ if (l) { sv_catpvn(ss, p, l); sv_catpvs(ss, " "); }
Branch: 1 branch
BranchTrueFalseTotal
if (l) { sv_catpvn(ss, p, l); sv_catpvs(ss, " "); } 70 0 70
224 70 sv_catpvs(ss, "'nonce-");
225 70 sv_catpvn(ss, np, nl);
226 70 sv_catpvs(ss, "'");
227 70 pcsp_add(aTHX_ out, "script-src", SvPVX(ss), SvCUR(ss));
228 70 SvREFCNT_dec(ss);
229 }
230  
231 { STRLEN l; const char *p;
232 70 ▶ v = cfg ? hv_fetchs(cfg, "object_src", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
v = cfg ? hv_fetchs(cfg, "object_src", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 70 0 70
233 0 ▶ p = (v && *v && SvOK(*v)) ? SvPV_const(*v, l) /* # uncoverable statement class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
p = (v && *v && SvOK(*v)) ? SvPV_const(*v, l) /* # uncoverable statement class:ignore_covered_err */ 0 0 0
p = (v && *v && SvOK(*v)) ? SvPV_const(*v, l) /* # uncoverable statement class:ignore_covered_err */ 0 0 0
234 70 ▶ : (l = sizeof(PCSP_DEF_OBJECT) - 1,
Branch: 1 branch
BranchTrueFalseTotal
: (l = sizeof(PCSP_DEF_OBJECT) - 1, 0 70 70
235 PCSP_DEF_OBJECT);
236 /*
237 # uncoverable branch false
238 # uncoverable condition right count:1 class:ignore_covered_err */
239 70 ▶ if (l) pcsp_add(aTHX_ out, "object-src", p, l);
Branch: 1 branch
BranchTrueFalseTotal
if (l) pcsp_add(aTHX_ out, "object-src", p, l); 70 0 70
240 }
241 { STRLEN l; const char *p;
242 70 ▶ v = cfg ? hv_fetchs(cfg, "base_uri", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
v = cfg ? hv_fetchs(cfg, "base_uri", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 70 0 70
243 0 ▶ p = (v && *v && SvOK(*v)) ? SvPV_const(*v, l) /* # uncoverable statement class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
p = (v && *v && SvOK(*v)) ? SvPV_const(*v, l) /* # uncoverable statement class:ignore_covered_err */ 0 0 0
p = (v && *v && SvOK(*v)) ? SvPV_const(*v, l) /* # uncoverable statement class:ignore_covered_err */ 0 0 0
244 70 ▶ : (l = sizeof(PCSP_DEF_BASE) - 1,
Branch: 1 branch
BranchTrueFalseTotal
: (l = sizeof(PCSP_DEF_BASE) - 1, 0 70 70
245 PCSP_DEF_BASE);
246 /*
247 # uncoverable branch false
248 # uncoverable condition right count:1 class:ignore_covered_err */
249 70 ▶ if (l) pcsp_add(aTHX_ out, "base-uri", p, l);
Branch: 1 branch
BranchTrueFalseTotal
if (l) pcsp_add(aTHX_ out, "base-uri", p, l); 70 0 70
250 }
251  
252 /* Anything else the application named, in the order it named them. */
253 /*
254 # uncoverable branch false
255 # uncoverable condition right count:1 class:ignore_covered_err */
256 70 ▶ if (cfg) {
Branch: 1 branch
BranchTrueFalseTotal
if (cfg) { 70 0 70
257 static const char *extra[] = { "style_src", "img_src", "connect_src",
258 "font_src", "frame_ancestors",
259 "form_action", "report_uri", NULL };
260 static const char *asname[] = { "style-src", "img-src", "connect-src",
261 "font-src", "frame-ancestors",
262 "form-action", "report-uri", NULL };
263 int i;
264 560 ▶ for (i = 0; extra[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; extra[i]; i++) { 490 70 560
265 490 SV **x = hv_fetch(cfg, extra[i], (I32)strlen(extra[i]), 0);
266 /*
267 # uncoverable condition right count:2 class:ignore_covered_err
268 # uncoverable condition right count:3 class:ignore_covered_err */
269 490 ▶ if (x && *x && SvOK(*x)) {
Branch: 3 branches
BranchTrueFalseTotal
if (x && *x && SvOK(*x)) { 26 464 490
if (x && *x && SvOK(*x)) { 26 0 26
if (x && *x && SvOK(*x)) { 26 0 26
270 STRLEN l;
271 26 const char *p = SvPV_const(*x, l);
272 /*
273 # uncoverable branch false
274 # uncoverable condition right count:1 class:ignore_covered_err */
275 26 ▶ if (l) pcsp_add(aTHX_ out, asname[i], p, l);
Branch: 1 branch
BranchTrueFalseTotal
if (l) pcsp_add(aTHX_ out, asname[i], p, l); 26 0 26
276 }
277 }
278 }
279  
280 70 return out;
281 }
282  
283 /* This request's policy: the configured directives frozen once as the text
284 * before and after the nonce, so a response is two copies and the nonce
285 * rather than eleven lookups and a dozen concatenations. The two halves are
286 * kept in the policy hash under private keys; it is not written after boot. */
287 #define PCSP_PRE_KEY "_prefix"
288 #define PCSP_SUF_KEY "_suffix"
289  
290 3794 static SV *pcsp_policy(pTHX_ HV *cfg, HV *env) {
291 3794 SV *nonce = pcsp_nonce(aTHX_ env);
292 STRLEN nl, pl, sl;
293 3794 const char *np = SvPV_const(nonce, nl);
294 3794 ▶ SV **pre = cfg ? hv_fetchs(cfg, PCSP_PRE_KEY, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **pre = cfg ? hv_fetchs(cfg, PCSP_PRE_KEY, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 3794 0 3794
295 3794 ▶ SV **suf = cfg ? hv_fetchs(cfg, PCSP_SUF_KEY, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **suf = cfg ? hv_fetchs(cfg, PCSP_SUF_KEY, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 3794 0 3794
296 SV *out;
297 /*
298 # uncoverable condition left count:2 class:ignore_covered_err
299 # uncoverable condition left count:3 class:ignore_covered_err
300 # uncoverable condition left count:4 class:ignore_covered_err */
301 3794 ▶ if (!(pre && *pre && suf && *suf)) {
Branch: 4 branches
BranchTrueFalseTotal
if (!(pre && *pre && suf && *suf)) { 3724 70 3794
if (!(pre && *pre && suf && *suf)) { 3724 0 3724
if (!(pre && *pre && suf && *suf)) { 3724 0 3724
if (!(pre && *pre && suf && *suf)) { 0 3724 3724
302 /* build once around a one-byte placeholder no directive can hold */
303 70 SV *full = pcsp_policy_build(aTHX_ cfg, "\x01", 1);
304 STRLEN fl;
305 70 const char *fp = SvPV_const(full, fl);
306 70 const char *at = (const char *)memchr(fp, 1, fl);
307 /*
308 # uncoverable branch true
309 # uncoverable condition left count:1 class:ignore_covered_err
310 # uncoverable condition left count:2 class:ignore_covered_err */
311 70 ▶ if (!at || !cfg) { /* no nonce slot: the text as built */
Branch: 2 branches
BranchTrueFalseTotal
if (!at || !cfg) { /* no nonce slot: the text as built */ 70 0 70
if (!at || !cfg) { /* no nonce slot: the text as built */ 0 70 70
312 0 SvREFCNT_dec(full); /* # uncoverable statement */
313 0 return pcsp_policy_build(aTHX_ cfg, np, nl); /* # uncoverable statement */
314 }
315 70 (void)hv_stores(cfg, PCSP_PRE_KEY, newSVpvn(fp, (STRLEN)(at - fp)));
316 70 (void)hv_stores(cfg, PCSP_SUF_KEY,
317 newSVpvn(at + 1, fl - (STRLEN)(at - fp) - 1));
318 70 SvREFCNT_dec(full);
319 70 pre = hv_fetchs(cfg, PCSP_PRE_KEY, 0);
320 70 suf = hv_fetchs(cfg, PCSP_SUF_KEY, 0);
321 }
322 3794 pl = SvCUR(*pre); sl = SvCUR(*suf);
323 3794 out = newSV(pl + nl + sl + 1);
324 3794 SvPOK_on(out);
325 3794 memcpy(SvPVX(out), SvPVX(*pre), pl);
326 3794 memcpy(SvPVX(out) + pl, np, nl);
327 3794 memcpy(SvPVX(out) + pl + nl, SvPVX(*suf), sl);
328 3794 SvPVX(out)[pl + nl + sl] = '\0';
329 3794 SvCUR_set(out, pl + nl + sl);
330 3794 return out;
331 }
332  
333 /* ---- the inline-handler check, in development ---------------------------- */
334  
335 /* Is this application in development?
336 *
337 * Scanning every response in production is a cost on the hot path to tell
338 * somebody something they cannot act on at the time. The counter below makes
339 * "it was not scanned" provable rather than merely unobserved. */
340 95 static int pcsp_dev(pTHX_ HV *cfg) {
341 95 ▶ SV **e = cfg ? hv_fetchs(cfg, "dev", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **e = cfg ? hv_fetchs(cfg, "dev", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 95 0 95
342 PERL_UNUSED_CONTEXT;
343 /*
344 # uncoverable condition right count:1 class:ignore_covered_err
345 # uncoverable condition right count:2 class:ignore_covered_err */
346 95 ▶ return (e && *e && SvTRUE(*e));
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvTRUE(*e)); 95 0 95
return (e && *e && SvTRUE(*e)); 95 0 95
return (e && *e && SvTRUE(*e)); 7 88 95
347 }
348  
349 static UV pcsp_n_scanned = 0; /* bodies scanned; 0 in production */
350 static UV pcsp_n_warned = 0;
351  
352 /* Templates already warned about, so the same page does not warn on every
353 * request. Noise is ignored, and being ignored is how the original problem
354 * happens. */
355 static HV *pcsp_seen = NULL;
356  
357 /* Case-insensitive needle search over n bytes. */
358 9 static const char *pcsp_find(const char *h, STRLEN hl, const char *needle) {
359 9 STRLEN nl = strlen(needle), i, j;
360 /*
361 # uncoverable branch true
362 # uncoverable condition left count:1 class:ignore_covered_err
363 # uncoverable condition left count:2 class:ignore_covered_err */
364 9 ▶ if (nl == 0 || hl < nl) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (nl == 0 || hl < nl) return NULL; 9 0 9
if (nl == 0 || hl < nl) return NULL; 0 9 9
365 129 ▶ for (i = 0; i + nl <= hl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + nl <= hl; i++) { 123 6 129
366 162 ▶ for (j = 0; j < nl; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < nl; j++) 159 3 162
367 159 ▶ if (toLOWER((U8)h[i + j]) != (unsigned char)needle[j]) break;
Branch: 2 branches
BranchTrueFalseTotal
if (toLOWER((U8)h[i + j]) != (unsigned char)needle[j]) break; 8 151 159
if (toLOWER((U8)h[i + j]) != (unsigned char)needle[j]) break; 120 39 159
368 123 ▶ if (j == nl) return h + i;
Branch: 1 branch
BranchTrueFalseTotal
if (j == nl) return h + i; 3 120 123
369 }
370 6 return NULL;
371 }
372  
373 /* An `on...=` attribute: `on`, at least one letter, then `=`. Matching the
374 * shape rather than a list of names catches onclick, onerror, onload and
375 * whatever was added to HTML last year. */
376 4 static int pcsp_find_handler(const char *b, STRLEN bl, char *out, size_t outn) {
377 STRLEN i;
378 111 ▶ for (i = 0; i + 3 < bl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 3 < bl; i++) { 108 3 111
379 STRLEN j;
380 /* must follow whitespace, or it is the tail of another word */
381 108 ▶ if (i && !isSPACE((U8)b[i - 1])) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (i && !isSPACE((U8)b[i - 1])) continue; 104 4 108
if (i && !isSPACE((U8)b[i - 1])) continue; 99 5 104
382 9 ▶ if (toLOWER((U8)b[i]) != 'o' || toLOWER((U8)b[i + 1]) != 'n') continue;
Branch: 6 branches
BranchTrueFalseTotal
if (toLOWER((U8)b[i]) != 'o' || toLOWER((U8)b[i + 1]) != 'n') continue; 0 9 9
if (toLOWER((U8)b[i]) != 'o' || toLOWER((U8)b[i + 1]) != 'n') continue; 0 0 0
if (toLOWER((U8)b[i]) != 'o' || toLOWER((U8)b[i + 1]) != 'n') continue; 1 8 9
if (toLOWER((U8)b[i]) != 'o' || toLOWER((U8)b[i + 1]) != 'n') continue; 0 1 1
if (toLOWER((U8)b[i]) != 'o' || toLOWER((U8)b[i + 1]) != 'n') continue; 0 0 0
if (toLOWER((U8)b[i]) != 'o' || toLOWER((U8)b[i + 1]) != 'n') continue; 0 1 1
383 1 j = i + 2;
384 6 ▶ while (j < bl && isALPHA((U8)b[j])) j++; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (j < bl && isALPHA((U8)b[j])) j++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 6 0 6
while (j < bl && isALPHA((U8)b[j])) j++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 5 1 6
385 /*
386 # uncoverable branch true
387 # uncoverable condition left count:1 class:ignore_covered_err
388 # uncoverable condition left count:2 class:ignore_covered_err
389 # uncoverable condition left count:3 class:ignore_covered_err */
390 1 ▶ if (j == i + 2 || j >= bl || b[j] != '=') continue;
Branch: 3 branches
BranchTrueFalseTotal
if (j == i + 2 || j >= bl || b[j] != '=') continue; 1 0 1
if (j == i + 2 || j >= bl || b[j] != '=') continue; 1 0 1
if (j == i + 2 || j >= bl || b[j] != '=') continue; 0 1 1
391 1 { size_t n = (size_t)(j - i);
392 /*
393 # uncoverable branch true
394 # uncoverable condition left count:1 class:ignore_covered_err */
395 1 ▶ if (n > outn - 1) n = outn - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (n > outn - 1) n = outn - 1; 0 1 1
396 1 Copy(b + i, out, n, char);
397 1 out[n] = '\0';
398 }
399 1 return 1;
400 }
401 3 return 0;
402 }
403  
404 2 static void pcsp_warn(pTHX_ SV *c, const char *fmt, SV *a, SV *b) {
405 2 dSP;
406 5 ▶ SV *msg = sv_2mortal(newSVpvf(fmt, SvPV_nolen(a), b ? SvPV_nolen(b) : ""));
Branch: 1 branch
BranchTrueFalseTotal
SV *msg = sv_2mortal(newSVpvf(fmt, SvPV_nolen(a), b ? SvPV_nolen(b) : "")); 1 1 2
407 2 ENTER; SAVETMPS;
408 2 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; 0 2 2
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; 0 2 2
409 /*
410 # uncoverable branch false
411 # uncoverable condition right count:1 class:ignore_covered_err */
412 2 ▶ if (call_method("log", G_SCALAR | G_EVAL) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (call_method("log", G_SCALAR | G_EVAL) > 0) { 2 0 2
413 2 SV *lg = SvREFCNT_inc(POPs);
414 2 PUTBACK;
415 /*
416 # uncoverable branch false
417 # uncoverable condition right count:1 class:ignore_covered_err
418 # uncoverable condition right count:2 class:ignore_covered_err */
419 2 ▶ if (!SvTRUE(ERRSV) && SvOK(lg)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && SvOK(lg)) { 2 0 2
if (!SvTRUE(ERRSV) && SvOK(lg)) { 2 0 2
if (!SvTRUE(ERRSV) && SvOK(lg)) { 2 0 2
420 2 ▶ PUSHMARK(SP); EXTEND(SP, 2);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 2); 0 2 2
PUSHMARK(SP); EXTEND(SP, 2); 0 2 2
421 2 PUSHs(lg); PUSHs(msg);
422 2 PUTBACK;
423 2 (void)call_method("warn", G_DISCARD | G_EVAL);
424 2 SPAGAIN;
425 }
426 2 SvREFCNT_dec(lg);
427 }
428 0 else PUTBACK; /* # uncoverable statement */
429 2 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 2 0 2
430 2 pcsp_n_warned++;
431 2 }
432  
433 /* Scan one rendered body. Development only, and it never touches the
434 * response: it has no authority over what is sent, because a checker that
435 * breaks the page is a checker that gets removed. */
436 2166 static void pcsp_scan(pTHX_ SV *c, SV *template, SV *bytes) {
437 2166 HV *cfg = pcsp_cfg_of(aTHX_ c);
438 SV **opt;
439 const char *b;
440 STRLEN bl;
441 char found[64];
442  
443 /*
444 # uncoverable condition left count:2 class:ignore_covered_err
445 # uncoverable condition left count:3 class:ignore_covered_err */
446 2259 ▶ if (!cfg || !bytes || !SvOK(bytes)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!cfg || !bytes || !SvOK(bytes)) return; 96 2070 2166
if (!cfg || !bytes || !SvOK(bytes)) return; 96 0 96
if (!cfg || !bytes || !SvOK(bytes)) return; 0 96 96
447 96 opt = hv_fetchs(cfg, "inline_check", 0);
448 /*
449 # uncoverable condition right count:2 class:ignore_covered_err
450 # uncoverable condition right count:3 class:ignore_covered_err
451 # uncoverable condition right count:4 class:ignore_covered_err */
452 96 ▶ if (opt && *opt && SvOK(*opt) && !SvTRUE(*opt)) return; /* turned off */
Branch: 4 branches
BranchTrueFalseTotal
if (opt && *opt && SvOK(*opt) && !SvTRUE(*opt)) return; /* turned off */ 1 95 96
if (opt && *opt && SvOK(*opt) && !SvTRUE(*opt)) return; /* turned off */ 1 0 1
if (opt && *opt && SvOK(*opt) && !SvTRUE(*opt)) return; /* turned off */ 1 0 1
if (opt && *opt && SvOK(*opt) && !SvTRUE(*opt)) return; /* turned off */ 1 0 1
453 95 ▶ if (!pcsp_dev(aTHX_ cfg)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!pcsp_dev(aTHX_ cfg)) return; 88 7 95
454  
455 7 b = SvPV_const(bytes, bl);
456 7 ▶ if (!bl) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!bl) return; 1 6 7
457 6 pcsp_n_scanned++;
458  
459 /* The escape hatch. A template that knowingly needs an inline handler
460 * says so in itself, which keeps the exemption next to the thing being
461 * exempted rather than in a config file that outlives the reason. A
462 * checker with no way out gets disabled wholesale instead. */
463 6 ▶ if (pcsp_find(b, bl, "csp-allow-inline")) return;
Branch: 1 branch
BranchTrueFalseTotal
if (pcsp_find(b, bl, "csp-allow-inline")) return; 1 5 6
464  
465 { /* once per template */
466 STRLEN tl;
467 10 ▶ const char *tp = template && SvOK(template) ? SvPV_const(template, tl)
Branch: 1 branch
BranchTrueFalseTotal
const char *tp = template && SvOK(template) ? SvPV_const(template, tl) 5 0 5
468 5 ▶ : (tl = 7, "unknown"); /* # uncoverable statement class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
: (tl = 7, "unknown"); /* # uncoverable statement class:ignore_covered_err */ 5 0 5
469 5 ▶ if (!pcsp_seen) pcsp_seen = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (!pcsp_seen) pcsp_seen = newHV(); 1 4 5
470 6 ▶ if (hv_exists(pcsp_seen, tp, (I32)tl)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(pcsp_seen, tp, (I32)tl)) return; 1 4 5
471  
472 4 ▶ if (pcsp_find_handler(b, bl, found, sizeof found)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pcsp_find_handler(b, bl, found, sizeof found)) { 1 3 4
473 1 (void)hv_store(pcsp_seen, tp, (I32)tl, &PL_sv_yes, 0);
474 1 pcsp_warn(aTHX_ c,
475 "Punk::Plugin::CSP: %s has an inline `%s=` handler, which a "
476 "script nonce does NOT cover - it needs 'unsafe-inline', and "
477 "adding that back defeats the policy for every page. Move the "
478 "handler into a nonced <script>, or put csp-allow-inline in "
479 "the template if it is deliberate.",
480 sv_2mortal(newSVpvn(tp, tl)),
481 sv_2mortal(newSVpvn(found, strlen(found))));
482 1 return;
483 }
484  
485 { /* Deferred to here, where it costs nothing extra now the body
486 * is in hand: a nonce in the markup that is not THIS request's is
487 * a page that came out of a cache. Every script on it is blocked,
488 * and the fault appears long after the change that caused it. */
489 3 const char *at = pcsp_find(b, bl, "nonce=\"");
490 3 ▶ if (at) {
Branch: 1 branch
BranchTrueFalseTotal
if (at) { 2 1 3
491 2 AV *av = pcx_av(aTHX_ c);
492 2 ▶ SV *e = av ? pcx_get(aTHX_ av, PCX_ENV) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *e = av ? pcx_get(aTHX_ av, PCX_ENV) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
493 /*
494 # uncoverable branch false
495 # uncoverable condition right count:1 class:ignore_covered_err
496 # uncoverable condition right count:2 class:ignore_covered_err
497 # uncoverable condition right count:3 class:ignore_covered_err */
498 2 ▶ if (e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) { 2 0 2
if (e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) { 2 0 2
if (e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) { 2 0 2
499 2 SV *mine = pcsp_nonce(aTHX_ (HV *)SvRV(e));
500 STRLEN ml;
501 2 const char *mp = SvPV_const(mine, ml);
502 2 const char *v = at + 7;
503 2 STRLEN room = bl - (STRLEN)(v - b);
504 2 ▶ if (!(room >= ml && memEQ(v, mp, ml))) { /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(room >= ml && memEQ(v, mp, ml))) { /* # uncoverable condition left count:1 class:ignore_covered_err */ 2 0 2
if (!(room >= ml && memEQ(v, mp, ml))) { /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 1 2
505 1 (void)hv_store(pcsp_seen, tp, (I32)tl, &PL_sv_yes, 0);
506 1 pcsp_warn(aTHX_ c,
507 "Punk::Plugin::CSP: %s rendered a nonce that is "
508 "not this request's%s - a cached page carries the "
509 "nonce it was rendered with, and every script on "
510 "it is blocked against a header carrying a "
511 "different one. A response that rendered a nonce "
512 "must not be cached.",
513 sv_2mortal(newSVpvn(tp, tl)), NULL);
514 }
515 }
516 }
517 }
518 }
519 }
520  
521 /* ---- the report endpoint ------------------------------------------------- */
522  
523 /* How many violations this process has been told about. A number nobody can
524 * see is a number nobody acts on, and a rising one during a report-only
525 * rollout is the whole signal you are rolling out for. */
526 static UV pcsp_n_reports = 0;
527 static UV pcsp_n_malformed = 0;
528  
529 /* The fields of a violation report worth keeping.
530 *
531 * EVERY ONE OF THESE IS REQUEST BYTES. `document-uri`, `blocked-uri`,
532 * `script-sample` and `referrer` all come from a client that can send
533 * anything, and they are on their way into a log line - the class this
534 * workspace has been bitten by three times.
535 *
536 * They are handed to the logger as a RECORD rather than pasted into a
537 * message, which is what makes that safe: punk_log.h renders a record's
538 * values through pl_logfmt_val, which quotes and escapes a newline rather
539 * than passing it through, "because a log line has to stay one line whatever
540 * it was handed". Reusing that is the point - a second escaper here would be
541 * a second thing to get wrong. */
542 static const char *PCSP_FIELDS[] = {
543 "document-uri", "referrer", "violated-directive", "effective-directive",
544 "blocked-uri", "source-file", "line-number", "script-sample",
545 "disposition", "status-code", NULL
546 };
547  
548 /* POST <report_uri>: take a violation report, log it, answer 204.
549 *
550 * cap = [ $app ]. Always 204, including for a body that made no sense:
551 * nothing reads the response, and an error status invites a retry from a
552 * browser that cannot fix what it sent.
553 */
554 XS_INTERNAL(pcsp_report_cb);
555 23 XS_INTERNAL(pcsp_report_cb) {
556 23 dXSARGS;
557 23 ▶ SV *c = items > 0 ? ST(0) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *c = items > 0 ? ST(0) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 23 0 23
558 23 HV *rec = newHV();
559 23 SV *json = NULL;
560 int i;
561 PERL_UNUSED_VAR(items);
562  
563 23 pcsp_n_reports++;
564  
565 /* $c->req->json - the body through File::Raw::JSON's ABI. A malformed
566 * body decodes to undef and is counted rather than argued with. */
567 {
568 23 dSP; int count;
569 23 ENTER; SAVETMPS;
570 23 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; 0 23 23
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; 0 23 23
571 23 count = call_method("req", G_SCALAR | G_EVAL);
572 23 SPAGAIN;
573 /*
574 # uncoverable branch false
575 # uncoverable condition right count:1 class:ignore_covered_err */
576 23 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 23 0 23
577 23 SV *req = SvREFCNT_inc(POPs);
578 23 PUTBACK;
579 /*
580 # uncoverable branch false
581 # uncoverable condition right count:1 class:ignore_covered_err
582 # uncoverable condition right count:2 class:ignore_covered_err */
583 23 ▶ if (!SvTRUE(ERRSV) && SvOK(req)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && SvOK(req)) { 23 0 23
if (!SvTRUE(ERRSV) && SvOK(req)) { 23 0 23
if (!SvTRUE(ERRSV) && SvOK(req)) { 23 0 23
584 23 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(req); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(req); PUTBACK; 0 23 23
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(req); PUTBACK; 0 23 23
585 23 count = call_method("json", G_SCALAR | G_EVAL);
586 23 SPAGAIN;
587 23 ▶ if (count > 0 && !SvTRUE(ERRSV)) json = SvREFCNT_inc(POPs); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (count > 0 && !SvTRUE(ERRSV)) json = SvREFCNT_inc(POPs); /* # uncoverable condition right count:1 class:ignore_covered_err */ 23 0 23
if (count > 0 && !SvTRUE(ERRSV)) json = SvREFCNT_inc(POPs); /* # uncoverable condition right count:1 class:ignore_covered_err */ 23 0 23
if (count > 0 && !SvTRUE(ERRSV)) json = SvREFCNT_inc(POPs); /* # uncoverable condition right count:1 class:ignore_covered_err */ 17 6 23
588 23 PUTBACK;
589 }
590 23 SvREFCNT_dec(req);
591 }
592 0 else PUTBACK; /* # uncoverable statement */
593 23 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 23 0 23
594 }
595  
596 /* A report arrives as { "csp-report": { ... } }. Newer browsers send the
597 * Reporting API shape instead; taking the inner object when it is there
598 * and the outer one otherwise handles both without pretending to
599 * understand either. */
600 {
601 23 HV *body = NULL;
602 /*
603 # uncoverable condition right count:2 class:ignore_covered_err
604 # uncoverable condition right count:3 class:ignore_covered_err */
605 23 ▶ if (json && SvROK(json) && SvTYPE(SvRV(json)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (json && SvROK(json) && SvTYPE(SvRV(json)) == SVt_PVHV) { 17 6 23
if (json && SvROK(json) && SvTYPE(SvRV(json)) == SVt_PVHV) { 17 0 17
if (json && SvROK(json) && SvTYPE(SvRV(json)) == SVt_PVHV) { 17 0 17
606 17 HV *top = (HV *)SvRV(json);
607 17 SV **inner = hv_fetchs(top, "csp-report", 0);
608 33 ▶ body = (inner && *inner && SvROK(*inner)
Branch: 2 branches
BranchTrueFalseTotal
body = (inner && *inner && SvROK(*inner) 16 0 16
body = (inner && *inner && SvROK(*inner) 16 0 16
609 16 ▶ && SvTYPE(SvRV(*inner)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*inner)) == SVt_PVHV) 16 0 16
610 33 ▶ ? (HV *)SvRV(*inner) : top;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(*inner) : top; 16 1 17
611 }
612 23 ▶ if (!body) pcsp_n_malformed++;
Branch: 1 branch
BranchTrueFalseTotal
if (!body) pcsp_n_malformed++; 6 17 23
613 193 ▶ for (i = 0; body && PCSP_FIELDS[i]; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; body && PCSP_FIELDS[i]; i++) { 187 6 193
for (i = 0; body && PCSP_FIELDS[i]; i++) { 170 17 187
614 170 I32 kl = (I32)strlen(PCSP_FIELDS[i]);
615 170 SV **v = hv_fetch(body, PCSP_FIELDS[i], kl, 0);
616 /*
617 # uncoverable condition right count:2 class:ignore_covered_err
618 # uncoverable condition right count:3 class:ignore_covered_err */
619 170 ▶ if (v && *v && SvOK(*v))
Branch: 3 branches
BranchTrueFalseTotal
if (v && *v && SvOK(*v)) 40 130 170
if (v && *v && SvOK(*v)) 40 0 40
if (v && *v && SvOK(*v)) 40 0 40
620 40 (void)hv_store(rec, PCSP_FIELDS[i], kl, newSVsv(*v), 0);
621 }
622 }
623 23 ▶ if (json) SvREFCNT_dec(json);
Branch: 1 branch
BranchTrueFalseTotal
if (json) SvREFCNT_dec(json); 17 6 23
624  
625 23 (void)hv_stores(rec, "message", newSVpvs("csp violation"));
626  
627 { /* $c->log->warn(\%rec) */
628 23 dSP; int count;
629 23 ENTER; SAVETMPS;
630 23 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; 0 23 23
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; 0 23 23
631 23 count = call_method("log", G_SCALAR | G_EVAL);
632 23 SPAGAIN;
633 /*
634 # uncoverable branch false
635 # uncoverable condition right count:1 class:ignore_covered_err */
636 23 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 23 0 23
637 23 SV *lg = SvREFCNT_inc(POPs);
638 23 PUTBACK;
639 /*
640 # uncoverable branch false
641 # uncoverable condition right count:1 class:ignore_covered_err
642 # uncoverable condition right count:2 class:ignore_covered_err */
643 23 ▶ if (!SvTRUE(ERRSV) && SvOK(lg)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && SvOK(lg)) { 23 0 23
if (!SvTRUE(ERRSV) && SvOK(lg)) { 23 0 23
if (!SvTRUE(ERRSV) && SvOK(lg)) { 23 0 23
644 23 ▶ PUSHMARK(SP); EXTEND(SP, 2);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 2); 0 23 23
PUSHMARK(SP); EXTEND(SP, 2); 0 23 23
645 23 PUSHs(lg);
646 23 PUSHs(sv_2mortal(newRV_inc((SV *)rec)));
647 23 PUTBACK;
648 23 (void)call_method("warn", G_DISCARD | G_EVAL);
649 23 SPAGAIN;
650 }
651 23 SvREFCNT_dec(lg);
652 }
653 0 else PUTBACK; /* # uncoverable statement */
654 23 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 23 0 23
655 }
656 23 SvREFCNT_dec((SV *)rec);
657  
658 { /* 204: no body, and no Content-Type for one */
659 23 AV *resp = newAV();
660 23 av_push(resp, newSViv(204));
661 23 av_push(resp, newRV_noinc((SV *)newAV()));
662 23 av_push(resp, newRV_noinc((SV *)newAV()));
663 23 ST(0) = sv_2mortal(newRV_noinc((SV *)resp));
664 }
665 23 XSRETURN(1);
666 }
667  
668 #endif /* PUNK_CSP_H */
« include/punk/punk_obs.h ↑ Summary xs/stencil.xs »