Refuse loopback and metadata targets; check the agent's token before reading its report

Certificate check, WHOIS lookup and port scanner connect to hosts that are typed in
or named by a plain-text reply. utils/networkGuard.ts now refuses loopback, link-local
(cloud metadata) and similar addresses for all three -- and a WHOIS server on a private
network -- and the connection goes to the address that was checked rather than looking
the name up again (no DNS rebinding). LAN hosts remain fine for certificate checks and
scans.

Agent endpoint: the token is checked before the body is read, the route is mounted ahead
of the 100 KB JSON parser so a real agent can send a large report (up to 4 MB), and
overlong text is trimmed instead of costing the host its whole report. An oversized or
malformed body is answered with 413 / 400 instead of a generic 500.

Tests (npm test --workspace server): the guard, the secrets route, the agent endpoint, the
Linux upload run against a fake server (token never on curl's arguments, private temp dir
removed), the systemd unit's directives, and the Windows folder permissions read back by
SID. 19 of the 26 new tests fail against the previous code.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-06 22:05:18 +02:00
1 parent 047fcb403e
commit dbbe4180e0
9 files changed
+710 -40

No files matched your search

+4 -5
View File
@@ -32,6 +32,7 @@ import { tagsRouter } from "./routes/tags.js";
import { portsRouter } from "./routes/ports.js";
import { alertsRouter } from "./routes/alerts.js";
import { generatorRouter } from "./routes/generator.js";
import { apiErrorHandler } from "./utils/apiErrorHandler.js";
import { initSecretExpiryScheduler } from "./services/secretExpiryScheduler.js";
import { initTailscaleKeyExpiryScheduler } from "./services/tailscaleKeyExpiryScheduler.js";
import { initLogRetentionScheduler } from "./services/logRetentionScheduler.js";
@@ -66,6 +67,8 @@ mkdirSync(sessionDir, { recursive: true });
const app = express();
app.set("trust proxy", 1);
// The agent endpoint comes first: it reads its own (larger) body, only after checking the token, and an agent has no use for a session.
app.use("/api/agent/report", agentReportRouter);
app.use(express.json());
app.use(
session({
@@ -100,7 +103,6 @@ app.use("/api/ipam", ipamRouter);
app.use("/api/dns", dnsRouter);
app.use("/api/servers", serversRouter);
app.use("/api/tasks", tasksRouter);
app.use("/api/agent/report", agentReportRouter);
app.use("/api/integrations", integrationsRouter);
app.use("/api/settings", settingsRouter);
app.use("/api/search", searchRouter);
@@ -119,10 +121,7 @@ if (existsSync(webDist)) {
app.get("*", (_req, res) => res.sendFile(join(webDist, "index.html")));
}
app.use((err: unknown, _req: express.Request, res: express.Response, _next: express.NextFunction) => {
console.error(err);
res.status(500).json({ error: "internal_error" });
});
app.use(apiErrorHandler);
app.listen(env.port, () => {
console.log(`homelab-manager listening on port ${env.port}`);