SMTP settings on the Settings page, with a test (1.2.0)
SMTP server, port, user, password and from address are now Hub settings in the database instead of SMTP_* in .env. The password is encrypted with HUB_ENCRYPTION_KEY and never sent to the browser; a blank field keeps it. POST /api/admin/settings/smtp-test logs in with the form's values (saved or not) and sends a test email, returning the server's reason on failure; it is rate limited and audited. On first start an existing SMTP_* setup is copied into Settings once. The audit log no longer records the SMTP password. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ls1vMaqhrehXcHJe32jLL9
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
/** The Hub dashboard's API (admin login required). */
|
||||
|
||||
const express = require('express');
|
||||
const rateLimit = require('express-rate-limit');
|
||||
const prisma = require('../config/db');
|
||||
const { protect } = require('../middleware/auth');
|
||||
const { licenceKey } = require('../utils/crypto');
|
||||
@@ -8,10 +9,13 @@ const { audit } = require('../utils/audit');
|
||||
const { getSettings, setSettings } = require('../utils/settings');
|
||||
const { problems, compareVersions } = require('../utils/alerts');
|
||||
const licenceToken = require('../utils/licenceToken');
|
||||
const { smtpConfigured, testSmtp } = require('../utils/mail');
|
||||
|
||||
const router = express.Router();
|
||||
router.use(protect);
|
||||
|
||||
const smtpTestLimiter = rateLimit({ windowMs: 15 * 60 * 1000, max: 10, standardHeaders: true, legacyHeaders: false, message: { message: 'Too many test emails — wait a few minutes.' } });
|
||||
|
||||
const wrap = fn => (req, res) => fn(req, res).catch(e => {
|
||||
const status = e.status || 500;
|
||||
if (status >= 500) console.error('[admin]', e);
|
||||
@@ -223,15 +227,47 @@ router.get('/audit', wrap(async (req, res) => {
|
||||
// ─── Settings ─────────────────────────────────────────────────────────────────
|
||||
|
||||
router.get('/settings', wrap(async (req, res) => {
|
||||
res.json({ settings: await getSettings(), publicKey: licenceToken.keyPair().publicKeyPem, smtp: !!process.env.SMTP_HOST });
|
||||
res.json({ settings: await getSettings(), publicKey: licenceToken.keyPair().publicKeyPem, smtp: await smtpConfigured() });
|
||||
}));
|
||||
|
||||
const SMTP_FIELDS = ['smtp_host', 'smtp_port', 'smtp_user', 'smtp_pass', 'smtp_pass_clear', 'smtp_from'];
|
||||
|
||||
function checkSmtp(values) {
|
||||
if (values.smtp_port != null && values.smtp_port !== '' && !/^\d{1,5}$/.test(String(values.smtp_port))) throw bad('SMTP port is a number, usually 587 or 465.');
|
||||
if (values.smtp_host && /[\s/]/.test(String(values.smtp_host).trim())) throw bad('SMTP server is a host name like mail.example.com, without https:// or a path.');
|
||||
}
|
||||
|
||||
// What the audit log records: never the password, only that it changed.
|
||||
function auditValues(values) {
|
||||
const { smtp_pass, smtp_pass_clear, ...rest } = values;
|
||||
if (smtp_pass_clear) rest.smtp_pass = '(removed)';
|
||||
else if (smtp_pass) rest.smtp_pass = '(changed)';
|
||||
return rest;
|
||||
}
|
||||
|
||||
router.put('/settings', wrap(async (req, res) => {
|
||||
const values = req.body?.settings || {};
|
||||
if (values.latest_version && !/^\d+\.\d+\.\d+$/.test(String(values.latest_version))) throw bad('Latest version looks like 1.41.0.');
|
||||
checkSmtp(values);
|
||||
const settings = await setSettings(values);
|
||||
await audit(req, 'settings_updated', null, values);
|
||||
res.json({ settings });
|
||||
await audit(req, 'settings_updated', null, auditValues(values));
|
||||
res.json({ settings, smtp: await smtpConfigured() });
|
||||
}));
|
||||
|
||||
// Tries the SMTP settings in the form (unsaved is fine): logs in and sends a test email.
|
||||
router.post('/settings/smtp-test', smtpTestLimiter, wrap(async (req, res) => {
|
||||
const values = Object.fromEntries(SMTP_FIELDS.filter(k => req.body?.settings?.[k] != null).map(k => [k, req.body.settings[k]]));
|
||||
checkSmtp(values);
|
||||
const to = text(req.body?.to, 200) || (await getSettings()).alert_email || req.admin.email;
|
||||
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(to)) throw bad('Send the test to a valid email address.');
|
||||
try {
|
||||
await testSmtp(values, to);
|
||||
} catch (e) {
|
||||
await audit(req, 'smtp_test_failed', null, { to, error: String(e.message).slice(0, 300) });
|
||||
throw bad(`The test failed: ${e.message}`);
|
||||
}
|
||||
await audit(req, 'smtp_test_sent', null, { to });
|
||||
res.json({ to });
|
||||
}));
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user