Skip to content

Commit 0d40cd0

Browse files
committed
chore: enforce suggested improvements
1 parent 71caddb commit 0d40cd0

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

src/Request.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Request
5858
*
5959
* @var array
6060
*/
61-
protected array $trustedIpHeaders = ['x-forwarded-for'];
61+
protected array $trustedIpHeaders = [];
6262

6363
/**
6464
* Get Param
@@ -164,6 +164,24 @@ public function setServer(string $key, string $value): static
164164
return $this;
165165
}
166166

167+
/**
168+
* Set trusted ip headers
169+
*
170+
* WARNING: Only set these headers if your application is behind a trusted proxy.
171+
* Trusting these headers when accepting direct client connections is a security risk.
172+
*
173+
* @param array $headers List of header names to trust (e.g., ['x-forwarded-for', 'x-real-ip'])
174+
* @return static
175+
*/
176+
public function setTrustedIpHeaders(array $headers): static
177+
{
178+
$this->trustedIpHeaders = array_filter(
179+
array_map('trim',
180+
array_map('strtolower', $headers))
181+
);
182+
return $this;
183+
}
184+
167185
/**
168186
* Get IP
169187
*
@@ -175,7 +193,7 @@ public function setServer(string $key, string $value): static
175193
*/
176194
public function getIP(): string
177195
{
178-
$remoteAddr = $this->getServer('remote_addr') ?? '0.0.0.0';
196+
$remoteAddr = $this->getServer('REMOTE_ADDR') ?? '0.0.0.0';
179197

180198
foreach ($this->trustedIpHeaders as $header) {
181199
$headerValue = $this->getHeader($header);

0 commit comments

Comments
 (0)