feat: 接入 SMTP,支持后台配置、注册邮箱验证与密码找回
新增邮件能力,配置入口在管理后台「邮件设置」,无需改环境变量或重启。 一、邮件发送模块(lib/mailer.js) 基于 nodemailer 封装。SMTP 配置存在 settings 表(key 前缀 smtp_), 按需构建 transporter 并做短时缓存,配置变更时失效重建。 把底层 SMTP 异常翻译成可读中文提示(认证失败 / 无法连接 / 端口或 加密方式不匹配等),便于管理员自行排查。 二、管理后台「邮件设置」面板 - 服务器、端口、加密方式(STARTTLS 587 / SSL 465)、用户名、 发件人名称与邮箱 - 密码只写不读:任何接口都不回显明文,仅返回 password_set 布尔值。 留空表示保持原密码,避免管理员只改端口就把密码清掉 - 「发送测试邮件」按钮验证连通性,默认发给当前管理员 - 「注册邮箱验证」开关:仅在 SMTP 配置完整时才允许开启,避免开启后 所有人都注册不了 三、注册邮箱验证 - 新增 email_verifications 表存验证码。只存 sha256 摘要,不存明文; 同一邮箱+用途同时只允许一条有效记录(唯一索引约束) - 验证码 6 位数字,10 分钟有效,失败累计 5 次即作废; 校验用 timingSafeEqual 做定时安全比较 - 校验放在所有格式校验之后,避免用户因格式错误白白消耗一次验证码 - 验证码一次性,校验通过即标记 consumed - 注册框新增验证码输入与「获取验证码」按钮(含 60 秒倒计时) - 该字段仅在站点开启邮箱验证且 SMTP 可用时显示, 与既有的管理员引导令牌字段处理方式一致 四、密码找回邮件 新增 /api/auth/forgot-password 与 /api/auth/reset-password-by-email, 与既有恢复码机制并行。邮箱不存在时返回与成功一致的响应,避免被用于 枚举注册用户。重置成功后递增 token_version 使旧令牌失效。 五、安全与限流 - 新增 mailLimiter:发信接口 15 分钟 5 次,防止被当作垃圾邮件发射器 - SMTP 配置接口需管理员权限(未登录 401、普通用户 403),实测通过 六、顺带修复 - schema.sql 补上 email_verifications 表、users.email_verified 列与 SMTP 设置项。此前只改了 migrations/ 而漏了 schema.sql,被 test/schema-parity.test.js 抓出——这正是当初加该测试的目的。 - 管理后台原有一条 `#adminContent > div:first-of-type` 的 !important flex 规则,依赖「第一个 div 即论坛名称区域」这一脆弱假设,新增的 邮件设置面板正好撞上导致布局错乱。改为显式的 .forum-name-form 类名。 - 全局输入框规则补上 padding 兜底,否则管理后台的输入框会塌陷成 17px。 - multer 2.2.0 -> 2.4.0,修复多个 DoS 与上传限制绕过漏洞 (GHSA-wc9g-mqfw-jrwm 等)。test/app.test.js 中原本断言 multer 必须 匹配 /^2\.2\./,会阻碍安全升级,改为断言不低于已修复的 2.3.0。 - i18n 补充 39 条新文案(zh 与 en 各一份)。 已用本地 SMTP 捕获服务器完成端到端验证:测试邮件真实送达、验证码 正确与错误分支、验证码一次性、密码重置生效且旧密码失效、防用户枚举、 权限控制(401/403)、密码不回显、SMTP 未就绪时拒绝开启验证。 完整测试套件 25/25 通过。
This commit is contained in:
1 parent
75cc78d5f8
commit
627d284b02
13 files changed
+1285
-90
No files matched your search
+175
-4
@@ -431,11 +431,11 @@ function renderAdminSettings() {
|
||||
container.innerHTML = `
|
||||
<h3 style="margin-bottom:16px;text-align:center;">${getIcon('settings')} 论坛名称</h3>
|
||||
|
||||
<div style="max-width:400px;margin:0 auto;width:100%;">
|
||||
<label style="font-weight:600;font-size:14px;display:block;margin-bottom:6px;">论坛名称</label>
|
||||
<input type="text" id="settingsForumName" style="width:100%;padding:10px 14px;border:1.5px solid #e2e8f0;border-radius:6px;font-size:15px;margin-bottom:12px;font-family:inherit;background:var(--bg);color:var(--text);" />
|
||||
<div class="forum-name-form">
|
||||
<label for="settingsForumName" style="font-weight:600;font-size:14px;display:block;margin-bottom:6px;width:100%;">论坛名称</label>
|
||||
<input type="text" id="settingsForumName" placeholder="论坛名称" />
|
||||
<button id="settingsForumSave" class="btn-primary" style="padding:10px 24px;">保存</button>
|
||||
<span id="settingsResult" style="margin-left:12px;font-size:14px;"></span>
|
||||
<span id="settingsResult" style="font-size:14px;"></span>
|
||||
</div>
|
||||
`;
|
||||
|
||||
@@ -461,6 +461,176 @@ function renderAdminSettings() {
|
||||
});
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 📧 邮件设置(SMTP)
|
||||
// ============================================================
|
||||
|
||||
function renderAdminSmtp() {
|
||||
const container = document.getElementById('adminContent');
|
||||
container.innerHTML = `
|
||||
<h3 style="margin-bottom:6px;text-align:center;">${getIcon('message')} 邮件设置</h3>
|
||||
<p style="text-align:center;color:var(--text-secondary);font-size:13px;margin-bottom:20px;">
|
||||
配置后可用于注册邮箱验证与密码找回。密码只写不读,保存后不会回显。
|
||||
</p>
|
||||
|
||||
<div class="smtp-form">
|
||||
<div id="smtpStatus" style="margin-bottom:16px;padding:10px 14px;border-radius:var(--radius-sm);font-size:13px;"></div>
|
||||
|
||||
<label class="smtp-check">
|
||||
<input type="checkbox" id="smtpEnabled" />
|
||||
启用邮件发送
|
||||
</label>
|
||||
|
||||
<label for="smtpHost">SMTP 服务器</label>
|
||||
<input type="text" id="smtpHost" placeholder="smtp.example.com" autocomplete="off" />
|
||||
|
||||
<div class="smtp-row">
|
||||
<div>
|
||||
<label for="smtpPort">端口</label>
|
||||
<input type="number" id="smtpPort" placeholder="587" min="1" max="65535" />
|
||||
</div>
|
||||
<div>
|
||||
<label for="smtpSecure">加密方式</label>
|
||||
<select id="smtpSecure">
|
||||
<option value="false">STARTTLS(587)</option>
|
||||
<option value="true">SSL/TLS(465)</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<label for="smtpUser">用户名</label>
|
||||
<input type="text" id="smtpUser" placeholder="通常为完整邮箱地址" autocomplete="off" />
|
||||
|
||||
<label for="smtpPassword">密码 / 授权码</label>
|
||||
<input type="password" id="smtpPassword" placeholder="留空表示不修改已保存的密码" autocomplete="new-password" />
|
||||
<div class="smtp-hint">QQ、163 等邮箱需填写「授权码」而非登录密码。</div>
|
||||
|
||||
<label for="smtpFromName">发件人名称</label>
|
||||
<input type="text" id="smtpFromName" placeholder="例如:论坛名称" />
|
||||
|
||||
<label for="smtpFromEmail">发件人邮箱 <span style="color:#ef4444;">*</span></label>
|
||||
<input type="email" id="smtpFromEmail" placeholder="noreply@example.com" />
|
||||
|
||||
<div class="smtp-actions">
|
||||
<button id="smtpSaveBtn" class="btn-primary" style="padding:10px 22px;">保存配置</button>
|
||||
<button id="smtpTestBtn" class="btn-secondary" style="padding:10px 22px;">发送测试邮件</button>
|
||||
<button id="smtpClearPwBtn" class="btn-secondary" style="padding:10px 22px;">清除密码</button>
|
||||
</div>
|
||||
|
||||
<hr />
|
||||
|
||||
<h4 style="font-size:15px;margin-bottom:6px;">注册邮箱验证</h4>
|
||||
<p style="font-size:13px;color:var(--text-secondary);margin-bottom:12px;">
|
||||
开启后,新用户注册必须填写邮箱收到的验证码。需先完成上方配置并确保测试邮件可送达。
|
||||
</p>
|
||||
<label class="smtp-check" style="margin-bottom:0;">
|
||||
<input type="checkbox" id="emailVerifyRequired" />
|
||||
要求注册时验证邮箱
|
||||
</label>
|
||||
</div>
|
||||
`;
|
||||
|
||||
const $ = id => document.getElementById(id);
|
||||
|
||||
// 载入现有配置
|
||||
API.getSmtpConfig().then(cfg => {
|
||||
$('smtpEnabled').checked = cfg.enabled;
|
||||
$('smtpHost').value = cfg.host || '';
|
||||
$('smtpPort').value = cfg.port || 587;
|
||||
$('smtpSecure').value = String(cfg.secure);
|
||||
$('smtpUser').value = cfg.user || '';
|
||||
$('smtpFromName').value = cfg.from_name || '';
|
||||
$('smtpFromEmail').value = cfg.from_email || '';
|
||||
// 密码永不回显,只用占位文字提示是否已设置
|
||||
$('smtpPassword').placeholder = cfg.password_set
|
||||
? '已保存密码,留空则不修改'
|
||||
: '尚未设置密码';
|
||||
|
||||
const status = $('smtpStatus');
|
||||
if (cfg.configured) {
|
||||
status.innerHTML = `${getIcon('success')} 配置完整,可以发信`;
|
||||
status.style.background = 'rgba(34,197,94,0.12)';
|
||||
status.style.color = '#16a34a';
|
||||
} else {
|
||||
status.innerHTML = `${getIcon('warning')} 尚未配置完成,邮件功能不可用`;
|
||||
status.style.background = 'rgba(245,158,11,0.12)';
|
||||
status.style.color = '#d97706';
|
||||
}
|
||||
}).catch(err => {
|
||||
showToast('加载配置失败:' + err.message, 'error');
|
||||
});
|
||||
|
||||
// 读取邮箱验证开关(该值在公开设置接口中)
|
||||
API.getSettings().then(s => {
|
||||
$('emailVerifyRequired').checked = s.email_verify_required === true;
|
||||
}).catch(() => {});
|
||||
|
||||
$('smtpSaveBtn').addEventListener('click', async () => {
|
||||
const payload = {
|
||||
enabled: $('smtpEnabled').checked,
|
||||
host: $('smtpHost').value.trim(),
|
||||
port: Number($('smtpPort').value || 587),
|
||||
secure: $('smtpSecure').value === 'true',
|
||||
user: $('smtpUser').value.trim(),
|
||||
from_name: $('smtpFromName').value.trim(),
|
||||
from_email: $('smtpFromEmail').value.trim(),
|
||||
};
|
||||
// 只有填了才提交,避免把已保存的密码覆盖成空
|
||||
const pw = $('smtpPassword').value;
|
||||
if (pw) payload.password = pw;
|
||||
|
||||
try {
|
||||
await API.saveSmtpConfig(payload);
|
||||
$('smtpPassword').value = '';
|
||||
showToast('配置已保存', 'success');
|
||||
renderAdminSmtp();
|
||||
} catch (err) {
|
||||
showToast(err.message, 'error');
|
||||
}
|
||||
});
|
||||
|
||||
$('smtpTestBtn').addEventListener('click', async function() {
|
||||
const btn = this;
|
||||
btn.disabled = true;
|
||||
btn.textContent = '发送中…';
|
||||
try {
|
||||
const r = await API.sendTestMail();
|
||||
showToast('测试邮件已发送至 ' + r.to, 'success');
|
||||
} catch (err) {
|
||||
showToast(err.message, 'error');
|
||||
} finally {
|
||||
btn.disabled = false;
|
||||
btn.textContent = '发送测试邮件';
|
||||
}
|
||||
});
|
||||
|
||||
$('smtpClearPwBtn').addEventListener('click', async () => {
|
||||
const ok = await showConfirm('确定要清除已保存的 SMTP 密码吗?', {
|
||||
title: '清除密码', confirmText: '清除', danger: true,
|
||||
});
|
||||
if (!ok) return;
|
||||
try {
|
||||
await API.clearSmtpPassword();
|
||||
showToast('密码已清除', 'success');
|
||||
renderAdminSmtp();
|
||||
} catch (err) {
|
||||
showToast(err.message, 'error');
|
||||
}
|
||||
});
|
||||
|
||||
$('emailVerifyRequired').addEventListener('change', async function() {
|
||||
const desired = this.checked;
|
||||
try {
|
||||
await API.setEmailVerifyRequired(desired);
|
||||
showToast(desired ? '已开启注册邮箱验证' : '已关闭注册邮箱验证', 'success');
|
||||
} catch (err) {
|
||||
// 失败时把开关状态回滚,避免界面与服务端不一致
|
||||
this.checked = !desired;
|
||||
showToast(err.message, 'error');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 🎨 自定义 CSS(独立标签页)- 已改用 Toast 提示
|
||||
// ============================================================
|
||||
@@ -834,6 +1004,7 @@ document.querySelector('.admin-nav')?.addEventListener('click', function(e) {
|
||||
links: renderAdminLinks,
|
||||
'custom-css': renderAdminCustomCSS,
|
||||
settings: renderAdminSettings,
|
||||
smtp: renderAdminSmtp,
|
||||
custom: renderAdminCustomPages
|
||||
};
|
||||
if (tabMap[tab]) tabMap[tab]();
|
||||
|
||||
Reference in new issue
Block a user