xs/csp.xs

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