xs/csp.xs

filter: Statementstmt 96.6% 58 / 60 Branchbran 64.5% 30 / 48 (1 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.4% 88 / 108 (1 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_xml.h ↑ Summary include/punk/punk_flash.h »
1 MODULE = Punk PACKAGE = Punk::Plugin::CSP
2  
3 PROTOTYPES: DISABLE
4  
5 # register($app, \%opts)
6 #
7 # Freezes the policy onto the app, where the compile step copies it into the
8 # compiled state and phd_effective reads it once per response. Every directive
9 # is checked HERE, at boot, because a value carrying a newline would split the
10 # response and a policy that is not the one that was written is worse than no
11 # policy at all.
12 void
13 register(self, app, opts = &PL_sv_undef)
14 SV *self
15 SV *app
16 SV *opts
17 CODE:
18 {
19 80 HV *h = app_hv(aTHX_ app);
20 80 HV *cfg = newHV();
21 PERL_UNUSED_VAR(self);
22 80 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 80 0 80
23 # uncoverable condition right count:1 class:ignore_covered_err
24 if (SvOK(opts)) {
25 80 ▶ HV *o;
Branch: 2 branches
BranchTrueFalseTotal
HV *o; 80 0 80
HV *o; 0 80 80
26 0 HE *he;
27 80 # uncoverable branch true
28 80 # uncoverable condition left count:1 class:ignore_covered_err
29 127 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 62 65 127
30 if (!(SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV))
31 62 ▶ croak("Punk::Plugin::CSP: options must be a hashref"); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk::Plugin::CSP: options must be a hashref"); /* # uncoverable statement */ 0 62 62
32 62 o = (HV *)SvRV(opts);
33 62 ▶ hv_iterinit(o);
Branch: 1 branch
BranchTrueFalseTotal
hv_iterinit(o); 10 52 62
34 while ((he = hv_iternext(o))) {
35 STRLEN kl;
36 10 ▶ const char *k = HePV(he, kl);
Branch: 2 branches
BranchTrueFalseTotal
const char *k = HePV(he, kl); 5 5 10
const char *k = HePV(he, kl); 5 0 5
37 5 SV *v = HeVAL(he);
38 if (strEQ(k, "report_only")) {
39 5 /* A hashref here is a second policy; its directives get
40 10 ▶ * the same boot-time check as the first one's. */
Branch: 1 branch
BranchTrueFalseTotal
* the same boot-time check as the first one's. */ 5 5 10
41 5 ▶ if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 0 5 5
42 HV *ro = (HV *)SvRV(v);
43 HE *rhe;
44 hv_iterinit(ro);
45 52 while ((rhe = hv_iternext(ro)))
46 47 pcsp_check_value(aTHX_ HePV(rhe, PL_na),
47 HeVAL(rhe));
48 }
49 65 }
50 else pcsp_check_value(aTHX_ k, v);
51 (void)hv_store(cfg, k, (I32)kl, newSVsv(v), 0);
52 }
53 }
54 (void)hv_stores(h, K_CSP, newRV_noinc((SV *)cfg));
55  
56 { /* The report endpoint, mounted by the PLUGIN.
57 *
58 * It is a public unauthenticated POST that takes a body from
59 * anybody, which is exactly why it belongs here rather than in
60 * the application: without limits it is a free log-flooding
61 * endpoint, and Punk has both limits already. Leaving them to the
62 * application means the one application that forgets has an open
63 65 * one.
64 65 ▶ *
Branch: 3 branches
BranchTrueFalseTotal
* 21 44 65
* 21 0 21
* 21 0 21
65 * Only when report_uri names a LOCAL path. An absolute URL is
66 21 * somebody else's collector and mounting a route for it would be
67 21 ▶ * wrong. */
Branch: 2 branches
BranchTrueFalseTotal
* wrong. */ 21 0 21
* wrong. */ 16 5 21
68 SV **ru = hv_fetchs(cfg, "report_uri", 0);
69 16 # uncoverable condition right count:2 class:ignore_covered_err
70 # uncoverable condition right count:3 class:ignore_covered_err
71 16 if (ru && *ru && SvOK(*ru)) {
72 16 STRLEN ul;
73 16 const char *up = SvPV_const(*ru, ul);
74 16 if (ul > 1 && up[0] == '/') { /* # uncoverable condition right count:1 class:ignore_covered_err */
75 16 SV *argv[5];
76 AV *cap = newAV();
77 SV *r;
78 av_push(cap, newSVsv(app));
79 16 argv[0] = sv_2mortal(newSVpvs("POST"));
80 16 argv[1] = sv_2mortal(newSVpvn(up, ul));
81 16 argv[2] = sv_2mortal(punk_closure(aTHX_ pcsp_report_cb, cap));
82 16 argv[3] = &PL_sv_undef;
83 { /* A violation report is small. Anything large is not
84 16 * a report, and parsing it first would be doing the
85 16 ▶ * work the limit exists to refuse. */
Branch: 1 branch
BranchTrueFalseTotal
* work the limit exists to refuse. */ 16 0 16
86 HV *o = newHV();
87 (void)hv_stores(o, K_MAX_BODY, newSViv(16384));
88 (void)hv_stores(o, K_SITEMAP, newSViv(0));
89 16 argv[4] = sv_2mortal(newRV_noinc((SV *)o));
90 16 }
91 16 r = pcx_call_meth(aTHX_ app, "route", argv, 5, 1);
92 16 # uncoverable branch false
93 16 # uncoverable condition right count:1 class:ignore_covered_err
94 16 if (r) SvREFCNT_dec(r);
95 16  
96 16 ▶ { /* and a rate limit, per address and low */
Branch: 1 branch
BranchTrueFalseTotal
{ /* and a rate limit, per address and low */ 16 0 16
97 SV *rl[6];
98 rl[0] = sv_2mortal(newSVpvs("for"));
99 rl[1] = sv_2mortal(newSVpvn(up, ul));
100 rl[2] = sv_2mortal(newSVpvs("limit"));
101 rl[3] = sv_2mortal(newSViv(60));
102 rl[4] = sv_2mortal(newSVpvs("window"));
103 rl[5] = sv_2mortal(newSViv(60));
104 r = pcx_call_meth(aTHX_ app, "rate_limit", rl, 6, 1);
105 # uncoverable branch false
106 65 # uncoverable condition right count:1 class:ignore_covered_err
107 65 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 65 0 65
108 }
109 }
110 65 }
111 65 }
112 65  
113 65 ▶ { /* $c->csp_nonce, for a handler building HTML or a JSON payload
Branch: 1 branch
BranchTrueFalseTotal
{ /* $c->csp_nonce, for a handler building HTML or a JSON payload 65 0 65
114 * that needs it. The same value the header carries and the same
115 * one a template renders, because all three read the one the env
116 * holds. */
117 CV *cv = get_cv("Punk::Plugin::CSP::_nonce", 0);
118 # uncoverable branch false class:ignore_covered_err
119 # uncoverable condition right count:1 class:ignore_covered_err
120 if (cv) {
121 SV *argv[2];
122 SV *r;
123 argv[0] = sv_2mortal(newSVpvs("csp_nonce"));
124 argv[1] = sv_2mortal(newRV_inc((SV *)cv));
125 r = pcx_call_meth(aTHX_ app, "helper", argv, 2, 1);
126 # uncoverable branch false
127 # uncoverable condition right count:1 class:ignore_covered_err
128 if (r) SvREFCNT_dec(r);
129 }
130 }
131 }
132  
133 # $c->csp_nonce - this request's nonce, minted on first ask.
134 #
135 # The same value the header carries, because both read the one the env holds:
136 # a nonce in the template that the header does not carry protects nothing and
137 # looks perfectly fine, which is the failure nobody notices.
138 SV *
139 _nonce(c)
140 SV *c
141 CODE:
142 {
143 5 AV *av = pcx_av(aTHX_ c);
144 5 ▶ 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 */ 5 0 5
145 5 ▶ # uncoverable branch false
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable branch false 5 0 5
# uncoverable branch false 5 0 5
# uncoverable branch false 5 0 5
146 5 # uncoverable condition right count:1 class:ignore_covered_err
147 # uncoverable condition right count:2 class:ignore_covered_err
148 0 # uncoverable condition right count:3 class:ignore_covered_err
149 if (e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV)
150 RETVAL = newSVsv(pcsp_nonce(aTHX_ (HV *)SvRV(e)));
151 else
152 RETVAL = newSV(0); /* # uncoverable statement */
153 }
154 OUTPUT:
155 RETVAL
156  
157 # What this process has been told about. A number nobody can see is a number
158 # nobody acts on, and a rising one during a report-only rollout is the whole
159 # reason to roll out that way.
160 void
161 stats(class = &PL_sv_undef)
162 SV *class
163 PPCODE:
164 {
165 PERL_UNUSED_VAR(class);
166 24 ▶ EXTEND(SP, 8);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 8); 0 24 24
167 24 mPUSHs(newSVpvs("reports")); mPUSHu(pcsp_n_reports);
168 24 mPUSHs(newSVpvs("malformed")); mPUSHu(pcsp_n_malformed);
169 24 mPUSHs(newSVpvs("scanned")); mPUSHu(pcsp_n_scanned);
170 24 mPUSHs(newSVpvs("warned")); mPUSHu(pcsp_n_warned);
171 24 XSRETURN(8);
172 }
« include/punk/punk_xml.h ↑ Summary include/punk/punk_flash.h »