| 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 |
3874 |
| static SV *pcsp_nonce(pTHX_ HV *env) { |
| 53 |
3874 |
| SV **e = hv_fetchs(env, PCSP_ENV_KEY, 0); |
| 54 |
|
| unsigned char raw[PCSP_NONCE_BYTES]; |
| 55 |
|
| SV *n; |
| 56 |
3874 |
▶ | if (e && *e && SvOK(*e)) return *e; |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && *e && SvOK(*e)) return *e; |
97 |
3777 |
3874 |
| if (e && *e && SvOK(*e)) return *e; |
97 |
0 |
97 |
| if (e && *e && SvOK(*e)) return *e; |
97 |
0 |
97 |
|
| 57 |
3777 |
| pk_ent_take(aTHX_ raw, sizeof raw); |
| 58 |
3777 |
| n = pk_b64url(aTHX_ raw, sizeof raw); |
| 59 |
3777 |
| (void)hv_stores(env, PCSP_ENV_KEY, n); |
| 60 |
3777 |
| return n; |
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
|
| /* Is CSP configured for the app this context belongs to? |
| 64 |
|
| * |
| 65 |
|
| * Read from the app hash rather than the compiled state because the view path |
| 66 |
|
| * has a context and not a state - and it has to be a per-app answer, since |
| 67 |
|
| * two Punk apps can share a process under Plack::Builder and only one of them |
| 68 |
|
| * may have asked for a policy. */ |
| 69 |
4318 |
| static HV *pcsp_cfg_of(pTHX_ SV *c) { |
| 70 |
|
| AV *av; |
| 71 |
|
| SV *app, **e; |
| 72 |
4318 |
▶ | if (!c || !SvROK(c)) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!c || !SvROK(c)) return NULL; |
4318 |
0 |
4318 |
| if (!c || !SvROK(c)) return NULL; |
0 |
4318 |
4318 |
|
| 73 |
4318 |
| av = pcx_av(aTHX_ c); |
| 74 |
4318 |
▶ | if (!av) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!av) return NULL; |
0 |
4318 |
4318 |
|
| 75 |
4318 |
| app = pcx_get(aTHX_ av, PCX_APP); |
| 76 |
4318 |
▶ | if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; |
4318 |
0 |
4318 |
| if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; |
4318 |
0 |
4318 |
| if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; |
0 |
4318 |
4318 |
|
| 77 |
4318 |
| e = hv_fetchs((HV *)SvRV(app), K_CSP, 0); |
| 78 |
174 |
▶ | return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) |
Branch: 3 branches
| Branch | True | False | Total |
| return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) |
174 |
0 |
174 |
| return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) |
174 |
0 |
174 |
| return (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV) |
174 |
0 |
174 |
|
| 79 |
4492 |
▶ | ? (HV *)SvRV(*e) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (HV *)SvRV(*e) : NULL; |
174 |
4144 |
4318 |
|
| 80 |
|
| } |
| 81 |
|
| |
| 82 |
|
| /* Put this request's nonce into the variables a template is about to be |
| 83 |
|
| * rendered with, so `{% csp_nonce %}` works with nothing passed by the |
| 84 |
|
| * application. |
| 85 |
|
| * |
| 86 |
|
| * SET, not set-if-absent, and that is the whole reason this is safe: a |
| 87 |
|
| * handler that renders with a hashref it keeps between requests would |
| 88 |
|
| * otherwise carry the first request's nonce for ever, and a template nonce |
| 89 |
|
| * that does not match the header is the failure mode nobody notices - the |
| 90 |
|
| * page works, the policy is decorative. |
| 91 |
|
| */ |
| 92 |
2162 |
| static void pcsp_bind_vars(pTHX_ SV *c, SV *data) { |
| 93 |
2162 |
| HV *cfg = pcsp_cfg_of(aTHX_ c); |
| 94 |
|
| AV *av; |
| 95 |
|
| SV *e; |
| 96 |
2162 |
▶ | if (!cfg) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cfg) return; |
2075 |
87 |
2162 |
|
| 97 |
87 |
▶ | if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; |
87 |
0 |
87 |
| if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; |
87 |
0 |
87 |
| if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; |
0 |
87 |
87 |
|
| 98 |
87 |
| av = pcx_av(aTHX_ c); |
| 99 |
87 |
▶ | if (!av) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!av) return; |
0 |
87 |
87 |
|
| 100 |
87 |
| e = pcx_get(aTHX_ av, PCX_ENV); |
| 101 |
87 |
▶ | if (!(e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV)) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV)) return; |
87 |
0 |
87 |
| if (!(e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV)) return; |
87 |
0 |
87 |
| if (!(e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV)) return; |
0 |
87 |
87 |
|
| 102 |
87 |
| (void)hv_stores((HV *)SvRV(data), "csp_nonce", |
| 103 |
|
| newSVsv(pcsp_nonce(aTHX_ (HV *)SvRV(e)))); |
| 104 |
|
| } |
| 105 |
|
| |
| 106 |
|
| /* ---- the policy ---------------------------------------------------------- */ |
| 107 |
|
| |
| 108 |
|
| /* The directives that are not negotiable, and why. |
| 109 |
|
| * |
| 110 |
|
| * `base-uri 'none'` is not optional and is the one people leave out: an |
| 111 |
|
| * injected <base href> rewrites every relative script URL, so a nonce on a |
| 112 |
|
| * relative <script src> would protect nothing at all. |
| 113 |
|
| * |
| 114 |
|
| * `object-src 'none'` closes the plugin-embedding bypasses a script-only |
| 115 |
|
| * policy leaves open. |
| 116 |
|
| * |
| 117 |
|
| * Both are overridable, because a policy nobody can adjust is a policy that |
| 118 |
|
| * gets removed rather than tuned - but they are present unless something is |
| 119 |
|
| * said, which is the direction that fails safe. |
| 120 |
|
| */ |
| 121 |
|
| #define PCSP_DEF_DEFAULT "'self'" |
| 122 |
|
| #define PCSP_DEF_SCRIPT "'self'" |
| 123 |
|
| #define PCSP_DEF_OBJECT "'none'" |
| 124 |
|
| #define PCSP_DEF_BASE "'none'" |
| 125 |
|
| |
| 126 |
|
| /* A directive value from configuration is on its way into a response header. |
| 127 |
|
| * |
| 128 |
|
| * A CR or an LF in one splits the response - the same class as the header |
| 129 |
|
| * name check in Punk::Plugin::RequestId, and the same answer: croak at boot |
| 130 |
|
| * naming the directive, rather than sanitising into a shape nobody asked for |
| 131 |
|
| * and shipping a policy that is not the one that was written. |
| 132 |
|
| */ |
| 133 |
55 |
| static void pcsp_check_value(pTHX_ const char *name, SV *v) { |
| 134 |
|
| STRLEN l, i; |
| 135 |
|
| const char *p; |
| 136 |
55 |
▶ | if (!v || !SvOK(v)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!v || !SvOK(v)) return; |
55 |
0 |
55 |
| if (!v || !SvOK(v)) return; |
0 |
55 |
55 |
|
| 137 |
55 |
| p = SvPV_const(v, l); |
| 138 |
795 |
▶ | for (i = 0; i < l; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < l; i++) { |
755 |
40 |
795 |
|
| 139 |
755 |
| unsigned char ch = (unsigned char)p[i]; |
| 140 |
755 |
▶ | if (ch == '\r' || ch == '\n' || ch == '\0' || ch == ';') |
Branch: 4 branches
| Branch | True | False | Total |
| if (ch == '\r' || ch == '\n' || ch == '\0' || ch == ';') |
750 |
5 |
755 |
| if (ch == '\r' || ch == '\n' || ch == '\0' || ch == ';') |
745 |
5 |
750 |
| if (ch == '\r' || ch == '\n' || ch == '\0' || ch == ';') |
745 |
0 |
745 |
| if (ch == '\r' || ch == '\n' || ch == '\0' || ch == ';') |
5 |
740 |
745 |
|
| 141 |
15 |
| croak("Punk::Plugin::CSP: the %s directive contains a byte that " |
| 142 |
|
| "cannot appear in one (a newline, a NUL or a semicolon) - " |
| 143 |
|
| "give each directive its own option rather than joining " |
| 144 |
|
| "them", name); |
| 145 |
|
| } |
| 146 |
|
| } |
| 147 |
|
| |
| 148 |
|
| /* Append `name value` to the policy, with the separator when it is not the |
| 149 |
|
| * first. */ |
| 150 |
293 |
| static void pcsp_add(pTHX_ SV *out, const char *name, const char *val, |
| 151 |
|
| STRLEN vlen) { |
| 152 |
293 |
▶ | if (SvCUR(out)) sv_catpvs(out, "; "); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvCUR(out)) sv_catpvs(out, "; "); |
226 |
67 |
293 |
|
| 153 |
293 |
| sv_catpv(out, name); |
| 154 |
293 |
▶ | if (vlen) { sv_catpvs(out, " "); sv_catpvn(out, val, vlen); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (vlen) { sv_catpvs(out, " "); sv_catpvn(out, val, vlen); } |
293 |
0 |
293 |
|
| 155 |
293 |
| } |
| 156 |
|
| |
| 157 |
|
| /* Build this request's policy. |
| 158 |
|
| * |
| 159 |
|
| * The nonce is spliced INTO script-src rather than appended to the finished |
| 160 |
|
| * string, so an application that configured its own script-src gets its |
| 161 |
|
| * sources and the nonce, in one directive, rather than a second script-src |
| 162 |
|
| * that browsers would ignore. |
| 163 |
|
| */ |
| 164 |
67 |
| static SV *pcsp_policy_build(pTHX_ HV *cfg, const char *np, STRLEN nl) { |
| 165 |
67 |
| SV *out = newSVpvs(""); |
| 166 |
|
| SV **v; |
| 167 |
|
| |
| 168 |
67 |
▶ | v = cfg ? hv_fetchs(cfg, "default_src", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| v = cfg ? hv_fetchs(cfg, "default_src", 0) : NULL; |
67 |
0 |
67 |
|
| 169 |
0 |
▶ | { STRLEN l; const char *p = (v && *v && SvOK(*v)) |
Branch: 2 branches
| Branch | True | False | Total |
| { STRLEN l; const char *p = (v && *v && SvOK(*v)) |
0 |
0 |
0 |
| { STRLEN l; const char *p = (v && *v && SvOK(*v)) |
0 |
0 |
0 |
|
| 170 |
67 |
▶ | ? SvPV_const(*v, l) : (l = sizeof(PCSP_DEF_DEFAULT) - 1, |
Branch: 1 branch
| Branch | True | False | Total |
| ? SvPV_const(*v, l) : (l = sizeof(PCSP_DEF_DEFAULT) - 1, |
0 |
67 |
67 |
|
| 171 |
|
| PCSP_DEF_DEFAULT); |
| 172 |
67 |
▶ | if (l) pcsp_add(aTHX_ out, "default-src", p, l); |
Branch: 1 branch
| Branch | True | False | Total |
| if (l) pcsp_add(aTHX_ out, "default-src", p, l); |
67 |
0 |
67 |
|
| 173 |
|
| } |
| 174 |
|
| |
| 175 |
|
| { /* script-src: the configured sources, then this request's nonce */ |
| 176 |
67 |
| SV *ss = newSVpvs(""); |
| 177 |
|
| STRLEN l; const char *p; |
| 178 |
67 |
▶ | v = cfg ? hv_fetchs(cfg, "script_src", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| v = cfg ? hv_fetchs(cfg, "script_src", 0) : NULL; |
67 |
0 |
67 |
|
| 179 |
30 |
▶ | p = (v && *v && SvOK(*v)) ? SvPV_const(*v, l) |
Branch: 2 branches
| Branch | True | False | Total |
| p = (v && *v && SvOK(*v)) ? SvPV_const(*v, l) |
15 |
0 |
15 |
| p = (v && *v && SvOK(*v)) ? SvPV_const(*v, l) |
15 |
0 |
15 |
|
| 180 |
67 |
▶ | : (l = sizeof(PCSP_DEF_SCRIPT) - 1, |
Branch: 1 branch
| Branch | True | False | Total |
| : (l = sizeof(PCSP_DEF_SCRIPT) - 1, |
15 |
52 |
67 |
|
| 181 |
|
| PCSP_DEF_SCRIPT); |
| 182 |
67 |
▶ | if (l) { sv_catpvn(ss, p, l); sv_catpvs(ss, " "); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (l) { sv_catpvn(ss, p, l); sv_catpvs(ss, " "); } |
67 |
0 |
67 |
|
| 183 |
67 |
| sv_catpvs(ss, "'nonce-"); |
| 184 |
67 |
| sv_catpvn(ss, np, nl); |
| 185 |
67 |
| sv_catpvs(ss, "'"); |
| 186 |
67 |
| pcsp_add(aTHX_ out, "script-src", SvPVX(ss), SvCUR(ss)); |
| 187 |
67 |
| SvREFCNT_dec(ss); |
| 188 |
|
| } |
| 189 |
|
| |
| 190 |
|
| { STRLEN l; const char *p; |
| 191 |
67 |
▶ | v = cfg ? hv_fetchs(cfg, "object_src", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| v = cfg ? hv_fetchs(cfg, "object_src", 0) : NULL; |
67 |
0 |
67 |
|
| 192 |
0 |
▶ | p = (v && *v && SvOK(*v)) ? SvPV_const(*v, l) |
Branch: 2 branches
| Branch | True | False | Total |
| p = (v && *v && SvOK(*v)) ? SvPV_const(*v, l) |
0 |
0 |
0 |
| p = (v && *v && SvOK(*v)) ? SvPV_const(*v, l) |
0 |
0 |
0 |
|
| 193 |
67 |
▶ | : (l = sizeof(PCSP_DEF_OBJECT) - 1, |
Branch: 1 branch
| Branch | True | False | Total |
| : (l = sizeof(PCSP_DEF_OBJECT) - 1, |
0 |
67 |
67 |
|
| 194 |
|
| PCSP_DEF_OBJECT); |
| 195 |
67 |
▶ | if (l) pcsp_add(aTHX_ out, "object-src", p, l); |
Branch: 1 branch
| Branch | True | False | Total |
| if (l) pcsp_add(aTHX_ out, "object-src", p, l); |
67 |
0 |
67 |
|
| 196 |
|
| } |
| 197 |
|
| { STRLEN l; const char *p; |
| 198 |
67 |
▶ | v = cfg ? hv_fetchs(cfg, "base_uri", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| v = cfg ? hv_fetchs(cfg, "base_uri", 0) : NULL; |
67 |
0 |
67 |
|
| 199 |
0 |
▶ | p = (v && *v && SvOK(*v)) ? SvPV_const(*v, l) |
Branch: 2 branches
| Branch | True | False | Total |
| p = (v && *v && SvOK(*v)) ? SvPV_const(*v, l) |
0 |
0 |
0 |
| p = (v && *v && SvOK(*v)) ? SvPV_const(*v, l) |
0 |
0 |
0 |
|
| 200 |
67 |
▶ | : (l = sizeof(PCSP_DEF_BASE) - 1, |
Branch: 1 branch
| Branch | True | False | Total |
| : (l = sizeof(PCSP_DEF_BASE) - 1, |
0 |
67 |
67 |
|
| 201 |
|
| PCSP_DEF_BASE); |
| 202 |
67 |
▶ | if (l) pcsp_add(aTHX_ out, "base-uri", p, l); |
Branch: 1 branch
| Branch | True | False | Total |
| if (l) pcsp_add(aTHX_ out, "base-uri", p, l); |
67 |
0 |
67 |
|
| 203 |
|
| } |
| 204 |
|
| |
| 205 |
|
| /* Anything else the application named, in the order it named them. */ |
| 206 |
67 |
▶ | if (cfg) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cfg) { |
67 |
0 |
67 |
|
| 207 |
|
| static const char *extra[] = { "style_src", "img_src", "connect_src", |
| 208 |
|
| "font_src", "frame_ancestors", |
| 209 |
|
| "form_action", "report_uri", NULL }; |
| 210 |
|
| static const char *asname[] = { "style-src", "img-src", "connect-src", |
| 211 |
|
| "font-src", "frame-ancestors", |
| 212 |
|
| "form-action", "report-uri", NULL }; |
| 213 |
|
| int i; |
| 214 |
536 |
▶ | for (i = 0; extra[i]; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; extra[i]; i++) { |
469 |
67 |
536 |
|
| 215 |
469 |
| SV **x = hv_fetch(cfg, extra[i], (I32)strlen(extra[i]), 0); |
| 216 |
469 |
▶ | if (x && *x && SvOK(*x)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (x && *x && SvOK(*x)) { |
25 |
444 |
469 |
| if (x && *x && SvOK(*x)) { |
25 |
0 |
25 |
| if (x && *x && SvOK(*x)) { |
25 |
0 |
25 |
|
| 217 |
|
| STRLEN l; |
| 218 |
25 |
| const char *p = SvPV_const(*x, l); |
| 219 |
25 |
▶ | if (l) pcsp_add(aTHX_ out, asname[i], p, l); |
Branch: 1 branch
| Branch | True | False | Total |
| if (l) pcsp_add(aTHX_ out, asname[i], p, l); |
25 |
0 |
25 |
|
| 220 |
|
| } |
| 221 |
|
| } |
| 222 |
|
| } |
| 223 |
|
| |
| 224 |
67 |
| return out; |
| 225 |
|
| } |
| 226 |
|
| |
| 227 |
|
| /* This request's policy: the configured directives frozen once as the text |
| 228 |
|
| * before and after the nonce, so a response is two copies and the nonce |
| 229 |
|
| * rather than eleven lookups and a dozen concatenations. The two halves are |
| 230 |
|
| * kept in the policy hash under private keys; it is not written after boot. */ |
| 231 |
|
| #define PCSP_PRE_KEY "_prefix" |
| 232 |
|
| #define PCSP_SUF_KEY "_suffix" |
| 233 |
|
| |
| 234 |
3782 |
| static SV *pcsp_policy(pTHX_ HV *cfg, HV *env) { |
| 235 |
3782 |
| SV *nonce = pcsp_nonce(aTHX_ env); |
| 236 |
|
| STRLEN nl, pl, sl; |
| 237 |
3782 |
| const char *np = SvPV_const(nonce, nl); |
| 238 |
3782 |
▶ | SV **pre = cfg ? hv_fetchs(cfg, PCSP_PRE_KEY, 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **pre = cfg ? hv_fetchs(cfg, PCSP_PRE_KEY, 0) : NULL; |
3782 |
0 |
3782 |
|
| 239 |
3782 |
▶ | SV **suf = cfg ? hv_fetchs(cfg, PCSP_SUF_KEY, 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **suf = cfg ? hv_fetchs(cfg, PCSP_SUF_KEY, 0) : NULL; |
3782 |
0 |
3782 |
|
| 240 |
|
| SV *out; |
| 241 |
3782 |
▶ | if (!(pre && *pre && suf && *suf)) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(pre && *pre && suf && *suf)) { |
3715 |
67 |
3782 |
| if (!(pre && *pre && suf && *suf)) { |
3715 |
0 |
3715 |
| if (!(pre && *pre && suf && *suf)) { |
3715 |
0 |
3715 |
| if (!(pre && *pre && suf && *suf)) { |
0 |
3715 |
3715 |
|
| 242 |
|
| /* build once around a one-byte placeholder no directive can hold */ |
| 243 |
67 |
| SV *full = pcsp_policy_build(aTHX_ cfg, "\x01", 1); |
| 244 |
|
| STRLEN fl; |
| 245 |
67 |
| const char *fp = SvPV_const(full, fl); |
| 246 |
67 |
| const char *at = (const char *)memchr(fp, 1, fl); |
| 247 |
67 |
▶ | if (!at || !cfg) { /* no nonce slot: the text as built */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!at || !cfg) { /* no nonce slot: the text as built */ |
67 |
0 |
67 |
| if (!at || !cfg) { /* no nonce slot: the text as built */ |
0 |
67 |
67 |
|
| 248 |
0 |
| SvREFCNT_dec(full); |
| 249 |
0 |
| return pcsp_policy_build(aTHX_ cfg, np, nl); |
| 250 |
|
| } |
| 251 |
67 |
| (void)hv_stores(cfg, PCSP_PRE_KEY, newSVpvn(fp, (STRLEN)(at - fp))); |
| 252 |
67 |
| (void)hv_stores(cfg, PCSP_SUF_KEY, |
| 253 |
|
| newSVpvn(at + 1, fl - (STRLEN)(at - fp) - 1)); |
| 254 |
67 |
| SvREFCNT_dec(full); |
| 255 |
67 |
| pre = hv_fetchs(cfg, PCSP_PRE_KEY, 0); |
| 256 |
67 |
| suf = hv_fetchs(cfg, PCSP_SUF_KEY, 0); |
| 257 |
|
| } |
| 258 |
3782 |
| pl = SvCUR(*pre); sl = SvCUR(*suf); |
| 259 |
3782 |
| out = newSV(pl + nl + sl + 1); |
| 260 |
3782 |
| SvPOK_on(out); |
| 261 |
3782 |
| memcpy(SvPVX(out), SvPVX(*pre), pl); |
| 262 |
3782 |
| memcpy(SvPVX(out) + pl, np, nl); |
| 263 |
3782 |
| memcpy(SvPVX(out) + pl + nl, SvPVX(*suf), sl); |
| 264 |
3782 |
| SvPVX(out)[pl + nl + sl] = '\0'; |
| 265 |
3782 |
| SvCUR_set(out, pl + nl + sl); |
| 266 |
3782 |
| return out; |
| 267 |
|
| } |
| 268 |
|
| |
| 269 |
|
| /* ---- the inline-handler check, in development ---------------------------- */ |
| 270 |
|
| |
| 271 |
|
| /* Is this application in development? |
| 272 |
|
| * |
| 273 |
|
| * Scanning every response in production is a cost on the hot path to tell |
| 274 |
|
| * somebody something they cannot act on at the time. The counter below makes |
| 275 |
|
| * "it was not scanned" provable rather than merely unobserved. */ |
| 276 |
87 |
| static int pcsp_dev(pTHX_ HV *cfg) { |
| 277 |
87 |
▶ | SV **e = cfg ? hv_fetchs(cfg, "dev", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **e = cfg ? hv_fetchs(cfg, "dev", 0) : NULL; |
87 |
0 |
87 |
|
| 278 |
|
| PERL_UNUSED_CONTEXT; |
| 279 |
87 |
▶ | return (e && *e && SvTRUE(*e)); |
Branch: 3 branches
| Branch | True | False | Total |
| return (e && *e && SvTRUE(*e)); |
87 |
0 |
87 |
| return (e && *e && SvTRUE(*e)); |
87 |
0 |
87 |
| return (e && *e && SvTRUE(*e)); |
0 |
87 |
87 |
|
| 280 |
|
| } |
| 281 |
|
| |
| 282 |
|
| static UV pcsp_n_scanned = 0; /* bodies scanned; 0 in production */ |
| 283 |
|
| static UV pcsp_n_warned = 0; |
| 284 |
|
| |
| 285 |
|
| /* Templates already warned about, so the same page does not warn on every |
| 286 |
|
| * request. Noise is ignored, and being ignored is how the original problem |
| 287 |
|
| * happens. */ |
| 288 |
|
| static HV *pcsp_seen = NULL; |
| 289 |
|
| |
| 290 |
|
| /* Case-insensitive needle search over n bytes. */ |
| 291 |
0 |
| static const char *pcsp_find(const char *h, STRLEN hl, const char *needle) { |
| 292 |
0 |
| STRLEN nl = strlen(needle), i, j; |
| 293 |
0 |
▶ | if (nl == 0 || hl < nl) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (nl == 0 || hl < nl) return NULL; |
0 |
0 |
0 |
| if (nl == 0 || hl < nl) return NULL; |
0 |
0 |
0 |
|
| 294 |
0 |
▶ | for (i = 0; i + nl <= hl; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + nl <= hl; i++) { |
0 |
0 |
0 |
|
| 295 |
0 |
▶ | for (j = 0; j < nl; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < nl; j++) |
0 |
0 |
0 |
|
| 296 |
0 |
▶ | if (toLOWER((U8)h[i + j]) != (unsigned char)needle[j]) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (toLOWER((U8)h[i + j]) != (unsigned char)needle[j]) break; |
0 |
0 |
0 |
| if (toLOWER((U8)h[i + j]) != (unsigned char)needle[j]) break; |
0 |
0 |
0 |
|
| 297 |
0 |
▶ | if (j == nl) return h + i; |
Branch: 1 branch
| Branch | True | False | Total |
| if (j == nl) return h + i; |
0 |
0 |
0 |
|
| 298 |
|
| } |
| 299 |
0 |
| return NULL; |
| 300 |
|
| } |
| 301 |
|
| |
| 302 |
|
| /* An `on...=` attribute: `on`, at least one letter, then `=`. Matching the |
| 303 |
|
| * shape rather than a list of names catches onclick, onerror, onload and |
| 304 |
|
| * whatever was added to HTML last year. */ |
| 305 |
0 |
| static int pcsp_find_handler(const char *b, STRLEN bl, char *out, size_t outn) { |
| 306 |
|
| STRLEN i; |
| 307 |
0 |
▶ | for (i = 0; i + 3 < bl; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 3 < bl; i++) { |
0 |
0 |
0 |
|
| 308 |
|
| STRLEN j; |
| 309 |
|
| /* must follow whitespace, or it is the tail of another word */ |
| 310 |
0 |
▶ | if (i && !isSPACE((U8)b[i - 1])) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (i && !isSPACE((U8)b[i - 1])) continue; |
0 |
0 |
0 |
| if (i && !isSPACE((U8)b[i - 1])) continue; |
0 |
0 |
0 |
|
| 311 |
0 |
▶ | if (toLOWER((U8)b[i]) != 'o' || toLOWER((U8)b[i + 1]) != 'n') continue; |
Branch: 6 branches
| Branch | True | False | Total |
| 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 |
0 |
0 |
| 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 |
0 |
0 |
| 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 |
0 |
0 |
|
| 312 |
0 |
| j = i + 2; |
| 313 |
0 |
▶ | while (j < bl && isALPHA((U8)b[j])) j++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (j < bl && isALPHA((U8)b[j])) j++; |
0 |
0 |
0 |
| while (j < bl && isALPHA((U8)b[j])) j++; |
0 |
0 |
0 |
|
| 314 |
0 |
▶ | if (j == i + 2 || j >= bl || b[j] != '=') continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (j == i + 2 || j >= bl || b[j] != '=') continue; |
0 |
0 |
0 |
| if (j == i + 2 || j >= bl || b[j] != '=') continue; |
0 |
0 |
0 |
| if (j == i + 2 || j >= bl || b[j] != '=') continue; |
0 |
0 |
0 |
|
| 315 |
0 |
| { size_t n = (size_t)(j - i); |
| 316 |
0 |
▶ | if (n > outn - 1) n = outn - 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > outn - 1) n = outn - 1; |
0 |
0 |
0 |
|
| 317 |
0 |
| Copy(b + i, out, n, char); |
| 318 |
0 |
| out[n] = '\0'; |
| 319 |
|
| } |
| 320 |
0 |
| return 1; |
| 321 |
|
| } |
| 322 |
0 |
| return 0; |
| 323 |
|
| } |
| 324 |
|
| |
| 325 |
0 |
| static void pcsp_warn(pTHX_ SV *c, const char *fmt, SV *a, SV *b) { |
| 326 |
0 |
| dSP; |
| 327 |
0 |
▶ | SV *msg = sv_2mortal(newSVpvf(fmt, SvPV_nolen(a), b ? SvPV_nolen(b) : "")); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *msg = sv_2mortal(newSVpvf(fmt, SvPV_nolen(a), b ? SvPV_nolen(b) : "")); |
0 |
0 |
0 |
|
| 328 |
0 |
| ENTER; SAVETMPS; |
| 329 |
0 |
▶ | PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
0 |
0 |
0 |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
0 |
0 |
0 |
|
| 330 |
0 |
▶ | if (call_method("log", G_SCALAR | G_EVAL) > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (call_method("log", G_SCALAR | G_EVAL) > 0) { |
0 |
0 |
0 |
|
| 331 |
0 |
| SV *lg = SvREFCNT_inc(POPs); |
| 332 |
0 |
| PUTBACK; |
| 333 |
0 |
▶ | if (!SvTRUE(ERRSV) && SvOK(lg)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV) && SvOK(lg)) { |
0 |
0 |
0 |
| if (!SvTRUE(ERRSV) && SvOK(lg)) { |
0 |
0 |
0 |
| if (!SvTRUE(ERRSV) && SvOK(lg)) { |
0 |
0 |
0 |
|
| 334 |
0 |
▶ | PUSHMARK(SP); EXTEND(SP, 2); |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 2); |
0 |
0 |
0 |
| PUSHMARK(SP); EXTEND(SP, 2); |
0 |
0 |
0 |
|
| 335 |
0 |
| PUSHs(lg); PUSHs(msg); |
| 336 |
0 |
| PUTBACK; |
| 337 |
0 |
| (void)call_method("warn", G_DISCARD | G_EVAL); |
| 338 |
0 |
| SPAGAIN; |
| 339 |
|
| } |
| 340 |
0 |
| SvREFCNT_dec(lg); |
| 341 |
|
| } |
| 342 |
0 |
| else PUTBACK; |
| 343 |
0 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 344 |
0 |
| pcsp_n_warned++; |
| 345 |
0 |
| } |
| 346 |
|
| |
| 347 |
|
| /* Scan one rendered body. Development only, and it never touches the |
| 348 |
|
| * response: it has no authority over what is sent, because a checker that |
| 349 |
|
| * breaks the page is a checker that gets removed. */ |
| 350 |
2156 |
| static void pcsp_scan(pTHX_ SV *c, SV *template, SV *bytes) { |
| 351 |
2156 |
| HV *cfg = pcsp_cfg_of(aTHX_ c); |
| 352 |
|
| SV **opt; |
| 353 |
|
| const char *b; |
| 354 |
|
| STRLEN bl; |
| 355 |
|
| char found[64]; |
| 356 |
|
| |
| 357 |
2243 |
▶ | if (!cfg || !bytes || !SvOK(bytes)) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!cfg || !bytes || !SvOK(bytes)) return; |
87 |
2069 |
2156 |
| if (!cfg || !bytes || !SvOK(bytes)) return; |
87 |
0 |
87 |
| if (!cfg || !bytes || !SvOK(bytes)) return; |
0 |
87 |
87 |
|
| 358 |
87 |
| opt = hv_fetchs(cfg, "inline_check", 0); |
| 359 |
87 |
▶ | if (opt && *opt && SvOK(*opt) && !SvTRUE(*opt)) return; /* turned off */ |
Branch: 4 branches
| Branch | True | False | Total |
| if (opt && *opt && SvOK(*opt) && !SvTRUE(*opt)) return; /* turned off */ |
0 |
87 |
87 |
| if (opt && *opt && SvOK(*opt) && !SvTRUE(*opt)) return; /* turned off */ |
0 |
0 |
0 |
| if (opt && *opt && SvOK(*opt) && !SvTRUE(*opt)) return; /* turned off */ |
0 |
0 |
0 |
| if (opt && *opt && SvOK(*opt) && !SvTRUE(*opt)) return; /* turned off */ |
0 |
0 |
0 |
|
| 360 |
87 |
▶ | if (!pcsp_dev(aTHX_ cfg)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pcsp_dev(aTHX_ cfg)) return; |
87 |
0 |
87 |
|
| 361 |
|
| |
| 362 |
0 |
| b = SvPV_const(bytes, bl); |
| 363 |
0 |
▶ | if (!bl) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bl) return; |
0 |
0 |
0 |
|
| 364 |
0 |
| pcsp_n_scanned++; |
| 365 |
|
| |
| 366 |
|
| /* The escape hatch. A template that knowingly needs an inline handler |
| 367 |
|
| * says so in itself, which keeps the exemption next to the thing being |
| 368 |
|
| * exempted rather than in a config file that outlives the reason. A |
| 369 |
|
| * checker with no way out gets disabled wholesale instead. */ |
| 370 |
0 |
▶ | if (pcsp_find(b, bl, "csp-allow-inline")) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pcsp_find(b, bl, "csp-allow-inline")) return; |
0 |
0 |
0 |
|
| 371 |
|
| |
| 372 |
|
| { /* once per template */ |
| 373 |
|
| STRLEN tl; |
| 374 |
0 |
▶ | const char *tp = template && SvOK(template) ? SvPV_const(template, tl) |
Branch: 1 branch
| Branch | True | False | Total |
| const char *tp = template && SvOK(template) ? SvPV_const(template, tl) |
0 |
0 |
0 |
|
| 375 |
0 |
▶ | : (tl = 7, "unknown"); |
Branch: 1 branch
| Branch | True | False | Total |
| : (tl = 7, "unknown"); |
0 |
0 |
0 |
|
| 376 |
0 |
▶ | if (!pcsp_seen) pcsp_seen = newHV(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pcsp_seen) pcsp_seen = newHV(); |
0 |
0 |
0 |
|
| 377 |
0 |
▶ | if (hv_exists(pcsp_seen, tp, (I32)tl)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_exists(pcsp_seen, tp, (I32)tl)) return; |
0 |
0 |
0 |
|
| 378 |
|
| |
| 379 |
0 |
▶ | if (pcsp_find_handler(b, bl, found, sizeof found)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pcsp_find_handler(b, bl, found, sizeof found)) { |
0 |
0 |
0 |
|
| 380 |
0 |
| (void)hv_store(pcsp_seen, tp, (I32)tl, &PL_sv_yes, 0); |
| 381 |
0 |
| pcsp_warn(aTHX_ c, |
| 382 |
|
| "Punk::Plugin::CSP: %s has an inline `%s=` handler, which a " |
| 383 |
|
| "script nonce does NOT cover - it needs 'unsafe-inline', and " |
| 384 |
|
| "adding that back defeats the policy for every page. Move the " |
| 385 |
|
| "handler into a nonced <script>, or put csp-allow-inline in " |
| 386 |
|
| "the template if it is deliberate.", |
| 387 |
|
| sv_2mortal(newSVpvn(tp, tl)), |
| 388 |
|
| sv_2mortal(newSVpvn(found, strlen(found)))); |
| 389 |
0 |
| return; |
| 390 |
|
| } |
| 391 |
|
| |
| 392 |
|
| { /* Deferred to here, where it costs nothing extra now the body |
| 393 |
|
| * is in hand: a nonce in the markup that is not THIS request's is |
| 394 |
|
| * a page that came out of a cache. Every script on it is blocked, |
| 395 |
|
| * and the fault appears long after the change that caused it. */ |
| 396 |
0 |
| const char *at = pcsp_find(b, bl, "nonce=\""); |
| 397 |
0 |
▶ | if (at) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (at) { |
0 |
0 |
0 |
|
| 398 |
0 |
| AV *av = pcx_av(aTHX_ c); |
| 399 |
0 |
▶ | SV *e = av ? pcx_get(aTHX_ av, PCX_ENV) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *e = av ? pcx_get(aTHX_ av, PCX_ENV) : NULL; |
0 |
0 |
0 |
|
| 400 |
0 |
▶ | if (e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) { |
0 |
0 |
0 |
| if (e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) { |
0 |
0 |
0 |
| if (e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) { |
0 |
0 |
0 |
|
| 401 |
0 |
| SV *mine = pcsp_nonce(aTHX_ (HV *)SvRV(e)); |
| 402 |
|
| STRLEN ml; |
| 403 |
0 |
| const char *mp = SvPV_const(mine, ml); |
| 404 |
0 |
| const char *v = at + 7; |
| 405 |
0 |
| STRLEN room = bl - (STRLEN)(v - b); |
| 406 |
0 |
▶ | if (!(room >= ml && memEQ(v, mp, ml))) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(room >= ml && memEQ(v, mp, ml))) { |
0 |
0 |
0 |
| if (!(room >= ml && memEQ(v, mp, ml))) { |
0 |
0 |
0 |
|
| 407 |
0 |
| (void)hv_store(pcsp_seen, tp, (I32)tl, &PL_sv_yes, 0); |
| 408 |
0 |
| pcsp_warn(aTHX_ c, |
| 409 |
|
| "Punk::Plugin::CSP: %s rendered a nonce that is " |
| 410 |
|
| "not this request's%s - a cached page carries the " |
| 411 |
|
| "nonce it was rendered with, and every script on " |
| 412 |
|
| "it is blocked against a header carrying a " |
| 413 |
|
| "different one. A response that rendered a nonce " |
| 414 |
|
| "must not be cached.", |
| 415 |
|
| sv_2mortal(newSVpvn(tp, tl)), NULL); |
| 416 |
|
| } |
| 417 |
|
| } |
| 418 |
|
| } |
| 419 |
|
| } |
| 420 |
|
| } |
| 421 |
|
| } |
| 422 |
|
| |
| 423 |
|
| /* ---- the report endpoint ------------------------------------------------- */ |
| 424 |
|
| |
| 425 |
|
| /* How many violations this process has been told about. A number nobody can |
| 426 |
|
| * see is a number nobody acts on, and a rising one during a report-only |
| 427 |
|
| * rollout is the whole signal you are rolling out for. */ |
| 428 |
|
| static UV pcsp_n_reports = 0; |
| 429 |
|
| static UV pcsp_n_malformed = 0; |
| 430 |
|
| |
| 431 |
|
| /* The fields of a violation report worth keeping. |
| 432 |
|
| * |
| 433 |
|
| * EVERY ONE OF THESE IS REQUEST BYTES. `document-uri`, `blocked-uri`, |
| 434 |
|
| * `script-sample` and `referrer` all come from a client that can send |
| 435 |
|
| * anything, and they are on their way into a log line - the class this |
| 436 |
|
| * workspace has been bitten by three times. |
| 437 |
|
| * |
| 438 |
|
| * They are handed to the logger as a RECORD rather than pasted into a |
| 439 |
|
| * message, which is what makes that safe: punk_log.h renders a record's |
| 440 |
|
| * values through pl_logfmt_val, which quotes and escapes a newline rather |
| 441 |
|
| * than passing it through, "because a log line has to stay one line whatever |
| 442 |
|
| * it was handed". Reusing that is the point - a second escaper here would be |
| 443 |
|
| * a second thing to get wrong. */ |
| 444 |
|
| static const char *PCSP_FIELDS[] = { |
| 445 |
|
| "document-uri", "referrer", "violated-directive", "effective-directive", |
| 446 |
|
| "blocked-uri", "source-file", "line-number", "script-sample", |
| 447 |
|
| "disposition", "status-code", NULL |
| 448 |
|
| }; |
| 449 |
|
| |
| 450 |
|
| /* POST <report_uri>: take a violation report, log it, answer 204. |
| 451 |
|
| * |
| 452 |
|
| * cap = [ $app ]. Always 204, including for a body that made no sense: |
| 453 |
|
| * nothing reads the response, and an error status invites a retry from a |
| 454 |
|
| * browser that cannot fix what it sent. |
| 455 |
|
| */ |
| 456 |
|
| XS_INTERNAL(pcsp_report_cb); |
| 457 |
20 |
| XS_INTERNAL(pcsp_report_cb) { |
| 458 |
20 |
| dXSARGS; |
| 459 |
20 |
▶ | SV *c = items > 0 ? ST(0) : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *c = items > 0 ? ST(0) : &PL_sv_undef; |
20 |
0 |
20 |
|
| 460 |
20 |
| HV *rec = newHV(); |
| 461 |
20 |
| SV *json = NULL; |
| 462 |
|
| int i; |
| 463 |
|
| PERL_UNUSED_VAR(items); |
| 464 |
|
| |
| 465 |
20 |
| pcsp_n_reports++; |
| 466 |
|
| |
| 467 |
|
| /* $c->req->json - the body through File::Raw::JSON's ABI. A malformed |
| 468 |
|
| * body decodes to undef and is counted rather than argued with. */ |
| 469 |
|
| { |
| 470 |
20 |
| dSP; int count; |
| 471 |
20 |
| ENTER; SAVETMPS; |
| 472 |
20 |
▶ | PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
0 |
20 |
20 |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
0 |
20 |
20 |
|
| 473 |
20 |
| count = call_method("req", G_SCALAR | G_EVAL); |
| 474 |
20 |
| SPAGAIN; |
| 475 |
20 |
▶ | if (count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 0) { |
20 |
0 |
20 |
|
| 476 |
20 |
| SV *req = SvREFCNT_inc(POPs); |
| 477 |
20 |
| PUTBACK; |
| 478 |
20 |
▶ | if (!SvTRUE(ERRSV) && SvOK(req)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV) && SvOK(req)) { |
20 |
0 |
20 |
| if (!SvTRUE(ERRSV) && SvOK(req)) { |
20 |
0 |
20 |
| if (!SvTRUE(ERRSV) && SvOK(req)) { |
20 |
0 |
20 |
|
| 479 |
20 |
▶ | PUSHMARK(SP); EXTEND(SP, 1); PUSHs(req); PUTBACK; |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(req); PUTBACK; |
0 |
20 |
20 |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(req); PUTBACK; |
0 |
20 |
20 |
|
| 480 |
20 |
| count = call_method("json", G_SCALAR | G_EVAL); |
| 481 |
20 |
| SPAGAIN; |
| 482 |
20 |
▶ | if (count > 0 && !SvTRUE(ERRSV)) json = SvREFCNT_inc(POPs); |
Branch: 3 branches
| Branch | True | False | Total |
| if (count > 0 && !SvTRUE(ERRSV)) json = SvREFCNT_inc(POPs); |
20 |
0 |
20 |
| if (count > 0 && !SvTRUE(ERRSV)) json = SvREFCNT_inc(POPs); |
20 |
0 |
20 |
| if (count > 0 && !SvTRUE(ERRSV)) json = SvREFCNT_inc(POPs); |
15 |
5 |
20 |
|
| 483 |
20 |
| PUTBACK; |
| 484 |
|
| } |
| 485 |
20 |
| SvREFCNT_dec(req); |
| 486 |
|
| } |
| 487 |
0 |
| else PUTBACK; |
| 488 |
20 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
20 |
0 |
20 |
|
| 489 |
|
| } |
| 490 |
|
| |
| 491 |
|
| /* A report arrives as { "csp-report": { ... } }. Newer browsers send the |
| 492 |
|
| * Reporting API shape instead; taking the inner object when it is there |
| 493 |
|
| * and the outer one otherwise handles both without pretending to |
| 494 |
|
| * understand either. */ |
| 495 |
|
| { |
| 496 |
20 |
| HV *body = NULL; |
| 497 |
20 |
▶ | if (json && SvROK(json) && SvTYPE(SvRV(json)) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (json && SvROK(json) && SvTYPE(SvRV(json)) == SVt_PVHV) { |
15 |
5 |
20 |
| if (json && SvROK(json) && SvTYPE(SvRV(json)) == SVt_PVHV) { |
15 |
0 |
15 |
| if (json && SvROK(json) && SvTYPE(SvRV(json)) == SVt_PVHV) { |
15 |
0 |
15 |
|
| 498 |
15 |
| HV *top = (HV *)SvRV(json); |
| 499 |
15 |
| SV **inner = hv_fetchs(top, "csp-report", 0); |
| 500 |
30 |
▶ | body = (inner && *inner && SvROK(*inner) |
Branch: 2 branches
| Branch | True | False | Total |
| body = (inner && *inner && SvROK(*inner) |
15 |
0 |
15 |
| body = (inner && *inner && SvROK(*inner) |
15 |
0 |
15 |
|
| 501 |
15 |
▶ | && SvTYPE(SvRV(*inner)) == SVt_PVHV) |
Branch: 1 branch
| Branch | True | False | Total |
| && SvTYPE(SvRV(*inner)) == SVt_PVHV) |
15 |
0 |
15 |
|
| 502 |
30 |
▶ | ? (HV *)SvRV(*inner) : top; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (HV *)SvRV(*inner) : top; |
15 |
0 |
15 |
|
| 503 |
|
| } |
| 504 |
20 |
▶ | if (!body) pcsp_n_malformed++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!body) pcsp_n_malformed++; |
5 |
15 |
20 |
|
| 505 |
170 |
▶ | for (i = 0; body && PCSP_FIELDS[i]; i++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; body && PCSP_FIELDS[i]; i++) { |
165 |
5 |
170 |
| for (i = 0; body && PCSP_FIELDS[i]; i++) { |
150 |
15 |
165 |
|
| 506 |
150 |
| I32 kl = (I32)strlen(PCSP_FIELDS[i]); |
| 507 |
150 |
| SV **v = hv_fetch(body, PCSP_FIELDS[i], kl, 0); |
| 508 |
150 |
▶ | if (v && *v && SvOK(*v)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (v && *v && SvOK(*v)) |
35 |
115 |
150 |
| if (v && *v && SvOK(*v)) |
35 |
0 |
35 |
| if (v && *v && SvOK(*v)) |
35 |
0 |
35 |
|
| 509 |
35 |
| (void)hv_store(rec, PCSP_FIELDS[i], kl, newSVsv(*v), 0); |
| 510 |
|
| } |
| 511 |
|
| } |
| 512 |
20 |
▶ | if (json) SvREFCNT_dec(json); |
Branch: 1 branch
| Branch | True | False | Total |
| if (json) SvREFCNT_dec(json); |
15 |
5 |
20 |
|
| 513 |
|
| |
| 514 |
20 |
| (void)hv_stores(rec, "message", newSVpvs("csp violation")); |
| 515 |
|
| |
| 516 |
|
| { /* $c->log->warn(\%rec) */ |
| 517 |
20 |
| dSP; int count; |
| 518 |
20 |
| ENTER; SAVETMPS; |
| 519 |
20 |
▶ | PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
0 |
20 |
20 |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
0 |
20 |
20 |
|
| 520 |
20 |
| count = call_method("log", G_SCALAR | G_EVAL); |
| 521 |
20 |
| SPAGAIN; |
| 522 |
20 |
▶ | if (count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 0) { |
20 |
0 |
20 |
|
| 523 |
20 |
| SV *lg = SvREFCNT_inc(POPs); |
| 524 |
20 |
| PUTBACK; |
| 525 |
20 |
▶ | if (!SvTRUE(ERRSV) && SvOK(lg)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV) && SvOK(lg)) { |
20 |
0 |
20 |
| if (!SvTRUE(ERRSV) && SvOK(lg)) { |
20 |
0 |
20 |
| if (!SvTRUE(ERRSV) && SvOK(lg)) { |
20 |
0 |
20 |
|
| 526 |
20 |
▶ | PUSHMARK(SP); EXTEND(SP, 2); |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 2); |
0 |
20 |
20 |
| PUSHMARK(SP); EXTEND(SP, 2); |
0 |
20 |
20 |
|
| 527 |
20 |
| PUSHs(lg); |
| 528 |
20 |
| PUSHs(sv_2mortal(newRV_inc((SV *)rec))); |
| 529 |
20 |
| PUTBACK; |
| 530 |
20 |
| (void)call_method("warn", G_DISCARD | G_EVAL); |
| 531 |
20 |
| SPAGAIN; |
| 532 |
|
| } |
| 533 |
20 |
| SvREFCNT_dec(lg); |
| 534 |
|
| } |
| 535 |
0 |
| else PUTBACK; |
| 536 |
20 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
20 |
0 |
20 |
|
| 537 |
|
| } |
| 538 |
20 |
| SvREFCNT_dec((SV *)rec); |
| 539 |
|
| |
| 540 |
|
| { /* 204: no body, and no Content-Type for one */ |
| 541 |
20 |
| AV *resp = newAV(); |
| 542 |
20 |
| av_push(resp, newSViv(204)); |
| 543 |
20 |
| av_push(resp, newRV_noinc((SV *)newAV())); |
| 544 |
20 |
| av_push(resp, newRV_noinc((SV *)newAV())); |
| 545 |
20 |
| ST(0) = sv_2mortal(newRV_noinc((SV *)resp)); |
| 546 |
|
| } |
| 547 |
20 |
| XSRETURN(1); |
| 548 |
|
| } |
| 549 |
|
| |
| 550 |
|
| #endif /* PUNK_CSP_H */ |