Files
Furrium Network 627d284b02 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 通过。
2026-09-21 21:45:41 +08:00

90 lines
3.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const test = require('node:test');
const assert = require('node:assert/strict');
const request = require('supertest');
const bcrypt = require('bcryptjs');
const multerPackage = require('multer/package.json');
const projectPackage = require('../package.json');
const { app, pool } = require('../server');
test.after(async () => {
await pool.end();
});
test('serves the frontend with hardened response headers', async () => {
const response = await request(app).get('/').expect(200);
assert.match(response.headers['content-type'], /^text\/html/);
assert.equal(response.headers['x-content-type-options'], 'nosniff');
assert.equal(response.headers['x-frame-options'], 'SAMEORIGIN');
assert.equal(response.headers['x-powered-by'], undefined);
});
test('does not serve backend source and deployment files', async () => {
for (const path of ['/server.js', '/schema.sql', '/package.json', '/docker-compose.yml']) {
await request(app).get(path).expect(404);
}
});
test('unknown API route returns JSON 404 instead of hanging', async () => {
const response = await request(app)
.get('/api/definitely-unknown')
.timeout({ response: 1000, deadline: 2000 })
.expect(404);
assert.deepEqual(response.body, { error: '接口不存在' });
assert.ok(response.headers.ratelimit);
});
test('request body limit returns a structured 413 response', async () => {
const response = await request(app)
.post('/api/auth/login')
.send({ email: 'user@example.test', password: 'x'.repeat(110000) })
.expect(413);
assert.equal(response.body.error, '请求内容过大');
});
test('registration and login validate required fields before database access', async () => {
let response = await request(app)
.post('/api/auth/register')
.send({ email: '', password: '', username: '' })
.expect(400);
assert.equal(response.body.error, '请填写完整信息');
response = await request(app)
.post('/api/auth/login')
.send({ email: '', password: '' })
.expect(400);
assert.equal(response.body.error, '请填写邮箱和密码');
});
test('protected routes reject unauthenticated requests', async () => {
const response = await request(app)
.post('/api/posts')
.send({ title: 'test', content: 'test' })
.expect(401);
assert.equal(response.body.error, '请先登录');
});
test('bcryptjs hashes passwords without native install scripts', async () => {
const hash = await bcrypt.hash('correct horse battery staple', 4);
assert.equal(await bcrypt.compare('correct horse battery staple', hash), true);
assert.equal(await bcrypt.compare('wrong password', hash), false);
});
test('security-sensitive dependencies are on patched release lines', () => {
// multer 2.2.0 及更早版本存在多个 DoS 与上传限制绕过漏洞
// (GHSA-wc9g-mqfw-jrwm 等),2.3.0 起修复。
// 这里断言「不低于已修复的版本」,而不是锁死某个小版本,
// 否则后续的安全升级反而会让测试失败。
const [major, minor] = multerPackage.version.split('.').map(Number);
const multerPatched = major > 2 || (major === 2 && minor >= 3);
assert.ok(
multerPatched,
`multer ${multerPackage.version} 低于已修复漏洞的 2.3.0`
);
assert.equal(projectPackage.dependencies['express-rate-limit'], '8.6.2');
assert.ok(
projectPackage.dependencies.nodemailer,
'nodemailer 是邮件功能的必需依赖'
);
});