Block local/private login hosts; clarify bot-check limits

This commit is contained in:
Clay 2026-12-03 15:04:26 -05:00
parent 6949c56c17
commit cd5beee4b4
2 changed files with 65 additions and 2 deletions

View file

@ -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.) 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. 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. 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 enable arbitrary server targets; consider SSRF/egress restrictions or allowlist if exposed publicly. 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.
## Files of interest ## Files of interest

View file

@ -1,4 +1,6 @@
import { Router } from 'express'; import { Router } from 'express';
import net from 'node:net';
import dns from 'node:dns/promises';
import * as oscar from '../oscar.js'; import * as oscar from '../oscar.js';
import * as sessions from '../sessions.js'; import * as sessions from '../sessions.js';
import { runPoller } from '../poller.js'; import { runPoller } from '../poller.js';
@ -49,6 +51,63 @@ export async function cleanupSessionById(sessionId, sessionStore) {
} }
} }
function isPrivateIpv4(ip) {
const parts = ip.split('.').map((n) => Number(n));
if (parts.length !== 4 || parts.some((n) => !Number.isInteger(n) || n < 0 || n > 255)) return true;
const [a, b] = parts;
if (a === 0 || a === 10 || a === 127) return true;
if (a === 169 && b === 254) return true;
if (a === 172 && b >= 16 && b <= 31) return true;
if (a === 192 && b === 168) return true;
if (a === 100 && b >= 64 && b <= 127) return true; // CGNAT
if (a === 192 && b === 0 && parts[2] === 0) return true;
if (a === 192 && b === 0 && parts[2] === 2) return true;
if (a === 198 && (b === 18 || b === 19)) return true;
if (a === 198 && b === 51 && parts[2] === 100) return true;
if (a === 203 && b === 0 && parts[2] === 113) return true;
if (a >= 224) return true; // multicast/reserved
return false;
}
function isPrivateIpv6(ip) {
const v = ip.toLowerCase().split('%')[0];
if (v === '::1' || v === '::') return true;
if (v.startsWith('fe8') || v.startsWith('fe9') || v.startsWith('fea') || v.startsWith('feb')) return true; // fe80::/10
if (v.startsWith('fc') || v.startsWith('fd')) return true; // fc00::/7
// IPv4-mapped ::ffff:x.x.x.x or ::ffff:7f00:1
const mapped = v.match(/^::ffff:(\d+\.\d+\.\d+\.\d+)$/i);
if (mapped) return isPrivateIpv4(mapped[1]);
const hexMapped = v.match(/^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i);
if (hexMapped) {
const hi = parseInt(hexMapped[1], 16); const lo = parseInt(hexMapped[2], 16);
return isPrivateIpv4(`${(hi >> 8) & 255}.${hi & 255}.${(lo >> 8) & 255}.${lo & 255}`);
}
return false;
}
function isPrivateIp(ip) {
const kind = net.isIP(ip);
if (kind === 4) return isPrivateIpv4(ip);
if (kind === 6) return isPrivateIpv6(ip);
return true;
}
async function isDisallowedHost(host) {
const h = String(host || '').trim().toLowerCase().replace(/\.+$/, '');
if (!h) return true;
if (h === 'localhost' || h.endsWith('.localhost') || h.endsWith('.local') || h.endsWith('.internal')) return true;
if (net.isIP(h)) return isPrivateIp(h);
try {
const results = await dns.lookup(h, { all: true, verbatim: true });
if (!results.length) return true;
return results.some((r) => isPrivateIp(r.address));
} catch {
// If DNS resolution fails, do not allow unresolved host to be used for SSRF checks bypass;
// let normal connection fail later? Better be safe: block.
return true;
}
}
function cleanHost(v) { function cleanHost(v) {
const s = String(v || '').trim().slice(0, 255); const s = String(v || '').trim().slice(0, 255);
if (!s) return ''; if (!s) return '';
@ -93,7 +152,11 @@ authRouter.post('/login', async (req, res) => {
const host = cleanHost(req.body?.host) || process.env.WEBAPI_HOST || process.env.TOC_HOST || 'oscar.claytonia.net'; const host = cleanHost(req.body?.host) || process.env.WEBAPI_HOST || process.env.TOC_HOST || 'oscar.claytonia.net';
const webapiPort = cleanPort(req.body?.webapiPort, cleanPort(process.env.WEBAPI_PORT, 8081)); const webapiPort = cleanPort(req.body?.webapiPort, cleanPort(process.env.WEBAPI_PORT, 8081));
const tocPort = cleanPort(req.body?.tocPort, cleanPort(process.env.TOC_PORT, 9898)); const tocPort = cleanPort(req.body?.tocPort, cleanPort(process.env.TOC_PORT, 9898));
const webapiBase = `http://${host}:${webapiPort}`; if (await isDisallowedHost(host)) {
return res.status(400).json({ error: 'Server host is not allowed. Local/private addresses are blocked.' });
}
const webapiBaseHost = net.isIP(host) === 6 ? `[${host}]` : host;
const webapiBase = `http://${webapiBaseHost}:${webapiPort}`;
// If this browser session already has an OSCAR session, clean it up first. // If this browser session already has an OSCAR session, clean it up first.
const existing = sessions.getSession(req.session.id); const existing = sessions.getSession(req.session.id);