File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 );
You can’t perform that action at this time.
0 commit comments