| 1 |
|
| /* po_target.h - where a webhook is allowed to point. |
| 2 |
|
| * |
| 3 |
|
| * A WEBHOOK URL IS ATTACKER-INFLUENCED INPUT, AND FETCHING IT IS AN SSRF. |
| 4 |
|
| * |
| 5 |
|
| * The server makes a request to a URL a user typed into a form. On a cloud |
| 6 |
|
| * instance that is a request from inside the perimeter, and the classic |
| 7 |
|
| * target is the metadata service on 169.254.169.254, which hands out |
| 8 |
|
| * credentials to anything that asks from the right place. Loopback reaches |
| 9 |
|
| * whatever else is bound on the box - an admin port, a database, this |
| 10 |
|
| * process. |
| 11 |
|
| * |
| 12 |
|
| * So the destination is validated BEFORE the request, and the default is |
| 13 |
|
| * refusal: loopback, link-local, the private ranges, and anything that is not |
| 14 |
|
| * plain http or https. An operator who genuinely needs an internal target |
| 15 |
|
| * adds it to an allowlist, which is a decision somebody made rather than a |
| 16 |
|
| * default nobody noticed. |
| 17 |
|
| * |
| 18 |
|
| * THIS IS NOT A COMPLETE SSRF DEFENCE AND DOES NOT PRETEND TO BE. A hostname |
| 19 |
|
| * that resolves to a private address defeats any check made on the string, |
| 20 |
|
| * which is why the policy is also applied to the ADDRESS at connect time by |
| 21 |
|
| * the caller. What this file does is refuse the literal cases, which is the |
| 22 |
|
| * majority of them, and refuse them where an operator can see the reason. |
| 23 |
|
| */ |
| 24 |
|
| #ifndef PO_TARGET_H |
| 25 |
|
| #define PO_TARGET_H |
| 26 |
|
| |
| 27 |
|
| #include "punk_observe/po_compat.h" |
| 28 |
|
| |
| 29 |
|
| #define PO_TGT_OK 0 |
| 30 |
|
| #define PO_TGT_BAD_SCHEME 1 |
| 31 |
|
| #define PO_TGT_LOOPBACK 2 |
| 32 |
|
| #define PO_TGT_LINK_LOCAL 3 |
| 33 |
|
| #define PO_TGT_PRIVATE 4 |
| 34 |
|
| #define PO_TGT_MALFORMED 5 |
| 35 |
|
| #define PO_TGT_NOT_ALLOWED 6 |
| 36 |
|
| |
| 37 |
|
| #define PO_TGT_HOST_MAX 256 |
| 38 |
|
| |
| 39 |
|
| /* Own case fold, not strncasecmp: that one lives in strings.h on POSIX and |
| 40 |
|
| * nowhere on MSVC, and it is locale-sensitive besides. A scheme comparison |
| 41 |
|
| * must not depend on a locale. */ |
| 42 |
78 |
| static int po_ascii_ncase(const char *a, const char *b, size_t n) { |
| 43 |
|
| size_t i; |
| 44 |
489 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
441 |
48 |
489 |
|
| 45 |
441 |
| unsigned char x = (unsigned char)a[i], y = (unsigned char)b[i]; |
| 46 |
441 |
▶ | if (x >= 'A' && x <= 'Z') x = (unsigned char)(x - 'A' + 'a'); |
Branch: 2 branches
| Branch | True | False | Total |
| if (x >= 'A' && x <= 'Z') x = (unsigned char)(x - 'A' + 'a'); |
295 |
146 |
441 |
| if (x >= 'A' && x <= 'Z') x = (unsigned char)(x - 'A' + 'a'); |
10 |
285 |
295 |
|
| 47 |
441 |
▶ | if (y >= 'A' && y <= 'Z') y = (unsigned char)(y - 'A' + 'a'); |
Branch: 2 branches
| Branch | True | False | Total |
| if (y >= 'A' && y <= 'Z') y = (unsigned char)(y - 'A' + 'a'); |
282 |
159 |
441 |
| if (y >= 'A' && y <= 'Z') y = (unsigned char)(y - 'A' + 'a'); |
0 |
282 |
282 |
|
| 48 |
441 |
▶ | if (x != y) return x < y ? -1 : 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (x != y) return x < y ? -1 : 1; |
30 |
411 |
441 |
| if (x != y) return x < y ? -1 : 1; |
13 |
17 |
30 |
|
| 49 |
411 |
▶ | if (!x) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!x) return 0; |
0 |
411 |
411 |
|
| 50 |
|
| } |
| 51 |
48 |
| return 0; |
| 52 |
|
| } |
| 53 |
|
| |
| 54 |
56 |
| static const char *po_tgt_reason(int rc) { |
| 55 |
56 |
| switch (rc) { |
| 56 |
14 |
| case PO_TGT_OK: return "ok"; |
| 57 |
8 |
| case PO_TGT_BAD_SCHEME: return "only http and https are allowed"; |
| 58 |
13 |
| case PO_TGT_LOOPBACK: return "loopback addresses are refused"; |
| 59 |
8 |
| case PO_TGT_LINK_LOCAL: return "link-local addresses are refused"; |
| 60 |
9 |
| case PO_TGT_PRIVATE: return "private addresses are refused"; |
| 61 |
1 |
| case PO_TGT_MALFORMED: return "that is not a URL"; |
| 62 |
3 |
| case PO_TGT_NOT_ALLOWED: return "not on the allowlist"; |
| 63 |
0 |
| default: return "refused"; |
| 64 |
|
| } |
| 65 |
|
| } |
| 66 |
|
| |
| 67 |
|
| /* Pull the host out of a URL, lowercased, without the port or the brackets |
| 68 |
|
| * around a literal IPv6 address. */ |
| 69 |
55 |
| static int po_tgt_host(const char *url, size_t len, char *out, size_t cap, |
| 70 |
|
| int *is_v6) { |
| 71 |
55 |
| size_t i = 0, n = 0; |
| 72 |
55 |
| int v6 = 0; |
| 73 |
|
| |
| 74 |
55 |
| *is_v6 = 0; |
| 75 |
55 |
▶ | if (len > 7 && po_ascii_ncase(url, "http://", 7) == 0) i = 7; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len > 7 && po_ascii_ncase(url, "http://", 7) == 0) i = 7; |
55 |
0 |
55 |
| if (len > 7 && po_ascii_ncase(url, "http://", 7) == 0) i = 7; |
32 |
23 |
55 |
|
| 76 |
23 |
▶ | else if (len > 8 && po_ascii_ncase(url, "https://", 8) == 0) i = 8; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (len > 8 && po_ascii_ncase(url, "https://", 8) == 0) i = 8; |
22 |
1 |
23 |
| else if (len > 8 && po_ascii_ncase(url, "https://", 8) == 0) i = 8; |
15 |
7 |
22 |
|
| 77 |
8 |
| else return PO_TGT_BAD_SCHEME; |
| 78 |
|
| |
| 79 |
|
| /* Userinfo. `http://metadata@127.0.0.1/` puts a decoy before the @, and a |
| 80 |
|
| * parser that stops at the first delimiter reads the decoy as the host. */ |
| 81 |
|
| { |
| 82 |
47 |
| size_t j, at = 0; |
| 83 |
696 |
▶ | for (j = i; j < len; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = i; j < len; j++) { |
696 |
0 |
696 |
|
| 84 |
696 |
▶ | if (url[j] == '@') at = j + 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (url[j] == '@') at = j + 1; |
1 |
695 |
696 |
|
| 85 |
696 |
▶ | if (url[j] == '/' || url[j] == '?' || url[j] == '#') break; |
Branch: 3 branches
| Branch | True | False | Total |
| if (url[j] == '/' || url[j] == '?' || url[j] == '#') break; |
649 |
47 |
696 |
| if (url[j] == '/' || url[j] == '?' || url[j] == '#') break; |
649 |
0 |
649 |
| if (url[j] == '/' || url[j] == '?' || url[j] == '#') break; |
649 |
0 |
649 |
|
| 86 |
|
| } |
| 87 |
47 |
▶ | if (at > i) i = at; |
Branch: 1 branch
| Branch | True | False | Total |
| if (at > i) i = at; |
1 |
46 |
47 |
|
| 88 |
|
| } |
| 89 |
|
| |
| 90 |
47 |
▶ | if (i < len && url[i] == '[') { v6 = 1; i++; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (i < len && url[i] == '[') { v6 = 1; i++; } |
47 |
0 |
47 |
| if (i < len && url[i] == '[') { v6 = 1; i++; } |
4 |
43 |
47 |
|
| 91 |
|
| |
| 92 |
650 |
▶ | for (; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; i < len; i++) { |
650 |
0 |
650 |
|
| 93 |
650 |
| char c = url[i]; |
| 94 |
650 |
▶ | if (v6 && c == ']') break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (v6 && c == ']') break; |
37 |
613 |
650 |
| if (v6 && c == ']') break; |
4 |
33 |
37 |
|
| 95 |
646 |
▶ | if (!v6 && (c == ':' || c == '/' || c == '?' || c == '#')) break; |
Branch: 5 branches
| Branch | True | False | Total |
| if (!v6 && (c == ':' || c == '/' || c == '?' || c == '#')) break; |
613 |
33 |
646 |
| if (!v6 && (c == ':' || c == '/' || c == '?' || c == '#')) break; |
609 |
4 |
613 |
| if (!v6 && (c == ':' || c == '/' || c == '?' || c == '#')) break; |
570 |
39 |
609 |
| if (!v6 && (c == ':' || c == '/' || c == '?' || c == '#')) break; |
570 |
0 |
570 |
| if (!v6 && (c == ':' || c == '/' || c == '?' || c == '#')) break; |
570 |
0 |
570 |
|
| 96 |
603 |
▶ | if (c == '/' || c == '?' || c == '#') break; |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '/' || c == '?' || c == '#') break; |
603 |
0 |
603 |
| if (c == '/' || c == '?' || c == '#') break; |
603 |
0 |
603 |
| if (c == '/' || c == '?' || c == '#') break; |
603 |
0 |
603 |
|
| 97 |
603 |
▶ | if (n + 1 >= cap) return PO_TGT_MALFORMED; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n + 1 >= cap) return PO_TGT_MALFORMED; |
0 |
603 |
603 |
|
| 98 |
603 |
▶ | out[n++] = (c >= 'A' && c <= 'Z') ? (char)(c - 'A' + 'a') : c; |
Branch: 2 branches
| Branch | True | False | Total |
| out[n++] = (c >= 'A' && c <= 'Z') ? (char)(c - 'A' + 'a') : c; |
348 |
255 |
603 |
| out[n++] = (c >= 'A' && c <= 'Z') ? (char)(c - 'A' + 'a') : c; |
11 |
337 |
348 |
|
| 99 |
|
| } |
| 100 |
47 |
| out[n] = '\0'; |
| 101 |
47 |
▶ | if (!n) return PO_TGT_MALFORMED; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return PO_TGT_MALFORMED; |
0 |
47 |
47 |
|
| 102 |
47 |
| *is_v6 = v6; |
| 103 |
47 |
| return PO_TGT_OK; |
| 104 |
|
| } |
| 105 |
|
| |
| 106 |
|
| /* Parse a dotted quad. Returns 0 when the host is not one, which is the |
| 107 |
|
| * common case (a name), not an error. */ |
| 108 |
38 |
| static int po_tgt_v4(const char *h, uint32_t *out) { |
| 109 |
|
| uint32_t o[4]; |
| 110 |
38 |
| int part = 0; |
| 111 |
38 |
| size_t i = 0; |
| 112 |
110 |
▶ | for (part = 0; part < 4; part++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (part = 0; part < 4; part++) { |
92 |
18 |
110 |
|
| 113 |
92 |
| uint32_t v = 0; |
| 114 |
92 |
| int digits = 0; |
| 115 |
222 |
▶ | while (h[i] >= '0' && h[i] <= '9') { |
Branch: 2 branches
| Branch | True | False | Total |
| while (h[i] >= '0' && h[i] <= '9') { |
150 |
72 |
222 |
| while (h[i] >= '0' && h[i] <= '9') { |
130 |
20 |
150 |
|
| 116 |
130 |
| v = v * 10 + (uint32_t)(h[i] - '0'); |
| 117 |
130 |
▶ | if (v > 255) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (v > 255) return 0; |
0 |
130 |
130 |
|
| 118 |
130 |
| i++; digits++; |
| 119 |
130 |
▶ | if (digits > 3) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (digits > 3) return 0; |
0 |
130 |
130 |
|
| 120 |
|
| } |
| 121 |
92 |
▶ | if (!digits) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!digits) return 0; |
20 |
72 |
92 |
|
| 122 |
72 |
| o[part] = v; |
| 123 |
72 |
▶ | if (part < 3) { if (h[i] != '.') return 0; i++; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (part < 3) { if (h[i] != '.') return 0; i++; } |
54 |
18 |
72 |
| if (part < 3) { if (h[i] != '.') return 0; i++; } |
0 |
54 |
54 |
|
| 124 |
|
| } |
| 125 |
18 |
▶ | if (h[i] != '\0') return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (h[i] != '\0') return 0; |
0 |
18 |
18 |
|
| 126 |
18 |
| *out = (o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3]; |
| 127 |
18 |
| return 1; |
| 128 |
|
| } |
| 129 |
|
| |
| 130 |
18 |
| static int po_tgt_v4_policy(uint32_t a) { |
| 131 |
18 |
▶ | if ((a >> 24) == 127) return PO_TGT_LOOPBACK; /* 127.0.0.0/8 */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a >> 24) == 127) return PO_TGT_LOOPBACK; /* 127.0.0.0/8 */ |
7 |
11 |
18 |
|
| 132 |
11 |
▶ | if (a == 0) return PO_TGT_LOOPBACK; /* 0.0.0.0 */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (a == 0) return PO_TGT_LOOPBACK; /* 0.0.0.0 */ |
1 |
10 |
11 |
|
| 133 |
10 |
▶ | if ((a >> 16) == 0xA9FE) return PO_TGT_LINK_LOCAL; /* 169.254.0.0/16 */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a >> 16) == 0xA9FE) return PO_TGT_LINK_LOCAL; /* 169.254.0.0/16 */ |
3 |
7 |
10 |
|
| 134 |
7 |
▶ | if ((a >> 24) == 10) return PO_TGT_PRIVATE; /* 10.0.0.0/8 */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a >> 24) == 10) return PO_TGT_PRIVATE; /* 10.0.0.0/8 */ |
3 |
4 |
7 |
|
| 135 |
4 |
▶ | if ((a >> 20) == 0xAC1) return PO_TGT_PRIVATE; /* 172.16.0.0/12 */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a >> 20) == 0xAC1) return PO_TGT_PRIVATE; /* 172.16.0.0/12 */ |
2 |
2 |
4 |
|
| 136 |
2 |
▶ | if ((a >> 16) == 0xC0A8) return PO_TGT_PRIVATE; /* 192.168.0.0/16 */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a >> 16) == 0xC0A8) return PO_TGT_PRIVATE; /* 192.168.0.0/16 */ |
1 |
1 |
2 |
|
| 137 |
1 |
▶ | if ((a >> 8) == 0xC00002) return PO_TGT_PRIVATE; /* 192.0.2.0/24 */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a >> 8) == 0xC00002) return PO_TGT_PRIVATE; /* 192.0.2.0/24 */ |
0 |
1 |
1 |
|
| 138 |
1 |
▶ | if ((a >> 22) == 0x191) return PO_TGT_PRIVATE; /* 100.64.0.0/10 */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ((a >> 22) == 0x191) return PO_TGT_PRIVATE; /* 100.64.0.0/10 */ |
1 |
0 |
1 |
|
| 139 |
0 |
| return PO_TGT_OK; |
| 140 |
|
| } |
| 141 |
|
| |
| 142 |
|
| /* IPv6, on the text. ::1 is loopback, fe80::/10 link-local, fc00::/7 unique |
| 143 |
|
| * local, and ::ffff:127.0.0.1 is loopback wearing a hat. */ |
| 144 |
4 |
| static int po_tgt_v6_policy(const char *h) { |
| 145 |
4 |
| size_t n = strlen(h); |
| 146 |
4 |
▶ | if (n == 0) return PO_TGT_MALFORMED; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) return PO_TGT_MALFORMED; |
0 |
4 |
4 |
|
| 147 |
4 |
▶ | if (strcmp(h, "::1") == 0 || strcmp(h, "::") == 0) return PO_TGT_LOOPBACK; |
Branch: 2 branches
| Branch | True | False | Total |
| if (strcmp(h, "::1") == 0 || strcmp(h, "::") == 0) return PO_TGT_LOOPBACK; |
3 |
1 |
4 |
| if (strcmp(h, "::1") == 0 || strcmp(h, "::") == 0) return PO_TGT_LOOPBACK; |
0 |
3 |
3 |
|
| 148 |
3 |
▶ | if (n >= 4 && h[0] == 'f' && h[1] == 'e' |
Branch: 3 branches
| Branch | True | False | Total |
| if (n >= 4 && h[0] == 'f' && h[1] == 'e' |
3 |
0 |
3 |
| if (n >= 4 && h[0] == 'f' && h[1] == 'e' |
2 |
1 |
3 |
| if (n >= 4 && h[0] == 'f' && h[1] == 'e' |
1 |
1 |
2 |
|
| 149 |
1 |
▶ | && (h[2] == '8' || h[2] == '9' || h[2] == 'a' || h[2] == 'b')) |
Branch: 4 branches
| Branch | True | False | Total |
| && (h[2] == '8' || h[2] == '9' || h[2] == 'a' || h[2] == 'b')) |
0 |
1 |
1 |
| && (h[2] == '8' || h[2] == '9' || h[2] == 'a' || h[2] == 'b')) |
0 |
0 |
0 |
| && (h[2] == '8' || h[2] == '9' || h[2] == 'a' || h[2] == 'b')) |
0 |
0 |
0 |
| && (h[2] == '8' || h[2] == '9' || h[2] == 'a' || h[2] == 'b')) |
0 |
0 |
0 |
|
| 150 |
1 |
| return PO_TGT_LINK_LOCAL; |
| 151 |
2 |
▶ | if (n >= 2 && h[0] == 'f' && (h[1] == 'c' || h[1] == 'd')) |
Branch: 4 branches
| Branch | True | False | Total |
| if (n >= 2 && h[0] == 'f' && (h[1] == 'c' || h[1] == 'd')) |
2 |
0 |
2 |
| if (n >= 2 && h[0] == 'f' && (h[1] == 'c' || h[1] == 'd')) |
1 |
1 |
2 |
| if (n >= 2 && h[0] == 'f' && (h[1] == 'c' || h[1] == 'd')) |
0 |
1 |
1 |
| if (n >= 2 && h[0] == 'f' && (h[1] == 'c' || h[1] == 'd')) |
0 |
0 |
0 |
|
| 152 |
1 |
| return PO_TGT_PRIVATE; |
| 153 |
|
| /* An IPv4-mapped address carries the v4 policy, not a v6 pass. */ |
| 154 |
1 |
▶ | if (n > 7 && po_ascii_ncase(h, "::ffff:", 7) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (n > 7 && po_ascii_ncase(h, "::ffff:", 7) == 0) { |
1 |
0 |
1 |
| if (n > 7 && po_ascii_ncase(h, "::ffff:", 7) == 0) { |
1 |
0 |
1 |
|
| 155 |
|
| uint32_t a; |
| 156 |
1 |
▶ | if (po_tgt_v4(h + 7, &a)) return po_tgt_v4_policy(a); |
Branch: 1 branch
| Branch | True | False | Total |
| if (po_tgt_v4(h + 7, &a)) return po_tgt_v4_policy(a); |
1 |
0 |
1 |
|
| 157 |
|
| } |
| 158 |
0 |
| return PO_TGT_OK; |
| 159 |
|
| } |
| 160 |
|
| |
| 161 |
|
| /* Names that resolve to loopback by convention. Refused by name as well as by |
| 162 |
|
| * address, because `http://localhost:5432/` never reaches a resolver check |
| 163 |
|
| * that is only applied to literals. */ |
| 164 |
20 |
| static int po_tgt_name_policy(const char *h) { |
| 165 |
20 |
| size_t n = strlen(h); |
| 166 |
20 |
▶ | if (strcmp(h, "localhost") == 0) return PO_TGT_LOOPBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(h, "localhost") == 0) return PO_TGT_LOOPBACK; |
3 |
17 |
20 |
|
| 167 |
17 |
▶ | if (n > 10 && strcmp(h + n - 10, ".localhost") == 0) return PO_TGT_LOOPBACK; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n > 10 && strcmp(h + n - 10, ".localhost") == 0) return PO_TGT_LOOPBACK; |
17 |
0 |
17 |
| if (n > 10 && strcmp(h + n - 10, ".localhost") == 0) return PO_TGT_LOOPBACK; |
1 |
16 |
17 |
|
| 168 |
16 |
▶ | if (n > 6 && strcmp(h + n - 6, ".local") == 0) return PO_TGT_LINK_LOCAL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n > 6 && strcmp(h + n - 6, ".local") == 0) return PO_TGT_LINK_LOCAL; |
16 |
0 |
16 |
| if (n > 6 && strcmp(h + n - 6, ".local") == 0) return PO_TGT_LINK_LOCAL; |
1 |
15 |
16 |
|
| 169 |
15 |
▶ | if (strcmp(h, "metadata.google.internal") == 0) return PO_TGT_LINK_LOCAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(h, "metadata.google.internal") == 0) return PO_TGT_LINK_LOCAL; |
3 |
12 |
15 |
|
| 170 |
12 |
▶ | if (n > 9 && strcmp(h + n - 9, ".internal") == 0) return PO_TGT_PRIVATE; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n > 9 && strcmp(h + n - 9, ".internal") == 0) return PO_TGT_PRIVATE; |
12 |
0 |
12 |
| if (n > 9 && strcmp(h + n - 9, ".internal") == 0) return PO_TGT_PRIVATE; |
1 |
11 |
12 |
|
| 171 |
11 |
| return PO_TGT_OK; |
| 172 |
|
| } |
| 173 |
|
| |
| 174 |
|
| /* An allowlist entry matches a whole host or a dot-anchored suffix. The |
| 175 |
|
| * anchor matters: an entry of `example.com` must not admit |
| 176 |
|
| * `example.com.attacker.net`, and a plain suffix compare does exactly that. */ |
| 177 |
6 |
| static int po_tgt_allowed(const char *h, const char *const *allow, int nallow) { |
| 178 |
|
| int i; |
| 179 |
6 |
| size_t hn = strlen(h); |
| 180 |
9 |
▶ | for (i = 0; i < nallow; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nallow; i++) { |
6 |
3 |
9 |
|
| 181 |
6 |
| size_t an = strlen(allow[i]); |
| 182 |
6 |
▶ | if (an == hn && memcmp(h, allow[i], an) == 0) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (an == hn && memcmp(h, allow[i], an) == 0) return 1; |
2 |
4 |
6 |
| if (an == hn && memcmp(h, allow[i], an) == 0) return 1; |
2 |
0 |
2 |
|
| 183 |
4 |
▶ | if (hn > an + 1 && h[hn - an - 1] == '.' |
Branch: 2 branches
| Branch | True | False | Total |
| if (hn > an + 1 && h[hn - an - 1] == '.' |
4 |
0 |
4 |
| if (hn > an + 1 && h[hn - an - 1] == '.' |
1 |
3 |
4 |
|
| 184 |
1 |
▶ | && memcmp(h + hn - an, allow[i], an) == 0) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| && memcmp(h + hn - an, allow[i], an) == 0) return 1; |
1 |
0 |
1 |
|
| 185 |
|
| } |
| 186 |
3 |
| return 0; |
| 187 |
|
| } |
| 188 |
|
| |
| 189 |
|
| /* The whole policy. |
| 190 |
|
| * |
| 191 |
|
| * `allow` overrides the range refusals - an operator who has decided that an |
| 192 |
|
| * internal host is a legitimate webhook target says so explicitly - but |
| 193 |
|
| * never overrides the scheme, because `file://` and `gopher://` are not |
| 194 |
|
| * webhook destinations under any policy. */ |
| 195 |
56 |
| static int po_target_ok(const char *url, size_t len, |
| 196 |
|
| const char *const *allow, int nallow) { |
| 197 |
|
| char host[PO_TGT_HOST_MAX]; |
| 198 |
56 |
| int v6 = 0, rc; |
| 199 |
|
| |
| 200 |
56 |
▶ | if (!url || !len) return PO_TGT_MALFORMED; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!url || !len) return PO_TGT_MALFORMED; |
56 |
0 |
56 |
| if (!url || !len) return PO_TGT_MALFORMED; |
1 |
55 |
56 |
|
| 201 |
55 |
| rc = po_tgt_host(url, len, host, sizeof(host), &v6); |
| 202 |
55 |
▶ | if (rc != PO_TGT_OK) return rc; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc != PO_TGT_OK) return rc; |
8 |
47 |
55 |
|
| 203 |
|
| |
| 204 |
47 |
▶ | if (allow && nallow) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (allow && nallow) { |
6 |
41 |
47 |
| if (allow && nallow) { |
6 |
0 |
6 |
|
| 205 |
6 |
▶ | if (po_tgt_allowed(host, allow, nallow)) return PO_TGT_OK; |
Branch: 1 branch
| Branch | True | False | Total |
| if (po_tgt_allowed(host, allow, nallow)) return PO_TGT_OK; |
3 |
3 |
6 |
|
| 206 |
|
| /* An allowlist that exists is exhaustive. Falling through to the |
| 207 |
|
| * default ranges would make it a suggestion. */ |
| 208 |
3 |
| return PO_TGT_NOT_ALLOWED; |
| 209 |
|
| } |
| 210 |
|
| |
| 211 |
41 |
▶ | if (v6) return po_tgt_v6_policy(host); |
Branch: 1 branch
| Branch | True | False | Total |
| if (v6) return po_tgt_v6_policy(host); |
4 |
37 |
41 |
|
| 212 |
|
| { |
| 213 |
|
| uint32_t a; |
| 214 |
37 |
▶ | if (po_tgt_v4(host, &a)) return po_tgt_v4_policy(a); |
Branch: 1 branch
| Branch | True | False | Total |
| if (po_tgt_v4(host, &a)) return po_tgt_v4_policy(a); |
17 |
20 |
37 |
|
| 215 |
|
| } |
| 216 |
20 |
| return po_tgt_name_policy(host); |
| 217 |
|
| } |
| 218 |
|
| |
| 219 |
|
| #endif /* PO_TARGET_H */ |