fix: 管理员初始化令牌输入框改为按需显示
注册模态框里固定显示「管理员初始化令牌(仅首次部署,可选)」输入框,
每个来注册的普通用户都会看到它,既令人困惑,也等于对外宣告存在一个
提权入口。该字段由 924f9ff 引入,与后端的引导令牌机制配套。
后端鉴权本身没有问题(令牌随机、check hasAdmin、advisory lock 防并发),
所以本次只调整展示层面,不改动注册与提权逻辑:
- GET /api/settings 新增 bootstrap_required 布尔值,仅在「部署者配置了
ADMIN_BOOTSTRAP_TOKEN」且「系统尚无管理员」时为 true。只返回布尔值,
不泄露令牌本身或其长度。
- index.html 中该输入框默认 display:none。
- js/auth.js 打开注册框时查询设置,按 bootstrap_required 决定显隐;
字段隐藏时按「未提供」处理,避免普通注册传入空串触发 403。
这样三种情形各得其所:
- 普通部署(未配置令牌):任何人都看不到该字段
- 首次部署(已配置令牌、尚无管理员):显示,部署者据此创建管理员
- 初始化完成:自动隐藏
新增 test/bootstrap-ui.test.js 锁住上述约定,覆盖「标记中默认隐藏」
「接口只暴露布尔值」「隐藏时不提交空令牌」「表单对字段缺失做保护」
四项。已实测注入故障可被捕获。
实测五种场景(未配置令牌 / 已配置待初始化 / 初始化后 / 泄露检查 /
普通注册)行为均符合预期。
This commit is contained in:
1 parent
f1c9aca4c7
commit
c6bf968178
4 files changed
+125
-3
No files matched your search
+1
-1
@@ -362,7 +362,7 @@
|
||||
<input type="text" id="regUsername" placeholder="用户名" />
|
||||
<input type="email" id="regEmail" placeholder="邮箱" />
|
||||
<input type="password" id="regPassword" placeholder="密码(至少6位)" />
|
||||
<input type="password" id="regBootstrapToken" placeholder="管理员初始化令牌(仅首次部署,可选)" autocomplete="off" />
|
||||
<input type="password" id="regBootstrapToken" placeholder="管理员初始化令牌" autocomplete="off" style="display:none;" />
|
||||
<div class="captcha-row">
|
||||
<span id="regCaptchaQuestion">3 + 5 = ?</span>
|
||||
<input type="text" id="regCaptchaInput" placeholder="答案" style="width:80px;" />
|
||||
|
||||
+24
-2
@@ -34,14 +34,36 @@ document.getElementById('loginSubmit').addEventListener('click', async () => {
|
||||
// 注册事件绑定
|
||||
document.getElementById('registerBtn').addEventListener('click', () => {
|
||||
refreshCaptcha('reg');
|
||||
syncBootstrapTokenField();
|
||||
document.getElementById('registerModal').classList.add('active');
|
||||
});
|
||||
|
||||
// 管理员初始化令牌输入框仅在首次部署且配置了引导令牌时出现。
|
||||
// 其余情况(普通部署、已初始化完成)普通用户完全看不到它。
|
||||
async function syncBootstrapTokenField() {
|
||||
const field = document.getElementById('regBootstrapToken');
|
||||
if (!field) return;
|
||||
try {
|
||||
const settings = await API.getSettings();
|
||||
const required = settings.bootstrap_required === true;
|
||||
field.style.display = required ? '' : 'none';
|
||||
if (!required) field.value = '';
|
||||
} catch (err) {
|
||||
// 取不到设置时按「不需要」处理,避免把内部机制暴露给普通用户。
|
||||
field.style.display = 'none';
|
||||
field.value = '';
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById('registerSubmit').addEventListener('click', async () => {
|
||||
const username = document.getElementById('regUsername').value.trim();
|
||||
const email = document.getElementById('regEmail').value.trim();
|
||||
const password = document.getElementById('regPassword').value;
|
||||
const bootstrapToken = document.getElementById('regBootstrapToken').value.trim();
|
||||
const bootstrapField = document.getElementById('regBootstrapToken');
|
||||
// 字段被隐藏时视为未提供,避免普通注册误传空串触发后端的令牌校验分支。
|
||||
const bootstrapToken = bootstrapField && bootstrapField.style.display !== 'none'
|
||||
? bootstrapField.value.trim()
|
||||
: '';
|
||||
const captchaInput = document.getElementById('regCaptchaInput').value.trim();
|
||||
const captchaAnswer = parseInt(document.getElementById('regCaptchaInput').dataset.answer);
|
||||
if (!username || !email || !password) { alert('请填写完整信息'); return; }
|
||||
@@ -58,7 +80,7 @@ document.getElementById('registerSubmit').addEventListener('click', async () =>
|
||||
document.getElementById('regUsername').value = '';
|
||||
document.getElementById('regEmail').value = '';
|
||||
document.getElementById('regPassword').value = '';
|
||||
document.getElementById('regBootstrapToken').value = '';
|
||||
if (bootstrapField) bootstrapField.value = '';
|
||||
document.getElementById('regCaptchaInput').value = '';
|
||||
|
||||
try {
|
||||
|
||||
@@ -310,6 +310,18 @@ app.get('/api/settings', async (req, res) => {
|
||||
const r = await pool.query('SELECT key, value FROM settings');
|
||||
const settings = {};
|
||||
r.rows.forEach(row => { settings[row.key] = row.value; });
|
||||
|
||||
// 仅在「部署者配置了引导令牌」且「尚未存在管理员」时,前端才需要
|
||||
// 展示管理员初始化入口。两者任一不满足,注册框就完全是普通注册。
|
||||
// 这里只返回布尔值,不泄露令牌本身或其长度。
|
||||
const bootstrapConfigured = Boolean(process.env.ADMIN_BOOTSTRAP_TOKEN);
|
||||
let adminExists = true;
|
||||
if (bootstrapConfigured) {
|
||||
const adminResult = await pool.query("SELECT EXISTS(SELECT 1 FROM users WHERE role = 'admin') AS exists");
|
||||
adminExists = adminResult.rows[0].exists;
|
||||
}
|
||||
settings.bootstrap_required = bootstrapConfigured && !adminExists;
|
||||
|
||||
res.json(settings);
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: '服务器错误' });
|
||||
|
||||
Vendored
+88
@@ -0,0 +1,88 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
|
||||
// 管理员初始化令牌(ADMIN_BOOTSTRAP_TOKEN)属于部署期的一次性机制,
|
||||
// 不应暴露给普通注册用户。历史上它的输入框固定显示在注册模态框里,
|
||||
// 每个来注册的人都会看到一个「管理员初始化令牌」字段,既困惑又等于
|
||||
// 对外宣告存在提权入口。
|
||||
//
|
||||
// 现在的约定:输入框默认隐藏,只有 /api/settings 返回
|
||||
// bootstrap_required === true 时才显示,该标志仅在「部署者配置了引导
|
||||
// 令牌」且「系统尚无管理员」时为真。
|
||||
//
|
||||
// 以下断言不依赖数据库,直接在文本层面锁住这些约定。
|
||||
|
||||
const root = path.join(__dirname, '..');
|
||||
const indexHTML = fs.readFileSync(path.join(root, 'index.html'), 'utf8');
|
||||
const authJS = fs.readFileSync(path.join(root, 'js', 'auth.js'), 'utf8');
|
||||
const serverJS = fs.readFileSync(path.join(root, 'server.js'), 'utf8');
|
||||
|
||||
test('bootstrap token field is hidden by default in the markup', () => {
|
||||
const field = indexHTML.match(/<input[^>]*id="regBootstrapToken"[^>]*>/i);
|
||||
assert.ok(field, '注册模态框中找不到 regBootstrapToken 输入框');
|
||||
assert.match(
|
||||
field[0],
|
||||
/style\s*=\s*"[^"]*display\s*:\s*none/i,
|
||||
'regBootstrapToken 必须默认隐藏,仅在首次部署且已配置引导令牌时由脚本显示'
|
||||
);
|
||||
});
|
||||
|
||||
test('/api/settings only exposes a boolean, never the token itself', () => {
|
||||
const route = serverJS.match(/app\.get\('\/api\/settings'[\s\S]*?\n\}\);/);
|
||||
assert.ok(route, '找不到 GET /api/settings 路由');
|
||||
const body = route[0];
|
||||
|
||||
assert.match(
|
||||
body,
|
||||
/bootstrap_required/,
|
||||
'接口需要返回 bootstrap_required 供前端判断是否显示初始化入口'
|
||||
);
|
||||
assert.match(
|
||||
body,
|
||||
/process\.env\.ADMIN_BOOTSTRAP_TOKEN/,
|
||||
'bootstrap_required 必须依据是否配置了引导令牌来判断'
|
||||
);
|
||||
assert.match(
|
||||
body,
|
||||
/role\s*=\s*'admin'/,
|
||||
'bootstrap_required 还必须检查是否已存在管理员'
|
||||
);
|
||||
|
||||
// 不得把令牌明文或其长度写进响应。
|
||||
assert.doesNotMatch(
|
||||
body,
|
||||
/settings\.\w*token\w*\s*=\s*process\.env\.ADMIN_BOOTSTRAP_TOKEN/i,
|
||||
'不得把引导令牌本身放进响应'
|
||||
);
|
||||
assert.doesNotMatch(
|
||||
body,
|
||||
/ADMIN_BOOTSTRAP_TOKEN\s*\)\s*\.\s*length|ADMIN_BOOTSTRAP_TOKEN\s*\.\s*length/,
|
||||
'不得返回引导令牌的长度,避免泄露部署配置细节'
|
||||
);
|
||||
});
|
||||
|
||||
test('registration ignores the hidden field instead of submitting an empty token', () => {
|
||||
// 字段隐藏时必须按「未提供」处理,否则会传空串进入后端的令牌校验
|
||||
// 分支,让普通注册收到 403。
|
||||
assert.match(
|
||||
authJS,
|
||||
/style\.display\s*!==\s*'none'/,
|
||||
'读取 regBootstrapToken 前必须判断其是否可见'
|
||||
);
|
||||
assert.match(
|
||||
authJS,
|
||||
/bootstrap_required/,
|
||||
'auth.js 需要依据 bootstrap_required 决定是否显示该字段'
|
||||
);
|
||||
});
|
||||
|
||||
test('registration form still works without the bootstrap field', () => {
|
||||
// 普通注册路径不得依赖该字段存在。
|
||||
assert.match(
|
||||
authJS,
|
||||
/if\s*\(\s*bootstrapField\s*\)/,
|
||||
'清理表单时必须对可能不存在的 bootstrap 字段做空值保护'
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user