Skip to content

Commit fcab403

Browse files
committed
Check IP address before making WS request
1 parent 172f885 commit fcab403

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
CHANGELOG
22
=========
33

4+
3.0.0
5+
-------------------
6+
7+
* `GeoIp2\WebService\Client` methods now throw an `InvalidArgumentException`
8+
if an invalid IP address is passed to them. Previously, they would make
9+
a request to the web service and throw a
10+
`GeoIp2\Exception\InvalidRequestException`.
11+
412
2.13.0 (2022-08-05)
513
-------------------
614

src/WebService/Client.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ private function userAgent(): string
135135
* @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent
136136
* class to the above exceptions. It will be thrown directly
137137
* if a 200 status code is returned but the body is invalid.
138+
* @throws \InvalidArgumentException if something other than a single IP address or "me" is
139+
* passed to the method
138140
*/
139141
public function city(string $ipAddress = 'me'): City
140142
{
@@ -165,6 +167,8 @@ public function city(string $ipAddress = 'me'): City
165167
* @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent class to the above exceptions. It
166168
* will be thrown directly if a 200 status code is returned but
167169
* the body is invalid.
170+
* @throws \InvalidArgumentException if something other than a single IP address or "me" is
171+
* passed to the method
168172
*/
169173
public function country(string $ipAddress = 'me'): Country
170174
{
@@ -195,6 +199,8 @@ public function country(string $ipAddress = 'me'): Country
195199
* @throws \GeoIp2\Exception\GeoIp2Exception This serves as the parent
196200
* class to the above exceptions. It will be thrown directly
197201
* if a 200 status code is returned but the body is invalid.
202+
* @throws \InvalidArgumentException if something other than a single IP address or "me" is
203+
* passed to the method
198204
*/
199205
public function insights(string $ipAddress = 'me'): Insights
200206
{
@@ -204,6 +210,11 @@ public function insights(string $ipAddress = 'me'): Insights
204210

205211
private function responseFor(string $endpoint, string $class, string $ipAddress): Country
206212
{
213+
if ($ipAddress !== 'me' && !filter_var($ipAddress, \FILTER_VALIDATE_IP)) {
214+
throw new \InvalidArgumentException(
215+
"The value \"$ipAddress\" is not a valid IP address."
216+
);
217+
}
207218
$path = implode('/', [self::$basePath, $endpoint, $ipAddress]);
208219

209220
try {

tests/GeoIp2/Test/WebService/ClientTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private function getResponse(string $service, string $ipAddress): array
6767
),
6868
'1.2.3.5' => $this->response('country', 200),
6969
'2.2.3.5' => $this->response('country', 200, 'bad body'),
70-
'1.2.3.6' => $this->response(
70+
'1.2.3' => $this->response(
7171
'error',
7272
400,
7373
[
@@ -316,11 +316,10 @@ public function testBadBodyException(): void
316316

317317
public function testInvalidIPException(): void
318318
{
319-
$this->expectException(\GeoIp2\Exception\InvalidRequestException::class);
320-
$this->expectExceptionCode(400);
321-
$this->expectExceptionMessage('The value "1.2.3" is not a valid ip address');
319+
$this->expectException(\InvalidArgumentException::class);
320+
$this->expectExceptionMessage('The value "1.2.3" is not a valid IP address');
322321

323-
$this->makeRequest('Country', '1.2.3.6');
322+
$this->makeRequest('Country', '1.2.3', callsToRequest: 0);
324323
}
325324

326325
public function testNoErrorBodyIPException(): void

0 commit comments

Comments
 (0)