Skip to content

Commit 1c137d1

Browse files
authored
Merge pull request #409 from kenjis/fix-filters
fix: add logic to skip filters if not in HTTP
2 parents 74cce7a + 392da4e commit 1c137d1

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

src/Filters/AuthRates.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace CodeIgniter\Shield\Filters;
66

77
use CodeIgniter\Filters\FilterInterface;
8+
use CodeIgniter\HTTP\IncomingRequest;
89
use CodeIgniter\HTTP\RedirectResponse;
910
use CodeIgniter\HTTP\RequestInterface;
1011
use CodeIgniter\HTTP\Response;
@@ -31,6 +32,10 @@ class AuthRates implements FilterInterface
3132
*/
3233
public function before(RequestInterface $request, $arguments = null)
3334
{
35+
if (! $request instanceof IncomingRequest) {
36+
return;
37+
}
38+
3439
$throttler = service('throttler');
3540

3641
// Restrict an IP address to no more than 10 requests

src/Filters/ChainAuth.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace CodeIgniter\Shield\Filters;
66

77
use CodeIgniter\Filters\FilterInterface;
8+
use CodeIgniter\HTTP\IncomingRequest;
89
use CodeIgniter\HTTP\RedirectResponse;
910
use CodeIgniter\HTTP\RequestInterface;
1011
use CodeIgniter\HTTP\Response;
@@ -35,6 +36,10 @@ class ChainAuth implements FilterInterface
3536
*/
3637
public function before(RequestInterface $request, $arguments = null)
3738
{
39+
if (! $request instanceof IncomingRequest) {
40+
return;
41+
}
42+
3843
helper('settings');
3944

4045
$chain = config('Auth')->authenticationChain;

src/Filters/SessionAuth.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace CodeIgniter\Shield\Filters;
66

77
use CodeIgniter\Filters\FilterInterface;
8+
use CodeIgniter\HTTP\IncomingRequest;
89
use CodeIgniter\HTTP\RedirectResponse;
910
use CodeIgniter\HTTP\RequestInterface;
1011
use CodeIgniter\HTTP\Response;
@@ -34,6 +35,10 @@ class SessionAuth implements FilterInterface
3435
*/
3536
public function before(RequestInterface $request, $arguments = null)
3637
{
38+
if (! $request instanceof IncomingRequest) {
39+
return;
40+
}
41+
3742
helper('setting');
3843

3944
/** @var Session $authenticator */

src/Filters/TokenAuth.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace CodeIgniter\Shield\Filters;
66

77
use CodeIgniter\Filters\FilterInterface;
8+
use CodeIgniter\HTTP\IncomingRequest;
89
use CodeIgniter\HTTP\RedirectResponse;
910
use CodeIgniter\HTTP\RequestInterface;
1011
use CodeIgniter\HTTP\Response;
@@ -34,6 +35,10 @@ class TokenAuth implements FilterInterface
3435
*/
3536
public function before(RequestInterface $request, $arguments = null)
3637
{
38+
if (! $request instanceof IncomingRequest) {
39+
return;
40+
}
41+
3742
helper('setting');
3843

3944
/** @var AccessTokens $authenticator */

tests/Unit/FilterInCliTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Unit;
6+
7+
use CodeIgniter\Filters\FilterInterface;
8+
use CodeIgniter\HTTP\CLIRequest;
9+
use CodeIgniter\Shield\Filters\AuthRates;
10+
use CodeIgniter\Shield\Filters\ChainAuth;
11+
use CodeIgniter\Shield\Filters\SessionAuth;
12+
use CodeIgniter\Shield\Filters\TokenAuth;
13+
use Generator;
14+
use Tests\Support\TestCase;
15+
16+
/**
17+
* @internal
18+
*/
19+
final class FilterInCliTest extends TestCase
20+
{
21+
/**
22+
* @dataProvider filterProvider
23+
*/
24+
public function testWhenInCliDoNothing(FilterInterface $filter): void
25+
{
26+
$clirequest = $this->createMock(CLIRequest::class);
27+
28+
$clirequest->expects($this->never())
29+
->method('getHeaderLine');
30+
31+
$filter->before($clirequest);
32+
}
33+
34+
public function filterProvider(): Generator
35+
{
36+
yield from [
37+
[new AuthRates()],
38+
[new ChainAuth()],
39+
[new SessionAuth()],
40+
[new TokenAuth()],
41+
];
42+
}
43+
}

0 commit comments

Comments
 (0)