include/punk/punk_proxy.h

filter: Statementstmt 96.6% 290 / 300 Branchbran 75.9% 287 / 378 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.1% 577 / 678 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_static.h ↑ Summary xs/static.xs »
1 #ifndef PUNK_PROXY_H
2 #define PUNK_PROXY_H
3  
4 /* punk_proxy.h - reverse-proxy trust, in C.
5 *
6 * The `proxy` keyword freezes a trust policy at to_app; pp_resolve runs once
7 * at the top of punk_serve, before routing, and OVERWRITES REMOTE_ADDR with
8 * the real client. Everything downstream - rate_limit (punk_ratelimit.h),
9 * $c->block_ip, the access log, $c->req->address - then reads the right
10 * address without any of them being changed. The mount path already rewrites
11 * env keys in place (punk_serve.h), so this is an established move.
12 *
13 * Why it exists: rate_limit keys on REMOTE_ADDR, and because Hyperman's
14 * arena makes a counter exact across the whole worker pool rather than per
15 * worker, a limiter behind a proxy puts EVERY client in one bucket - a
16 * 100/min rule throttles the whole site at 100/min. block_ip, keyed the same
17 * way, bans the load balancer.
18 *
19 * The hop rule, which is what almost every implementation gets wrong:
20 * X-Forwarded-For reads `client, proxy1, proxy2` and each hop APPENDS the
21 * address it received the connection FROM. The socket peer is the last
22 * proxy and never appears in the header it forwarded. So with N trusted
23 * proxies the client sits at index N-1 counting from the RIGHT. Taking the
24 * leftmost entry is the spoofable version: the client writes that one.
25 *
26 * Everything here is prefixed pp_ / PP_. Needs nothing but punk_compat.h;
27 * included before punk_serve.h, which calls pp_resolve.
28 */
29  
30 #include <string.h>
31  
32 /* A chain longer than this is pathological. We keep the RIGHTMOST entries,
33 * which is the end the hop walk starts from; 'all' mode scans forward for
34 * the leftmost separately and is unaffected. */
35 #define PP_MAX_HOPS 32
36  
37 /* Addresses are held as 16 bytes, v4 stored v4-mapped (::ffff:a.b.c.d), so
38 * one compare serves both families and a v4 CIDR written by an operator
39 * still matches the v4-mapped form a dual-stack listener hands us. */
40 typedef struct {
41 unsigned char addr[16];
42 int bits; /* prefix length in the 128-bit space */
43 } pp_cidr;
44  
45 typedef struct {
46 int hops; /* >0 fixed count; 0 = use cidrs; -1 = all */
47 pp_cidr *cidrs;
48 int ncidrs;
49 char *for_key; /* pre-built env keys: HTTP_X_FORWARDED_FOR */
50 STRLEN for_len;
51 char *proto_key;
52 STRLEN proto_len;
53 char *host_key;
54 STRLEN host_len;
55 char *port_key;
56 STRLEN port_len;
57 } pp_policy;
58  
59 static void pp_free(pTHX_ pp_policy *p); /* the error path in pp_compile */
60  
61 /* ---- address parsing ----------------------------------------------------
62 * Hand-rolled rather than inet_pton: this runs on attacker-controlled bytes
63 * that become a shared-memory key, the grammar wanted here is stricter than
64 * the libc one (no octal, no shortened v4), and it keeps the Windows story
65 * free of ws2tcpip.h. */
66  
67 /* Strict dotted quad: four decimal octets, no leading zeros (a leading zero
68 * is octal to some resolvers and decimal to others, which is exactly the
69 * ambiguity an address filter must not inherit), nothing trailing. */
70 110 static int pp_parse_v4(const char *s, STRLEN len, unsigned char out[4]) {
71 110 STRLEN i = 0;
72 int oct;
73 453 ▶ for (oct = 0; oct < 4; oct++) {
Branch: 1 branch
BranchTrueFalseTotal
for (oct = 0; oct < 4; oct++) { 369 84 453
74 369 int v = 0, nd = 0;
75 369 ▶ if (oct) {
Branch: 1 branch
BranchTrueFalseTotal
if (oct) { 259 110 369
76 259 ▶ if (i >= len || s[i] != '.') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i >= len || s[i] != '.') return 0; 257 2 259
if (i >= len || s[i] != '.') return 0; 1 256 257
77 256 i++;
78 }
79 857 ▶ while (i < len && s[i] >= '0' && s[i] <= '9') {
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && s[i] >= '0' && s[i] <= '9') { 773 84 857
while (i < len && s[i] >= '0' && s[i] <= '9') { 515 258 773
while (i < len && s[i] >= '0' && s[i] <= '9') { 501 14 515
80 501 ▶ if (nd == 0 && s[i] == '0' && i + 1 < len
Branch: 3 branches
BranchTrueFalseTotal
if (nd == 0 && s[i] == '0' && i + 1 < len 353 148 501
if (nd == 0 && s[i] == '0' && i + 1 < len 84 269 353
if (nd == 0 && s[i] == '0' && i + 1 < len 72 12 84
81 72 ▶ && s[i + 1] >= '0' && s[i + 1] <= '9')
Branch: 2 branches
BranchTrueFalseTotal
&& s[i + 1] >= '0' && s[i + 1] <= '9') 2 70 72
&& s[i + 1] >= '0' && s[i + 1] <= '9') 2 0 2
82 2 return 0; /* leading zero */
83 499 v = v * 10 + (s[i] - '0');
84 499 ▶ if (v > 255) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (v > 255) return 0; 8 491 499
85 491 nd++; i++;
86 491 ▶ if (nd > 3) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nd > 3) return 0; 0 491 491
87 }
88 356 ▶ if (!nd) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!nd) return 0; 13 343 356
89 343 out[oct] = (unsigned char)v;
90 }
91 84 return i == len;
92 }
93  
94 /* RFC 4291 text form, including `::` compression and an embedded v4 tail. */
95 24 static int pp_parse_v6(const char *s, STRLEN len, unsigned char out[16]) {
96 unsigned char b[16];
97 24 int gi = 0, dc = -1;
98 24 STRLEN i = 0;
99  
100 24 memset(b, 0, sizeof(b));
101 24 ▶ if (len < 2) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 2) return 0; 0 24 24
102 24 ▶ if (s[0] == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (s[0] == ':') { 22 2 24
103 2 ▶ if (s[1] != ':') return 0; /* a lone leading ':' */
Branch: 1 branch
BranchTrueFalseTotal
if (s[1] != ':') return 0; /* a lone leading ':' */ 0 2 2
104 2 dc = 0; i = 2;
105 2 ▶ if (i == len) { memcpy(out, b, 16); return 1; } /* "::" */
Branch: 1 branch
BranchTrueFalseTotal
if (i == len) { memcpy(out, b, 16); return 1; } /* "::" */ 2 0 2
106 }
107  
108 18 for (;;) {
109 42 STRLEN j = i;
110 42 int hex = 0, nd = 0, isv4 = 0;
111 42 ▶ if (gi >= 8) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (gi >= 8) return 0; 0 42 42
112 185 ▶ while (j < len && s[j] != ':') {
Branch: 2 branches
BranchTrueFalseTotal
while (j < len && s[j] != ':') { 173 12 185
while (j < len && s[j] != ':') { 149 24 173
113 149 ▶ if (s[j] == '.') { isv4 = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (s[j] == '.') { isv4 = 1; break; } 6 143 149
114 143 j++;
115 }
116 42 ▶ if (isv4) {
Branch: 1 branch
BranchTrueFalseTotal
if (isv4) { 6 36 42
117 unsigned char v4[4];
118 6 STRLEN k = i;
119 53 ▶ while (k < len && s[k] != ':') k++;
Branch: 2 branches
BranchTrueFalseTotal
while (k < len && s[k] != ':') k++; 47 6 53
while (k < len && s[k] != ':') k++; 47 0 47
120 10 ▶ if (k != len) return 0; /* the v4 tail must be last */
Branch: 1 branch
BranchTrueFalseTotal
if (k != len) return 0; /* the v4 tail must be last */ 0 6 6
121 6 ▶ if (gi > 6) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (gi > 6) return 0; 0 6 6
122 6 ▶ if (!pp_parse_v4(s + i, len - i, v4)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!pp_parse_v4(s + i, len - i, v4)) return 0; 4 2 6
123 2 memcpy(b + gi * 2, v4, 4);
124 2 gi += 2;
125 2 break;
126 }
127 126 ▶ while (i < j) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < j) { 94 32 126
128 94 int c = (unsigned char)s[i], v;
129 94 ▶ if (c >= '0' && c <= '9') v = c - '0';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') v = c - '0'; 94 0 94
if (c >= '0' && c <= '9') v = c - '0'; 57 37 94
130 37 ▶ else if (c >= 'a' && c <= 'f') v = c - 'a' + 10;
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 'a' && c <= 'f') v = c - 'a' + 10; 37 0 37
else if (c >= 'a' && c <= 'f') v = c - 'a' + 10; 34 3 37
131 3 ▶ else if (c >= 'A' && c <= 'F') v = c - 'A' + 10;
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 'A' && c <= 'F') v = c - 'A' + 10; 3 0 3
else if (c >= 'A' && c <= 'F') v = c - 'A' + 10; 0 3 3
132 3 else return 0;
133 91 hex = (hex << 4) | v;
134 91 nd++;
135 91 ▶ if (nd > 4) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nd > 4) return 0; 1 90 91
136 90 i++;
137 }
138 32 ▶ if (!nd) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!nd) return 0; 1 31 32
139 31 b[gi * 2] = (unsigned char)(hex >> 8);
140 31 b[gi * 2 + 1] = (unsigned char)(hex & 0xff);
141 31 gi++;
142 31 ▶ if (i == len) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i == len) break; 9 22 31
143 22 i++; /* the ':' */
144 22 ▶ if (i < len && s[i] == ':') {
Branch: 2 branches
BranchTrueFalseTotal
if (i < len && s[i] == ':') { 22 0 22
if (i < len && s[i] == ':') { 15 7 22
145 15 ▶ if (dc >= 0) return 0; /* a second '::' */
Branch: 1 branch
BranchTrueFalseTotal
if (dc >= 0) return 0; /* a second '::' */ 1 14 15
146 14 dc = gi;
147 14 i++;
148 14 ▶ if (i == len) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i == len) break; 3 11 14
149 7 ▶ } else if (i == len) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (i == len) { 0 7 7
150 0 return 0; /* a trailing single ':' */
151 }
152 }
153  
154 14 ▶ if (dc < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (dc < 0) { 0 14 14
155 0 ▶ if (gi != 8) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (gi != 8) return 0; 0 0 0
156 0 memcpy(out, b, 16);
157 0 return 1;
158 }
159 14 ▶ if (gi >= 8) return 0; /* '::' must cover >= 1 group */
Branch: 1 branch
BranchTrueFalseTotal
if (gi >= 8) return 0; /* '::' must cover >= 1 group */ 0 14 14
160 14 memset(out, 0, 16);
161 14 memcpy(out, b, (size_t)dc * 2);
162 14 memcpy(out + 16 - (size_t)(gi - dc) * 2, b + dc * 2,
163 14 (size_t)(gi - dc) * 2);
164 14 return 1;
165 }
166  
167 /* One address in either family into 16 bytes. Surrounding whitespace is
168 * trimmed; a bracketed `[::1]:443` and a `1.2.3.4:5678` port suffix are
169 * accepted and dropped, because real proxies emit both and rejecting them
170 * would silently fall back to the peer. *is_v4 reports which family was
171 * written, which is what a CIDR's prefix length is relative to.
172 *
173 * `sp`/`sl` report the span actually parsed - the address WITHOUT the
174 * brackets, port or padding. Callers that go on to store the value must use
175 * that span and not the raw token, or REMOTE_ADDR ends up as
176 * "1.2.3.4:5678" and the same client hashes to two rate-limit keys. */
177 106 static int pp_parse_addr(const char *s, STRLEN len, unsigned char out[16],
178 int *is_v4, const char **sp, STRLEN *sl) {
179 unsigned char v4[4];
180 106 int dots = 0, colons = 0;
181 STRLEN i;
182  
183 106 ▶ while (len && (*s == ' ' || *s == '\t')) { s++; len--; }
Branch: 3 branches
BranchTrueFalseTotal
while (len && (*s == ' ' || *s == '\t')) { s++; len--; } 104 2 106
while (len && (*s == ' ' || *s == '\t')) { s++; len--; } 0 104 104
while (len && (*s == ' ' || *s == '\t')) { s++; len--; } 0 104 104
184 106 ▶ while (len && (s[len - 1] == ' ' || s[len - 1] == '\t')) len--;
Branch: 3 branches
BranchTrueFalseTotal
while (len && (s[len - 1] == ' ' || s[len - 1] == '\t')) len--; 104 2 106
while (len && (s[len - 1] == ' ' || s[len - 1] == '\t')) len--; 0 104 104
while (len && (s[len - 1] == ' ' || s[len - 1] == '\t')) len--; 0 104 104
185  
186 106 ▶ if (len > 1 && *s == '[') { /* [addr] or [addr]:port */
Branch: 2 branches
BranchTrueFalseTotal
if (len > 1 && *s == '[') { /* [addr] or [addr]:port */ 104 2 106
if (len > 1 && *s == '[') { /* [addr] or [addr]:port */ 2 102 104
187 2 const char *close = (const char *)memchr(s, ']', len);
188 2 ▶ if (!close) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!close) return 0; 0 2 2
189 2 s++;
190 2 len = (STRLEN)(close - s);
191 }
192 106 ▶ if (!len || len > 45) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!len || len > 45) return 0; 104 2 106
if (!len || len > 45) return 0; 0 104 104
193  
194 1020 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 916 104 1020
195 916 ▶ if (s[i] == '.') dots++;
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] == '.') dots++; 258 658 916
196 658 ▶ else if (s[i] == ':') colons++;
Branch: 1 branch
BranchTrueFalseTotal
else if (s[i] == ':') colons++; 45 613 658
197 }
198 104 ▶ if (dots == 3 && colons == 1) { /* v4:port */
Branch: 2 branches
BranchTrueFalseTotal
if (dots == 3 && colons == 1) { /* v4:port */ 84 20 104
if (dots == 3 && colons == 1) { /* v4:port */ 1 83 84
199 5 ▶ while (len && s[len - 1] != ':') len--;
Branch: 2 branches
BranchTrueFalseTotal
while (len && s[len - 1] != ':') len--; 5 0 5
while (len && s[len - 1] != ':') len--; 4 1 5
200 1 ▶ if (len) len--;
Branch: 1 branch
BranchTrueFalseTotal
if (len) len--; 1 0 1
201 }
202 104 ▶ if (!len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!len) return 0; 0 104 104
203  
204 104 ▶ if (sp) *sp = s;
Branch: 1 branch
BranchTrueFalseTotal
if (sp) *sp = s; 60 44 104
205 104 ▶ if (sl) *sl = len;
Branch: 1 branch
BranchTrueFalseTotal
if (sl) *sl = len; 60 44 104
206  
207 104 ▶ if (pp_parse_v4(s, len, v4)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pp_parse_v4(s, len, v4)) { 80 24 104
208 80 memset(out, 0, 10);
209 80 out[10] = 0xff; out[11] = 0xff;
210 80 memcpy(out + 12, v4, 4);
211 80 ▶ if (is_v4) *is_v4 = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (is_v4) *is_v4 = 1; 80 0 80
212 80 return 1;
213 }
214 24 ▶ if (is_v4) *is_v4 = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (is_v4) *is_v4 = 0; 24 0 24
215 24 return pp_parse_v6(s, len, out);
216 }
217  
218 /* `addr` or `addr/prefixlen`. A v4 prefix is taken in the v4 space and
219 * lifted into the mapped range, so 10.0.0.0/8 is stored as /104 and an
220 * operator never has to know that. Returns 0 on anything malformed, which
221 * the keyword turns into a boot croak - a mistyped CIDR must not become a
222 * policy that quietly trusts nothing. */
223 17 static int pp_parse_cidr(const char *s, STRLEN len, pp_cidr *out) {
224 17 const char *slash = (const char *)memchr(s, '/', len);
225 17 ▶ STRLEN alen = slash ? (STRLEN)(slash - s) : len;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN alen = slash ? (STRLEN)(slash - s) : len; 15 2 17
226 17 int is_v4 = 0, bits;
227  
228 17 ▶ if (!pp_parse_addr(s, alen, out->addr, &is_v4, NULL, NULL)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!pp_parse_addr(s, alen, out->addr, &is_v4, NULL, NULL)) return 0; 1 16 17
229  
230 16 ▶ if (!slash) {
Branch: 1 branch
BranchTrueFalseTotal
if (!slash) { 1 15 16
231 1 out->bits = 128; /* a bare address is a host */
232 1 return 1;
233 }
234 {
235 15 STRLEN i = alen + 1;
236 15 int v = 0, nd = 0;
237 32 ▶ while (i < len && s[i] >= '0' && s[i] <= '9') {
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && s[i] >= '0' && s[i] <= '9') { 17 15 32
while (i < len && s[i] >= '0' && s[i] <= '9') { 17 0 17
while (i < len && s[i] >= '0' && s[i] <= '9') { 17 0 17
238 17 v = v * 10 + (s[i] - '0');
239 17 ▶ if (v > 128) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (v > 128) return 0; 0 17 17
240 17 nd++; i++;
241 17 ▶ if (nd > 3) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nd > 3) return 0; 0 17 17
242 }
243 15 ▶ if (!nd || i != len) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!nd || i != len) return 0; 15 0 15
if (!nd || i != len) return 0; 0 15 15
244 15 bits = v;
245 }
246 15 ▶ if (is_v4) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_v4) { 12 3 15
247 12 ▶ if (bits > 32) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (bits > 32) return 0; 1 11 12
248 11 bits += 96; /* into the mapped range */
249 }
250 14 out->bits = bits;
251 14 return 1;
252 }
253  
254 60 static int pp_cidr_match(const pp_cidr *c, const unsigned char a[16]) {
255 60 int full = c->bits >> 3, rem = c->bits & 7;
256 60 ▶ if (full && memcmp(c->addr, a, (size_t)full) != 0) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (full && memcmp(c->addr, a, (size_t)full) != 0) return 0; 60 0 60
if (full && memcmp(c->addr, a, (size_t)full) != 0) return 0; 33 27 60
257 27 ▶ if (rem) {
Branch: 1 branch
BranchTrueFalseTotal
if (rem) { 1 26 27
258 1 unsigned char m = (unsigned char)(0xff << (8 - rem));
259 1 ▶ if ((c->addr[full] & m) != (a[full] & m)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ((c->addr[full] & m) != (a[full] & m)) return 0; 0 1 1
260 }
261 27 return 1;
262 }
263  
264 47 static int pp_trusted(const pp_policy *p, const unsigned char a[16]) {
265 int i;
266 80 ▶ for (i = 0; i < p->ncidrs; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < p->ncidrs; i++) 60 20 80
267 60 ▶ if (pp_cidr_match(&p->cidrs[i], a)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (pp_cidr_match(&p->cidrs[i], a)) return 1; 27 33 60
268 20 return 0;
269 }
270  
271 /* ---- the header walk ---------------------------------------------------- */
272  
273 /* The leftmost comma-separated entry, trimmed. */
274 20 static const char *pp_first_token(const char *s, STRLEN len, STRLEN *outlen) {
275 20 const char *comma = (const char *)memchr(s, ',', len);
276 20 ▶ STRLEN l = comma ? (STRLEN)(comma - s) : len;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN l = comma ? (STRLEN)(comma - s) : len; 2 18 20
277 20 ▶ while (l && (*s == ' ' || *s == '\t')) { s++; l--; }
Branch: 3 branches
BranchTrueFalseTotal
while (l && (*s == ' ' || *s == '\t')) { s++; l--; } 20 0 20
while (l && (*s == ' ' || *s == '\t')) { s++; l--; } 0 20 20
while (l && (*s == ' ' || *s == '\t')) { s++; l--; } 0 20 20
278 20 ▶ while (l && (s[l - 1] == ' ' || s[l - 1] == '\t')) l--;
Branch: 3 branches
BranchTrueFalseTotal
while (l && (s[l - 1] == ' ' || s[l - 1] == '\t')) l--; 20 0 20
while (l && (s[l - 1] == ' ' || s[l - 1] == '\t')) l--; 0 20 20
while (l && (s[l - 1] == ' ' || s[l - 1] == '\t')) l--; 0 20 20
279 20 *outlen = l;
280 20 return s;
281 }
282  
283 /* The rightmost entries, trimmed, tok[0] being the last one on the line.
284 * Empty entries are KEPT rather than skipped: dropping them would shift the
285 * hop count, and a malformed entry must end the walk, not shorten it. */
286 54 static int pp_collect_right(const char *s, STRLEN len,
287 const char **tok, STRLEN *tlen, int max) {
288 54 int n = 0;
289 54 STRLEN end = len;
290 56 for (;;) {
291 110 STRLEN start = end;
292 const char *p;
293 STRLEN l;
294 110 ▶ if (n >= max) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n >= max) break; 1 109 110
295 1070 ▶ while (start > 0 && s[start - 1] != ',') start--;
Branch: 2 branches
BranchTrueFalseTotal
while (start > 0 && s[start - 1] != ',') start--; 1017 53 1070
while (start > 0 && s[start - 1] != ',') start--; 961 56 1017
296 109 p = s + start;
297 109 l = end - start;
298 167 ▶ while (l && (*p == ' ' || *p == '\t')) { p++; l--; }
Branch: 3 branches
BranchTrueFalseTotal
while (l && (*p == ' ' || *p == '\t')) { p++; l--; } 166 1 167
while (l && (*p == ' ' || *p == '\t')) { p++; l--; } 57 109 166
while (l && (*p == ' ' || *p == '\t')) { p++; l--; } 1 108 109
299 111 ▶ while (l && (p[l - 1] == ' ' || p[l - 1] == '\t')) l--;
Branch: 3 branches
BranchTrueFalseTotal
while (l && (p[l - 1] == ' ' || p[l - 1] == '\t')) l--; 110 1 111
while (l && (p[l - 1] == ' ' || p[l - 1] == '\t')) l--; 2 108 110
while (l && (p[l - 1] == ' ' || p[l - 1] == '\t')) l--; 0 108 108
300 109 tok[n] = p;
301 109 tlen[n] = l;
302 109 n++;
303 109 ▶ if (start == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (start == 0) break; 53 56 109
304 56 end = start - 1;
305 }
306 54 return n;
307 }
308  
309 /* The real client, as a pointer into `xff` or into `peer`. Never returns
310 * NULL: with nothing trustworthy to say, the answer is the socket peer,
311 * which is the address that is true by construction. */
312 58 static const char *pp_xff_client(const char *xff, STRLEN len,
313 const pp_policy *p,
314 const char *peer, STRLEN plen,
315 STRLEN *outlen) {
316 const char *tok[PP_MAX_HOPS];
317 STRLEN tlen[PP_MAX_HOPS];
318 unsigned char a[16];
319 const char *sp;
320 STRLEN sl;
321 int n, i, isv4;
322  
323 58 *outlen = plen;
324 58 ▶ if (!xff || !len) return peer;
Branch: 2 branches
BranchTrueFalseTotal
if (!xff || !len) return peer; 57 1 58
if (!xff || !len) return peer; 2 55 57
325  
326 55 ▶ if (p->hops < 0) { /* trust => 'all' */
Branch: 1 branch
BranchTrueFalseTotal
if (p->hops < 0) { /* trust => 'all' */ 1 54 55
327 STRLEN l;
328 1 const char *t = pp_first_token(xff, len, &l);
329 1 ▶ if (l && pp_parse_addr(t, l, a, &isv4, &sp, &sl)) {
Branch: 2 branches
BranchTrueFalseTotal
if (l && pp_parse_addr(t, l, a, &isv4, &sp, &sl)) { 1 0 1
if (l && pp_parse_addr(t, l, a, &isv4, &sp, &sl)) { 1 0 1
330 1 *outlen = sl;
331 1 return sp;
332 }
333 0 return peer;
334 }
335  
336 54 n = pp_collect_right(xff, len, tok, tlen, PP_MAX_HOPS);
337 54 ▶ if (n <= 0) return peer;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) return peer; 0 54 54
338  
339 54 ▶ if (p->hops > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->hops > 0) { 40 14 54
340 /* N trusted proxies: the client is at index N-1 from the right. A
341 * chain shorter than declared is a misconfiguration or a client
342 * that sent nothing - either way the peer is the honest answer, and
343 * reaching for the leftmost entry here is precisely the bug. */
344 40 i = p->hops - 1;
345 40 ▶ if (i >= n) return peer;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= n) return peer; 2 38 40
346 38 ▶ if (!pp_parse_addr(tok[i], tlen[i], a, &isv4, &sp, &sl)) return peer;
Branch: 1 branch
BranchTrueFalseTotal
if (!pp_parse_addr(tok[i], tlen[i], a, &isv4, &sp, &sl)) return peer; 9 29 38
347 29 *outlen = sl;
348 29 return sp;
349 }
350  
351 /* CIDR mode. The peer must itself be a trusted proxy, or the header is
352 * not ours to believe at all. */
353 14 ▶ if (!(pp_parse_addr(peer, plen, a, &isv4, NULL, NULL) && pp_trusted(p, a)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(pp_parse_addr(peer, plen, a, &isv4, NULL, NULL) && pp_trusted(p, a))) 14 0 14
if (!(pp_parse_addr(peer, plen, a, &isv4, NULL, NULL) && pp_trusted(p, a))) 1 13 14
354 1 return peer;
355 22 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 21 1 22
356 21 ▶ if (!pp_parse_addr(tok[i], tlen[i], a, &isv4, &sp, &sl)) return peer;
Branch: 1 branch
BranchTrueFalseTotal
if (!pp_parse_addr(tok[i], tlen[i], a, &isv4, &sp, &sl)) return peer; 0 21 21
357 21 ▶ if (!pp_trusted(p, a)) { *outlen = sl; return sp; }
Branch: 1 branch
BranchTrueFalseTotal
if (!pp_trusted(p, a)) { *outlen = sl; return sp; } 12 9 21
358 }
359 /* every entry was a trusted proxy: the leftmost is the client */
360 1 ▶ if (!pp_parse_addr(tok[n - 1], tlen[n - 1], a, &isv4, &sp, &sl))
Branch: 1 branch
BranchTrueFalseTotal
if (!pp_parse_addr(tok[n - 1], tlen[n - 1], a, &isv4, &sp, &sl)) 0 1 1
361 0 return peer;
362 1 *outlen = sl;
363 1 return sp;
364 }
365  
366 /* ---- the boot-time build ------------------------------------------------- */
367  
368 /* A header name into the PSGI env key it arrives as: uppercased, '-' to '_',
369 * HTTP_ prefixed. Done once at to_app so a request is a hash lookup on a
370 * string we already own. */
371 184 static char *pp_env_key(pTHX_ const char *name, STRLEN nlen, STRLEN *outlen) {
372 char *k;
373 STRLEN i;
374 184 Newx(k, nlen + 6, char);
375 184 memcpy(k, "HTTP_", 5);
376 3129 ▶ for (i = 0; i < nlen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nlen; i++) { 2945 184 3129
377 2945 char c = name[i];
378 2945 ▶ k[5 + i] = (c == '-') ? '_' : (char)toUPPER((unsigned char)c);
Branch: 2 branches
BranchTrueFalseTotal
k[5 + i] = (c == '-') ? '_' : (char)toUPPER((unsigned char)c); 2577 368 2945
k[5 + i] = (c == '-') ? '_' : (char)toUPPER((unsigned char)c); 2023 554 2577
379 }
380 184 k[5 + nlen] = '\0';
381 184 *outlen = nlen + 5;
382 184 return k;
383 }
384  
385 184 static char *pp_key_opt(pTHX_ HV *cfg, const char *opt, const char *dflt,
386 STRLEN *outlen) {
387 184 SV **v = hv_fetch(cfg, opt, (I32)strlen(opt), 0);
388 STRLEN nl;
389 const char *n;
390 184 ▶ if (v && *v && !SvOK(*v)) return NULL; /* undef switches it off */
Branch: 3 branches
BranchTrueFalseTotal
if (v && *v && !SvOK(*v)) return NULL; /* undef switches it off */ 1 183 184
if (v && *v && !SvOK(*v)) return NULL; /* undef switches it off */ 1 0 1
if (v && *v && !SvOK(*v)) return NULL; /* undef switches it off */ 0 1 1
391 185 ▶ n = (v && *v) ? SvPV_const(*v, nl) : (nl = strlen(dflt), dflt);
Branch: 2 branches
BranchTrueFalseTotal
n = (v && *v) ? SvPV_const(*v, nl) : (nl = strlen(dflt), dflt); 1 183 184
n = (v && *v) ? SvPV_const(*v, nl) : (nl = strlen(dflt), dflt); 1 0 1
392 184 ▶ if (!nl) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) return NULL; 0 184 184
393 184 return pp_env_key(aTHX_ n, nl, outlen);
394 }
395  
396 /* Freeze the keyword's config into the policy a request reads. Croaks on
397 * anything malformed - a mistyped CIDR or a nonsense hop count must fail at
398 * boot, where it is one line of output, not per request where it is a
399 * silently wrong address in a rate-limit key. `appenv` is $app->env. */
400 54 static pp_policy *pp_compile(pTHX_ HV *cfg, const char *appenv) {
401 pp_policy *p;
402 54 SV **t = hv_fetchs(cfg, "trust", 0);
403  
404 54 Newxz(p, 1, pp_policy);
405 54 p->hops = 1; /* bare `proxy` = one hop */
406  
407 54 ▶ if (t && *t && SvOK(*t)) {
Branch: 3 branches
BranchTrueFalseTotal
if (t && *t && SvOK(*t)) { 49 5 54
if (t && *t && SvOK(*t)) { 49 0 49
if (t && *t && SvOK(*t)) { 49 0 49
408 49 ▶ if (SvROK(*t) && SvTYPE(SvRV(*t)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(*t) && SvTYPE(SvRV(*t)) == SVt_PVAV) { 16 33 49
if (SvROK(*t) && SvTYPE(SvRV(*t)) == SVt_PVAV) { 15 1 16
409 15 AV *av = (AV *)SvRV(*t);
410 15 SSize_t i, n = av_len(av) + 1;
411 15 ▶ if (n <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) { 1 14 15
412 1 pp_free(aTHX_ p);
413 1 croak("Punk: proxy: trust => [] trusts nothing - drop the "
414 "keyword instead, or name the proxy networks");
415 }
416 14 Newxz(p->cidrs, (int)n, pp_cidr);
417 29 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 17 12 29
418 17 SV **e = av_fetch(av, i, 0);
419 STRLEN sl;
420 const char *s;
421 17 ▶ if (!(e && *e && SvOK(*e))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) { 17 0 17
if (!(e && *e && SvOK(*e))) { 17 0 17
if (!(e && *e && SvOK(*e))) { 0 17 17
422 0 pp_free(aTHX_ p);
423 0 croak("Punk: proxy: trust list entry %d is undef",
424 (int)i);
425 }
426 17 s = SvPV_const(*e, sl);
427 17 ▶ if (!pp_parse_cidr(s, sl, &p->cidrs[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pp_parse_cidr(s, sl, &p->cidrs[i])) { 2 15 17
428 2 SV *msg = sv_2mortal(newSVpvf(
429 "Punk: proxy: '%.*s' is not an address or CIDR",
430 (int)sl, s));
431 2 pp_free(aTHX_ p);
432 2 croak("%s", SvPV_nolen(msg));
433 }
434 }
435 12 p->ncidrs = (int)n;
436 12 p->hops = 0;
437 }
438 34 ▶ else if (!SvROK(*t) && !looks_like_number(*t)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (!SvROK(*t) && !looks_like_number(*t)) { 33 1 34
else if (!SvROK(*t) && !looks_like_number(*t)) { 5 28 33
439 STRLEN sl;
440 5 const char *s = SvPV_const(*t, sl);
441 5 ▶ if (sl == 3 && memEQ(s, "all", 3)) {
Branch: 2 branches
BranchTrueFalseTotal
if (sl == 3 && memEQ(s, "all", 3)) { 4 1 5
if (sl == 3 && memEQ(s, "all", 3)) { 4 0 4
442 4 ▶ if (!(appenv && strEQ(appenv, "development"))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(appenv && strEQ(appenv, "development"))) { 4 0 4
if (!(appenv && strEQ(appenv, "development"))) { 1 3 4
443 1 pp_free(aTHX_ p);
444 1 croak("Punk: proxy: trust => 'all' believes any "
445 "X-Forwarded-For from anyone, so with no proxy in "
446 "front it is a total bypass - it is allowed only "
447 "under PUNK_ENV=development. Name a hop count or "
448 "the proxy networks instead");
449 }
450 3 p->hops = -1;
451 } else {
452 1 SV *msg = sv_2mortal(newSVpvf(
453 "Punk: proxy: trust must be a hop count, an arrayref of "
454 "CIDRs, or 'all' - not '%.*s'", (int)sl, s));
455 1 pp_free(aTHX_ p);
456 1 croak("%s", SvPV_nolen(msg));
457 }
458 }
459 29 ▶ else if (SvROK(*t)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvROK(*t)) { 1 28 29
460 1 pp_free(aTHX_ p);
461 1 croak("Punk: proxy: trust must be a hop count, an arrayref of "
462 "CIDRs, or 'all'");
463 }
464 else {
465 28 IV n = SvIV(*t);
466 28 ▶ if (n < 1 || n > PP_MAX_HOPS) {
Branch: 2 branches
BranchTrueFalseTotal
if (n < 1 || n > PP_MAX_HOPS) { 26 2 28
if (n < 1 || n > PP_MAX_HOPS) { 0 26 26
467 2 pp_free(aTHX_ p);
468 2 croak("Punk: proxy: trust => %" IVdf " is not a usable hop "
469 "count (1..%d)", n, PP_MAX_HOPS);
470 }
471 26 p->hops = (int)n;
472 }
473 }
474  
475 46 p->for_key = pp_key_opt(aTHX_ cfg, "for_header",
476 "X-Forwarded-For", &p->for_len);
477 46 p->proto_key = pp_key_opt(aTHX_ cfg, "proto_header",
478 "X-Forwarded-Proto", &p->proto_len);
479 46 p->host_key = pp_key_opt(aTHX_ cfg, "host_header",
480 "X-Forwarded-Host", &p->host_len);
481 46 p->port_key = pp_key_opt(aTHX_ cfg, "port_header",
482 "X-Forwarded-Port", &p->port_len);
483 46 return p;
484 }
485  
486 /* ---- the per-request resolution ----------------------------------------- */
487  
488 54 static void pp_free(pTHX_ pp_policy *p) {
489 54 ▶ if (!p) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return; 0 54 54
490 54 Safefree(p->cidrs);
491 54 Safefree(p->for_key);
492 54 Safefree(p->proto_key);
493 54 Safefree(p->host_key);
494 54 Safefree(p->port_key);
495 54 Safefree(p);
496 }
497  
498 /* Called once per request from the top of punk_serve, and only when a
499 * policy was declared - an app without `proxy` pays one predictable
500 * branch and nothing else. */
501 40 static void pp_resolve(pTHX_ const pp_policy *p, HV *env) {
502 SV **e;
503 SV *peersv;
504 const char *peer;
505 40 STRLEN plen = 0;
506  
507 40 e = hv_fetchs(env, "REMOTE_ADDR", 0);
508 40 ▶ peersv = (e && *e && SvOK(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
peersv = (e && *e && SvOK(*e)) ? *e : NULL; 40 0 40
peersv = (e && *e && SvOK(*e)) ? *e : NULL; 40 0 40
peersv = (e && *e && SvOK(*e)) ? *e : NULL; 40 0 40
509 80 ▶ peer = peersv ? SvPV_const(peersv, plen) : "";
Branch: 1 branch
BranchTrueFalseTotal
peer = peersv ? SvPV_const(peersv, plen) : ""; 40 0 40
510  
511 /* the socket peer, kept before anything is rewritten, so an edge ban
512 * still has the address the connection actually came from */
513 40 ▶ (void)hv_stores(env, "punk.peer_addr",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(env, "punk.peer_addr", 40 0 40
514 peersv ? newSVsv(peersv) : newSVpvs(""));
515  
516 /* ONE trust decision, and every rewrite below is behind it. A hop count
517 * or `all` trusts whoever connected, by declaration. A network list
518 * trusts a peer inside it and nobody else: the forwarded scheme, host
519 * and port from any other peer are a stranger's claims about a request
520 * that never went near the proxy. */
521 40 ▶ if (p->hops == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->hops == 0) { 14 26 40
522 unsigned char a[16];
523 int isv4;
524 26 if (!(pp_parse_addr(peer, plen, a, &isv4, NULL, NULL)
525 12 && pp_trusted(p, a)))
526 9 return;
527 }
528  
529 31 ▶ e = p->for_key ? hv_fetch(env, p->for_key, (I32)p->for_len, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
e = p->for_key ? hv_fetch(env, p->for_key, (I32)p->for_len, 0) : NULL; 31 0 31
530 31 ▶ if (e && *e && SvOK(*e)) {
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) { 22 9 31
if (e && *e && SvOK(*e)) { 22 0 22
if (e && *e && SvOK(*e)) { 22 0 22
531 STRLEN xl, clen;
532 22 const char *x = SvPV_const(*e, xl);
533 22 const char *client = pp_xff_client(x, xl, p, peer, plen, &clen);
534 22 ▶ if (clen != plen || (clen && memNE(client, peer, clen))) {
Branch: 3 branches
BranchTrueFalseTotal
if (clen != plen || (clen && memNE(client, peer, clen))) { 2 20 22
if (clen != plen || (clen && memNE(client, peer, clen))) { 2 0 2
if (clen != plen || (clen && memNE(client, peer, clen))) { 2 0 2
535 /* copy before the store: `peer` points into the SV it frees */
536 22 SV *newsv = newSVpvn(client, clen);
537 22 (void)hv_stores(env, "REMOTE_ADDR", newsv);
538 /* the port described the proxy's socket and is now a lie */
539 22 (void)hv_delete(env, "REMOTE_PORT", 11, G_DISCARD);
540 }
541 }
542  
543 31 ▶ if (p->proto_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->proto_key) { 31 0 31
544 31 e = hv_fetch(env, p->proto_key, (I32)p->proto_len, 0);
545 31 ▶ if (e && *e && SvOK(*e)) {
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) { 9 22 31
if (e && *e && SvOK(*e)) { 9 0 9
if (e && *e && SvOK(*e)) { 9 0 9
546 STRLEN vl, l;
547 9 const char *v = SvPV_const(*e, vl);
548 9 const char *t = pp_first_token(v, vl, &l);
549 9 ▶ if (l == 5 && foldEQ(t, "https", 5)) {
Branch: 2 branches
BranchTrueFalseTotal
if (l == 5 && foldEQ(t, "https", 5)) { 8 1 9
if (l == 5 && foldEQ(t, "https", 5)) { 8 0 8
550 8 (void)hv_stores(env, "psgi.url_scheme", newSVpvs("https"));
551 8 (void)hv_stores(env, "HTTPS", newSVpvs("on"));
552 1 ▶ } else if (l == 4 && foldEQ(t, "http", 4)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (l == 4 && foldEQ(t, "http", 4)) { 0 1 1
} else if (l == 4 && foldEQ(t, "http", 4)) { 0 0 0
553 0 (void)hv_stores(env, "psgi.url_scheme", newSVpvs("http"));
554 0 (void)hv_delete(env, "HTTPS", 5, G_DISCARD);
555 }
556 }
557 }
558  
559 31 ▶ if (p->host_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->host_key) { 31 0 31
560 31 e = hv_fetch(env, p->host_key, (I32)p->host_len, 0);
561 31 ▶ if (e && *e && SvOK(*e)) {
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) { 5 26 31
if (e && *e && SvOK(*e)) { 5 0 5
if (e && *e && SvOK(*e)) { 5 0 5
562 STRLEN vl, l;
563 5 const char *v = SvPV_const(*e, vl);
564 5 const char *t = pp_first_token(v, vl, &l);
565 5 ▶ if (l) (void)hv_stores(env, "HTTP_HOST", newSVpvn(t, l));
Branch: 1 branch
BranchTrueFalseTotal
if (l) (void)hv_stores(env, "HTTP_HOST", newSVpvn(t, l)); 5 0 5
566 }
567 }
568  
569 31 ▶ if (p->port_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->port_key) { 31 0 31
570 31 e = hv_fetch(env, p->port_key, (I32)p->port_len, 0);
571 31 ▶ if (e && *e && SvOK(*e)) {
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) { 5 26 31
if (e && *e && SvOK(*e)) { 5 0 5
if (e && *e && SvOK(*e)) { 5 0 5
572 STRLEN vl, l;
573 5 const char *v = SvPV_const(*e, vl);
574 5 const char *t = pp_first_token(v, vl, &l);
575 STRLEN i;
576 5 ▶ int ok = l > 0 && l <= 5;
Branch: 2 branches
BranchTrueFalseTotal
int ok = l > 0 && l <= 5; 5 0 5
int ok = l > 0 && l <= 5; 5 0 5
577 20 ▶ for (i = 0; ok && i < l; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; ok && i < l; i++) 19 1 20
for (i = 0; ok && i < l; i++) 15 4 19
578 15 ▶ if (t[i] < '0' || t[i] > '9') ok = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (t[i] < '0' || t[i] > '9') ok = 0; 15 0 15
if (t[i] < '0' || t[i] > '9') ok = 0; 1 14 15
579 5 ▶ if (ok) (void)hv_stores(env, "SERVER_PORT", newSVpvn(t, l));
Branch: 1 branch
BranchTrueFalseTotal
if (ok) (void)hv_stores(env, "SERVER_PORT", newSVpvn(t, l)); 4 1 5
580 }
581 }
582 }
583  
584 #endif /* PUNK_PROXY_H */
« include/punk/punk_static.h ↑ Summary xs/static.xs »