fix(plugins): repair plain-HTTP egress and forward the private-egress opt-out

Two pre-existing bugs in the plugin egress guard, found by running a plugin
against a real service end to end.

1. Every plain-HTTP request a plugin made was refused, whatever host it had
   declared. Node pre-normalises `net.connect()` args into an [options, cb]
   array and passes THAT array as the single argument; undici's plain-HTTP
   connector takes this path, its TLS connector does not. classifyConnect read
   `host` off the array, got undefined, and fell back to 'localhost' — so a
   fetch to a declared, public host was rejected with the nonsense message
   "localhost is not in the plugin's declared hosts". It failed closed, so it
   was never a security hole, and it went unnoticed because the only shipped
   egress plugin uses HTTPS. unwrapConnectArgs() unwraps the normalised form
   before anything reads host/path.

2. TREK_PLUGIN_ALLOW_PRIVATE_EGRESS could never have any effect. The guard that
   reads it runs INSIDE the child, whose env is scrubbed to a four-entry
   whitelist that never included it — so a documented setting (wiki/
   Environment-Variables.md) was wired to nothing, and no plugin could reach a
   self-hoster's LAN service no matter what the operator set. Forwarded only
   when set, so the default stays the secure block-private policy.

Regression tests cover the normalised form in both directions: the real host is
now resolved, and an undeclared host, a private IP and a unix socket are all
still refused when passed that way.
This commit is contained in:
jubnl
2026-07-11 19:23:24 +02:00
parent a87b656b43
commit 2899465dac
4 changed files with 89 additions and 7 deletions
@@ -6,7 +6,7 @@
import { describe, it, expect } from 'vitest';
import net from 'node:net';
import {
isBlockedIp, makeHostAllow, classifyConnect, dgramSendTarget, dgramConnectTarget,
isBlockedIp, makeHostAllow, classifyConnect, unwrapConnectArgs, dgramSendTarget, dgramConnectTarget,
} from '../../../src/nest/plugins/runtime/egress-policy';
const isIP = (s: string) => net.isIP(s) !== 0;
@@ -116,3 +116,57 @@ describe('dgramConnectTarget', () => {
expect(dgramConnectTarget([53, () => {}])).toBeNull();
});
});
// ─────────────────────────────────────────────────────────────────────────────
// Node's pre-normalised connect args (the plain-HTTP egress bug)
// ─────────────────────────────────────────────────────────────────────────────
//
// net.connect() normalises its arguments into an [options, callback] array and calls
// Socket.prototype.connect with THAT ARRAY as the single argument. undici's plain-HTTP
// connector goes through this path; its TLS connector does not.
//
// Before unwrapConnectArgs, `host` read as undefined off the array and classifyConnect
// fell back to 'localhost' — so EVERY plain-HTTP request a plugin made was misclassified
// and refused with "localhost is not in the plugin's declared hosts", no matter which
// host it had declared. It only escaped notice because the sole shipped egress plugin
// uses HTTPS.
describe('unwrapConnectArgs (pre-normalised connect args)', () => {
// Exactly what undici's plain-HTTP connector produces.
const normalised = [[{ highWaterMark: 65536, localAddress: null, port: '18080', host: 'gotify.example.com' }, null]];
it('unwraps the [options, cb] array Node passes as a single argument', () => {
expect(unwrapConnectArgs(normalised)[0]).toEqual(
expect.objectContaining({ host: 'gotify.example.com', port: '18080' }),
);
});
it('leaves a conventional argument list alone', () => {
expect(unwrapConnectArgs([{ host: 'a.example.com', port: 443 }])).toEqual([{ host: 'a.example.com', port: 443 }]);
expect(unwrapConnectArgs([443, 'a.example.com'])).toEqual([443, 'a.example.com']);
});
it('classifyConnect reads the REAL host out of it, not the localhost fallback', () => {
expect(classifyConnect(normalised, isIP)).toEqual({ kind: 'hostname', host: 'gotify.example.com' });
});
it('a normalised literal-IP target is still classified as an IP (so the private-IP block applies)', () => {
expect(classifyConnect([[{ port: 80, host: '169.254.169.254' }, null]], isIP)).toEqual({
kind: 'literal-ip',
host: '169.254.169.254',
});
});
it('a normalised unix-socket target is still classified as local', () => {
expect(classifyConnect([[{ path: '/var/run/docker.sock' }, null]], isIP)).toEqual({
kind: 'local',
host: '/var/run/docker.sock',
});
});
it('an undeclared host in normalised form is still refused by the allow-list', () => {
const allow = makeHostAllow(['gotify.example.com']);
const target = classifyConnect([[{ port: 80, host: 'evil.example.com' }, null]], isIP);
expect(allow(target.host)).toBe(false);
});
});