Keep strict Origin validation. Send same-origin referrer policy so browser form POST does not produce an opaque null Origin.
58 lines
4.7 KiB
PHP
58 lines
4.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
// Independent staging entrance. Does not bootstrap or alter XB authentication.
|
|
$config = json_decode(file_get_contents('/var/www/wwwroot/fuc-xb-test/shared/gate/config.json'), true, 16, JSON_THROW_ON_ERROR);
|
|
header('Cache-Control: private, no-store');
|
|
header('Pragma: no-cache');
|
|
header('X-Robots-Tag: noindex, nofollow, noarchive');
|
|
header("Content-Security-Policy: default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'");
|
|
header('X-Content-Type-Options: nosniff');
|
|
// Keep same-origin form provenance; no-referrer makes browser POST Origin null.
|
|
header('Referrer-Policy: same-origin');
|
|
$method = $_SERVER['REQUEST_METHOD'] ?? '';
|
|
$cookieOptions = ['expires' => time() + 3600, 'path' => '/', 'secure' => true, 'httponly' => true, 'samesite' => 'Strict'];
|
|
$csrfOptions = ['expires' => time() + 600, 'path' => '/stage-login', 'secure' => true, 'httponly' => true, 'samesite' => 'Strict'];
|
|
$allowedHosts = ['fuc-xb-test.gemcook.net', 'fuc-xb-test.tomtojapan.online'];
|
|
$host = strtolower(explode(':', $_SERVER['HTTP_HOST'] ?? '')[0]);
|
|
if (!in_array($host, $allowedHosts, true)) { http_response_code(400); exit; }
|
|
if (!in_array($method, ['GET', 'POST'], true)) { header('Allow: GET, POST'); http_response_code(405); exit; }
|
|
if (isset($_COOKIE['fuc_stage_access']) && is_string($_COOKIE['fuc_stage_access']) && hash_equals($config['cookie_token'], $_COOKIE['fuc_stage_access'])) {
|
|
header('Location: /', true, 303); exit;
|
|
}
|
|
$message = '';
|
|
if ($method === 'POST') {
|
|
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
|
|
$csrf = $_POST['csrf'] ?? '';
|
|
$csrfCookie = $_COOKIE['fuc_stage_csrf'] ?? '';
|
|
$parts = is_string($csrf) ? explode('.', $csrf) : [];
|
|
$validCsrf = count($parts) === 3 && ctype_digit($parts[0]) && (int)$parts[0] <= time() && (int)$parts[0] >= time() - 600
|
|
&& preg_match('/^[a-f0-9]{32}$/D', $parts[1]) && is_string($csrfCookie) && hash_equals($csrfCookie, $csrf)
|
|
&& hash_equals(hash_hmac('sha256', $parts[0].'.'.$parts[1], $config['cookie_token']), $parts[2]);
|
|
$user = $_POST['username'] ?? null;
|
|
$password = $_POST['password'] ?? null;
|
|
$validOrigin = in_array($origin, array_map(fn($h) => 'https://'.$h, $allowedHosts), true);
|
|
if (!$validCsrf || !$validOrigin || !is_string($user) || !is_string($password) || strlen($user) > 256 || strlen($password) > 1024) {
|
|
http_response_code(403); $message = '验证已失效,请重新输入。';
|
|
} else {
|
|
// Reuse the existing SHA-512 crypt hash. No plaintext password is stored.
|
|
$computed = crypt($password, $config['password_hash']);
|
|
if (hash_equals($config['username'], $user) && hash_equals($config['password_hash'], $computed)) {
|
|
setcookie('fuc_stage_access', $config['cookie_token'], $cookieOptions);
|
|
$csrfOptions['expires'] = time() - 3600; setcookie('fuc_stage_csrf', '', $csrfOptions);
|
|
header('Location: /', true, 303); exit;
|
|
}
|
|
http_response_code(403); $message = '账号或密码错误。';
|
|
}
|
|
}
|
|
$csrf = time().'.'.bin2hex(random_bytes(16));
|
|
$csrf .= '.'.hash_hmac('sha256', $csrf, $config['cookie_token']);
|
|
setcookie('fuc_stage_csrf', $csrf, $csrfOptions);
|
|
header('Content-Type: text/html; charset=utf-8');
|
|
$escape = fn(string $s): string => htmlspecialchars($s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
|
?>
|
|
<!doctype html><html lang="zh-CN"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>FUC-XB 测试入口</title>
|
|
<style>body{font:16px system-ui,sans-serif;background:#f6f5fa;color:#262139;margin:0;display:grid;min-height:100vh;place-items:center}main{box-sizing:border-box;background:white;padding:32px;border-radius:16px;width:min(420px,calc(100vw - 32px));box-shadow:0 8px 40px #26213910}h1{font-size:24px;margin:0 0 12px}p{color:#666;line-height:1.6}label{display:block;margin-top:20px}input,button{box-sizing:border-box;width:100%;font:inherit;padding:12px;border:1px solid #d8d4e1;border-radius:8px}input{margin-top:8px}button{margin-top:24px;background:#7367f0;color:white;border:0;cursor:pointer}.error{color:#b42318}a{color:#6254cf}</style>
|
|
<main><h1>FUC-XB 测试入口</h1><p>请输入测试站的额外访问账号和密码。</p>
|
|
<?php if ($message !== ''): ?><p class="error" role="alert"><?= $escape($message) ?></p><?php endif ?>
|
|
<form method="post" action="/stage-login"><input type="hidden" name="csrf" value="<?= $escape($csrf) ?>"><label>访问账号<input name="username" autocomplete="username" required maxlength="256"></label><label>访问密码<input name="password" type="password" autocomplete="current-password" required maxlength="1024"></label><button type="submit">进入测试站</button></form></main></html>
|