mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-29 00:32:29 +00:00
Make port optional in X-Forwarded-For
This parser was being overly restrictive which presented integration issues with haproxy which doesn't make it easy to pass the port info
This commit is contained in:
parent
87d7010495
commit
e4ef38403b
3 changed files with 90 additions and 55 deletions
|
@ -26,6 +26,8 @@
|
||||||
*
|
*
|
||||||
* X-Forwarded-For: 203.0.113.42:31337
|
* X-Forwarded-For: 203.0.113.42:31337
|
||||||
*
|
*
|
||||||
|
* The port is optional and will be set to zero if absent.
|
||||||
|
*
|
||||||
* @param s is input data
|
* @param s is input data
|
||||||
* @param n if -1 implies strlen
|
* @param n if -1 implies strlen
|
||||||
* @param ip receives ip on success if not NULL
|
* @param ip receives ip on success if not NULL
|
||||||
|
@ -34,26 +36,32 @@
|
||||||
* @see RFC7239's poorly designed Forwarded header
|
* @see RFC7239's poorly designed Forwarded header
|
||||||
*/
|
*/
|
||||||
int ParseForwarded(const char *s, size_t n, uint32_t *ip, uint16_t *port) {
|
int ParseForwarded(const char *s, size_t n, uint32_t *ip, uint16_t *port) {
|
||||||
|
int c, t;
|
||||||
size_t i;
|
size_t i;
|
||||||
uint32_t x;
|
uint32_t x;
|
||||||
int c, j, t;
|
|
||||||
if (n == -1) n = s ? strlen(s) : 0;
|
if (n == -1) n = s ? strlen(s) : 0;
|
||||||
for (t = x = j = i = 0; i < n;) {
|
if (n) {
|
||||||
c = s[i++] & 255;
|
t = x = i = 0;
|
||||||
if (isdigit(c)) {
|
do {
|
||||||
t *= 10;
|
c = s[i++] & 255;
|
||||||
t += c - '0';
|
if (isdigit(c)) {
|
||||||
if (t > 255) return -1;
|
t *= 10;
|
||||||
} else if (c == '.') {
|
t += c - '0';
|
||||||
x <<= 8;
|
if (t > 255) return -1;
|
||||||
x |= t;
|
} else if (c == '.') {
|
||||||
t = 0;
|
x <<= 8;
|
||||||
++j;
|
x |= t;
|
||||||
} else if (c == ':') {
|
t = 0;
|
||||||
x <<= 8;
|
} else if (c == ':') {
|
||||||
x |= t;
|
break;
|
||||||
t = 0;
|
} else {
|
||||||
if (j != 3) return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
} while (i < n);
|
||||||
|
x <<= 8;
|
||||||
|
x |= t;
|
||||||
|
t = 0;
|
||||||
|
if (c == ':') {
|
||||||
while (i < n) {
|
while (i < n) {
|
||||||
c = s[i++] & 255;
|
c = s[i++] & 255;
|
||||||
if (isdigit(c)) {
|
if (isdigit(c)) {
|
||||||
|
@ -64,13 +72,11 @@ int ParseForwarded(const char *s, size_t n, uint32_t *ip, uint16_t *port) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!x || !t) return -1;
|
|
||||||
if (ip) *ip = x;
|
|
||||||
if (port) *port = t;
|
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
if (ip) *ip = x;
|
||||||
|
if (port) *port = t;
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -89,43 +89,12 @@ TEST(ParseIp, test) {
|
||||||
EXPECT_EQ(-1, ParseIp("hello..example", -1));
|
EXPECT_EQ(-1, ParseIp("hello..example", -1));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(ParseForwarded, test) {
|
|
||||||
uint32_t ip = 7;
|
|
||||||
uint16_t port = 7;
|
|
||||||
EXPECT_EQ(-1, ParseForwarded("", -1, &ip, &port));
|
|
||||||
EXPECT_EQ(-1, ParseForwarded("0.0.0.0", -1, &ip, &port));
|
|
||||||
EXPECT_EQ(-1, ParseForwarded("8.8.8.8", -1, &ip, &port));
|
|
||||||
EXPECT_EQ(-1, ParseForwarded("[::1]:123", -1, &ip, &port));
|
|
||||||
EXPECT_EQ(7, ip);
|
|
||||||
EXPECT_EQ(7, port);
|
|
||||||
EXPECT_EQ(0, ParseForwarded("0.0.0.1:123", -1, &ip, &port));
|
|
||||||
EXPECT_EQ(0x00000001, ip);
|
|
||||||
EXPECT_EQ(123, port);
|
|
||||||
EXPECT_EQ(0, ParseForwarded("1.2.3.4:123", -1, &ip, &port));
|
|
||||||
EXPECT_EQ(0x01020304, ip);
|
|
||||||
EXPECT_EQ(123, port);
|
|
||||||
EXPECT_EQ(0, ParseForwarded("128.2.3.4:123", -1, &ip, &port));
|
|
||||||
EXPECT_EQ(0x80020304, ip);
|
|
||||||
EXPECT_EQ(123, port);
|
|
||||||
EXPECT_EQ(0, ParseForwarded("255.255.255.255:123", -1, &ip, &port));
|
|
||||||
EXPECT_EQ(0xFFFFFFFF, ip);
|
|
||||||
EXPECT_EQ(123, port);
|
|
||||||
EXPECT_EQ(0, ParseForwarded("203.0.113.0:123", -1, &ip, &port));
|
|
||||||
EXPECT_EQ(0xcb007100, ip);
|
|
||||||
EXPECT_EQ(123, port);
|
|
||||||
EXPECT_EQ(0, ParseForwarded("203.0.113.42:31337", -1, &ip, &port));
|
|
||||||
EXPECT_EQ(-1, ParseForwarded("...:123", -1, &ip, &port));
|
|
||||||
EXPECT_EQ(-1, ParseForwarded("203.0.113.0:123123123", -1, &ip, &port));
|
|
||||||
}
|
|
||||||
|
|
||||||
BENCH(IsAcceptableHost, bench) {
|
BENCH(IsAcceptableHost, bench) {
|
||||||
uint32_t ip;
|
uint32_t ip;
|
||||||
uint16_t port;
|
uint16_t port;
|
||||||
EZBENCH2("IsAcceptableHost 127.0.0.1", donothing,
|
EZBENCH2("IsAcceptableHost 127.0.0.1", donothing,
|
||||||
IsAcceptableHost("127.0.0.1", 9));
|
IsAcceptableHost("127.0.0.1", 9));
|
||||||
EZBENCH2("IsAcceptablePort 80", donothing, IsAcceptablePort("80", 2));
|
EZBENCH2("IsAcceptablePort 80", donothing, IsAcceptablePort("80", 2));
|
||||||
EZBENCH2("ParseForwarded 80", donothing,
|
|
||||||
ParseForwarded("203.0.113.42:31337", 20, &ip, &port));
|
|
||||||
EZBENCH2("IsAcceptableHost foo.example", donothing,
|
EZBENCH2("IsAcceptableHost foo.example", donothing,
|
||||||
IsAcceptableHost("foo.example:31337", 17));
|
IsAcceptableHost("foo.example:31337", 17));
|
||||||
}
|
}
|
||||||
|
|
60
test/net/http/parseforwarded_test.c
Normal file
60
test/net/http/parseforwarded_test.c
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||||
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||||
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||||
|
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
||||||
|
│ │
|
||||||
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||||
|
│ any purpose with or without fee is hereby granted, provided that the │
|
||||||
|
│ above copyright notice and this permission notice appear in all copies. │
|
||||||
|
│ │
|
||||||
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||||
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||||
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||||
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||||
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||||
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||||
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/testlib/ezbench.h"
|
||||||
|
#include "libc/testlib/testlib.h"
|
||||||
|
#include "net/http/http.h"
|
||||||
|
|
||||||
|
TEST(ParseForwarded, test) {
|
||||||
|
uint32_t ip = 7;
|
||||||
|
uint16_t port = 7;
|
||||||
|
EXPECT_EQ(-1, ParseForwarded("", -1, &ip, &port));
|
||||||
|
EXPECT_EQ(-1, ParseForwarded("[::1]:123", -1, &ip, &port));
|
||||||
|
EXPECT_EQ(-1, ParseForwarded("203.0.113.0:123123123", -1, &ip, &port));
|
||||||
|
EXPECT_EQ(7, ip);
|
||||||
|
EXPECT_EQ(7, port);
|
||||||
|
EXPECT_EQ(0, ParseForwarded("0.0.0.0", -1, &ip, &port));
|
||||||
|
EXPECT_EQ(0x00000000, ip);
|
||||||
|
EXPECT_EQ(0, port);
|
||||||
|
EXPECT_EQ(0, ParseForwarded("8.8.8.8", -1, &ip, &port));
|
||||||
|
EXPECT_EQ(0x08080808, ip);
|
||||||
|
EXPECT_EQ(0, port);
|
||||||
|
EXPECT_EQ(0, ParseForwarded("0.0.0.1:123", -1, &ip, &port));
|
||||||
|
EXPECT_EQ(0x00000001, ip);
|
||||||
|
EXPECT_EQ(123, port);
|
||||||
|
EXPECT_EQ(0, ParseForwarded("1.2.3.4:123", -1, &ip, &port));
|
||||||
|
EXPECT_EQ(0x01020304, ip);
|
||||||
|
EXPECT_EQ(123, port);
|
||||||
|
EXPECT_EQ(0, ParseForwarded("128.2.3.4:123", -1, &ip, &port));
|
||||||
|
EXPECT_EQ(0x80020304, ip);
|
||||||
|
EXPECT_EQ(123, port);
|
||||||
|
EXPECT_EQ(0, ParseForwarded("255.255.255.255:123", -1, &ip, &port));
|
||||||
|
EXPECT_EQ(0xFFFFFFFF, ip);
|
||||||
|
EXPECT_EQ(123, port);
|
||||||
|
EXPECT_EQ(0, ParseForwarded("203.0.113.0:123", -1, &ip, &port));
|
||||||
|
EXPECT_EQ(0xcb007100, ip);
|
||||||
|
EXPECT_EQ(123, port);
|
||||||
|
EXPECT_EQ(0, ParseForwarded("203.0.113.42:31337", -1, &ip, &port));
|
||||||
|
}
|
||||||
|
|
||||||
|
BENCH(ParseForwarded, bench) {
|
||||||
|
uint32_t ip;
|
||||||
|
uint16_t port;
|
||||||
|
EZBENCH2("ParseForwarded 80", donothing,
|
||||||
|
ParseForwarded("203.0.113.42:31337", 20, &ip, &port));
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue