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

+8 -2
View File
@@ -1,5 +1,6 @@
import * as net from "node:net";
import { domainToASCII } from "node:url";
import { resolveTarget } from "../utils/networkGuard.js";
/**
* Looks up when a domain registration expires. RDAP (the JSON successor to WHOIS) is used wherever the TLD
@@ -40,9 +41,14 @@ export const defaultDeps: LookupDeps = {
}
return { status: res.status, json };
},
whoisQuery(server, query) {
async whoisQuery(server, query) {
// The WHOIS server is named in a plain-text reply on port 43, which anyone on the path could rewrite — so it has to look like a
// hostname and must not be this machine, a cloud metadata address or anything on a private network. Connect to the address
// that passed, not to the name again.
if (!/^[A-Za-z0-9.-]{1,253}$/.test(server)) throw new Error(`Refusing to query "${server}": not a hostname.`);
const address = await resolveTarget(server, { allowPrivate: false });
return new Promise((resolve, reject) => {
const socket = net.connect({ host: server, port: 43 });
const socket = net.connect({ host: address, port: 43 });
let out = "";
socket.setTimeout(12_000, () => {
socket.destroy();
+4
View File
@@ -1,5 +1,6 @@
import * as net from "node:net";
import * as dns from "node:dns/promises";
import { classifyAddress } from "../utils/networkGuard.js";
export const MAX_SCAN_SPAN = 20_000;
@@ -102,6 +103,9 @@ export async function resolveScanTarget(address: string): Promise<string> {
throw new Error(`Couldn't resolve "${trimmed}".`);
}
}
if (ips.some((a) => classifyAddress(a) === "metadata")) {
throw new Error(`"${trimmed}" is a cloud metadata address, which can't be scanned.`);
}
const ip = ips.find(isPrivateAddress);
if (!ip) {
throw new Error(
+8 -2
View File
@@ -3,6 +3,7 @@ import { isIP } from "node:net";
import { eq, isNotNull } from "drizzle-orm";
import { db } from "../db/client.js";
import { secrets } from "../db/schema.js";
import { resolveTarget } from "../utils/networkGuard.js";
/**
* Opens a real TLS connection and returns the server certificate's expiry as
@@ -12,12 +13,17 @@ import { secrets } from "../db/schema.js";
* commonly present self-signed or internal-CA certificates that a verifying
* connection would refuse before ever exposing the dates — including an
* already-expired one, which is exactly the case worth reporting.
*
* The host is typed in by an operator, so it can't be just anything: loopback, link-local (cloud metadata) and similar
* addresses are refused, and the connection goes to the one address that passed the check rather than to the name again
* (see utils/networkGuard.ts). LAN and public hosts are fine — those are what certificates are checked on.
*/
export function fetchCertExpiry(host: string, port: number, timeoutMs = 10_000): Promise<string> {
export async function fetchCertExpiry(host: string, port: number, timeoutMs = 10_000): Promise<string> {
const address = await resolveTarget(host, { allowPrivate: true });
return new Promise((resolve, reject) => {
const socket = tls.connect(
{
host,
host: address,
port,
// SNI must be a hostname — Node warns (and some servers reject) when an IP is sent as the server name.
servername: isIP(host) === 0 ? host : undefined,