セキュリティ:転送のセキュリティホールの修正

守矢諏訪子 <suwako@076.moe>、2026-11-10 15:26
コミット 3a8fe888b22495e1af91d6372042d6de7b117c79 ・親 023e3e1 ・ファイルを見る

2個のファイル、+7 -1

変更 route.php
@@ -121,7 +121,7 @@
121121 // .xhtmlで終わるURLのリダイレクト処理
122122 if (preg_match('/(.+)\.xhtml$/', $uri, $matches)) {
123123 // 末尾の.xhtmlを取り除く
124- $newUri = $matches[1];
124+ $newUri = safe_local_path($matches[1]);
125125 header('HTTP/1.1 301 Moved Permanently');
126126 header('Location: ' . $newUri);
127127 exit();
変更 util.php
@@ -379,6 +379,12 @@
379379 return $token;
380380 }
381381
382+function safe_local_path(mixed $path, string $default = '/'): string {
383+ if (!is_string($path) || $path === '' || $path[0] !== '/') return $default;
384+ if (preg_match('/[\x00-\x1f\x7f]/', $path)) return $default;
385+ return '/'.ltrim($path, '/\\');
386+}
387+
382388 function verify_csrf_token(string $token): bool {
383389 return null !== getcookie('csrf_token') && hash_equals(getcookie('csrf_token'), $token);
384390 }