セキュリティ:転送のセキュリティホールの修正
守矢諏訪子 <suwako@076.moe>、2026-11-10 15:26
コミット 3a8fe888b22495e1af91d6372042d6de7b117c79
・親 023e3e1
・ファイルを見る
2個のファイル、+7 -1
変更
route.php
| @@ -121,7 +121,7 @@ | ||
| 121 | 121 | // .xhtmlで終わるURLのリダイレクト処理 |
| 122 | 122 | if (preg_match('/(.+)\.xhtml$/', $uri, $matches)) { |
| 123 | 123 | // 末尾の.xhtmlを取り除く |
| 124 | - $newUri = $matches[1]; | |
| 124 | + $newUri = safe_local_path($matches[1]); | |
| 125 | 125 | header('HTTP/1.1 301 Moved Permanently'); |
| 126 | 126 | header('Location: ' . $newUri); |
| 127 | 127 | exit(); |
変更
util.php
| @@ -379,6 +379,12 @@ | ||
| 379 | 379 | return $token; |
| 380 | 380 | } |
| 381 | 381 | |
| 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 | + | |
| 382 | 388 | function verify_csrf_token(string $token): bool { |
| 383 | 389 | return null !== getcookie('csrf_token') && hash_equals(getcookie('csrf_token'), $token); |
| 384 | 390 | } |