@@ -17,13 +17,13 @@ class BaseCsrfVerifier implements IMiddleware
1717 * For example: /admin/*
1818 * @var array|null
1919 */
20- protected ? array $ except = null ;
20+ protected array $ except = [] ;
2121
2222 /**
2323 * Urls to include. Can be used to include urls from a certain path.
2424 * @var array|null
2525 */
26- protected ? array $ include = null ;
26+ protected array $ include = [] ;
2727
2828 /**
2929 * @var ITokenProvider
@@ -38,18 +38,35 @@ public function __construct()
3838 $ this ->tokenProvider = new CookieTokenProvider ();
3939 }
4040
41+ protected function isIncluded (Request $ request ): bool
42+ {
43+ if (count ($ this ->include ) > 0 ) {
44+ foreach ($ this ->include as $ includeUrl ) {
45+ $ includeUrl = rtrim ($ includeUrl , '/ ' );
46+ if ($ includeUrl [strlen ($ includeUrl ) - 1 ] === '* ' ) {
47+ $ includeUrl = rtrim ($ includeUrl , '* ' );
48+ return $ request ->getUrl ()->contains ($ includeUrl );
49+ }
50+
51+ return ($ includeUrl === rtrim ($ request ->getUrl ()->getRelativeUrl (false ), '/ ' ));
52+ }
53+ }
54+
55+ return false ;
56+ }
57+
4158 /**
4259 * Check if the url matches the urls in the except property
4360 * @param Request $request
4461 * @return bool
4562 */
4663 protected function skip (Request $ request ): bool
4764 {
48- if ($ this -> except === null || count ($ this ->except ) === 0 ) {
65+ if (count ($ this ->except ) === 0 ) {
4966 return false ;
5067 }
5168
52- foreach ($ this ->except as $ url ) {
69+ foreach ($ this ->except as $ url ) {
5370 $ url = rtrim ($ url , '/ ' );
5471 if ($ url [strlen ($ url ) - 1 ] === '* ' ) {
5572 $ url = rtrim ($ url , '* ' );
@@ -60,20 +77,9 @@ protected function skip(Request $request): bool
6077
6178 if ($ skip === true ) {
6279
63- if (is_array ($ this ->include ) === true && count ($ this ->include ) > 0 ) {
64- foreach ($ this ->include as $ includeUrl ) {
65- $ includeUrl = rtrim ($ includeUrl , '/ ' );
66- if ($ includeUrl [strlen ($ includeUrl ) - 1 ] === '* ' ) {
67- $ includeUrl = rtrim ($ includeUrl , '* ' );
68- $ skip = !$ request ->getUrl ()->contains ($ includeUrl );
69- break ;
70- }
71-
72- $ skip = !($ includeUrl === rtrim ($ request ->getUrl ()->getRelativeUrl (false ), '/ ' ));
73- }
74- }
80+ $ skip = !$ this ->isIncluded ($ request );
7581
76- if ($ skip === false ) {
82+ if ($ skip === false ) {
7783 continue ;
7884 }
7985
@@ -92,12 +98,11 @@ protected function skip(Request $request): bool
9298 */
9399 public function handle (Request $ request ): void
94100 {
95- if ($ this ->skip ($ request ) === false && $ request ->isPostBack () === true ) {
101+ if ($ this ->skip ($ request ) === false && ( $ request ->isPostBack () === true || $ this -> isIncluded ( $ request ) === true ) ) {
96102
97103 $ token = $ request ->getInputHandler ()->value (
98104 static ::POST_KEY ,
99105 $ request ->getHeader (static ::HEADER_KEY ),
100- Request::$ requestTypesPost
101106 );
102107
103108 if ($ this ->tokenProvider ->validate ((string )$ token ) === false ) {
0 commit comments