| 1 |
|
| #ifndef PSAML_BOOT_H |
| 2 |
|
| #define PSAML_BOOT_H |
| 3 |
|
| |
| 4 |
|
| /* Boot: the options, the three keywords, the two helpers, and what each |
| 5 |
|
| * records on the application hash. |
| 6 |
|
| * |
| 7 |
|
| * This header builds the SURFACE. It mounts no routes, writes no cookie |
| 8 |
|
| * and consumes no assertion: phase 8 reads what is recorded here and |
| 9 |
|
| * mounts the four routes. The split is deliberate and it is what makes |
| 10 |
|
| * this phase testable with no HTTP at all. |
| 11 |
|
| * |
| 12 |
|
| * State lives as keys on the application's own hash rather than in a |
| 13 |
|
| * struct, which is what punk_sitemap.h and pfeed_*.h do and for the same |
| 14 |
|
| * reason: the application is already the thing whose lifetime this state |
| 15 |
|
| * shares, and a struct would be a second one to keep in step with it. |
| 16 |
|
| * |
| 17 |
|
| * punk_saml.opts the validated option hash |
| 18 |
|
| * punk_saml.idps HV of name -> the provider's config |
| 19 |
|
| * punk_saml.idp_order AV of names, in declaration order |
| 20 |
|
| * punk_saml.login HV of path, on_login, on_error |
| 21 |
|
| * punk_saml.mount the normalised login mount |
| 22 |
|
| * punk_saml.acs the assertion consumer URL, at on_compile |
| 23 |
|
| * punk_saml.entity_id the entity id, at on_compile |
| 24 |
|
| * |
| 25 |
|
| * Must be included after psaml_reg.h. */ |
| 26 |
|
| |
| 27 |
|
| #include "psaml_reg.h" |
| 28 |
|
| #include "psaml_error.h" |
| 29 |
|
| #include "psaml_idp.h" |
| 30 |
|
| #include "psaml_metadata.h" |
| 31 |
|
| #include "psaml_flow.h" |
| 32 |
|
| #include "psaml_request.h" |
| 33 |
|
| |
| 34 |
|
| static const char *const PSAML_OPTS[] = { |
| 35 |
|
| "secret", "entity_id", "prefix", "key", "cert", |
| 36 |
|
| "sign_requests", "sign_metadata", "require_signed", "skew", |
| 37 |
|
| "flow_ttl", "max_response", "allow_sha1", "allow_idp_initiated", |
| 38 |
|
| "default_to", "name_id_format", "force_authn", "metadata", |
| 39 |
|
| "metadata_refresh", "render", NULL |
| 40 |
|
| }; |
| 41 |
|
| |
| 42 |
|
| static const char *const PSAML_IDP_OPTS[] = { |
| 43 |
|
| "metadata", "entity_id", "sso_url", "certs", NULL |
| 44 |
|
| }; |
| 45 |
|
| |
| 46 |
|
| static const char *const PSAML_LOGIN_OPTS[] = { |
| 47 |
|
| "on_login", "on_error", NULL |
| 48 |
|
| }; |
| 49 |
|
| |
| 50 |
|
| /* ---- the options ------------------------------------------------------ */ |
| 51 |
|
| |
| 52 |
|
| /* Validated and normalised at the `plugin` line, with two exceptions that |
| 53 |
|
| * cannot be settled until on_compile: entity_id, which defaults to the |
| 54 |
|
| * metadata URL and so needs `host`, and the ACS URL, which needs `host` |
| 55 |
|
| * and the login mount. `host` may be declared on either side of the |
| 56 |
|
| * plugin line, so neither may be read here. */ |
| 57 |
34 |
| PERL_STATIC_INLINE HV *psaml_opts(pTHX_ SV *app, SV *optsv) { |
| 58 |
34 |
▶ | HV *in = psaml_is_hash(aTHX_ optsv) ? (HV *)SvRV(optsv) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| HV *in = psaml_is_hash(aTHX_ optsv) ? (HV *)SvRV(optsv) : NULL; |
34 |
0 |
34 |
|
| 59 |
34 |
| HV *out = newHV(); |
| 60 |
|
| SV *v; |
| 61 |
|
| |
| 62 |
34 |
| psaml_check_opts(aTHX_ "option", in, PSAML_OPTS); |
| 63 |
|
| |
| 64 |
|
| /* secret. Croaks here rather than at the first login, because a plugin |
| 65 |
|
| * that mints one per worker fails one login in four the moment a |
| 66 |
|
| * redirect lands on a worker other than the one that started it, and |
| 67 |
|
| * that failure looks like an intermittent identity-provider fault |
| 68 |
|
| * rather than a missing option. Punk-Push and Punk-Challenge both paid |
| 69 |
|
| * for this lesson already. A list is accepted so a key can be rotated |
| 70 |
|
| * with both live. */ |
| 71 |
33 |
| v = psaml_hget(aTHX_ in, "secret"); |
| 72 |
33 |
▶ | if (!v || !SvOK(v) || (!SvROK(v) && !SvCUR(v))) |
Branch: 4 branches
| Branch | True | False | Total |
| if (!v || !SvOK(v) || (!SvROK(v) && !SvCUR(v))) |
32 |
1 |
33 |
| if (!v || !SvOK(v) || (!SvROK(v) && !SvCUR(v))) |
32 |
0 |
32 |
| if (!v || !SvOK(v) || (!SvROK(v) && !SvCUR(v))) |
30 |
2 |
32 |
| if (!v || !SvOK(v) || (!SvROK(v) && !SvCUR(v))) |
0 |
30 |
30 |
|
| 73 |
1 |
| croak("%s: `secret` is required - it signs the flow cookie, and a " |
| 74 |
|
| "secret minted per worker fails one login in four. Generate one " |
| 75 |
|
| "with `punk saml key` and keep it with the application's other " |
| 76 |
|
| "secrets", PSAML_WHO); |
| 77 |
32 |
▶ | if (psaml_is_array(aTHX_ v)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (psaml_is_array(aTHX_ v)) { |
1 |
31 |
32 |
|
| 78 |
1 |
| AV *keys = (AV *)SvRV(v); |
| 79 |
1 |
▶ | if (av_len(keys) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(keys) < 0) |
1 |
0 |
1 |
|
| 80 |
1 |
| croak("%s: `secret` was an empty list", PSAML_WHO); |
| 81 |
0 |
| (void)hv_stores(out, "secret", newSVsv(v)); |
| 82 |
|
| } |
| 83 |
31 |
▶ | else if (SvROK(v)) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (SvROK(v)) { |
1 |
30 |
31 |
|
| 84 |
1 |
| croak("%s: `secret` takes a string, or a list of them to rotate", |
| 85 |
|
| PSAML_WHO); |
| 86 |
|
| } |
| 87 |
|
| else { |
| 88 |
30 |
| (void)hv_stores(out, "secret", newSVsv(v)); |
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
|
| /* prefix */ |
| 92 |
30 |
| v = psaml_hget(aTHX_ in, "prefix"); |
| 93 |
30 |
▶ | (void)hv_stores(out, "prefix", |
Branch: 3 branches
| Branch | True | False | Total |
| (void)hv_stores(out, "prefix", |
0 |
30 |
30 |
| (void)hv_stores(out, "prefix", |
0 |
0 |
0 |
| (void)hv_stores(out, "prefix", |
0 |
0 |
0 |
|
| 94 |
|
| (v && SvOK(v) && SvCUR(v)) ? newSVsv(v) |
| 95 |
|
| : newSVpvs("/saml")); |
| 96 |
|
| |
| 97 |
|
| /* require_signed. The whole point of the plugin is the signature, so |
| 98 |
|
| * an unrecognised word here is refused rather than defaulted: a |
| 99 |
|
| * misspelling that quietly meant `either` would be a weaker check than |
| 100 |
|
| * the deployer asked for, silently. */ |
| 101 |
|
| { |
| 102 |
30 |
| SV *r = psaml_hget(aTHX_ in, "require_signed"); |
| 103 |
30 |
| const char *rp = "either"; |
| 104 |
30 |
| STRLEN rl = 6; |
| 105 |
31 |
▶ | if (r && SvOK(r)) rp = SvPV_const(r, rl); |
Branch: 2 branches
| Branch | True | False | Total |
| if (r && SvOK(r)) rp = SvPV_const(r, rl); |
1 |
29 |
30 |
| if (r && SvOK(r)) rp = SvPV_const(r, rl); |
1 |
0 |
1 |
|
| 106 |
30 |
▶ | if (!((rl == 9 && memEQ(rp, "assertion", 9)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!((rl == 9 && memEQ(rp, "assertion", 9)) |
0 |
30 |
30 |
| if (!((rl == 9 && memEQ(rp, "assertion", 9)) |
0 |
0 |
0 |
|
| 107 |
30 |
▶ | || (rl == 8 && memEQ(rp, "response", 8)) |
Branch: 2 branches
| Branch | True | False | Total |
| || (rl == 8 && memEQ(rp, "response", 8)) |
0 |
30 |
30 |
| || (rl == 8 && memEQ(rp, "response", 8)) |
0 |
0 |
0 |
|
| 108 |
30 |
▶ | || (rl == 6 && memEQ(rp, "either", 6)) |
Branch: 2 branches
| Branch | True | False | Total |
| || (rl == 6 && memEQ(rp, "either", 6)) |
29 |
1 |
30 |
| || (rl == 6 && memEQ(rp, "either", 6)) |
0 |
29 |
29 |
|
| 109 |
1 |
▶ | || (rl == 4 && memEQ(rp, "both", 4)))) |
Branch: 2 branches
| Branch | True | False | Total |
| || (rl == 4 && memEQ(rp, "both", 4)))) |
0 |
1 |
1 |
| || (rl == 4 && memEQ(rp, "both", 4)))) |
0 |
0 |
0 |
|
| 110 |
1 |
| croak("%s: `require_signed` takes assertion, response, either or " |
| 111 |
|
| "both, not '%.*s'", PSAML_WHO, (int)rl, rp); |
| 112 |
29 |
| (void)hv_stores(out, "require_signed", newSVpvn(rp, rl)); |
| 113 |
|
| } |
| 114 |
|
| |
| 115 |
29 |
| (void)hv_stores(out, "skew", newSViv(psaml_opt_uv(aTHX_ in, "skew", 120))); |
| 116 |
28 |
| (void)hv_stores(out, "flow_ttl", |
| 117 |
|
| newSViv(psaml_opt_uv(aTHX_ in, "flow_ttl", 600))); |
| 118 |
28 |
| (void)hv_stores(out, "max_response", |
| 119 |
|
| newSViv(psaml_opt_uv(aTHX_ in, "max_response", 262144))); |
| 120 |
28 |
| (void)hv_stores(out, "metadata_refresh", |
| 121 |
|
| newSViv(psaml_opt_uv(aTHX_ in, "metadata_refresh", 3600))); |
| 122 |
|
| |
| 123 |
|
| /* the booleans, normalised so nothing downstream reads a string */ |
| 124 |
|
| { |
| 125 |
|
| static const char *const flags[] = { |
| 126 |
|
| "sign_requests", "sign_metadata", "allow_sha1", |
| 127 |
|
| "allow_idp_initiated", "force_authn", NULL |
| 128 |
|
| }; |
| 129 |
|
| int i; |
| 130 |
168 |
▶ | for (i = 0; flags[i]; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; flags[i]; i++) |
140 |
28 |
168 |
|
| 131 |
140 |
| (void)hv_store(out, flags[i], (I32)strlen(flags[i]), |
| 132 |
|
| newSViv(psaml_opt_bool(aTHX_ in, flags[i], 0)), 0); |
| 133 |
|
| /* metadata defaults ON: an SP that does not publish its metadata has |
| 134 |
|
| * to be configured by hand at the other end, every time */ |
| 135 |
28 |
| (void)hv_stores(out, "metadata", |
| 136 |
|
| newSViv(psaml_opt_bool(aTHX_ in, "metadata", 1))); |
| 137 |
|
| } |
| 138 |
|
| |
| 139 |
|
| /* the pass-through strings and callbacks */ |
| 140 |
|
| { |
| 141 |
|
| static const char *const keep[] = { |
| 142 |
|
| "entity_id", "key", "cert", "default_to", "name_id_format", |
| 143 |
|
| "render", NULL |
| 144 |
|
| }; |
| 145 |
|
| int i; |
| 146 |
196 |
▶ | for (i = 0; keep[i]; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; keep[i]; i++) { |
168 |
28 |
196 |
|
| 147 |
168 |
| SV *k = psaml_hget(aTHX_ in, keep[i]); |
| 148 |
168 |
▶ | if (k && SvOK(k)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (k && SvOK(k)) |
2 |
166 |
168 |
| if (k && SvOK(k)) |
2 |
0 |
2 |
|
| 149 |
2 |
| (void)hv_store(out, keep[i], (I32)strlen(keep[i]), newSVsv(k), 0); |
| 150 |
|
| } |
| 151 |
28 |
▶ | if (!hv_exists(out, "default_to", 10)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hv_exists(out, "default_to", 10)) |
28 |
0 |
28 |
|
| 152 |
28 |
| (void)hv_stores(out, "default_to", newSVpvs("/")); |
| 153 |
|
| } |
| 154 |
|
| |
| 155 |
|
| /* An option that silently does nothing is worse than one that refuses: |
| 156 |
|
| * a deployer who wrote sign_requests and got unsigned requests has been |
| 157 |
|
| * told the opposite of the truth by their own configuration. */ |
| 158 |
28 |
▶ | if (SvTRUE(*hv_fetchs(out, "sign_requests", 0)) && !hv_exists(out, "key", 3)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(*hv_fetchs(out, "sign_requests", 0)) && !hv_exists(out, "key", 3)) |
1 |
27 |
28 |
| if (SvTRUE(*hv_fetchs(out, "sign_requests", 0)) && !hv_exists(out, "key", 3)) |
1 |
0 |
1 |
|
| 159 |
1 |
| croak("%s: `sign_requests` needs `key`, the SP private key in PEM", |
| 160 |
|
| PSAML_WHO); |
| 161 |
27 |
▶ | if (SvTRUE(*hv_fetchs(out, "sign_metadata", 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTRUE(*hv_fetchs(out, "sign_metadata", 0)) |
1 |
26 |
27 |
|
| 162 |
1 |
▶ | && !(hv_exists(out, "key", 3) && hv_exists(out, "cert", 4))) |
Branch: 2 branches
| Branch | True | False | Total |
| && !(hv_exists(out, "key", 3) && hv_exists(out, "cert", 4))) |
1 |
0 |
1 |
| && !(hv_exists(out, "key", 3) && hv_exists(out, "cert", 4))) |
1 |
0 |
1 |
|
| 163 |
1 |
| croak("%s: `sign_metadata` needs `key` and `cert`", PSAML_WHO); |
| 164 |
|
| |
| 165 |
|
| { |
| 166 |
26 |
| SV *r = psaml_hget(aTHX_ out, "render"); |
| 167 |
26 |
▶ | if (r && SvROK(r) && !psaml_is_code(aTHX_ r)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (r && SvROK(r) && !psaml_is_code(aTHX_ r)) |
0 |
26 |
26 |
| if (r && SvROK(r) && !psaml_is_code(aTHX_ r)) |
0 |
0 |
0 |
| if (r && SvROK(r) && !psaml_is_code(aTHX_ r)) |
0 |
0 |
0 |
|
| 168 |
0 |
| croak("%s: `render` takes a coderef or the name of a context method", |
| 169 |
|
| PSAML_WHO); |
| 170 |
|
| } |
| 171 |
|
| |
| 172 |
|
| PERL_UNUSED_ARG(app); |
| 173 |
26 |
| return out; |
| 174 |
|
| } |
| 175 |
|
| |
| 176 |
|
| /* ---- host ------------------------------------------------------------- */ |
| 177 |
|
| |
| 178 |
|
| /* The declared origin, which must exist and must be https. |
| 179 |
|
| * |
| 180 |
|
| * The request is never consulted. The assertion consumer URL derives from |
| 181 |
|
| * this and the identity provider compares it against what it was |
| 182 |
|
| * configured with character for character, so a plugin that guessed the |
| 183 |
|
| * host from a request header would be letting the request choose which |
| 184 |
|
| * URL counts as this application. |
| 185 |
|
| * |
| 186 |
|
| * https because the phase-8 flow cookie carries SameSite=None, which is |
| 187 |
|
| * the only value a browser sends on the cross-site POST from the |
| 188 |
|
| * provider, and which browsers drop without Secure. The croak is loud |
| 189 |
|
| * because the alternative is silent: a plain-http deployment boots |
| 190 |
|
| * happily and then fails at every ACS with `unsolicited`, with nothing in |
| 191 |
|
| * the log to say why. */ |
| 192 |
17 |
| PERL_STATIC_INLINE SV *psaml_host(pTHX_ SV *app) { |
| 193 |
17 |
| SV *host = psaml_can(aTHX_ app, "host") |
| 194 |
17 |
▶ | ? sv_2mortal(psaml_call(aTHX_ app, "host", NULL, 0)) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? sv_2mortal(psaml_call(aTHX_ app, "host", NULL, 0)) : NULL; |
17 |
0 |
17 |
|
| 195 |
17 |
| STRLEN hl = 0; |
| 196 |
|
| const char *hp; |
| 197 |
17 |
▶ | if (!host || !SvOK(host) || !SvCUR(host)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!host || !SvOK(host) || !SvCUR(host)) |
16 |
1 |
17 |
| if (!host || !SvOK(host) || !SvCUR(host)) |
16 |
0 |
16 |
| if (!host || !SvOK(host) || !SvCUR(host)) |
0 |
16 |
16 |
|
| 198 |
1 |
| croak("%s: this application has no `host`. The assertion consumer URL " |
| 199 |
|
| "derives from it and the identity provider compares that URL " |
| 200 |
|
| "character for character, so it cannot be guessed from a " |
| 201 |
|
| "request", PSAML_WHO); |
| 202 |
16 |
| hp = SvPV_const(host, hl); |
| 203 |
16 |
▶ | if (!(hl > 8 && memEQ(hp, "https://", 8))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(hl > 8 && memEQ(hp, "https://", 8))) |
16 |
0 |
16 |
| if (!(hl > 8 && memEQ(hp, "https://", 8))) |
1 |
15 |
16 |
|
| 204 |
1 |
| croak("%s: `host` must be https, not '%.*s'. The flow cookie is " |
| 205 |
|
| "SameSite=None, which is the only value sent on the cross-site " |
| 206 |
|
| "POST from the provider, and which browsers drop without " |
| 207 |
|
| "Secure. Over http every login fails at the ACS as " |
| 208 |
|
| "`unsolicited`", PSAML_WHO, (int)hl, hp); |
| 209 |
15 |
| return host; |
| 210 |
|
| } |
| 211 |
|
| |
| 212 |
|
| /* An origin and a path joined with exactly one slash between them. */ |
| 213 |
14 |
| PERL_STATIC_INLINE SV *psaml_join_url(pTHX_ SV *base, const char *path) { |
| 214 |
|
| STRLEN bl; |
| 215 |
14 |
| const char *bp = SvPV_const(base, bl); |
| 216 |
|
| SV *out; |
| 217 |
14 |
▶ | while (bl && bp[bl - 1] == '/') bl--; |
Branch: 2 branches
| Branch | True | False | Total |
| while (bl && bp[bl - 1] == '/') bl--; |
14 |
0 |
14 |
| while (bl && bp[bl - 1] == '/') bl--; |
0 |
14 |
14 |
|
| 218 |
14 |
| out = newSVpvn(bp, bl); |
| 219 |
14 |
▶ | if (*path != '/') sv_catpvs(out, "/"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (*path != '/') sv_catpvs(out, "/"); |
0 |
14 |
14 |
|
| 220 |
14 |
| sv_catpv(out, path); |
| 221 |
14 |
| return out; |
| 222 |
|
| } |
| 223 |
|
| |
| 224 |
|
| /* The login mount, normalised: a leading slash, no trailing one. The |
| 225 |
|
| * recorded value and the routes phase 8 mounts must be the same string, |
| 226 |
|
| * so there is one place that decides what it looks like. */ |
| 227 |
22 |
| PERL_STATIC_INLINE SV *psaml_mount_path(pTHX_ SV *path) { |
| 228 |
|
| STRLEN pl; |
| 229 |
|
| const char *pp; |
| 230 |
|
| SV *out; |
| 231 |
22 |
▶ | if (!path || !SvOK(path) || !SvCUR(path)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!path || !SvOK(path) || !SvCUR(path)) |
22 |
0 |
22 |
| if (!path || !SvOK(path) || !SvCUR(path)) |
22 |
0 |
22 |
| if (!path || !SvOK(path) || !SvCUR(path)) |
0 |
22 |
22 |
|
| 232 |
0 |
| croak("%s: saml_login needs a path to mount under", PSAML_WHO); |
| 233 |
22 |
| pp = SvPV_const(path, pl); |
| 234 |
23 |
▶ | while (pl > 1 && pp[pl - 1] == '/') pl--; |
Branch: 2 branches
| Branch | True | False | Total |
| while (pl > 1 && pp[pl - 1] == '/') pl--; |
23 |
0 |
23 |
| while (pl > 1 && pp[pl - 1] == '/') pl--; |
1 |
22 |
23 |
|
| 235 |
22 |
| out = newSVpvs(""); |
| 236 |
22 |
▶ | if (*pp != '/') sv_catpvs(out, "/"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (*pp != '/') sv_catpvs(out, "/"); |
0 |
22 |
22 |
|
| 237 |
22 |
| sv_catpvn(out, pp, pl); |
| 238 |
22 |
| return out; |
| 239 |
|
| } |
| 240 |
|
| |
| 241 |
|
| /* ---- saml_idp --------------------------------------------------------- */ |
| 242 |
|
| |
| 243 |
|
| XS_INTERNAL(psaml_kw_idp); |
| 244 |
28 |
| XS_INTERNAL(psaml_kw_idp) { |
| 245 |
28 |
| dXSARGS; |
| 246 |
28 |
| SV *app = psaml_cap_slot(aTHX_ cv, 0); |
| 247 |
|
| HV *cfg, *in; |
| 248 |
|
| HV *idps; |
| 249 |
|
| AV *order; |
| 250 |
|
| SV *name, *conf; |
| 251 |
|
| SV *meta, *ent, *sso, *certs; |
| 252 |
|
| STRLEN nl; |
| 253 |
|
| const char *np; |
| 254 |
|
| |
| 255 |
28 |
▶ | if (!app) { PERL_UNUSED_VAR(ax); XSRETURN_EMPTY; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!app) { PERL_UNUSED_VAR(ax); XSRETURN_EMPTY; } |
0 |
28 |
28 |
|
| 256 |
28 |
▶ | if (items < 1 || !SvOK(ST(0)) || SvROK(ST(0))) |
Branch: 3 branches
| Branch | True | False | Total |
| if (items < 1 || !SvOK(ST(0)) || SvROK(ST(0))) |
28 |
0 |
28 |
| if (items < 1 || !SvOK(ST(0)) || SvROK(ST(0))) |
28 |
0 |
28 |
| if (items < 1 || !SvOK(ST(0)) || SvROK(ST(0))) |
0 |
28 |
28 |
|
| 257 |
0 |
| croak("%s: saml_idp takes a name and a hashref", PSAML_WHO); |
| 258 |
28 |
| name = ST(0); |
| 259 |
28 |
| np = SvPV_const(name, nl); |
| 260 |
28 |
▶ | if (items < 2 || !psaml_is_hash(aTHX_ ST(1))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (items < 2 || !psaml_is_hash(aTHX_ ST(1))) |
28 |
0 |
28 |
| if (items < 2 || !psaml_is_hash(aTHX_ ST(1))) |
0 |
28 |
28 |
|
| 261 |
0 |
| croak("%s: saml_idp '%.*s' needs a hashref of settings", |
| 262 |
|
| PSAML_WHO, (int)nl, np); |
| 263 |
28 |
| in = (HV *)SvRV(ST(1)); |
| 264 |
28 |
| psaml_check_opts(aTHX_ "saml_idp setting", in, PSAML_IDP_OPTS); |
| 265 |
|
| |
| 266 |
27 |
| meta = psaml_hget(aTHX_ in, "metadata"); |
| 267 |
27 |
| ent = psaml_hget(aTHX_ in, "entity_id"); |
| 268 |
27 |
| sso = psaml_hget(aTHX_ in, "sso_url"); |
| 269 |
27 |
| certs = psaml_hget(aTHX_ in, "certs"); |
| 270 |
|
| |
| 271 |
|
| /* The two forms are exclusive and giving both croaks, rather than |
| 272 |
|
| * letting one silently win. Which one won would decide which key |
| 273 |
|
| * verifies the assertion, and a deployer who supplied both believes |
| 274 |
|
| * whichever they wrote last. */ |
| 275 |
27 |
▶ | if (meta && SvOK(meta) && (ent || sso || certs)) |
Branch: 5 branches
| Branch | True | False | Total |
| if (meta && SvOK(meta) && (ent || sso || certs)) |
18 |
9 |
27 |
| if (meta && SvOK(meta) && (ent || sso || certs)) |
18 |
0 |
18 |
| if (meta && SvOK(meta) && (ent || sso || certs)) |
18 |
0 |
18 |
| if (meta && SvOK(meta) && (ent || sso || certs)) |
17 |
1 |
18 |
| if (meta && SvOK(meta) && (ent || sso || certs)) |
0 |
17 |
17 |
|
| 276 |
1 |
| croak("%s: saml_idp '%.*s' gives both `metadata` and the explicit " |
| 277 |
|
| "entity_id/sso_url/certs. They are exclusive: one of them would " |
| 278 |
|
| "decide which key verifies an assertion, and it would not be " |
| 279 |
|
| "obvious which", PSAML_WHO, (int)nl, np); |
| 280 |
26 |
▶ | if (!(meta && SvOK(meta))) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(meta && SvOK(meta))) { |
17 |
9 |
26 |
| if (!(meta && SvOK(meta))) { |
0 |
17 |
17 |
|
| 281 |
9 |
▶ | if (!(ent && SvOK(ent)) || !(sso && SvOK(sso)) || !certs) |
Branch: 5 branches
| Branch | True | False | Total |
| if (!(ent && SvOK(ent)) || !(sso && SvOK(sso)) || !certs) |
8 |
1 |
9 |
| if (!(ent && SvOK(ent)) || !(sso && SvOK(sso)) || !certs) |
8 |
0 |
8 |
| if (!(ent && SvOK(ent)) || !(sso && SvOK(sso)) || !certs) |
8 |
0 |
8 |
| if (!(ent && SvOK(ent)) || !(sso && SvOK(sso)) || !certs) |
8 |
0 |
8 |
| if (!(ent && SvOK(ent)) || !(sso && SvOK(sso)) || !certs) |
0 |
8 |
8 |
|
| 282 |
1 |
| croak("%s: saml_idp '%.*s' needs either `metadata`, or all three of " |
| 283 |
|
| "`entity_id`, `sso_url` and `certs`", PSAML_WHO, (int)nl, np); |
| 284 |
|
| } |
| 285 |
|
| |
| 286 |
25 |
| idps = (HV *)SvRV(psaml_app_hash(aTHX_ app, PSAML_K_IDPS)); |
| 287 |
25 |
| order = (AV *)SvRV(psaml_app_array(aTHX_ app, PSAML_K_IDPORDER)); |
| 288 |
|
| |
| 289 |
25 |
▶ | if (hv_exists(idps, np, (I32)nl)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_exists(idps, np, (I32)nl)) |
1 |
24 |
25 |
|
| 290 |
1 |
| croak("%s: saml_idp '%.*s' is declared twice. Two providers under one " |
| 291 |
|
| "name is a configuration nobody meant", PSAML_WHO, (int)nl, np); |
| 292 |
|
| |
| 293 |
24 |
| cfg = newHV(); |
| 294 |
24 |
| (void)hv_stores(cfg, "name", newSVsv(name)); |
| 295 |
24 |
▶ | if (meta && SvOK(meta)) (void)hv_stores(cfg, "metadata", newSVsv(meta)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (meta && SvOK(meta)) (void)hv_stores(cfg, "metadata", newSVsv(meta)); |
16 |
8 |
24 |
| if (meta && SvOK(meta)) (void)hv_stores(cfg, "metadata", newSVsv(meta)); |
16 |
0 |
16 |
|
| 296 |
24 |
▶ | if (ent && SvOK(ent)) (void)hv_stores(cfg, "entity_id", newSVsv(ent)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (ent && SvOK(ent)) (void)hv_stores(cfg, "entity_id", newSVsv(ent)); |
8 |
16 |
24 |
| if (ent && SvOK(ent)) (void)hv_stores(cfg, "entity_id", newSVsv(ent)); |
8 |
0 |
8 |
|
| 297 |
24 |
▶ | if (sso && SvOK(sso)) (void)hv_stores(cfg, "sso_url", newSVsv(sso)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (sso && SvOK(sso)) (void)hv_stores(cfg, "sso_url", newSVsv(sso)); |
8 |
16 |
24 |
| if (sso && SvOK(sso)) (void)hv_stores(cfg, "sso_url", newSVsv(sso)); |
8 |
0 |
8 |
|
| 298 |
24 |
▶ | if (certs) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (certs) { |
8 |
16 |
24 |
|
| 299 |
|
| /* normalised to an arrayref here, so phase 7 reads one shape */ |
| 300 |
8 |
▶ | if (psaml_is_array(aTHX_ certs)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (psaml_is_array(aTHX_ certs)) { |
4 |
4 |
8 |
|
| 301 |
4 |
| (void)hv_stores(cfg, "certs", newSVsv(certs)); |
| 302 |
|
| } |
| 303 |
4 |
▶ | else if (SvROK(certs)) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (SvROK(certs)) { |
0 |
4 |
4 |
|
| 304 |
0 |
| croak("%s: saml_idp '%.*s': `certs` takes a PEM or an arrayref of " |
| 305 |
|
| "them", PSAML_WHO, (int)nl, np); |
| 306 |
|
| } |
| 307 |
|
| else { |
| 308 |
4 |
| AV *av = newAV(); |
| 309 |
4 |
| av_push(av, newSVsv(certs)); |
| 310 |
4 |
| (void)hv_stores(cfg, "certs", newRV_noinc((SV *)av)); |
| 311 |
|
| } |
| 312 |
|
| } |
| 313 |
24 |
| conf = newRV_noinc((SV *)cfg); |
| 314 |
24 |
| (void)hv_store(idps, np, (I32)nl, conf, 0); |
| 315 |
24 |
| av_push(order, newSVsv(name)); |
| 316 |
|
| |
| 317 |
|
| /* Nothing is fetched here. `metadata` is a URL and this is the plugin |
| 318 |
|
| * line: a network call at compile time would make an application that |
| 319 |
|
| * cannot boot when the provider is briefly down. Phase 7 fetches at |
| 320 |
|
| * on_compile. */ |
| 321 |
24 |
| XSRETURN_EMPTY; |
| 322 |
|
| } |
| 323 |
|
| |
| 324 |
|
| /* ---- saml_login ------------------------------------------------------- */ |
| 325 |
|
| |
| 326 |
|
| XS_INTERNAL(psaml_kw_login); |
| 327 |
23 |
| XS_INTERNAL(psaml_kw_login) { |
| 328 |
23 |
| dXSARGS; |
| 329 |
23 |
| SV *app = psaml_cap_slot(aTHX_ cv, 0); |
| 330 |
|
| HV *in, *rec; |
| 331 |
|
| SV *on_login, *on_error, *mount; |
| 332 |
|
| |
| 333 |
23 |
▶ | if (!app) { PERL_UNUSED_VAR(ax); XSRETURN_EMPTY; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!app) { PERL_UNUSED_VAR(ax); XSRETURN_EMPTY; } |
0 |
23 |
23 |
|
| 334 |
|
| |
| 335 |
|
| /* Declared once. Any number of providers live under one mount, and a |
| 336 |
|
| * second mount would mean two ACS URLs, only one of which any given |
| 337 |
|
| * provider was configured with. */ |
| 338 |
23 |
▶ | if (psaml_app_get(aTHX_ app, PSAML_K_LOGIN)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (psaml_app_get(aTHX_ app, PSAML_K_LOGIN)) |
1 |
22 |
23 |
|
| 339 |
1 |
| croak("%s: saml_login is declared twice. One application has one login " |
| 340 |
|
| "mount; every saml_idp lives under it", PSAML_WHO); |
| 341 |
|
| |
| 342 |
22 |
▶ | if (items < 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items < 1) |
0 |
22 |
22 |
|
| 343 |
0 |
| croak("%s: saml_login takes a path and a hashref", PSAML_WHO); |
| 344 |
22 |
| mount = psaml_mount_path(aTHX_ ST(0)); |
| 345 |
22 |
▶ | if (items < 2 || !psaml_is_hash(aTHX_ ST(1))) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (items < 2 || !psaml_is_hash(aTHX_ ST(1))) { |
22 |
0 |
22 |
| if (items < 2 || !psaml_is_hash(aTHX_ ST(1))) { |
0 |
22 |
22 |
|
| 346 |
0 |
| SvREFCNT_dec(mount); |
| 347 |
0 |
| croak("%s: saml_login needs a hashref with `on_login`", PSAML_WHO); |
| 348 |
|
| } |
| 349 |
22 |
| in = (HV *)SvRV(ST(1)); |
| 350 |
22 |
| psaml_check_opts(aTHX_ "saml_login setting", in, PSAML_LOGIN_OPTS); |
| 351 |
|
| |
| 352 |
22 |
| on_login = psaml_hget(aTHX_ in, "on_login"); |
| 353 |
22 |
▶ | if (!psaml_is_code(aTHX_ on_login)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!psaml_is_code(aTHX_ on_login)) { |
2 |
20 |
22 |
|
| 354 |
2 |
| SvREFCNT_dec(mount); |
| 355 |
2 |
| croak("%s: saml_login needs `on_login`, a coderef called with the " |
| 356 |
|
| "context and the verified identity", PSAML_WHO); |
| 357 |
|
| } |
| 358 |
20 |
| on_error = psaml_hget(aTHX_ in, "on_error"); |
| 359 |
20 |
▶ | if (on_error && SvOK(on_error) && !psaml_is_code(aTHX_ on_error)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (on_error && SvOK(on_error) && !psaml_is_code(aTHX_ on_error)) { |
2 |
18 |
20 |
| if (on_error && SvOK(on_error) && !psaml_is_code(aTHX_ on_error)) { |
2 |
0 |
2 |
| if (on_error && SvOK(on_error) && !psaml_is_code(aTHX_ on_error)) { |
1 |
1 |
2 |
|
| 360 |
1 |
| SvREFCNT_dec(mount); |
| 361 |
1 |
| croak("%s: saml_login's `on_error` takes a coderef", PSAML_WHO); |
| 362 |
|
| } |
| 363 |
|
| |
| 364 |
19 |
| rec = newHV(); |
| 365 |
19 |
| (void)hv_stores(rec, "path", SvREFCNT_inc_simple_NN(mount)); |
| 366 |
19 |
| (void)hv_stores(rec, "on_login", newSVsv(on_login)); |
| 367 |
19 |
▶ | if (on_error && SvOK(on_error)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (on_error && SvOK(on_error)) |
1 |
18 |
19 |
| if (on_error && SvOK(on_error)) |
1 |
0 |
1 |
|
| 368 |
1 |
| (void)hv_stores(rec, "on_error", newSVsv(on_error)); |
| 369 |
|
| |
| 370 |
19 |
| psaml_app_set(aTHX_ app, PSAML_K_LOGIN, newRV_noinc((SV *)rec)); |
| 371 |
19 |
| psaml_app_set(aTHX_ app, PSAML_K_MOUNT, mount); |
| 372 |
|
| |
| 373 |
|
| /* No routes here. Phase 8 mounts them at on_compile, where `host` is |
| 374 |
|
| * certainly declared and the ACS URL can be computed once. */ |
| 375 |
19 |
| XSRETURN_EMPTY; |
| 376 |
|
| } |
| 377 |
|
| |
| 378 |
|
| /* ---- the helpers ------------------------------------------------------ */ |
| 379 |
|
| |
| 380 |
|
| XS_INTERNAL(psaml_help_idps); |
| 381 |
1 |
| XS_INTERNAL(psaml_help_idps) { |
| 382 |
1 |
| dXSARGS; |
| 383 |
1 |
| SV *app = psaml_cap_slot(aTHX_ cv, 0); |
| 384 |
1 |
▶ | SV *ordsv = app ? psaml_app_get(aTHX_ app, PSAML_K_IDPORDER) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *ordsv = app ? psaml_app_get(aTHX_ app, PSAML_K_IDPORDER) : NULL; |
1 |
0 |
1 |
|
| 385 |
|
| AV *order; |
| 386 |
|
| SSize_t i, n; |
| 387 |
|
| PERL_UNUSED_VAR(items); |
| 388 |
1 |
▶ | if (!ordsv || !SvROK(ordsv)) XSRETURN_EMPTY; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!ordsv || !SvROK(ordsv)) XSRETURN_EMPTY; |
1 |
0 |
1 |
| if (!ordsv || !SvROK(ordsv)) XSRETURN_EMPTY; |
0 |
1 |
1 |
|
| 389 |
1 |
| order = (AV *)SvRV(ordsv); |
| 390 |
1 |
| n = av_len(order) + 1; |
| 391 |
1 |
| SP -= items; |
| 392 |
1 |
▶ | EXTEND(SP, n); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, n); |
1 |
0 |
1 |
| EXTEND(SP, n); |
0 |
1 |
1 |
|
| 393 |
3 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
2 |
1 |
3 |
|
| 394 |
2 |
| SV **e = av_fetch(order, i, 0); |
| 395 |
2 |
▶ | PUSHs(sv_2mortal(newSVsv(e && *e ? *e : &PL_sv_undef))); |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHs(sv_2mortal(newSVsv(e && *e ? *e : &PL_sv_undef))); |
2 |
0 |
2 |
| PUSHs(sv_2mortal(newSVsv(e && *e ? *e : &PL_sv_undef))); |
2 |
0 |
2 |
|
| 396 |
|
| } |
| 397 |
1 |
| PUTBACK; |
| 398 |
1 |
| return; |
| 399 |
|
| } |
| 400 |
|
| |
| 401 |
|
| XS_INTERNAL(psaml_help_url); |
| 402 |
3 |
| XS_INTERNAL(psaml_help_url) { |
| 403 |
3 |
| dXSARGS; |
| 404 |
3 |
| SV *app = psaml_cap_slot(aTHX_ cv, 0); |
| 405 |
|
| SV *mount, *idps, *out; |
| 406 |
|
| STRLEN nl; |
| 407 |
|
| const char *np; |
| 408 |
|
| SSize_t i; |
| 409 |
|
| |
| 410 |
3 |
▶ | if (!app || items < 2) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!app || items < 2) |
3 |
0 |
3 |
| if (!app || items < 2) |
0 |
3 |
3 |
|
| 411 |
0 |
| croak("%s: saml_url takes a provider name", PSAML_WHO); |
| 412 |
3 |
▶ | if (!SvOK(ST(1)) || SvROK(ST(1))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvOK(ST(1)) || SvROK(ST(1))) |
3 |
0 |
3 |
| if (!SvOK(ST(1)) || SvROK(ST(1))) |
0 |
3 |
3 |
|
| 413 |
0 |
| croak("%s: saml_url takes a provider name", PSAML_WHO); |
| 414 |
3 |
| np = SvPV_const(ST(1), nl); |
| 415 |
|
| |
| 416 |
3 |
| idps = psaml_app_get(aTHX_ app, PSAML_K_IDPS); |
| 417 |
3 |
| mount = psaml_app_get(aTHX_ app, PSAML_K_MOUNT); |
| 418 |
3 |
▶ | if (!mount) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!mount) |
0 |
3 |
3 |
|
| 419 |
0 |
| croak("%s: saml_url needs a saml_login mount", PSAML_WHO); |
| 420 |
|
| |
| 421 |
|
| /* An unknown provider croaks naming the configured ones. This is |
| 422 |
|
| * called from a template, and the alternative to a croak is an anchor |
| 423 |
|
| * with an href that 404s, which looks like a broken provider rather |
| 424 |
|
| * than a typo in the page. */ |
| 425 |
3 |
▶ | if (!(idps && SvROK(idps) && hv_exists((HV *)SvRV(idps), np, (I32)nl))) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(idps && SvROK(idps) && hv_exists((HV *)SvRV(idps), np, (I32)nl))) { |
3 |
0 |
3 |
| if (!(idps && SvROK(idps) && hv_exists((HV *)SvRV(idps), np, (I32)nl))) { |
3 |
0 |
3 |
| if (!(idps && SvROK(idps) && hv_exists((HV *)SvRV(idps), np, (I32)nl))) { |
1 |
2 |
3 |
|
| 426 |
1 |
| SV *known = sv_2mortal(newSVpvs("")); |
| 427 |
1 |
| SV *ordsv = psaml_app_get(aTHX_ app, PSAML_K_IDPORDER); |
| 428 |
1 |
▶ | if (ordsv && SvROK(ordsv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ordsv && SvROK(ordsv)) { |
1 |
0 |
1 |
| if (ordsv && SvROK(ordsv)) { |
1 |
0 |
1 |
|
| 429 |
1 |
| AV *order = (AV *)SvRV(ordsv); |
| 430 |
3 |
▶ | for (i = 0; i <= av_len(order); i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= av_len(order); i++) { |
2 |
1 |
3 |
|
| 431 |
2 |
| SV **e = av_fetch(order, i, 0); |
| 432 |
2 |
▶ | if (i) sv_catpvs(known, ", "); |
Branch: 1 branch
| Branch | True | False | Total |
| if (i) sv_catpvs(known, ", "); |
1 |
1 |
2 |
|
| 433 |
2 |
▶ | if (e && *e) sv_catsv(known, *e); |
Branch: 2 branches
| Branch | True | False | Total |
| if (e && *e) sv_catsv(known, *e); |
2 |
0 |
2 |
| if (e && *e) sv_catsv(known, *e); |
2 |
0 |
2 |
|
| 434 |
|
| } |
| 435 |
|
| } |
| 436 |
2 |
▶ | croak("%s: no saml_idp named '%.*s' (configured: %s)", PSAML_WHO, |
Branch: 1 branch
| Branch | True | False | Total |
| croak("%s: no saml_idp named '%.*s' (configured: %s)", PSAML_WHO, |
1 |
0 |
1 |
|
| 437 |
|
| (int)nl, np, SvCUR(known) ? SvPV_nolen(known) : "none"); |
| 438 |
|
| } |
| 439 |
|
| |
| 440 |
2 |
| out = newSVsv(mount); |
| 441 |
2 |
| sv_catpvs(out, "/login/"); |
| 442 |
2 |
| sv_catpvn(out, np, nl); |
| 443 |
|
| |
| 444 |
|
| /* `to` is recorded on the query string here and goes through |
| 445 |
|
| * $c->safe_path in phase 8, where it is used. It is not sanitised |
| 446 |
|
| * twice: one place decides, and that place is the one that acts on it. */ |
| 447 |
2 |
▶ | if (items > 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 2) { |
1 |
1 |
2 |
|
| 448 |
|
| int j; |
| 449 |
1 |
| int first = 1; |
| 450 |
2 |
▶ | for (j = 2; j + 1 < items; j += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 2; j + 1 < items; j += 2) { |
1 |
1 |
2 |
|
| 451 |
|
| STRLEN kl, vl; |
| 452 |
1 |
| const char *kp = SvPV_const(ST(j), kl); |
| 453 |
1 |
▶ | const char *vp = SvPV_const(ST(j + 1), vl); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *vp = SvPV_const(ST(j + 1), vl); |
1 |
0 |
1 |
|
| 454 |
|
| STRLEN q; |
| 455 |
1 |
| sv_catpvs(out, ""); |
| 456 |
1 |
▶ | sv_catpvn(out, first ? "?" : "&", 1); |
Branch: 1 branch
| Branch | True | False | Total |
| sv_catpvn(out, first ? "?" : "&", 1); |
1 |
0 |
1 |
|
| 457 |
1 |
| first = 0; |
| 458 |
1 |
| sv_catpvn(out, kp, kl); |
| 459 |
1 |
| sv_catpvs(out, "="); |
| 460 |
13 |
▶ | for (q = 0; q < vl; q++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (q = 0; q < vl; q++) { |
12 |
1 |
13 |
|
| 461 |
12 |
| unsigned char c = (unsigned char)vp[q]; |
| 462 |
12 |
▶ | if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') |
Branch: 4 branches
| Branch | True | False | Total |
| if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') |
8 |
4 |
12 |
| if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') |
8 |
0 |
8 |
| if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') |
8 |
4 |
12 |
| if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') |
0 |
8 |
8 |
|
| 463 |
4 |
▶ | || (c >= '0' && c <= '9') || c == '-' || c == '_' |
Branch: 4 branches
| Branch | True | False | Total |
| || (c >= '0' && c <= '9') || c == '-' || c == '_' |
3 |
1 |
4 |
| || (c >= '0' && c <= '9') || c == '-' || c == '_' |
2 |
1 |
3 |
| || (c >= '0' && c <= '9') || c == '-' || c == '_' |
3 |
0 |
3 |
| || (c >= '0' && c <= '9') || c == '-' || c == '_' |
3 |
0 |
3 |
|
| 464 |
3 |
▶ | || c == '.' || c == '~') { |
Branch: 2 branches
| Branch | True | False | Total |
| || c == '.' || c == '~') { |
3 |
0 |
3 |
| || c == '.' || c == '~') { |
0 |
3 |
3 |
|
| 465 |
9 |
| sv_catpvn(out, (const char *)&c, 1); |
| 466 |
|
| } |
| 467 |
|
| else { |
| 468 |
|
| char esc[4]; |
| 469 |
3 |
| my_snprintf(esc, sizeof esc, "%%%02X", c); |
| 470 |
3 |
| sv_catpvn(out, esc, 3); |
| 471 |
|
| } |
| 472 |
|
| } |
| 473 |
|
| } |
| 474 |
|
| } |
| 475 |
|
| |
| 476 |
2 |
| SP -= items; |
| 477 |
2 |
▶ | XPUSHs(sv_2mortal(out)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(out)); |
0 |
2 |
2 |
|
| 478 |
2 |
| PUTBACK; |
| 479 |
2 |
| return; |
| 480 |
|
| } |
| 481 |
|
| |
| 482 |
|
| /* ---- the boot fetch --------------------------------------------------- */ |
| 483 |
|
| |
| 484 |
|
| /* Metadata bytes, from wherever the provider's `metadata` points. |
| 485 |
|
| * |
| 486 |
|
| * A `file:` URL or a bare path is read here; an http(s) URL goes through |
| 487 |
|
| * the fetch table on an agent built with ua_new, and the future is |
| 488 |
|
| * resolved BLOCKING, because this runs at on_compile, before the server |
| 489 |
|
| * forks. Fetch 0.23 is the floor because it pid-stamps its loop: a loop |
| 490 |
|
| * created in the parent and inherited by every worker is a hang this |
| 491 |
|
| * family has already recorded once. |
| 492 |
|
| * |
| 493 |
|
| * A FAILURE HERE IS A CROAK, not a warning. An application whose only |
| 494 |
|
| * login is SAML cannot sign anyone in without its provider, and refusing |
| 495 |
|
| * to boot says so at deploy time, in the deploy log, to the person |
| 496 |
|
| * deploying. A warning says it at the first login, to a user, as a 403, |
| 497 |
|
| * hours later, to somebody who cannot act on it. */ |
| 498 |
|
| /* The fetch table's map callback: shape the settled request into a |
| 499 |
|
| * [status, body] pair. Must not croak, and must return a +1 SV. */ |
| 500 |
0 |
| static SV *psaml_fetch_map(pTHX_ int ok, int status, AV *headers, |
| 501 |
|
| SV *body, SV *err, void *ud) { |
| 502 |
0 |
| AV *out = newAV(); |
| 503 |
|
| PERL_UNUSED_ARG(headers); |
| 504 |
|
| PERL_UNUSED_ARG(err); |
| 505 |
|
| PERL_UNUSED_ARG(ud); |
| 506 |
0 |
▶ | av_push(out, newSViv(ok ? status : 0)); |
Branch: 1 branch
| Branch | True | False | Total |
| av_push(out, newSViv(ok ? status : 0)); |
0 |
0 |
0 |
|
| 507 |
0 |
▶ | av_push(out, (ok && body && SvOK(body)) ? newSVsv(body) : newSV(0)); |
Branch: 3 branches
| Branch | True | False | Total |
| av_push(out, (ok && body && SvOK(body)) ? newSVsv(body) : newSV(0)); |
0 |
0 |
0 |
| av_push(out, (ok && body && SvOK(body)) ? newSVsv(body) : newSV(0)); |
0 |
0 |
0 |
| av_push(out, (ok && body && SvOK(body)) ? newSVsv(body) : newSV(0)); |
0 |
0 |
0 |
|
| 508 |
0 |
| return newRV_noinc((SV *)out); |
| 509 |
|
| } |
| 510 |
|
| |
| 511 |
11 |
| PERL_STATIC_INLINE SV *psaml_fetch_metadata(pTHX_ SV *app, SV *src, |
| 512 |
|
| const char *idp_name) { |
| 513 |
|
| STRLEN sl; |
| 514 |
11 |
| const char *sp = SvPVbyte(src, sl); |
| 515 |
11 |
▶ | int is_http = (sl > 7 && memEQ(sp, "http://", 7)) |
Branch: 1 branch
| Branch | True | False | Total |
| int is_http = (sl > 7 && memEQ(sp, "http://", 7)) |
11 |
0 |
11 |
|
| 516 |
22 |
▶ | || (sl > 8 && memEQ(sp, "https://", 8)); |
Branch: 3 branches
| Branch | True | False | Total |
| || (sl > 8 && memEQ(sp, "https://", 8)); |
11 |
0 |
11 |
| || (sl > 8 && memEQ(sp, "https://", 8)); |
11 |
0 |
11 |
| || (sl > 8 && memEQ(sp, "https://", 8)); |
0 |
11 |
11 |
|
| 517 |
|
| |
| 518 |
11 |
▶ | if (!is_http) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_http) { |
11 |
0 |
11 |
|
| 519 |
|
| /* file:/path or a bare path */ |
| 520 |
11 |
| const char *path = sp; |
| 521 |
11 |
| STRLEN pl = sl; |
| 522 |
|
| PerlIO *fh; |
| 523 |
|
| SV *out; |
| 524 |
11 |
▶ | if (pl > 5 && memEQ(path, "file:", 5)) { path += 5; pl -= 5; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (pl > 5 && memEQ(path, "file:", 5)) { path += 5; pl -= 5; } |
11 |
0 |
11 |
| if (pl > 5 && memEQ(path, "file:", 5)) { path += 5; pl -= 5; } |
11 |
0 |
11 |
|
| 525 |
|
| { |
| 526 |
11 |
| SV *pn = sv_2mortal(newSVpvn(path, pl)); |
| 527 |
11 |
| fh = PerlIO_open(SvPV_nolen(pn), "rb"); |
| 528 |
11 |
▶ | if (!fh) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!fh) |
0 |
11 |
11 |
|
| 529 |
0 |
| croak("%s: saml_idp '%s': cannot read metadata from %s: %s", |
| 530 |
|
| PSAML_WHO, idp_name, SvPV_nolen(pn), Strerror(errno)); |
| 531 |
|
| } |
| 532 |
11 |
| out = newSVpvs(""); |
| 533 |
|
| { |
| 534 |
|
| char buf[8192]; |
| 535 |
|
| SSize_t n; |
| 536 |
22 |
▶ | while ((n = PerlIO_read(fh, buf, sizeof buf)) > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| while ((n = PerlIO_read(fh, buf, sizeof buf)) > 0) |
11 |
11 |
22 |
|
| 537 |
11 |
| sv_catpvn(out, buf, (STRLEN)n); |
| 538 |
|
| } |
| 539 |
11 |
| PerlIO_close(fh); |
| 540 |
11 |
| return out; |
| 541 |
|
| } |
| 542 |
|
| |
| 543 |
|
| { |
| 544 |
0 |
| const fetch_abi *FE = psaml_fetch(aTHX); |
| 545 |
|
| SV *kv[4]; |
| 546 |
|
| SV *ua, *future, *res; |
| 547 |
|
| |
| 548 |
0 |
| kv[0] = sv_2mortal(newSVpvs("timeout")); |
| 549 |
0 |
| kv[1] = sv_2mortal(newSVnv(10.0)); |
| 550 |
0 |
| kv[2] = sv_2mortal(newSVpvs("tls_verify")); |
| 551 |
0 |
| kv[3] = sv_2mortal(newSViv(1)); |
| 552 |
0 |
| ua = FE->ua_new(aTHX_ kv, 4); |
| 553 |
0 |
▶ | if (!ua) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ua) |
0 |
0 |
0 |
|
| 554 |
0 |
| croak("%s: saml_idp '%s': could not build a user agent to fetch " |
| 555 |
|
| "metadata", PSAML_WHO, idp_name); |
| 556 |
0 |
| sv_2mortal(ua); |
| 557 |
|
| |
| 558 |
|
| /* `map` is invoked UNCONDITIONALLY by the table, with no NULL guard, |
| 559 |
|
| * so it must always be passed. Passing NULL is a SEGV at the first |
| 560 |
|
| * boot fetch, which is a long way from where it looks like it came |
| 561 |
|
| * from. Punk-OAuth2's pox_http.h says this in a comment; this dist |
| 562 |
|
| * learned it the other way. */ |
| 563 |
0 |
| future = FE->request(aTHX_ ua, "GET", SvPV_nolen(src), NULL, 0, |
| 564 |
|
| NULL, 0, 10.0, -1, psaml_fetch_map, NULL); |
| 565 |
0 |
▶ | if (!future) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!future) |
0 |
0 |
0 |
|
| 566 |
0 |
| croak("%s: saml_idp '%s': the metadata request could not be made", |
| 567 |
|
| PSAML_WHO, idp_name); |
| 568 |
0 |
| sv_2mortal(future); |
| 569 |
|
| |
| 570 |
|
| /* resolved blocking: this is boot, there is no loop to yield to yet, |
| 571 |
|
| * and the whole point is that the application does not start without |
| 572 |
|
| * its provider */ |
| 573 |
0 |
| res = psaml_call(aTHX_ future, "get", NULL, 0); |
| 574 |
0 |
▶ | if (!res) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!res) |
0 |
0 |
0 |
|
| 575 |
0 |
| croak("%s: saml_idp '%s': fetching %s produced no response", |
| 576 |
|
| PSAML_WHO, idp_name, SvPV_nolen(src)); |
| 577 |
0 |
| sv_2mortal(res); |
| 578 |
|
| |
| 579 |
|
| /* the map above shaped it into [status, body] */ |
| 580 |
|
| { |
| 581 |
|
| AV *pair; |
| 582 |
|
| SV **st, **bd; |
| 583 |
|
| IV status; |
| 584 |
0 |
▶ | if (!SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVAV) |
0 |
0 |
0 |
| if (!SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVAV) |
0 |
0 |
0 |
|
| 585 |
0 |
| croak("%s: saml_idp '%s': fetching %s produced no usable response", |
| 586 |
|
| PSAML_WHO, idp_name, SvPV_nolen(src)); |
| 587 |
0 |
| pair = (AV *)SvRV(res); |
| 588 |
0 |
| st = av_fetch(pair, 0, 0); |
| 589 |
0 |
| bd = av_fetch(pair, 1, 0); |
| 590 |
0 |
▶ | status = (st && *st) ? SvIV(*st) : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| status = (st && *st) ? SvIV(*st) : 0; |
0 |
0 |
0 |
| status = (st && *st) ? SvIV(*st) : 0; |
0 |
0 |
0 |
|
| 591 |
0 |
▶ | if (status < 200 || status > 299) |
Branch: 2 branches
| Branch | True | False | Total |
| if (status < 200 || status > 299) |
0 |
0 |
0 |
| if (status < 200 || status > 299) |
0 |
0 |
0 |
|
| 592 |
0 |
| croak("%s: saml_idp '%s': fetching %s answered %" IVdf, |
| 593 |
|
| PSAML_WHO, idp_name, SvPV_nolen(src), status); |
| 594 |
0 |
▶ | if (!bd || !*bd || !SvOK(*bd) || !SvCUR(*bd)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (!bd || !*bd || !SvOK(*bd) || !SvCUR(*bd)) |
0 |
0 |
0 |
| if (!bd || !*bd || !SvOK(*bd) || !SvCUR(*bd)) |
0 |
0 |
0 |
| if (!bd || !*bd || !SvOK(*bd) || !SvCUR(*bd)) |
0 |
0 |
0 |
| if (!bd || !*bd || !SvOK(*bd) || !SvCUR(*bd)) |
0 |
0 |
0 |
|
| 595 |
0 |
| croak("%s: saml_idp '%s': fetching %s answered an empty body", |
| 596 |
|
| PSAML_WHO, idp_name, SvPV_nolen(src)); |
| 597 |
|
| PERL_UNUSED_ARG(app); |
| 598 |
0 |
| return newSVsv(*bd); |
| 599 |
|
| } |
| 600 |
|
| } |
| 601 |
|
| } |
| 602 |
|
| |
| 603 |
|
| |
| 604 |
|
| /* Metadata bytes into a provider's recorded configuration. |
| 605 |
|
| * |
| 606 |
|
| * Shared by the boot resolution and the rotation refetch below, so a |
| 607 |
|
| * provider re-read mid-run ends up with exactly the configuration it |
| 608 |
|
| * would have had at boot. Two code paths filling the same hash by |
| 609 |
|
| * different rules would be a bug that could only appear after a key |
| 610 |
|
| * rotation, which is the least observable moment there is. */ |
| 611 |
11 |
| PERL_STATIC_INLINE void psaml_idp_merge(pTHX_ HV *cfg, SV *bytes, HV *opts) { |
| 612 |
11 |
| SV *want = psaml_hget(aTHX_ cfg, "entity_id"); |
| 613 |
13 |
▶ | SV *read = psaml_idp_from_metadata(aTHX_ bytes, |
Branch: 1 branch
| Branch | True | False | Total |
| SV *read = psaml_idp_from_metadata(aTHX_ bytes, |
2 |
9 |
11 |
|
| 614 |
4 |
▶ | (want && SvOK(want)) ? SvPV_nolen(want) : NULL, |
Branch: 1 branch
| Branch | True | False | Total |
| (want && SvOK(want)) ? SvPV_nolen(want) : NULL, |
2 |
0 |
2 |
|
| 615 |
|
| psaml_opt_bool(aTHX_ opts, "enforce_cert_validity", 0)); |
| 616 |
11 |
| HV *got = (HV *)SvRV(read); |
| 617 |
|
| HE *he; |
| 618 |
11 |
| sv_2mortal(read); |
| 619 |
11 |
| hv_iterinit(got); |
| 620 |
77 |
▶ | while ((he = hv_iternext(got))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(got))) { |
66 |
11 |
77 |
|
| 621 |
|
| STRLEN kl; |
| 622 |
66 |
▶ | const char *k = HePV(he, kl); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *k = HePV(he, kl); |
0 |
66 |
66 |
|
| 623 |
66 |
| (void)hv_store(cfg, k, (I32)kl, newSVsv(HeVAL(he)), 0); |
| 624 |
|
| } |
| 625 |
11 |
| } |
| 626 |
|
| |
| 627 |
|
| |
| 628 |
|
| /* ---- the rotation refetch ---------------------------------------------- */ |
| 629 |
|
| |
| 630 |
|
| /* A provider that rotates its signing key signs the next assertion with a |
| 631 |
|
| * certificate this application has never seen. Every login then fails |
| 632 |
|
| * with `bad_signature` until somebody notices and redeploys - an outage |
| 633 |
|
| * caused by the far side doing something routine that it announced only |
| 634 |
|
| * in its metadata. |
| 635 |
|
| * |
| 636 |
|
| * So: one re-read of the metadata, on the failure, and one retry. |
| 637 |
|
| * |
| 638 |
|
| * ONLY for a provider configured from `metadata`. An explicit |
| 639 |
|
| * entity_id/sso_url/certs trio is what the deployer typed, and this dist |
| 640 |
|
| * does not overwrite it from the network. |
| 641 |
|
| * |
| 642 |
|
| * RATE LIMITED, and that is the security half rather than a politeness. |
| 643 |
|
| * `bad_signature` is also exactly what a forged Response produces, so an |
| 644 |
|
| * unlimited refetch would let anybody make this application fetch a URL |
| 645 |
|
| * as fast as they can POST to the ACS. `metadata_refresh` is the floor |
| 646 |
|
| * between two refetches, and the stamp is written BEFORE the fetch so a |
| 647 |
|
| * provider that hangs until the timeout cannot leave the limit unset. |
| 648 |
|
| * |
| 649 |
|
| * PER WORKER: the stamp lives on the provider's configuration hash, of |
| 650 |
|
| * which every worker holds its own copy after the fork. A deployment with |
| 651 |
|
| * N workers therefore allows at most N refetches per interval. That is |
| 652 |
|
| * the same bound the replay store lives with, and it is written down |
| 653 |
|
| * here because it is the kind of thing that looks like a leak later. |
| 654 |
|
| * |
| 655 |
|
| * CROAKS on a fetch failure, so the caller must be inside a G_EVAL: a |
| 656 |
|
| * provider that is down during somebody's login must produce a refused |
| 657 |
|
| * login, never a 500. */ |
| 658 |
6 |
| PERL_STATIC_INLINE int psaml_idp_refetch(pTHX_ SV *app, HV *opts, |
| 659 |
|
| SV *idp_name, HV *cfg, IV now) { |
| 660 |
6 |
| SV *meta = psaml_hget(aTHX_ cfg, "metadata"); |
| 661 |
|
| SV **le; |
| 662 |
|
| IV every, last; |
| 663 |
|
| SV *bytes; |
| 664 |
|
| |
| 665 |
6 |
▶ | if (!meta || !SvOK(meta)) return 0; /* an explicit trio */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!meta || !SvOK(meta)) return 0; /* an explicit trio */ |
4 |
2 |
6 |
| if (!meta || !SvOK(meta)) return 0; /* an explicit trio */ |
0 |
4 |
4 |
|
| 666 |
4 |
▶ | if (!idp_name || !SvOK(idp_name)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!idp_name || !SvOK(idp_name)) return 0; |
4 |
0 |
4 |
| if (!idp_name || !SvOK(idp_name)) return 0; |
0 |
4 |
4 |
|
| 667 |
|
| |
| 668 |
4 |
| every = psaml_opt_iv(aTHX_ opts, "metadata_refresh", 3600); |
| 669 |
4 |
▶ | if (every <= 0) return 0; /* turned off deliberately */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (every <= 0) return 0; /* turned off deliberately */ |
1 |
3 |
4 |
|
| 670 |
|
| |
| 671 |
3 |
| le = hv_fetchs(cfg, "last_refresh", 0); |
| 672 |
3 |
▶ | last = (le && *le && SvOK(*le)) ? SvIV(*le) : 0; |
Branch: 3 branches
| Branch | True | False | Total |
| last = (le && *le && SvOK(*le)) ? SvIV(*le) : 0; |
1 |
2 |
3 |
| last = (le && *le && SvOK(*le)) ? SvIV(*le) : 0; |
1 |
0 |
1 |
| last = (le && *le && SvOK(*le)) ? SvIV(*le) : 0; |
1 |
0 |
1 |
|
| 673 |
3 |
▶ | if (last && now - last < every) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (last && now - last < every) return 0; |
1 |
2 |
3 |
| if (last && now - last < every) return 0; |
1 |
0 |
1 |
|
| 674 |
|
| |
| 675 |
2 |
| (void)hv_stores(cfg, "last_refresh", newSViv(now)); |
| 676 |
|
| |
| 677 |
2 |
| bytes = sv_2mortal(psaml_fetch_metadata(aTHX_ app, meta, |
| 678 |
|
| SvPV_nolen(idp_name))); |
| 679 |
2 |
| psaml_idp_merge(aTHX_ cfg, bytes, opts); |
| 680 |
2 |
| return 1; |
| 681 |
|
| } |
| 682 |
|
| |
| 683 |
|
| |
| 684 |
|
| /* Is this failure one a key rotation would explain? |
| 685 |
|
| * |
| 686 |
|
| * `bad_signature` is a signature no configured key verifies, and |
| 687 |
|
| * `no_key` is no configured key at all. Both are answered by reading the |
| 688 |
|
| * metadata again. `bad_digest` is NOT: the signature verified and the |
| 689 |
|
| * content did not match it, which is tampering, and refetching would be |
| 690 |
|
| * fetching a URL on an attacker's say-so for no possible benefit. */ |
| 691 |
8 |
| PERL_STATIC_INLINE int psaml_rotation_explains(pTHX_ SV *err) { |
| 692 |
|
| SV *code; |
| 693 |
8 |
▶ | if (!err || !SvROK(err) || SvTYPE(SvRV(err)) != SVt_PVHV) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!err || !SvROK(err) || SvTYPE(SvRV(err)) != SVt_PVHV) return 0; |
8 |
0 |
8 |
| if (!err || !SvROK(err) || SvTYPE(SvRV(err)) != SVt_PVHV) return 0; |
8 |
0 |
8 |
| if (!err || !SvROK(err) || SvTYPE(SvRV(err)) != SVt_PVHV) return 0; |
0 |
8 |
8 |
|
| 694 |
8 |
| code = psaml_hget(aTHX_ (HV *)SvRV(err), "code"); |
| 695 |
8 |
▶ | if (!code || !SvOK(code)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!code || !SvOK(code)) return 0; |
8 |
0 |
8 |
| if (!code || !SvOK(code)) return 0; |
0 |
8 |
8 |
|
| 696 |
8 |
| return strEQ(SvPV_nolen(code), PSAML_E_BAD_SIGNATURE) |
| 697 |
10 |
▶ | || strEQ(SvPV_nolen(code), PSAML_E_NO_KEY); |
Branch: 2 branches
| Branch | True | False | Total |
| || strEQ(SvPV_nolen(code), PSAML_E_NO_KEY); |
2 |
6 |
8 |
| || strEQ(SvPV_nolen(code), PSAML_E_NO_KEY); |
0 |
2 |
2 |
|
| 698 |
|
| } |
| 699 |
|
| |
| 700 |
|
| |
| 701 |
|
| /* ---- the routes -------------------------------------------------------- */ |
| 702 |
|
| |
| 703 |
|
| /* WantAssertionsSigned in the published metadata reflects what this |
| 704 |
|
| * application actually requires. `response` alone is the one setting that |
| 705 |
|
| * does not require a signed assertion. */ |
| 706 |
14 |
| PERL_STATIC_INLINE int psaml_want_assertions_signed(pTHX_ HV *opts) { |
| 707 |
14 |
| SV *r = psaml_hget(aTHX_ opts, "require_signed"); |
| 708 |
28 |
▶ | return !(r && SvOK(r) && strEQ(SvPV_nolen(r), "response")); |
Branch: 3 branches
| Branch | True | False | Total |
| return !(r && SvOK(r) && strEQ(SvPV_nolen(r), "response")); |
14 |
0 |
14 |
| return !(r && SvOK(r) && strEQ(SvPV_nolen(r), "response")); |
14 |
0 |
14 |
| return !(r && SvOK(r) && strEQ(SvPV_nolen(r), "response")); |
14 |
0 |
14 |
|
| 709 |
|
| } |
| 710 |
|
| |
| 711 |
|
| |
| 712 |
|
| /* Everything below runs per request. The four routes are mounted at |
| 713 |
|
| * on_compile from the ONE mount string phase 4 recorded, so the ACS URL |
| 714 |
|
| * in the AuthnRequest, in the metadata, in the Destination check and in |
| 715 |
|
| * the route that receives it are four readings of one variable. */ |
| 716 |
|
| |
| 717 |
|
| PERL_STATIC_INLINE SV *psaml_ctx_app(pTHX_ SV *c) { |
| 718 |
|
| return psaml_call(aTHX_ c, "app", NULL, 0); |
| 719 |
|
| } |
| 720 |
|
| |
| 721 |
94 |
| PERL_STATIC_INLINE HV *psaml_opts_of(pTHX_ SV *app) { |
| 722 |
94 |
| SV *o = psaml_app_get(aTHX_ app, PSAML_K_OPTS); |
| 723 |
94 |
▶ | if (!o || !SvROK(o)) croak("%s: the plugin is not configured", PSAML_WHO); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!o || !SvROK(o)) croak("%s: the plugin is not configured", PSAML_WHO); |
94 |
0 |
94 |
| if (!o || !SvROK(o)) croak("%s: the plugin is not configured", PSAML_WHO); |
0 |
94 |
94 |
|
| 724 |
94 |
| return (HV *)SvRV(o); |
| 725 |
|
| } |
| 726 |
|
| |
| 727 |
|
| /* GET <mount>/metadata */ |
| 728 |
|
| XS_INTERNAL(psaml_rt_metadata); |
| 729 |
14 |
| XS_INTERNAL(psaml_rt_metadata) { |
| 730 |
14 |
| dXSARGS; |
| 731 |
14 |
| SV *app = psaml_cap_slot(aTHX_ cv, 0); |
| 732 |
14 |
▶ | SV *c = items > 0 ? ST(0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *c = items > 0 ? ST(0) : NULL; |
14 |
0 |
14 |
|
| 733 |
|
| HV *opts; |
| 734 |
|
| SV *doc, *argv[2], *res; |
| 735 |
|
| |
| 736 |
14 |
▶ | if (!app || !c) { PERL_UNUSED_VAR(ax); XSRETURN_EMPTY; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!app || !c) { PERL_UNUSED_VAR(ax); XSRETURN_EMPTY; } |
14 |
0 |
14 |
| if (!app || !c) { PERL_UNUSED_VAR(ax); XSRETURN_EMPTY; } |
0 |
14 |
14 |
|
| 737 |
14 |
| opts = psaml_opts_of(aTHX_ app); |
| 738 |
14 |
| doc = sv_2mortal(psaml_sp_metadata(aTHX_ |
| 739 |
|
| psaml_app_get(aTHX_ app, PSAML_K_ENTITY), |
| 740 |
|
| psaml_app_get(aTHX_ app, PSAML_K_ACS), |
| 741 |
|
| psaml_hget(aTHX_ opts, "cert"), |
| 742 |
|
| psaml_hget(aTHX_ opts, "name_id_format"), |
| 743 |
|
| psaml_opt_bool(aTHX_ opts, "sign_requests", 0), |
| 744 |
|
| psaml_want_assertions_signed(aTHX_ opts))); |
| 745 |
|
| |
| 746 |
14 |
▶ | if (psaml_opt_bool(aTHX_ opts, "sign_metadata", 0)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (psaml_opt_bool(aTHX_ opts, "sign_metadata", 0)) { |
0 |
14 |
14 |
|
| 747 |
0 |
| SV *id = sv_2mortal(psaml_request_id(aTHX)); |
| 748 |
0 |
| SV *withid = sv_2mortal(newSVpvs("")); |
| 749 |
|
| STRLEN dl; |
| 750 |
0 |
| const char *dp = SvPVbyte(doc, dl); |
| 751 |
0 |
| const char *gt = (const char *)memchr(dp, ' ', dl); |
| 752 |
|
| /* the ID goes on the root before it is signed, because a Reference |
| 753 |
|
| * names an ID and the document as built carries none */ |
| 754 |
0 |
| sv_catpvn(withid, dp, (STRLEN)(gt - dp)); |
| 755 |
0 |
| sv_catpvs(withid, " ID=\""); |
| 756 |
0 |
| sv_catsv(withid, id); |
| 757 |
0 |
| sv_catpvs(withid, "\""); |
| 758 |
0 |
| sv_catpvn(withid, gt, dl - (STRLEN)(gt - dp)); |
| 759 |
0 |
| doc = sv_2mortal(psaml_sign_document(aTHX_ withid, id, |
| 760 |
|
| psaml_hget(aTHX_ opts, "key"), |
| 761 |
|
| psaml_hget(aTHX_ opts, "cert"))); |
| 762 |
|
| } |
| 763 |
|
| |
| 764 |
|
| /* A raw PSGI triplet, because the media type here is the |
| 765 |
|
| * specification's and neither $c->text nor Punk 0.46's $c->xml will |
| 766 |
|
| * send it: text sends text/plain and xml sends application/xml. This |
| 767 |
|
| * is the shape Punk-Feed returns, for the same reason. */ |
| 768 |
|
| { |
| 769 |
14 |
| AV *resp = newAV(), *hdrs = newAV(), *body = newAV(); |
| 770 |
14 |
| av_push(hdrs, newSVpvs("Content-Type")); |
| 771 |
14 |
| av_push(hdrs, newSVpvs(PSAML_CT_METADATA)); |
| 772 |
14 |
| av_push(hdrs, newSVpvs("Content-Length")); |
| 773 |
14 |
| av_push(hdrs, newSViv((IV)SvCUR(doc))); |
| 774 |
14 |
| av_push(body, newSVsv(doc)); |
| 775 |
14 |
| av_push(resp, newSViv(200)); |
| 776 |
14 |
| av_push(resp, newRV_noinc((SV *)hdrs)); |
| 777 |
14 |
| av_push(resp, newRV_noinc((SV *)body)); |
| 778 |
14 |
| res = newRV_noinc((SV *)resp); |
| 779 |
|
| } |
| 780 |
|
| PERL_UNUSED_VAR(argv); |
| 781 |
|
| /* ST(0)/XSRETURN, never a captured SP: see the note at psaml_rt_login. */ |
| 782 |
14 |
| ST(0) = sv_2mortal(res); |
| 783 |
14 |
| XSRETURN(1); |
| 784 |
|
| } |
| 785 |
|
| |
| 786 |
|
| /* GET <mount>/login/:idp and GET <mount>/login */ |
| 787 |
|
| XS_INTERNAL(psaml_rt_login); |
| 788 |
23 |
| XS_INTERNAL(psaml_rt_login) { |
| 789 |
23 |
| dXSARGS; |
| 790 |
23 |
| SV *app = psaml_cap_slot(aTHX_ cv, 0); |
| 791 |
23 |
▶ | SV *c = items > 0 ? ST(0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *c = items > 0 ? ST(0) : NULL; |
23 |
0 |
23 |
|
| 792 |
|
| HV *opts, *idps, *rec; |
| 793 |
23 |
| SV *name = NULL, *cfgsv, *to, *id, *instant, *xml, *url, *argv[3], *res; |
| 794 |
|
| AV *records, *order; |
| 795 |
|
| SV *cookie, *value, *setc; |
| 796 |
|
| IV ttl, now; |
| 797 |
|
| HV *cfg; |
| 798 |
|
| |
| 799 |
23 |
▶ | if (!app || !c) { PERL_UNUSED_VAR(ax); XSRETURN_EMPTY; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!app || !c) { PERL_UNUSED_VAR(ax); XSRETURN_EMPTY; } |
23 |
0 |
23 |
| if (!app || !c) { PERL_UNUSED_VAR(ax); XSRETURN_EMPTY; } |
0 |
23 |
23 |
|
| 800 |
23 |
| opts = psaml_opts_of(aTHX_ app); |
| 801 |
23 |
| idps = (HV *)SvRV(psaml_app_get(aTHX_ app, PSAML_K_IDPS)); |
| 802 |
23 |
| order = (AV *)SvRV(psaml_app_get(aTHX_ app, PSAML_K_IDPORDER)); |
| 803 |
|
| |
| 804 |
23 |
▶ | if (items > 1 && SvOK(ST(1)) && SvCUR(ST(1))) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (items > 1 && SvOK(ST(1)) && SvCUR(ST(1))) { |
0 |
23 |
23 |
| if (items > 1 && SvOK(ST(1)) && SvCUR(ST(1))) { |
0 |
0 |
0 |
| if (items > 1 && SvOK(ST(1)) && SvCUR(ST(1))) { |
0 |
0 |
0 |
|
| 805 |
0 |
| name = ST(1); |
| 806 |
|
| } |
| 807 |
23 |
▶ | else if (av_len(order) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (av_len(order) == 0) { |
23 |
0 |
23 |
|
| 808 |
|
| /* exactly one provider: /login with no name is unambiguous */ |
| 809 |
23 |
| SV **e = av_fetch(order, 0, 0); |
| 810 |
23 |
▶ | name = (e && *e) ? *e : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| name = (e && *e) ? *e : NULL; |
23 |
0 |
23 |
| name = (e && *e) ? *e : NULL; |
23 |
0 |
23 |
|
| 811 |
|
| } |
| 812 |
23 |
▶ | if (!name) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!name) { |
0 |
23 |
23 |
|
| 813 |
|
| /* more than one provider and none named: a 404, because guessing |
| 814 |
|
| * which one the user meant is guessing which company signs them in */ |
| 815 |
0 |
| argv[0] = sv_2mortal(newSVpvs("no such login")); |
| 816 |
0 |
| argv[1] = sv_2mortal(newSViv(404)); |
| 817 |
0 |
| res = psaml_call(aTHX_ c, "text", argv, 2); |
| 818 |
0 |
▶ | if (!res) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!res) XSRETURN_EMPTY; |
0 |
0 |
0 |
|
| 819 |
0 |
| ST(0) = sv_2mortal(res); |
| 820 |
0 |
| XSRETURN(1); |
| 821 |
|
| } |
| 822 |
|
| |
| 823 |
|
| { |
| 824 |
|
| STRLEN nl; |
| 825 |
23 |
| const char *np = SvPV_const(name, nl); |
| 826 |
23 |
| SV **e = hv_fetch(idps, np, (I32)nl, 0); |
| 827 |
23 |
▶ | if (!e || !*e || !SvROK(*e)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!e || !*e || !SvROK(*e)) { |
23 |
0 |
23 |
| if (!e || !*e || !SvROK(*e)) { |
23 |
0 |
23 |
| if (!e || !*e || !SvROK(*e)) { |
0 |
23 |
23 |
|
| 828 |
0 |
| argv[0] = sv_2mortal(newSVpvs("no such login")); |
| 829 |
0 |
| argv[1] = sv_2mortal(newSViv(404)); |
| 830 |
0 |
| res = psaml_call(aTHX_ c, "text", argv, 2); |
| 831 |
0 |
▶ | if (!res) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!res) XSRETURN_EMPTY; |
0 |
0 |
0 |
|
| 832 |
0 |
| ST(0) = sv_2mortal(res); |
| 833 |
0 |
| XSRETURN(1); |
| 834 |
|
| } |
| 835 |
23 |
| cfgsv = *e; |
| 836 |
|
| } |
| 837 |
23 |
| cfg = (HV *)SvRV(cfgsv); |
| 838 |
|
| |
| 839 |
|
| /* `to` came from a link somebody wrote, so it goes through safe_path |
| 840 |
|
| * before it is recorded and never after */ |
| 841 |
|
| { |
| 842 |
|
| SV *raw; |
| 843 |
23 |
| argv[0] = sv_2mortal(newSVpvs("to")); |
| 844 |
23 |
| raw = sv_2mortal(psaml_call(aTHX_ c, "param", argv, 1)); |
| 845 |
23 |
▶ | argv[0] = raw ? raw : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| argv[0] = raw ? raw : &PL_sv_undef; |
4 |
19 |
23 |
|
| 846 |
23 |
| argv[1] = psaml_hget(aTHX_ opts, "default_to"); |
| 847 |
23 |
| to = sv_2mortal(psaml_call(aTHX_ c, "safe_path", argv, 2)); |
| 848 |
23 |
▶ | if (!to || !SvOK(to)) to = psaml_hget(aTHX_ opts, "default_to"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!to || !SvOK(to)) to = psaml_hget(aTHX_ opts, "default_to"); |
23 |
0 |
23 |
| if (!to || !SvOK(to)) to = psaml_hget(aTHX_ opts, "default_to"); |
0 |
23 |
23 |
|
| 849 |
|
| } |
| 850 |
|
| |
| 851 |
23 |
| now = (IV)time(NULL); |
| 852 |
23 |
| ttl = psaml_opt_iv(aTHX_ opts, "flow_ttl", 600); |
| 853 |
23 |
| id = sv_2mortal(psaml_request_id(aTHX)); |
| 854 |
|
| |
| 855 |
|
| { |
| 856 |
|
| char b[64]; |
| 857 |
23 |
| STRLEN bn = psaml_time_format(b, sizeof b, now); |
| 858 |
23 |
| instant = sv_2mortal(newSVpvn(b, bn)); |
| 859 |
|
| } |
| 860 |
23 |
| xml = sv_2mortal(psaml_authn_request(aTHX_ id, instant, |
| 861 |
|
| psaml_hget(aTHX_ cfg, "sso_url"), |
| 862 |
|
| psaml_app_get(aTHX_ app, PSAML_K_ACS), |
| 863 |
|
| psaml_app_get(aTHX_ app, PSAML_K_ENTITY), |
| 864 |
|
| psaml_hget(aTHX_ opts, "name_id_format"), |
| 865 |
|
| psaml_opt_bool(aTHX_ opts, "force_authn", 0))); |
| 866 |
|
| |
| 867 |
23 |
▶ | url = sv_2mortal(psaml_redirect_url(aTHX_ |
Branch: 1 branch
| Branch | True | False | Total |
| url = sv_2mortal(psaml_redirect_url(aTHX_ |
0 |
23 |
23 |
|
| 868 |
|
| psaml_hget(aTHX_ cfg, "sso_url"), xml, id, |
| 869 |
|
| psaml_opt_bool(aTHX_ opts, "sign_requests", 0) |
| 870 |
|
| ? psaml_hget(aTHX_ opts, "key") : NULL)); |
| 871 |
|
| |
| 872 |
|
| /* the record into the cookie. Nothing is stored server side: the |
| 873 |
|
| * cookie is the store and the signature is what makes it one. */ |
| 874 |
23 |
| argv[0] = sv_2mortal(newSVpvs(PSAML_FLOW_COOKIE)); |
| 875 |
23 |
| cookie = sv_2mortal(psaml_call(aTHX_ c, "cookie", argv, 1)); |
| 876 |
23 |
| records = psaml_flow_parse(aTHX_ cookie, |
| 877 |
|
| psaml_hget(aTHX_ opts, "secret"), now, ttl); |
| 878 |
23 |
| sv_2mortal(newRV_noinc((SV *)records)); |
| 879 |
|
| |
| 880 |
23 |
| rec = newHV(); |
| 881 |
23 |
| (void)hv_stores(rec, "id", newSVsv(id)); |
| 882 |
23 |
| (void)hv_stores(rec, "idp", newSVsv(name)); |
| 883 |
23 |
▶ | (void)hv_stores(rec, "to", newSVsv(to ? to : &PL_sv_undef)); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_stores(rec, "to", newSVsv(to ? to : &PL_sv_undef)); |
23 |
0 |
23 |
|
| 884 |
23 |
| (void)hv_stores(rec, "issued", newSViv(now)); |
| 885 |
23 |
| psaml_flow_add(aTHX_ records, newRV_noinc((SV *)rec)); |
| 886 |
|
| |
| 887 |
23 |
| value = sv_2mortal(psaml_flow_serialise(aTHX_ records, |
| 888 |
|
| psaml_hget(aTHX_ opts, "secret"))); |
| 889 |
23 |
| setc = sv_2mortal(psaml_flow_cookie(aTHX_ value, |
| 890 |
|
| psaml_app_get(aTHX_ app, PSAML_K_MOUNT), ttl)); |
| 891 |
23 |
| argv[0] = sv_2mortal(newSVpvs("Set-Cookie")); |
| 892 |
23 |
| argv[1] = setc; |
| 893 |
23 |
| SvREFCNT_dec(psaml_call(aTHX_ c, "header", argv, 2)); |
| 894 |
|
| |
| 895 |
23 |
| argv[0] = url; |
| 896 |
23 |
| argv[1] = sv_2mortal(newSViv(302)); |
| 897 |
23 |
| res = psaml_call(aTHX_ c, "redirect", argv, 2); |
| 898 |
|
| /* ST(0) and XSRETURN, NEVER a captured SP. |
| 899 |
|
| * |
| 900 |
|
| * This body calls into Perl repeatedly - param, safe_path, cookie, |
| 901 |
|
| * header, redirect - and Perl code reallocates the argument stack, so |
| 902 |
|
| * the `sp` dXSARGS captured at entry is stale by the time the answer is |
| 903 |
|
| * pushed. ST() indexes from PL_stack_base through `ax`, which survives |
| 904 |
|
| * a realloc. Getting this wrong is a SEGV, not a wrong answer, and it |
| 905 |
|
| * is the second time this dist has paid for it. */ |
| 906 |
23 |
▶ | if (!res) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!res) XSRETURN_EMPTY; |
0 |
23 |
23 |
|
| 907 |
23 |
| ST(0) = sv_2mortal(res); |
| 908 |
23 |
| XSRETURN(1); |
| 909 |
|
| } |
| 910 |
|
| |
| 911 |
|
| |
| 912 |
|
| /* POST <mount>/acs |
| 913 |
|
| * |
| 914 |
|
| * The one request in this application that is supposed to look exactly |
| 915 |
|
| * like a cross-site request forgery. What protects it is not a token, it |
| 916 |
|
| * is phase 6 in its entirety. */ |
| 917 |
|
| XS_INTERNAL(psaml_rt_acs); |
| 918 |
36 |
| XS_INTERNAL(psaml_rt_acs) { |
| 919 |
36 |
| dXSARGS; |
| 920 |
36 |
| SV *app = psaml_cap_slot(aTHX_ cv, 0); |
| 921 |
36 |
▶ | SV *c = items > 0 ? ST(0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *c = items > 0 ? ST(0) : NULL; |
36 |
0 |
36 |
|
| 922 |
|
| HV *opts, *idps, *login; |
| 923 |
|
| AV *records; |
| 924 |
36 |
| SV *cookie, *relay, *field, *rec = NULL, *argv[4], *res; |
| 925 |
36 |
| SV *idp_name = NULL, *to = NULL, *flow_id = NULL; |
| 926 |
|
| IV ttl, now; |
| 927 |
36 |
| int failed = 0; |
| 928 |
36 |
| SV *err = NULL, *identity = NULL; |
| 929 |
|
| |
| 930 |
36 |
▶ | if (!app || !c) { PERL_UNUSED_VAR(ax); XSRETURN_EMPTY; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!app || !c) { PERL_UNUSED_VAR(ax); XSRETURN_EMPTY; } |
36 |
0 |
36 |
| if (!app || !c) { PERL_UNUSED_VAR(ax); XSRETURN_EMPTY; } |
0 |
36 |
36 |
|
| 931 |
36 |
| opts = psaml_opts_of(aTHX_ app); |
| 932 |
36 |
| idps = (HV *)SvRV(psaml_app_get(aTHX_ app, PSAML_K_IDPS)); |
| 933 |
36 |
| login = (HV *)SvRV(psaml_app_get(aTHX_ app, PSAML_K_LOGIN)); |
| 934 |
36 |
| now = (IV)time(NULL); |
| 935 |
36 |
| ttl = psaml_opt_iv(aTHX_ opts, "flow_ttl", 600); |
| 936 |
|
| |
| 937 |
36 |
| argv[0] = sv_2mortal(newSVpvs("RelayState")); |
| 938 |
36 |
| relay = sv_2mortal(psaml_call(aTHX_ c, "param", argv, 1)); |
| 939 |
36 |
| argv[0] = sv_2mortal(newSVpvs("SAMLResponse")); |
| 940 |
36 |
| field = sv_2mortal(psaml_call(aTHX_ c, "param", argv, 1)); |
| 941 |
|
| |
| 942 |
36 |
| argv[0] = sv_2mortal(newSVpvs(PSAML_FLOW_COOKIE)); |
| 943 |
36 |
| cookie = sv_2mortal(psaml_call(aTHX_ c, "cookie", argv, 1)); |
| 944 |
36 |
| records = psaml_flow_parse(aTHX_ cookie, |
| 945 |
|
| psaml_hget(aTHX_ opts, "secret"), now, ttl); |
| 946 |
36 |
| sv_2mortal(newRV_noinc((SV *)records)); |
| 947 |
|
| |
| 948 |
36 |
▶ | if (relay && SvOK(relay) && SvCUR(relay)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (relay && SvOK(relay) && SvCUR(relay)) |
21 |
15 |
36 |
| if (relay && SvOK(relay) && SvCUR(relay)) |
21 |
0 |
21 |
| if (relay && SvOK(relay) && SvCUR(relay)) |
20 |
1 |
21 |
|
| 949 |
20 |
| rec = psaml_flow_take(aTHX_ records, relay); |
| 950 |
36 |
▶ | if (rec) sv_2mortal(rec); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rec) sv_2mortal(rec); |
11 |
25 |
36 |
|
| 951 |
|
| |
| 952 |
|
| /* THE COOKIE IS WRITTEN BACK NOW, BEFORE ANY VERIFICATION. |
| 953 |
|
| * |
| 954 |
|
| * One flow, one answer, whichever way it goes. A record left in place |
| 955 |
|
| * on failure would let an attacker retry a tampered assertion against |
| 956 |
|
| * the same flow until something got through, and the flow record is |
| 957 |
|
| * the only thing standing between an SP-initiated login and a replay. */ |
| 958 |
|
| { |
| 959 |
36 |
| SV *value = sv_2mortal(psaml_flow_serialise(aTHX_ records, |
| 960 |
|
| psaml_hget(aTHX_ opts, "secret"))); |
| 961 |
36 |
| SV *setc = sv_2mortal(psaml_flow_cookie(aTHX_ value, |
| 962 |
|
| psaml_app_get(aTHX_ app, PSAML_K_MOUNT), ttl)); |
| 963 |
36 |
| argv[0] = sv_2mortal(newSVpvs("Set-Cookie")); |
| 964 |
36 |
| argv[1] = setc; |
| 965 |
36 |
| SvREFCNT_dec(psaml_call(aTHX_ c, "header", argv, 2)); |
| 966 |
|
| } |
| 967 |
|
| |
| 968 |
36 |
▶ | if (rec) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rec) { |
11 |
25 |
36 |
|
| 969 |
11 |
| HV *r = (HV *)SvRV(rec); |
| 970 |
11 |
| idp_name = psaml_hget(aTHX_ r, "idp"); |
| 971 |
11 |
| to = psaml_hget(aTHX_ r, "to"); |
| 972 |
11 |
| flow_id = psaml_hget(aTHX_ r, "id"); |
| 973 |
|
| } |
| 974 |
25 |
▶ | else if (psaml_opt_bool(aTHX_ opts, "allow_idp_initiated", 0)) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (psaml_opt_bool(aTHX_ opts, "allow_idp_initiated", 0)) { |
8 |
17 |
25 |
|
| 975 |
|
| /* no record, and the deployment has decided its provider may start a |
| 976 |
|
| * login. RelayState is then the provider's own and IS treated as a |
| 977 |
|
| * path, which is the one place this dist reads it that way, so it |
| 978 |
|
| * goes through safe_path. */ |
| 979 |
8 |
▶ | argv[0] = relay ? relay : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| argv[0] = relay ? relay : &PL_sv_undef; |
7 |
1 |
8 |
|
| 980 |
8 |
| argv[1] = psaml_hget(aTHX_ opts, "default_to"); |
| 981 |
8 |
| to = sv_2mortal(psaml_call(aTHX_ c, "safe_path", argv, 2)); |
| 982 |
8 |
▶ | if (av_len((AV *)SvRV(psaml_app_get(aTHX_ app, PSAML_K_IDPORDER))) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len((AV *)SvRV(psaml_app_get(aTHX_ app, PSAML_K_IDPORDER))) == 0) { |
8 |
0 |
8 |
|
| 983 |
8 |
| SV **e = av_fetch((AV *)SvRV(psaml_app_get(aTHX_ app, PSAML_K_IDPORDER)), |
| 984 |
|
| 0, 0); |
| 985 |
8 |
▶ | idp_name = (e && *e) ? *e : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| idp_name = (e && *e) ? *e : NULL; |
8 |
0 |
8 |
| idp_name = (e && *e) ? *e : NULL; |
8 |
0 |
8 |
|
| 986 |
|
| } |
| 987 |
|
| } |
| 988 |
|
| |
| 989 |
|
| /* No flow record, and the provider may not start a login: that is |
| 990 |
|
| * `unsolicited`, and it is decided HERE rather than by handing the |
| 991 |
|
| * verifier a Response with no provider to check it against. Calling |
| 992 |
|
| * verify with no certs produces a plain croak about a missing option, |
| 993 |
|
| * which is a configuration message answering a security question. */ |
| 994 |
36 |
▶ | if (!rec && !psaml_opt_bool(aTHX_ opts, "allow_idp_initiated", 0)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!rec && !psaml_opt_bool(aTHX_ opts, "allow_idp_initiated", 0)) { |
25 |
11 |
36 |
| if (!rec && !psaml_opt_bool(aTHX_ opts, "allow_idp_initiated", 0)) { |
17 |
8 |
25 |
|
| 995 |
17 |
| failed = 1; |
| 996 |
|
| /* psaml_error_new already hands back a mortal; mortalising it again |
| 997 |
|
| * here would free it twice. */ |
| 998 |
17 |
| err = psaml_error_new(aTHX_ PSAML_E_UNSOLICITED, |
| 999 |
|
| newSVpvs("no flow record matches this RelayState, and " |
| 1000 |
|
| "allow_idp_initiated is off")); |
| 1001 |
|
| } |
| 1002 |
|
| else |
| 1003 |
|
| { |
| 1004 |
19 |
| dSP; |
| 1005 |
19 |
| HV *cfg = NULL; |
| 1006 |
|
| int attempt; |
| 1007 |
19 |
▶ | if (idp_name) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (idp_name) { |
19 |
0 |
19 |
|
| 1008 |
|
| STRLEN nl; |
| 1009 |
19 |
| const char *np = SvPV_const(idp_name, nl); |
| 1010 |
19 |
| SV **e = hv_fetch(idps, np, (I32)nl, 0); |
| 1011 |
19 |
▶ | if (e && *e && SvROK(*e)) cfg = (HV *)SvRV(*e); |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && *e && SvROK(*e)) cfg = (HV *)SvRV(*e); |
19 |
0 |
19 |
| if (e && *e && SvROK(*e)) cfg = (HV *)SvRV(*e); |
19 |
0 |
19 |
| if (e && *e && SvROK(*e)) cfg = (HV *)SvRV(*e); |
19 |
0 |
19 |
|
| 1012 |
|
| } |
| 1013 |
|
| |
| 1014 |
|
| /* Twice at most. The second attempt happens only when the first |
| 1015 |
|
| * failed in a way a key rotation explains AND re-reading the |
| 1016 |
|
| * provider's metadata actually produced something new; every other |
| 1017 |
|
| * failure leaves the loop on the first pass. `certs` is read off cfg |
| 1018 |
|
| * inside the loop, so the retry uses what the refetch just wrote. */ |
| 1019 |
28 |
▶ | for (attempt = 0; attempt < 2; attempt++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (attempt = 0; attempt < 2; attempt++) { |
27 |
1 |
28 |
|
| 1020 |
|
| |
| 1021 |
27 |
▶ | if (attempt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (attempt) { |
8 |
19 |
27 |
|
| 1022 |
8 |
| int refetched = 0; |
| 1023 |
8 |
▶ | if (!cfg || !psaml_rotation_explains(aTHX_ err)) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cfg || !psaml_rotation_explains(aTHX_ err)) break; |
8 |
0 |
8 |
| if (!cfg || !psaml_rotation_explains(aTHX_ err)) break; |
6 |
2 |
8 |
|
| 1024 |
|
| |
| 1025 |
|
| /* through Perl, for the G_EVAL: psaml_idp_refetch croaks when the |
| 1026 |
|
| * provider cannot be reached, and an unreachable provider during |
| 1027 |
|
| * somebody's login is a refused login, never a 500. */ |
| 1028 |
6 |
| ENTER; |
| 1029 |
6 |
| SAVETMPS; |
| 1030 |
6 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
6 |
6 |
|
| 1031 |
6 |
▶ | XPUSHs(sv_2mortal(newSVpvs("Punk::Plugin::SAML"))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvs("Punk::Plugin::SAML"))); |
0 |
6 |
6 |
|
| 1032 |
6 |
▶ | XPUSHs(app); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(app); |
0 |
6 |
6 |
|
| 1033 |
6 |
▶ | XPUSHs(idp_name); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(idp_name); |
0 |
6 |
6 |
|
| 1034 |
6 |
▶ | XPUSHs(sv_2mortal(newSViv(now))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(now))); |
0 |
6 |
6 |
|
| 1035 |
6 |
| PUTBACK; |
| 1036 |
|
| { |
| 1037 |
6 |
| int count = call_method("_refetch_idp", G_SCALAR | G_EVAL); |
| 1038 |
6 |
| SPAGAIN; |
| 1039 |
6 |
▶ | if (SvTRUE(ERRSV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { |
6 |
0 |
6 |
| if (SvTRUE(ERRSV)) { |
0 |
6 |
6 |
|
| 1040 |
|
| /* its own line: "the login failed" and "the provider could not |
| 1041 |
|
| * be re-read" are two different operational problems and a |
| 1042 |
|
| * deployment chasing the first should not have to infer the |
| 1043 |
|
| * second. */ |
| 1044 |
0 |
▶ | SV *why = sv_2mortal(newSVsv(ERRSV)); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *why = sv_2mortal(newSVsv(ERRSV)); |
0 |
0 |
0 |
|
| 1045 |
0 |
▶ | if (count > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 0) (void)POPs; |
0 |
0 |
0 |
|
| 1046 |
0 |
| Perl_warn(aTHX_ "saml: metadata refetch failed: %s", |
| 1047 |
|
| SvPV_nolen(why)); |
| 1048 |
|
| } |
| 1049 |
6 |
▶ | else if (count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (count > 0) { |
6 |
0 |
6 |
|
| 1050 |
|
| /* one POP, then the test: SvTRUE(POPs) evaluates its argument |
| 1051 |
|
| * more than once and would move the stack pointer with it. */ |
| 1052 |
6 |
| SV *r2 = POPs; |
| 1053 |
6 |
| refetched = SvTRUE(r2) ? 1 : 0; |
| 1054 |
|
| } |
| 1055 |
6 |
| PUTBACK; |
| 1056 |
|
| } |
| 1057 |
6 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
6 |
0 |
6 |
|
| 1058 |
6 |
| LEAVE; |
| 1059 |
|
| |
| 1060 |
6 |
▶ | if (!refetched) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!refetched) break; |
4 |
2 |
6 |
|
| 1061 |
|
| /* the first failure is discarded only now that there is going to be |
| 1062 |
|
| * a second answer to replace it */ |
| 1063 |
2 |
| SvREFCNT_dec(err); |
| 1064 |
2 |
| err = NULL; |
| 1065 |
2 |
| failed = 0; |
| 1066 |
|
| } |
| 1067 |
|
| |
| 1068 |
21 |
| ENTER; |
| 1069 |
21 |
| SAVETMPS; |
| 1070 |
|
| { |
| 1071 |
|
| /* G_EVAL around the whole verification: every check throws a |
| 1072 |
|
| * blessed Punk::SAML::Error, and the route turns that into one |
| 1073 |
|
| * page and one log line. */ |
| 1074 |
|
| int i; |
| 1075 |
|
| SV *args[24]; |
| 1076 |
21 |
| int n = 0; |
| 1077 |
21 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
21 |
21 |
|
| 1078 |
21 |
| args[n++] = sv_2mortal(newSVpvs("Punk::SAML::Response")); |
| 1079 |
21 |
▶ | args[n++] = field ? field : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| args[n++] = field ? field : &PL_sv_undef; |
21 |
0 |
21 |
|
| 1080 |
21 |
| args[n++] = sv_2mortal(newSVpvs("entity_id")); |
| 1081 |
21 |
| args[n++] = psaml_app_get(aTHX_ app, PSAML_K_ENTITY); |
| 1082 |
21 |
| args[n++] = sv_2mortal(newSVpvs("acs_url")); |
| 1083 |
21 |
| args[n++] = psaml_app_get(aTHX_ app, PSAML_K_ACS); |
| 1084 |
21 |
| args[n++] = sv_2mortal(newSVpvs("idp")); |
| 1085 |
21 |
▶ | args[n++] = idp_name ? idp_name : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| args[n++] = idp_name ? idp_name : &PL_sv_undef; |
21 |
0 |
21 |
|
| 1086 |
21 |
| args[n++] = sv_2mortal(newSVpvs("idp_entity_id")); |
| 1087 |
21 |
▶ | args[n++] = cfg ? psaml_hget(aTHX_ cfg, "entity_id") : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| args[n++] = cfg ? psaml_hget(aTHX_ cfg, "entity_id") : &PL_sv_undef; |
21 |
0 |
21 |
|
| 1088 |
21 |
| args[n++] = sv_2mortal(newSVpvs("certs")); |
| 1089 |
21 |
▶ | args[n++] = cfg ? psaml_hget(aTHX_ cfg, "certs") : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| args[n++] = cfg ? psaml_hget(aTHX_ cfg, "certs") : &PL_sv_undef; |
21 |
0 |
21 |
|
| 1090 |
21 |
| args[n++] = sv_2mortal(newSVpvs("now")); |
| 1091 |
21 |
| args[n++] = sv_2mortal(newSViv(now)); |
| 1092 |
21 |
| args[n++] = sv_2mortal(newSVpvs("skew")); |
| 1093 |
21 |
| args[n++] = sv_2mortal(newSViv(psaml_opt_iv(aTHX_ opts, "skew", 120))); |
| 1094 |
21 |
| args[n++] = sv_2mortal(newSVpvs("require_signed")); |
| 1095 |
21 |
| args[n++] = psaml_hget(aTHX_ opts, "require_signed"); |
| 1096 |
21 |
| args[n++] = sv_2mortal(newSVpvs("allow_sha1")); |
| 1097 |
21 |
| args[n++] = sv_2mortal(newSViv(psaml_opt_bool(aTHX_ opts, |
| 1098 |
|
| "allow_sha1", 0))); |
| 1099 |
21 |
| args[n++] = sv_2mortal(newSVpvs("allow_idp_initiated")); |
| 1100 |
21 |
| args[n++] = sv_2mortal(newSViv(psaml_opt_bool(aTHX_ opts, |
| 1101 |
|
| "allow_idp_initiated", 0))); |
| 1102 |
21 |
| args[n++] = sv_2mortal(newSVpvs("in_response_to")); |
| 1103 |
21 |
▶ | args[n++] = flow_id ? flow_id : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| args[n++] = flow_id ? flow_id : &PL_sv_undef; |
13 |
8 |
21 |
|
| 1104 |
525 |
▶ | for (i = 0; i < n; i++) XPUSHs(args[i] ? args[i] : &PL_sv_undef); |
Branch: 3 branches
| Branch | True | False | Total |
| for (i = 0; i < n; i++) XPUSHs(args[i] ? args[i] : &PL_sv_undef); |
0 |
504 |
504 |
| for (i = 0; i < n; i++) XPUSHs(args[i] ? args[i] : &PL_sv_undef); |
504 |
0 |
504 |
| for (i = 0; i < n; i++) XPUSHs(args[i] ? args[i] : &PL_sv_undef); |
504 |
21 |
525 |
|
| 1105 |
21 |
| PUTBACK; |
| 1106 |
|
| } |
| 1107 |
|
| { |
| 1108 |
21 |
| int count = call_method("_verify_field", G_SCALAR | G_EVAL); |
| 1109 |
21 |
| SPAGAIN; |
| 1110 |
21 |
▶ | if (SvTRUE(ERRSV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { |
21 |
0 |
21 |
| if (SvTRUE(ERRSV)) { |
9 |
12 |
21 |
|
| 1111 |
9 |
| failed = 1; |
| 1112 |
|
| /* +1 and NOT mortalised here: this is inside the ENTER/SAVETMPS |
| 1113 |
|
| * frame, and the FREETMPS below would free it before the failure |
| 1114 |
|
| * path reads its `code`. Mortalised after LEAVE instead. */ |
| 1115 |
9 |
▶ | err = newSVsv(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| err = newSVsv(ERRSV); |
9 |
0 |
9 |
|
| 1116 |
9 |
▶ | if (count > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 0) (void)POPs; |
9 |
0 |
9 |
|
| 1117 |
|
| } |
| 1118 |
12 |
▶ | else if (count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (count > 0) { |
12 |
0 |
12 |
|
| 1119 |
12 |
| SV *r2 = POPs; |
| 1120 |
12 |
| identity = SvREFCNT_inc_simple_NN(r2); |
| 1121 |
|
| } |
| 1122 |
21 |
| PUTBACK; |
| 1123 |
|
| } |
| 1124 |
21 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
21 |
0 |
21 |
|
| 1125 |
21 |
| LEAVE; |
| 1126 |
21 |
▶ | if (identity) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (identity) break; |
12 |
9 |
21 |
|
| 1127 |
|
| |
| 1128 |
|
| } /* for attempt */ |
| 1129 |
|
| |
| 1130 |
|
| /* mortalised out here, not inside the loop: an `err` freed at the end |
| 1131 |
|
| * of the first pass is one the second pass would read after it was |
| 1132 |
|
| * gone. */ |
| 1133 |
19 |
▶ | if (identity) sv_2mortal(identity); |
Branch: 1 branch
| Branch | True | False | Total |
| if (identity) sv_2mortal(identity); |
12 |
7 |
19 |
|
| 1134 |
19 |
▶ | if (err) sv_2mortal(err); |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) sv_2mortal(err); |
7 |
12 |
19 |
|
| 1135 |
|
| } |
| 1136 |
|
| |
| 1137 |
|
| /* Anything that reached here as a plain string is a bug or a |
| 1138 |
|
| * misconfiguration rather than a check result, and on_error is |
| 1139 |
|
| * documented to receive a Punk::SAML::Error. Wrap it so that contract |
| 1140 |
|
| * holds on every path. */ |
| 1141 |
36 |
▶ | if (failed && err && !(SvROK(err) && sv_isobject(err))) |
Branch: 4 branches
| Branch | True | False | Total |
| if (failed && err && !(SvROK(err) && sv_isobject(err))) |
24 |
12 |
36 |
| if (failed && err && !(SvROK(err) && sv_isobject(err))) |
24 |
0 |
24 |
| if (failed && err && !(SvROK(err) && sv_isobject(err))) |
24 |
0 |
24 |
| if (failed && err && !(SvROK(err) && sv_isobject(err))) |
0 |
24 |
24 |
|
| 1142 |
0 |
| err = psaml_error_new(aTHX_ PSAML_E_CONFIG, newSVsv(err)); |
| 1143 |
36 |
▶ | if (!failed && !identity) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!failed && !identity) { |
12 |
24 |
36 |
| if (!failed && !identity) { |
0 |
12 |
12 |
|
| 1144 |
0 |
| failed = 1; |
| 1145 |
0 |
| err = psaml_error_new(aTHX_ PSAML_E_XML_SHAPE, |
| 1146 |
|
| newSVpvs("the Response produced no identity")); |
| 1147 |
|
| } |
| 1148 |
|
| |
| 1149 |
36 |
▶ | if (failed || !identity) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (failed || !identity) { |
12 |
24 |
36 |
| if (failed || !identity) { |
0 |
12 |
12 |
|
| 1150 |
|
| /* the code goes to the log with the provider; the REASON never |
| 1151 |
|
| * reaches the browser. A verifier that told the far side which check |
| 1152 |
|
| * it failed would be telling an attacker which one to work on. */ |
| 1153 |
24 |
| SV *on_error = psaml_hget(aTHX_ login, "on_error"); |
| 1154 |
|
| { |
| 1155 |
24 |
▶ | SV *code = (err && SvROK(err) && SvTYPE(SvRV(err)) == SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| SV *code = (err && SvROK(err) && SvTYPE(SvRV(err)) == SVt_PVHV) |
24 |
0 |
24 |
| SV *code = (err && SvROK(err) && SvTYPE(SvRV(err)) == SVt_PVHV) |
24 |
0 |
24 |
|
| 1156 |
48 |
▶ | ? psaml_hget(aTHX_ (HV *)SvRV(err), "code") : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? psaml_hget(aTHX_ (HV *)SvRV(err), "code") : NULL; |
24 |
0 |
24 |
|
| 1157 |
24 |
▶ | SV *msg = (err && SvROK(err) && SvTYPE(SvRV(err)) == SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| SV *msg = (err && SvROK(err) && SvTYPE(SvRV(err)) == SVt_PVHV) |
24 |
0 |
24 |
| SV *msg = (err && SvROK(err) && SvTYPE(SvRV(err)) == SVt_PVHV) |
24 |
0 |
24 |
|
| 1158 |
48 |
▶ | ? psaml_hget(aTHX_ (HV *)SvRV(err), "message") : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? psaml_hget(aTHX_ (HV *)SvRV(err), "message") : NULL; |
24 |
0 |
24 |
|
| 1159 |
24 |
| SV *line = sv_2mortal(newSVpvs("saml: ")); |
| 1160 |
24 |
▶ | sv_catsv(line, code ? code : sv_2mortal(newSVpvs("error"))); |
Branch: 1 branch
| Branch | True | False | Total |
| sv_catsv(line, code ? code : sv_2mortal(newSVpvs("error"))); |
0 |
24 |
24 |
|
| 1161 |
24 |
▶ | if (idp_name) { sv_catpvs(line, " idp="); sv_catsv(line, idp_name); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (idp_name) { sv_catpvs(line, " idp="); sv_catsv(line, idp_name); } |
7 |
17 |
24 |
|
| 1162 |
24 |
▶ | if (msg) { sv_catpvs(line, " "); sv_catsv(line, msg); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg) { sv_catpvs(line, " "); sv_catsv(line, msg); } |
24 |
0 |
24 |
|
| 1163 |
|
| /* warn, not $c->log: this reaches psgi.errors on every server and |
| 1164 |
|
| * does not tie the plugin to a logger's signature. The CODE and the |
| 1165 |
|
| * provider go here; none of it reaches the browser. */ |
| 1166 |
24 |
| Perl_warn(aTHX_ "%s", SvPV_nolen(line)); |
| 1167 |
|
| } |
| 1168 |
24 |
▶ | if (psaml_is_code(aTHX_ on_error)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (psaml_is_code(aTHX_ on_error)) { |
1 |
23 |
24 |
|
| 1169 |
|
| SV *cbargs[2]; |
| 1170 |
1 |
| cbargs[0] = c; |
| 1171 |
1 |
| cbargs[1] = err; |
| 1172 |
1 |
| res = psaml_call_cv(aTHX_ on_error, cbargs, 2); |
| 1173 |
1 |
▶ | if (!res) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!res) XSRETURN_EMPTY; |
0 |
1 |
1 |
|
| 1174 |
1 |
| ST(0) = sv_2mortal(res); |
| 1175 |
1 |
| XSRETURN(1); |
| 1176 |
|
| } |
| 1177 |
23 |
| argv[0] = sv_2mortal(newSVpvs( |
| 1178 |
|
| "<!doctype html><title>Sign-in failed</title>" |
| 1179 |
|
| "<h1>Sign-in failed</h1><p>Please try again.</p>")); |
| 1180 |
23 |
| argv[1] = sv_2mortal(newSViv(403)); |
| 1181 |
23 |
| res = psaml_call(aTHX_ c, "html", argv, 2); |
| 1182 |
23 |
▶ | if (!res) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!res) XSRETURN_EMPTY; |
0 |
23 |
23 |
|
| 1183 |
23 |
| ST(0) = sv_2mortal(res); |
| 1184 |
23 |
| XSRETURN(1); |
| 1185 |
|
| } |
| 1186 |
|
| |
| 1187 |
|
| /* session_rotate BEFORE on_login, because that is session fixation: the |
| 1188 |
|
| * session that existed before authentication must not be the one that |
| 1189 |
|
| * exists after. The plugin does it so no on_login body has to remember |
| 1190 |
|
| * to, and it is called regardless of what the session store is - the |
| 1191 |
|
| * cost is nothing and the day it becomes server side is the day it |
| 1192 |
|
| * matters. */ |
| 1193 |
12 |
▶ | if (psaml_can(aTHX_ c, "session_rotate")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (psaml_can(aTHX_ c, "session_rotate")) |
12 |
0 |
12 |
|
| 1194 |
12 |
| SvREFCNT_dec(psaml_call(aTHX_ c, "session_rotate", NULL, 0)); |
| 1195 |
|
| |
| 1196 |
|
| /* recorded for the logout 0.02 may bring, and read never */ |
| 1197 |
|
| /* Recorded for the logout 0.02 may bring, and read never. |
| 1198 |
|
| * |
| 1199 |
|
| * $c->session takes NO arguments and hands back the session hashref; |
| 1200 |
|
| * storing is done into that hash. Calling it as session(key, value) |
| 1201 |
|
| * is a SEGV rather than an error, which is how this was found. */ |
| 1202 |
12 |
▶ | if (psaml_can(aTHX_ c, "session")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (psaml_can(aTHX_ c, "session")) { |
12 |
0 |
12 |
|
| 1203 |
12 |
| SV *sess = sv_2mortal(psaml_call(aTHX_ c, "session", NULL, 0)); |
| 1204 |
12 |
▶ | if (sess && SvROK(sess) && SvTYPE(SvRV(sess)) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (sess && SvROK(sess) && SvTYPE(SvRV(sess)) == SVt_PVHV) { |
12 |
0 |
12 |
| if (sess && SvROK(sess) && SvTYPE(SvRV(sess)) == SVt_PVHV) { |
12 |
0 |
12 |
| if (sess && SvROK(sess) && SvTYPE(SvRV(sess)) == SVt_PVHV) { |
12 |
0 |
12 |
|
| 1205 |
12 |
| HV *sh = (HV *)SvRV(sess); |
| 1206 |
12 |
| HV *note = newHV(); |
| 1207 |
12 |
▶ | HV *idh = (SvROK(identity) && SvTYPE(SvRV(identity)) == SVt_PVHV) |
Branch: 1 branch
| Branch | True | False | Total |
| HV *idh = (SvROK(identity) && SvTYPE(SvRV(identity)) == SVt_PVHV) |
12 |
0 |
12 |
|
| 1208 |
24 |
▶ | ? (HV *)SvRV(identity) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (HV *)SvRV(identity) : NULL; |
12 |
0 |
12 |
|
| 1209 |
|
| SV *v; |
| 1210 |
12 |
▶ | if (idp_name) (void)hv_stores(note, "idp", newSVsv(idp_name)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (idp_name) (void)hv_stores(note, "idp", newSVsv(idp_name)); |
12 |
0 |
12 |
|
| 1211 |
12 |
▶ | if (idh) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (idh) { |
12 |
0 |
12 |
|
| 1212 |
12 |
| v = psaml_hget(aTHX_ idh, "name_id"); |
| 1213 |
12 |
▶ | if (v) (void)hv_stores(note, "name_id", newSVsv(v)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (v) (void)hv_stores(note, "name_id", newSVsv(v)); |
12 |
0 |
12 |
|
| 1214 |
12 |
| v = psaml_hget(aTHX_ idh, "session_index"); |
| 1215 |
12 |
▶ | if (v) (void)hv_stores(note, "session_index", newSVsv(v)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (v) (void)hv_stores(note, "session_index", newSVsv(v)); |
12 |
0 |
12 |
|
| 1216 |
|
| } |
| 1217 |
12 |
| (void)hv_stores(sh, "punk_saml", newRV_noinc((SV *)note)); |
| 1218 |
|
| } |
| 1219 |
|
| } |
| 1220 |
|
| |
| 1221 |
|
| { |
| 1222 |
12 |
| SV *on_login = psaml_hget(aTHX_ login, "on_login"); |
| 1223 |
|
| SV *cbargs[2]; |
| 1224 |
12 |
| cbargs[0] = c; |
| 1225 |
12 |
| cbargs[1] = identity; |
| 1226 |
12 |
| res = psaml_call_cv(aTHX_ on_login, cbargs, 2); |
| 1227 |
|
| } |
| 1228 |
|
| |
| 1229 |
|
| /* on_login's return value is the answer when there is one; otherwise |
| 1230 |
|
| * the plugin redirects to where the login started */ |
| 1231 |
12 |
▶ | if (!res) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!res) { |
12 |
0 |
12 |
|
| 1232 |
12 |
▶ | argv[0] = to ? to : psaml_hget(aTHX_ opts, "default_to"); |
Branch: 1 branch
| Branch | True | False | Total |
| argv[0] = to ? to : psaml_hget(aTHX_ opts, "default_to"); |
0 |
12 |
12 |
|
| 1233 |
12 |
| argv[1] = sv_2mortal(newSViv(303)); |
| 1234 |
12 |
| res = psaml_call(aTHX_ c, "redirect", argv, 2); |
| 1235 |
|
| } |
| 1236 |
12 |
▶ | if (!res) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!res) XSRETURN_EMPTY; |
0 |
12 |
12 |
|
| 1237 |
12 |
| ST(0) = sv_2mortal(res); |
| 1238 |
12 |
| XSRETURN(1); |
| 1239 |
|
| } |
| 1240 |
|
| |
| 1241 |
15 |
| PERL_STATIC_INLINE void psaml_mount_routes(pTHX_ SV *app) { |
| 1242 |
15 |
| SV *mount = psaml_app_get(aTHX_ app, PSAML_K_MOUNT); |
| 1243 |
15 |
| HV *opts = psaml_opts_of(aTHX_ app); |
| 1244 |
|
| AV *cap; |
| 1245 |
|
| SV *path; |
| 1246 |
|
| |
| 1247 |
15 |
▶ | if (psaml_opt_bool(aTHX_ opts, "metadata", 1)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (psaml_opt_bool(aTHX_ opts, "metadata", 1)) { |
15 |
0 |
15 |
|
| 1248 |
15 |
| cap = newAV(); av_push(cap, newSVsv(app)); |
| 1249 |
15 |
| path = sv_2mortal(newSVsv(mount)); |
| 1250 |
15 |
| sv_catpvs(path, "/metadata"); |
| 1251 |
15 |
| psaml_route(aTHX_ app, "GET", path, psaml_rt_metadata, cap); |
| 1252 |
|
| } |
| 1253 |
|
| |
| 1254 |
15 |
| cap = newAV(); av_push(cap, newSVsv(app)); |
| 1255 |
15 |
| path = sv_2mortal(newSVsv(mount)); |
| 1256 |
15 |
| sv_catpvs(path, "/login/:idp"); |
| 1257 |
15 |
| psaml_route(aTHX_ app, "GET", path, psaml_rt_login, cap); |
| 1258 |
|
| |
| 1259 |
15 |
| cap = newAV(); av_push(cap, newSVsv(app)); |
| 1260 |
15 |
| path = sv_2mortal(newSVsv(mount)); |
| 1261 |
15 |
| sv_catpvs(path, "/login"); |
| 1262 |
15 |
| psaml_route(aTHX_ app, "GET", path, psaml_rt_login, cap); |
| 1263 |
|
| |
| 1264 |
15 |
| cap = newAV(); av_push(cap, newSVsv(app)); |
| 1265 |
15 |
| path = sv_2mortal(newSVsv(mount)); |
| 1266 |
15 |
| sv_catpvs(path, "/acs"); |
| 1267 |
15 |
| psaml_route(aTHX_ app, "POST", path, psaml_rt_acs, cap); |
| 1268 |
15 |
| } |
| 1269 |
|
| |
| 1270 |
|
| /* ---- on_compile ------------------------------------------------------- */ |
| 1271 |
|
| |
| 1272 |
|
| /* Everything that needs `host`, or needs every keyword to have run. |
| 1273 |
|
| * |
| 1274 |
|
| * saml_login with no provider is caught HERE rather than at the keyword, |
| 1275 |
|
| * because the two may be declared in either order and a plugin that |
| 1276 |
|
| * insisted on one order would be enforcing a rule the application never |
| 1277 |
|
| * agreed to. */ |
| 1278 |
|
| XS_INTERNAL(psaml_on_compile); |
| 1279 |
18 |
| XS_INTERNAL(psaml_on_compile) { |
| 1280 |
18 |
| dXSARGS; |
| 1281 |
18 |
| SV *app = psaml_cap_slot(aTHX_ cv, 0); |
| 1282 |
|
| SV *login, *host, *entity, *mount, *ordsv; |
| 1283 |
|
| HV *opts; |
| 1284 |
|
| |
| 1285 |
|
| PERL_UNUSED_VAR(items); |
| 1286 |
18 |
▶ | if (!app) { PERL_UNUSED_VAR(ax); XSRETURN_EMPTY; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!app) { PERL_UNUSED_VAR(ax); XSRETURN_EMPTY; } |
0 |
18 |
18 |
|
| 1287 |
|
| |
| 1288 |
18 |
| login = psaml_app_get(aTHX_ app, PSAML_K_LOGIN); |
| 1289 |
18 |
▶ | if (!login) XSRETURN_EMPTY; /* the plugin without a login mount does |
Branch: 1 branch
| Branch | True | False | Total |
| if (!login) XSRETURN_EMPTY; /* the plugin without a login mount does |
0 |
18 |
18 |
|
| 1290 |
|
| * nothing, and that is allowed: an |
| 1291 |
|
| * application may load it to publish |
| 1292 |
|
| * metadata and nothing else */ |
| 1293 |
|
| |
| 1294 |
18 |
| ordsv = psaml_app_get(aTHX_ app, PSAML_K_IDPORDER); |
| 1295 |
18 |
▶ | if (!ordsv || !SvROK(ordsv) || av_len((AV *)SvRV(ordsv)) < 0) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!ordsv || !SvROK(ordsv) || av_len((AV *)SvRV(ordsv)) < 0) |
17 |
1 |
18 |
| if (!ordsv || !SvROK(ordsv) || av_len((AV *)SvRV(ordsv)) < 0) |
17 |
0 |
17 |
| if (!ordsv || !SvROK(ordsv) || av_len((AV *)SvRV(ordsv)) < 0) |
0 |
17 |
17 |
|
| 1296 |
1 |
| croak("%s: saml_login is declared but no saml_idp is. There is nothing " |
| 1297 |
|
| "to log in against", PSAML_WHO); |
| 1298 |
|
| |
| 1299 |
17 |
| host = psaml_host(aTHX_ app); |
| 1300 |
15 |
| mount = psaml_app_get(aTHX_ app, PSAML_K_MOUNT); |
| 1301 |
|
| { |
| 1302 |
|
| STRLEN ml; |
| 1303 |
15 |
| const char *mp = SvPV_const(mount, ml); |
| 1304 |
15 |
| SV *acs = newSVsv(host); |
| 1305 |
|
| STRLEN al; |
| 1306 |
15 |
| const char *ap = SvPV_const(acs, al); |
| 1307 |
15 |
▶ | while (al && ap[al - 1] == '/') al--; |
Branch: 2 branches
| Branch | True | False | Total |
| while (al && ap[al - 1] == '/') al--; |
15 |
0 |
15 |
| while (al && ap[al - 1] == '/') al--; |
0 |
15 |
15 |
|
| 1308 |
15 |
| SvCUR_set(acs, al); |
| 1309 |
15 |
| sv_catpvn(acs, mp, ml); |
| 1310 |
15 |
| sv_catpvs(acs, "/acs"); |
| 1311 |
|
| /* One variable, computed once. The AuthnRequest, the metadata, the |
| 1312 |
|
| * Destination check and the Recipient check all read it, and four |
| 1313 |
|
| * derivations of one URL is four chances for them to disagree. */ |
| 1314 |
15 |
| psaml_app_set(aTHX_ app, PSAML_K_ACS, acs); |
| 1315 |
|
| } |
| 1316 |
|
| |
| 1317 |
15 |
| opts = (HV *)SvRV(psaml_app_get(aTHX_ app, PSAML_K_OPTS)); |
| 1318 |
15 |
| entity = psaml_hget(aTHX_ opts, "entity_id"); |
| 1319 |
15 |
▶ | if (entity && SvOK(entity)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (entity && SvOK(entity)) { |
1 |
14 |
15 |
| if (entity && SvOK(entity)) { |
1 |
0 |
1 |
|
| 1320 |
1 |
| psaml_app_set(aTHX_ app, PSAML_K_ENTITY, newSVsv(entity)); |
| 1321 |
|
| } |
| 1322 |
|
| else { |
| 1323 |
|
| /* the metadata URL, which is what an identity provider will have been |
| 1324 |
|
| * given to identify us by if we did not say otherwise */ |
| 1325 |
14 |
| SV *pfx = psaml_hget(aTHX_ opts, "prefix"); |
| 1326 |
14 |
| SV *url = psaml_join_url(aTHX_ host, SvPV_nolen(pfx)); |
| 1327 |
14 |
| sv_catpvs(url, "/metadata"); |
| 1328 |
14 |
| psaml_app_set(aTHX_ app, PSAML_K_ENTITY, url); |
| 1329 |
|
| } |
| 1330 |
|
| |
| 1331 |
|
| /* Every provider resolved HERE, before the fork. A `metadata` source is |
| 1332 |
|
| * fetched or read and run through psaml_idp_from_metadata; an explicit |
| 1333 |
|
| * trio is used as given. */ |
| 1334 |
|
| { |
| 1335 |
15 |
| HV *idps = (HV *)SvRV(psaml_app_get(aTHX_ app, PSAML_K_IDPS)); |
| 1336 |
15 |
| AV *order = (AV *)SvRV(ordsv); |
| 1337 |
15 |
| SSize_t i, n = av_len(order) + 1; |
| 1338 |
32 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
17 |
15 |
32 |
|
| 1339 |
17 |
| SV **e = av_fetch(order, i, 0); |
| 1340 |
|
| STRLEN nl; |
| 1341 |
|
| const char *np; |
| 1342 |
|
| SV **cfge; |
| 1343 |
|
| HV *cfg; |
| 1344 |
|
| SV *meta; |
| 1345 |
17 |
▶ | if (!e || !*e) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!e || !*e) continue; |
17 |
0 |
17 |
| if (!e || !*e) continue; |
0 |
17 |
17 |
|
| 1346 |
17 |
| np = SvPV_const(*e, nl); |
| 1347 |
17 |
| cfge = hv_fetch(idps, np, (I32)nl, 0); |
| 1348 |
17 |
▶ | if (!cfge || !*cfge || !SvROK(*cfge)) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!cfge || !*cfge || !SvROK(*cfge)) continue; |
17 |
0 |
17 |
| if (!cfge || !*cfge || !SvROK(*cfge)) continue; |
17 |
0 |
17 |
| if (!cfge || !*cfge || !SvROK(*cfge)) continue; |
0 |
17 |
17 |
|
| 1349 |
17 |
| cfg = (HV *)SvRV(*cfge); |
| 1350 |
17 |
| meta = psaml_hget(aTHX_ cfg, "metadata"); |
| 1351 |
17 |
▶ | if (meta && SvOK(meta)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (meta && SvOK(meta)) { |
9 |
8 |
17 |
| if (meta && SvOK(meta)) { |
9 |
0 |
9 |
|
| 1352 |
9 |
| SV *bytes = sv_2mortal(psaml_fetch_metadata(aTHX_ app, meta, np)); |
| 1353 |
9 |
| psaml_idp_merge(aTHX_ cfg, bytes, opts); |
| 1354 |
|
| /* No `last_refresh` is stamped here. The clock the rotation |
| 1355 |
|
| * refetch runs on starts at the first REFETCH, not at boot: a |
| 1356 |
|
| * provider that rotates its key an hour after this application |
| 1357 |
|
| * started should be picked up at the first failed login, not an |
| 1358 |
|
| * interval after one. */ |
| 1359 |
|
| /* WantAuthnRequestsSigned, acted on. With a key, sign_requests |
| 1360 |
|
| * turns itself on; without one a croak naming the option, because |
| 1361 |
|
| * a provider that demands signed requests refuses every unsigned |
| 1362 |
|
| * one and every login fails. */ |
| 1363 |
9 |
▶ | if (psaml_opt_bool(aTHX_ cfg, "want_authn_requests_signed", 0)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (psaml_opt_bool(aTHX_ cfg, "want_authn_requests_signed", 0)) { |
0 |
9 |
9 |
|
| 1364 |
0 |
▶ | if (hv_exists(opts, "key", 3)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_exists(opts, "key", 3)) |
0 |
0 |
0 |
|
| 1365 |
0 |
| (void)hv_stores(opts, "sign_requests", newSViv(1)); |
| 1366 |
|
| else |
| 1367 |
0 |
| croak("%s: saml_idp '%.*s' publishes " |
| 1368 |
|
| "WantAuthnRequestsSigned=\"true\" and this application " |
| 1369 |
|
| "has no `key`. Every AuthnRequest would be refused; " |
| 1370 |
|
| "configure `key` with the SP private key", |
| 1371 |
|
| PSAML_WHO, (int)nl, np); |
| 1372 |
|
| } |
| 1373 |
|
| } |
| 1374 |
|
| } |
| 1375 |
|
| } |
| 1376 |
|
| |
| 1377 |
|
| /* The ACS is a POST with no session and no token, from another origin. |
| 1378 |
|
| * It is the one request in the application that is SUPPOSED to look |
| 1379 |
|
| * exactly like a cross-site request forgery, and csrf must not check |
| 1380 |
|
| * it. |
| 1381 |
|
| * |
| 1382 |
|
| * MEASURED rather than assumed, which is what the plan asked for: the |
| 1383 |
|
| * exempt list is an arrayref on the application hash under `csrf`, it |
| 1384 |
|
| * is reachable here, and matching is a prefix match. So the path is |
| 1385 |
|
| * added. A silent requirement would be a 403 on every login. */ |
| 1386 |
|
| { |
| 1387 |
15 |
| SV **csrf = hv_fetchs(psaml_app_hv(aTHX_ app), "csrf", 0); |
| 1388 |
15 |
▶ | if (csrf && *csrf && SvROK(*csrf) && SvTYPE(SvRV(*csrf)) == SVt_PVHV) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (csrf && *csrf && SvROK(*csrf) && SvTYPE(SvRV(*csrf)) == SVt_PVHV) { |
1 |
14 |
15 |
| if (csrf && *csrf && SvROK(*csrf) && SvTYPE(SvRV(*csrf)) == SVt_PVHV) { |
1 |
0 |
1 |
| if (csrf && *csrf && SvROK(*csrf) && SvTYPE(SvRV(*csrf)) == SVt_PVHV) { |
1 |
0 |
1 |
| if (csrf && *csrf && SvROK(*csrf) && SvTYPE(SvRV(*csrf)) == SVt_PVHV) { |
1 |
0 |
1 |
|
| 1389 |
1 |
| HV *ccfg = (HV *)SvRV(*csrf); |
| 1390 |
1 |
| SV **ex = hv_fetchs(ccfg, "exempt", 0); |
| 1391 |
|
| AV *list; |
| 1392 |
|
| SV *acs; |
| 1393 |
1 |
▶ | if (ex && *ex && SvROK(*ex) && SvTYPE(SvRV(*ex)) == SVt_PVAV) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (ex && *ex && SvROK(*ex) && SvTYPE(SvRV(*ex)) == SVt_PVAV) { |
0 |
1 |
1 |
| if (ex && *ex && SvROK(*ex) && SvTYPE(SvRV(*ex)) == SVt_PVAV) { |
0 |
0 |
0 |
| if (ex && *ex && SvROK(*ex) && SvTYPE(SvRV(*ex)) == SVt_PVAV) { |
0 |
0 |
0 |
| if (ex && *ex && SvROK(*ex) && SvTYPE(SvRV(*ex)) == SVt_PVAV) { |
0 |
0 |
0 |
|
| 1394 |
0 |
| list = (AV *)SvRV(*ex); |
| 1395 |
|
| } |
| 1396 |
|
| else { |
| 1397 |
1 |
| list = newAV(); |
| 1398 |
1 |
| (void)hv_stores(ccfg, "exempt", newRV_noinc((SV *)list)); |
| 1399 |
|
| } |
| 1400 |
1 |
| acs = newSVsv(mount); |
| 1401 |
1 |
| sv_catpvs(acs, "/acs"); |
| 1402 |
1 |
| av_push(list, acs); |
| 1403 |
|
| } |
| 1404 |
|
| } |
| 1405 |
|
| |
| 1406 |
15 |
| psaml_mount_routes(aTHX_ app); |
| 1407 |
15 |
| XSRETURN_EMPTY; |
| 1408 |
|
| } |
| 1409 |
|
| |
| 1410 |
|
| |
| 1411 |
|
| /* ---- installing ------------------------------------------------------- */ |
| 1412 |
|
| |
| 1413 |
|
| /* The keywords, and ONLY the keywords. |
| 1414 |
|
| * |
| 1415 |
|
| * Punk makes a second install_kw by the same owner a no-op, which is what |
| 1416 |
|
| * lets `use Punk::Plugin::SAML` and `plugin \'SAML\'` both ask and only the |
| 1417 |
|
| * first do anything. That is not true of `helper`, which refuses a second |
| 1418 |
|
| * registration of the same name even by the same owner, nor of |
| 1419 |
|
| * `on_compile`, which would simply run twice. So those go in register, |
| 1420 |
|
| * once, and this is called from both. */ |
| 1421 |
60 |
| PERL_STATIC_INLINE void psaml_install_kw(pTHX_ SV *app) { |
| 1422 |
|
| AV *cap; |
| 1423 |
60 |
| cap = newAV(); av_push(cap, newSVsv(app)); |
| 1424 |
60 |
| psaml_keyword(aTHX_ app, "saml_idp", psaml_kw_idp, cap); |
| 1425 |
60 |
| cap = newAV(); av_push(cap, newSVsv(app)); |
| 1426 |
60 |
| psaml_keyword(aTHX_ app, "saml_login", psaml_kw_login, cap); |
| 1427 |
60 |
| } |
| 1428 |
|
| |
| 1429 |
|
| /* The helpers and the compile hook, from register only, and once. |
| 1430 |
|
| * |
| 1431 |
|
| * An application that says `use Punk::Plugin::SAML` and never |
| 1432 |
|
| * `plugin \'SAML\'` gets the keywords and no helpers, which is right: with |
| 1433 |
|
| * no plugin line there is no secret, no options and nothing for a helper |
| 1434 |
|
| * to answer from. */ |
| 1435 |
26 |
| PERL_STATIC_INLINE void psaml_install_rest(pTHX_ SV *app) { |
| 1436 |
|
| AV *cap; |
| 1437 |
26 |
▶ | if (psaml_app_get(aTHX_ app, PSAML_K_INSTALLED)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (psaml_app_get(aTHX_ app, PSAML_K_INSTALLED)) return; |
0 |
26 |
26 |
|
| 1438 |
26 |
| psaml_app_set(aTHX_ app, PSAML_K_INSTALLED, newSViv(1)); |
| 1439 |
26 |
| cap = newAV(); av_push(cap, newSVsv(app)); |
| 1440 |
26 |
| psaml_helper(aTHX_ app, "saml_idps", psaml_help_idps, cap); |
| 1441 |
26 |
| cap = newAV(); av_push(cap, newSVsv(app)); |
| 1442 |
26 |
| psaml_helper(aTHX_ app, "saml_url", psaml_help_url, cap); |
| 1443 |
26 |
| cap = newAV(); av_push(cap, newSVsv(app)); |
| 1444 |
26 |
| psaml_at_compile(aTHX_ app, psaml_on_compile, cap); |
| 1445 |
|
| } |
| 1446 |
|
| |
| 1447 |
|
| #endif /* PSAML_BOOT_H */ |