mirror of
https://github.com/shlinkio/shlink.git
synced 2026-02-28 04:03:12 +08:00
Use IpAddressFactory from akrabat/ip-address-middleware
This commit is contained in:
@@ -15,7 +15,6 @@ use Laminas\Filter\Word\CamelCaseToSeparator;
|
||||
use Laminas\Filter\Word\CamelCaseToUnderscore;
|
||||
use Laminas\InputFilter\InputFilter;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory;
|
||||
use Shlinkio\Shlink\Common\Util\DateRange;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlMode;
|
||||
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
||||
@@ -38,6 +37,8 @@ use function strtolower;
|
||||
use function trim;
|
||||
use function ucfirst;
|
||||
|
||||
use const Shlinkio\Shlink\IP_ADDRESS_REQUEST_ATTRIBUTE;
|
||||
|
||||
function generateRandomShortCode(int $length, ShortUrlMode $mode = ShortUrlMode::STRICT): string
|
||||
{
|
||||
static $nanoIdClient;
|
||||
@@ -288,7 +289,7 @@ function splitByComma(string|null $value): array
|
||||
|
||||
function ipAddressFromRequest(ServerRequestInterface $request): string|null
|
||||
{
|
||||
return $request->getAttribute(IpAddressMiddlewareFactory::REQUEST_ATTR);
|
||||
return $request->getAttribute(IP_ADDRESS_REQUEST_ATTRIBUTE);
|
||||
}
|
||||
|
||||
function geolocationFromRequest(ServerRequestInterface $request): Location|null
|
||||
|
||||
@@ -89,8 +89,8 @@ class RedirectTest extends ApiTestCase
|
||||
'https://blog.alejandrocelaya.com/2017/12/09/acmailer-7-0-the-most-important-release-in-a-long-time/',
|
||||
];
|
||||
|
||||
$clientDetection = require __DIR__ . '/../../../../config/autoload/client-detection.global.php';
|
||||
foreach ($clientDetection['ip_address_resolution']['headers_to_inspect'] as $header) {
|
||||
$ipAddressConfig = require __DIR__ . '/../../../../config/autoload/ip-address.global.php';
|
||||
foreach ($ipAddressConfig['rka']['ip_address']['headers_to_inspect'] as $header) {
|
||||
yield sprintf('rule: IP address in "%s" header', $header) => [
|
||||
[
|
||||
RequestOptions::HEADERS => [$header => '1.2.3.4'],
|
||||
|
||||
@@ -14,7 +14,6 @@ use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
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;
|
||||
@@ -24,6 +23,8 @@ use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
||||
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
|
||||
use Throwable;
|
||||
|
||||
use const Shlinkio\Shlink\IP_ADDRESS_REQUEST_ATTRIBUTE;
|
||||
|
||||
class IpGeolocationMiddlewareTest extends TestCase
|
||||
{
|
||||
private MockObject & IpLocationResolverInterface $ipLocationResolver;
|
||||
@@ -76,10 +77,7 @@ class IpGeolocationMiddlewareTest extends TestCase
|
||||
$this->ipLocationResolver->expects($this->never())->method('resolveIpLocation');
|
||||
$this->logger->expects($this->never())->method('warning');
|
||||
|
||||
$request = ServerRequestFactory::fromGlobals()->withAttribute(
|
||||
IpAddressMiddlewareFactory::REQUEST_ATTR,
|
||||
$ipAddress,
|
||||
);
|
||||
$request = ServerRequestFactory::fromGlobals()->withAttribute(IP_ADDRESS_REQUEST_ATTRIBUTE, $ipAddress);
|
||||
$this->handler->expects($this->once())->method('handle')->with($this->callback(
|
||||
function (ServerRequestInterface $req): bool {
|
||||
$location = $req->getAttribute(Location::class);
|
||||
@@ -104,10 +102,7 @@ class IpGeolocationMiddlewareTest extends TestCase
|
||||
);
|
||||
$this->logger->expects($this->never())->method('warning');
|
||||
|
||||
$request = ServerRequestFactory::fromGlobals()->withAttribute(
|
||||
IpAddressMiddlewareFactory::REQUEST_ATTR,
|
||||
'1.2.3.4',
|
||||
);
|
||||
$request = ServerRequestFactory::fromGlobals()->withAttribute(IP_ADDRESS_REQUEST_ATTRIBUTE, '1.2.3.4');
|
||||
$this->handler->expects($this->once())->method('handle')->with($this->callback(
|
||||
function (ServerRequestInterface $req): bool {
|
||||
$location = $req->getAttribute(Location::class);
|
||||
@@ -147,10 +142,7 @@ class IpGeolocationMiddlewareTest extends TestCase
|
||||
->willThrowException($exception);
|
||||
$this->logger->expects($this->once())->method($loggerMethod)->with($expectedLoggedMessage, ['e' => $exception]);
|
||||
|
||||
$request = ServerRequestFactory::fromGlobals()->withAttribute(
|
||||
IpAddressMiddlewareFactory::REQUEST_ATTR,
|
||||
'1.2.3.4',
|
||||
);
|
||||
$request = ServerRequestFactory::fromGlobals()->withAttribute(IP_ADDRESS_REQUEST_ATTRIBUTE, '1.2.3.4');
|
||||
$this->handler->expects($this->once())->method('handle')->with($this->callback(
|
||||
function (ServerRequestInterface $req): bool {
|
||||
$location = $req->getAttribute(Location::class);
|
||||
|
||||
@@ -7,11 +7,11 @@ use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\Attributes\TestWith;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory;
|
||||
use Shlinkio\Shlink\Core\Model\DeviceType;
|
||||
use Shlinkio\Shlink\Core\RedirectRule\Entity\RedirectCondition;
|
||||
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
||||
|
||||
use const Shlinkio\Shlink\IP_ADDRESS_REQUEST_ATTRIBUTE;
|
||||
use const ShlinkioTest\Shlink\ANDROID_USER_AGENT;
|
||||
use const ShlinkioTest\Shlink\DESKTOP_USER_AGENT;
|
||||
use const ShlinkioTest\Shlink\IOS_USER_AGENT;
|
||||
@@ -88,7 +88,7 @@ class RedirectConditionTest extends TestCase
|
||||
{
|
||||
$request = ServerRequestFactory::fromGlobals();
|
||||
if ($remoteIp !== null) {
|
||||
$request = $request->withAttribute(IpAddressMiddlewareFactory::REQUEST_ATTR, $remoteIp);
|
||||
$request = $request->withAttribute(IP_ADDRESS_REQUEST_ATTRIBUTE, $remoteIp);
|
||||
}
|
||||
|
||||
$result = RedirectCondition::forIpAddress($ipToMatch)->matchesRequest($request);
|
||||
|
||||
@@ -9,7 +9,6 @@ use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory;
|
||||
use Shlinkio\Shlink\Core\Model\DeviceType;
|
||||
use Shlinkio\Shlink\Core\RedirectRule\Entity\RedirectCondition;
|
||||
use Shlinkio\Shlink\Core\RedirectRule\Entity\ShortUrlRedirectRule;
|
||||
@@ -18,6 +17,7 @@ use Shlinkio\Shlink\Core\RedirectRule\ShortUrlRedirectRuleServiceInterface;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
|
||||
|
||||
use const Shlinkio\Shlink\IP_ADDRESS_REQUEST_ATTRIBUTE;
|
||||
use const ShlinkioTest\Shlink\ANDROID_USER_AGENT;
|
||||
use const ShlinkioTest\Shlink\DESKTOP_USER_AGENT;
|
||||
use const ShlinkioTest\Shlink\IOS_USER_AGENT;
|
||||
@@ -90,22 +90,22 @@ class ShortUrlRedirectionResolverTest extends TestCase
|
||||
'https://example.com/from-rule',
|
||||
];
|
||||
yield 'matching static IP address' => [
|
||||
$request()->withAttribute(IpAddressMiddlewareFactory::REQUEST_ATTR, '1.2.3.4'),
|
||||
$request()->withAttribute(IP_ADDRESS_REQUEST_ATTRIBUTE, '1.2.3.4'),
|
||||
RedirectCondition::forIpAddress('1.2.3.4'),
|
||||
'https://example.com/from-rule',
|
||||
];
|
||||
yield 'matching CIDR block' => [
|
||||
$request()->withAttribute(IpAddressMiddlewareFactory::REQUEST_ATTR, '192.168.1.35'),
|
||||
$request()->withAttribute(IP_ADDRESS_REQUEST_ATTRIBUTE, '192.168.1.35'),
|
||||
RedirectCondition::forIpAddress('192.168.1.0/24'),
|
||||
'https://example.com/from-rule',
|
||||
];
|
||||
yield 'matching wildcard IP address' => [
|
||||
$request()->withAttribute(IpAddressMiddlewareFactory::REQUEST_ATTR, '1.2.5.5'),
|
||||
$request()->withAttribute(IP_ADDRESS_REQUEST_ATTRIBUTE, '1.2.5.5'),
|
||||
RedirectCondition::forIpAddress('1.2.*.*'),
|
||||
'https://example.com/from-rule',
|
||||
];
|
||||
yield 'non-matching IP address' => [
|
||||
$request()->withAttribute(IpAddressMiddlewareFactory::REQUEST_ATTR, '4.3.2.1'),
|
||||
$request()->withAttribute(IP_ADDRESS_REQUEST_ATTRIBUTE, '4.3.2.1'),
|
||||
RedirectCondition::forIpAddress('1.2.3.4'),
|
||||
'https://example.com/foo/bar',
|
||||
];
|
||||
|
||||
@@ -12,7 +12,6 @@ use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory;
|
||||
use Shlinkio\Shlink\Core\Config\Options\TrackingOptions;
|
||||
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||
@@ -20,6 +19,8 @@ use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
||||
use Shlinkio\Shlink\Core\Visit\RequestTracker;
|
||||
use Shlinkio\Shlink\Core\Visit\VisitsTrackerInterface;
|
||||
|
||||
use const Shlinkio\Shlink\IP_ADDRESS_REQUEST_ATTRIBUTE;
|
||||
|
||||
class RequestTrackerTest extends TestCase
|
||||
{
|
||||
private const LONG_URL = 'https://domain.com/foo/bar?some=thing';
|
||||
@@ -67,15 +68,15 @@ class RequestTrackerTest extends TestCase
|
||||
ServerRequestFactory::fromGlobals()->withQueryParams(['foobar' => null]),
|
||||
];
|
||||
yield 'exact remote address' => [ServerRequestFactory::fromGlobals()->withAttribute(
|
||||
IpAddressMiddlewareFactory::REQUEST_ATTR,
|
||||
IP_ADDRESS_REQUEST_ATTRIBUTE,
|
||||
'80.90.100.110',
|
||||
)];
|
||||
yield 'matching wildcard remote address' => [ServerRequestFactory::fromGlobals()->withAttribute(
|
||||
IpAddressMiddlewareFactory::REQUEST_ATTR,
|
||||
IP_ADDRESS_REQUEST_ATTRIBUTE,
|
||||
'1.2.3.4',
|
||||
)];
|
||||
yield 'matching CIDR block remote address' => [ServerRequestFactory::fromGlobals()->withAttribute(
|
||||
IpAddressMiddlewareFactory::REQUEST_ATTR,
|
||||
IP_ADDRESS_REQUEST_ATTRIBUTE,
|
||||
'192.168.10.100',
|
||||
)];
|
||||
}
|
||||
@@ -102,7 +103,7 @@ class RequestTrackerTest extends TestCase
|
||||
);
|
||||
|
||||
$this->requestTracker->trackIfApplicable($shortUrl, ServerRequestFactory::fromGlobals()->withAttribute(
|
||||
IpAddressMiddlewareFactory::REQUEST_ATTR,
|
||||
IP_ADDRESS_REQUEST_ATTRIBUTE,
|
||||
'invalid',
|
||||
));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user