Harden login: restrict ports to default+/-5, generic delayed login failures
This commit is contained in:
parent
f500554281
commit
7cf684c133
3 changed files with 83 additions and 62 deletions
|
|
@ -104,7 +104,7 @@ Against local open-oscar-server 0.25.0 with `DISABLE_AUTH=true`, TOC listener 98
|
|||
2. **UI polish follow-up:** unread indicators, chat room membership/invite-send UI, better mobile layout. (Tab close/leave for PMs and chat rooms added 2026-12-03.)
|
||||
3. **Harden TOC client:** handle reconnect/backoff, keepalive timeout, parse CONFIG2 buddy list properly, handle TOC1/TOC2 variants, handle chat errors/room not found.
|
||||
4. **Chat UX:** show chat room membership, leave button, invite-send UI, better system messages, error states.
|
||||
5. **Security follow-up:** configurable login host/ports now block localhost/private/reserved IPs and hostnames resolving to them (`isDisallowedHost` in `src/routes/auth.js`), but consider a strict allowlist for public deployment. Bot check is basic (scriptable); rely on login/API rate limits and consider stronger CAPTCHA if abuse appears.
|
||||
5. **Security follow-up:** login target host/ports are validated: localhost/private/reserved IPs and hostnames resolving to them are blocked (`isDisallowedHost`), and Web API/TOC ports must be within +/-5 of configured default ports (`isAllowedPort`) to limit port scanning. Login failures always return generic `login failed` after a minimum 15s delay to reduce timing/oracle abuse. Bot check is basic (scriptable); rely on rate limits and consider stronger CAPTCHA if abuse appears.
|
||||
|
||||
## Files of interest
|
||||
|
||||
|
|
|
|||
|
|
@ -72,3 +72,10 @@ export function cleanPort(v, fallback) {
|
|||
if (!Number.isInteger(n) || n < 1 || n > 65535) return fallback;
|
||||
return n;
|
||||
}
|
||||
|
||||
export function isAllowedPort(port, base, delta = 5) {
|
||||
const p = Number(port);
|
||||
const b = Number(base);
|
||||
if (!Number.isInteger(p) || !Number.isInteger(b)) return false;
|
||||
return Math.abs(p - b) <= delta;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import * as sessions from '../sessions.js';
|
|||
import { runPoller } from '../poller.js';
|
||||
import { startTOCSession, stopTOCSession } from '../toc-session.js';
|
||||
import { cleanScreenName } from '../validate.js';
|
||||
import { cleanHost, cleanPort, isDisallowedHost } from '../host-check.js';
|
||||
import { cleanHost, cleanPort, isAllowedPort, isDisallowedHost } from '../host-check.js';
|
||||
import { markWebuiOnline, markWebuiOfflineIfLast } from '../web-presence.js';
|
||||
|
||||
export const authRouter = Router();
|
||||
|
|
@ -123,29 +123,40 @@ async function tryTocLogin(rec, { screenName, host, tocPort, password, webapiBas
|
|||
return next;
|
||||
}
|
||||
|
||||
const LOGIN_FAIL_DELAY_MS = 15_000;
|
||||
|
||||
async function failLogin(res, startedAt) {
|
||||
const elapsed = Date.now() - startedAt;
|
||||
const waitMs = Math.max(0, LOGIN_FAIL_DELAY_MS - elapsed);
|
||||
if (waitMs > 0) await new Promise((resolve) => setTimeout(resolve, waitMs));
|
||||
return res.status(401).json({ error: 'login failed' });
|
||||
}
|
||||
|
||||
// Login flow:
|
||||
// 1) Validate captcha + required credentials.
|
||||
// 2) Validate target host/ports and block private/local targets (SSRF guard).
|
||||
// 2) Validate target host/ports, block private/local targets (SSRF guard), and only allow
|
||||
// ports within +/-5 of configured default Web API/TOC ports to limit scanning.
|
||||
// 3) Tear down any existing session for this browser/screen name, then regenerate
|
||||
// the express-session id (session fixation protection).
|
||||
// 4) Try Web API login/startSession for buddy/presence/poller.
|
||||
// 5) Try TOC login for IM/chat/invites (works even when Web API chat is unavailable).
|
||||
// 6) If either transport succeeds, store session record, start poller (if Web API),
|
||||
// mark ICQ webui presence overlay, and return transport status.
|
||||
// 6) On failure, always return the same generic error after a minimum delay to reduce
|
||||
// timing/oracle abuse. On success, return transport status.
|
||||
authRouter.post('/login', async (req, res) => {
|
||||
const startedAt = Date.now();
|
||||
try {
|
||||
const screenName = cleanScreenName(req.body?.screenName);
|
||||
const password = readPassword(req);
|
||||
if (!readCaptcha(req)) {
|
||||
return res.status(400).json({ error: 'Bot check failed. Please solve the math problem.' });
|
||||
}
|
||||
if (!screenName || !password) {
|
||||
return res.status(400).json({ error: 'screenName and password are required' });
|
||||
}
|
||||
if (!readCaptcha(req)) return failLogin(res, startedAt);
|
||||
if (!screenName || !password) return failLogin(res, startedAt);
|
||||
|
||||
const { host, webapiPort: _webapiPort, tocPort, webapiBase } = resolveLoginTarget(req.body);
|
||||
if (await isDisallowedHost(host)) {
|
||||
return res.status(400).json({ error: 'Server host is not allowed. Local/private addresses are blocked.' });
|
||||
const { host, webapiPort, tocPort, webapiBase } = resolveLoginTarget(req.body);
|
||||
const webapiBasePort = cleanPort(process.env.WEBAPI_PORT, 8081);
|
||||
const tocBasePort = cleanPort(process.env.TOC_PORT, 9898);
|
||||
if (!isAllowedPort(webapiPort, webapiBasePort) || !isAllowedPort(tocPort, tocBasePort)) {
|
||||
return failLogin(res, startedAt);
|
||||
}
|
||||
if (await isDisallowedHost(host)) return failLogin(res, startedAt);
|
||||
|
||||
// If this browser session already has an OSCAR session, clean it up first.
|
||||
const existing = sessions.getSession(req.session.id);
|
||||
|
|
@ -180,8 +191,7 @@ authRouter.post('/login', async (req, res) => {
|
|||
|
||||
if (!rec) {
|
||||
console.error('[login] failed:', webErr?.message || tocErr?.message);
|
||||
const status = (webErr?.statusCode === 401 || webErr?.statusCode === 403) ? 401 : 502;
|
||||
return res.status(status).json({ error: 'Login failed. Check screen name/password, bot check, and server settings.' });
|
||||
return failLogin(res, startedAt);
|
||||
}
|
||||
|
||||
sessions.setSession(req.session.id, rec);
|
||||
|
|
@ -196,6 +206,10 @@ authRouter.post('/login', async (req, res) => {
|
|||
|
||||
markWebuiOnline(screenName);
|
||||
return res.json({ ok: true, screenName, transport: { webapi: !!rec.aimsid, toc: !!rec.tocConnected } });
|
||||
} catch (err) {
|
||||
console.error('[login] unexpected error:', err?.message || err);
|
||||
return failLogin(res, startedAt);
|
||||
}
|
||||
});
|
||||
|
||||
authRouter.post('/logout', requireLogin, async (req, res) => {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue