Skip to content

Commit

Permalink
Merge pull request #2272 from acelaya-forks/feature/geolocate-localho…
Browse files Browse the repository at this point in the history
…st-fix

Make sure IpGeolocationMiddleware skips localhost
  • Loading branch information
acelaya authored Nov 19, 2024
2 parents 052c9e7 + f57f159 commit c323bfc
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Common\Util\IpAddress;
use Shlinkio\Shlink\Core\Config\Options\TrackingOptions;
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException;
use Shlinkio\Shlink\IpGeolocation\GeoLite2\DbUpdaterInterface;
Expand Down Expand Up @@ -46,7 +47,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
private function geolocateIpAddress(string|null $ipAddress): Location
{
try {
return $ipAddress === null ? Location::empty() : $this->ipLocationResolver->resolveIpLocation($ipAddress);
return $ipAddress === null || $ipAddress === IpAddress::LOCALHOST
? Location::empty()
: $this->ipLocationResolver->resolveIpLocation($ipAddress);
} catch (WrongIpException $e) {
$this->logger->warning('Tried to locate IP address, but it seems to be wrong. {e}', ['e' => $e]);
return Location::empty();
Expand Down
5 changes: 0 additions & 5 deletions module/Core/src/Visit/Entity/Visit.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,6 @@ public function getVisitLocation(): VisitLocation|null
return $this->visitLocation;
}

public function isLocatable(): bool
{
return $this->hasRemoteAddr() && $this->remoteAddr !== IpAddress::LOCALHOST;
}

public function locate(VisitLocation $visitLocation): self
{
$this->visitLocation = $visitLocation;
Expand Down
2 changes: 1 addition & 1 deletion module/Core/src/Visit/Geolocation/VisitLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private function locateVisits(iterable $results, VisitGeolocationHelperInterface
}

// If the IP address is non-locatable, locate it as empty to prevent next processes to pick it again
$location = Location::emptyInstance();
$location = Location::empty();
}

$this->locateVisit($visit, VisitLocation::fromGeolocation($location), $helper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Psr\Log\LoggerInterface;
use RuntimeException;
use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory;
use Shlinkio\Shlink\Common\Util\IpAddress;
use Shlinkio\Shlink\Core\Config\Options\TrackingOptions;
use Shlinkio\Shlink\Core\Geolocation\Middleware\IpGeolocationMiddleware;
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException;
Expand Down Expand Up @@ -67,13 +68,18 @@ public function warningIsLoggedIfGeoLiteDbDoesNotExist(): void
}

#[Test]
public function emptyLocationIsReturnedIfIpAddressDoesNotExistInRequest(): void
#[TestWith([null])]
#[TestWith([IpAddress::LOCALHOST])]
public function emptyLocationIsReturnedIfIpAddressIsNotLocatable(string|null $ipAddress): void
{
$this->dbUpdater->expects($this->once())->method('databaseFileExists')->willReturn(true);
$this->ipLocationResolver->expects($this->never())->method('resolveIpLocation');
$this->logger->expects($this->never())->method('warning');

$request = ServerRequestFactory::fromGlobals();
$request = ServerRequestFactory::fromGlobals()->withAttribute(
IpAddressMiddlewareFactory::REQUEST_ATTR,
$ipAddress,
);
$this->handler->expects($this->once())->method('handle')->with($this->callback(
function (ServerRequestInterface $req): bool {
$location = $req->getAttribute(Location::class);
Expand Down

0 comments on commit c323bfc

Please sign in to comment.