Skip to content

Commit 1ce3397

Browse files
committed
CSRFのスキップ設定を調整
ワイルドカードに対応できるようにした
1 parent 10f6b97 commit 1ce3397

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

plugins/baser-core/src/BaserCorePlugin.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
372372
$authSetting = Configure::read('BcPrefixAuth.' . $prefix);
373373

374374
// 設定ファイルでスキップの定義がされている場合はスキップ
375-
if(in_array($request->getPath(), $this->getSkipCsrfUrl())) return true;
375+
if($this->isSkipCsrfUrl($request->getPath())) return true;
376376

377377
// 領域が REST API でない場合はスキップしない
378378
if (empty($authSetting['isRestApi'])) return false;
@@ -410,6 +410,47 @@ protected function getSkipCsrfUrl(): array
410410
return $skipUrl;
411411
}
412412

413+
/**
414+
* CSRF をスキップするURLかどうかをワイルドカードパターンで判定
415+
*
416+
* ワイルドカード(*)を使用したパターンマッチングに対応
417+
* 例: '/api/*' は '/api/users', '/api/posts' などにマッチ
418+
*
419+
* @param string $path チェック対象のパス
420+
* @return bool スキップする場合は true
421+
* @noTodo
422+
* @checked
423+
* @unitTest
424+
*/
425+
protected function isSkipCsrfUrl(string $path): bool
426+
{
427+
$skipUrls = $this->getSkipCsrfUrl();
428+
429+
foreach ($skipUrls as $skipUrl) {
430+
// 完全一致チェック(従来の動作を維持)
431+
if ($path === $skipUrl) {
432+
return true;
433+
}
434+
435+
// ワイルドカードパターンマッチング
436+
if (strpos($skipUrl, '*') !== false) {
437+
// ワイルドカードを正規表現に変換
438+
$pattern = str_replace(
439+
['*', '/'],
440+
['.*', '\/'],
441+
$skipUrl
442+
);
443+
$pattern = '/^' . $pattern . '$/';
444+
445+
if (preg_match($pattern, $path)) {
446+
return true;
447+
}
448+
}
449+
}
450+
451+
return false;
452+
}
453+
413454
/**
414455
* 認証サービスプロバイダ生成
415456
*

0 commit comments

Comments
 (0)