Skip to content

Commit

Permalink
Fallback to local IP on extraction (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anilm3 authored Mar 8, 2023
1 parent 1e816da commit 9a0f54a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/extension/ip_extraction.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ static zend_string *_try_extract(const zval *nonnull server,
static bool _parse_x_forwarded_for(
zend_string *nonnull value, ipaddr *nonnull out);
static bool _parse_plain(zend_string *nonnull zvalue, ipaddr *nonnull out);
static bool _parse_plain_raw(zend_string *nonnull zvalue, ipaddr *nonnull out);
static bool _parse_forwarded(zend_string *nonnull zvalue, ipaddr *nonnull out);
static bool _parse_via(zend_string *nonnull zvalue, ipaddr *nonnull out);

Expand Down Expand Up @@ -199,7 +200,7 @@ zend_string *nullable dd_ip_extraction_find(
relevant_ip_header ip_headers[MAX_HEADER_ID] = {};
int found_headers = _dd_request_headers(server, ip_headers);
if (found_headers == 0) {
return _try_extract(server, _remote_addr_key, &_parse_plain);
return _try_extract(server, _remote_addr_key, &_parse_plain_raw);
}

if (found_headers > 1) {
Expand Down Expand Up @@ -458,6 +459,11 @@ static bool _parse_via(zend_string *nonnull zvalue, ipaddr *nonnull out)
return succ;
}

static bool _parse_plain_raw(zend_string *nonnull zvalue, ipaddr *nonnull out)
{
return _parse_ip_address(ZSTR_VAL(zvalue), ZSTR_LEN(zvalue), out);
}

static bool _parse_plain(zend_string *nonnull zvalue, ipaddr *nonnull out)
{
return _parse_ip_address(ZSTR_VAL(zvalue), ZSTR_LEN(zvalue), out) &&
Expand Down
8 changes: 8 additions & 0 deletions tests/extension/extract_ip_addr.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ test('true_client_ip', '8.8.8.8');

echo "remote address fallback: 8.8.8.8\n";
var_dump(extract_ip_addr(['REMOTE_ADDR' => '8.8.8.8']));
echo "\n";

echo "remote address fallback: 192.168.1.1\n";
var_dump(extract_ip_addr(['REMOTE_ADDR' => '192.168.1.1']));

?>
--EXPECTF--
Expand Down Expand Up @@ -234,3 +238,7 @@ string(7) "8.8.8.8"

remote address fallback: 8.8.8.8
string(7) "8.8.8.8"

remote address fallback: 192.168.1.1
string(11) "192.168.1.1"

0 comments on commit 9a0f54a

Please sign in to comment.