mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 17:03:15 +00:00
refactor(plugins): generate SDK egress policy from the server
This commit is contained in:
@@ -8,13 +8,17 @@ import {
|
||||
makeHostAllow,
|
||||
classifyConnect,
|
||||
snapshotConnectArgs,
|
||||
guardSocketConnect,
|
||||
guardDnsLookup,
|
||||
guardDatagramCall,
|
||||
firstBlockedAddress,
|
||||
unwrapConnectArgs,
|
||||
dgramSendTarget,
|
||||
dgramConnectTarget,
|
||||
} from '../../../src/nest/plugins/runtime/egress-policy';
|
||||
|
||||
import net from 'node:net';
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
|
||||
const isIP = (s: string) => net.isIP(s) !== 0;
|
||||
|
||||
@@ -182,6 +186,106 @@ describe('snapshotConnectArgs', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('guardSocketConnect', () => {
|
||||
const allowed = makeHostAllow(['api.example.com', '127.0.0.1']);
|
||||
const refuse = (host: string) => new Error(`Not declared: ${host}`);
|
||||
const lookup = () => {};
|
||||
const policy = { allowed, refuse, isIp: isIP, blockPrivate: true, lookup };
|
||||
|
||||
it('passes a detached, checked option record to the socket', () => {
|
||||
const socket = vi.fn();
|
||||
const connect = guardSocketConnect(socket, policy);
|
||||
const options = { host: 'api.example.com', port: 443 };
|
||||
connect(options);
|
||||
|
||||
expect(socket).toHaveBeenCalledWith({ host: 'api.example.com', port: 443, lookup }, undefined);
|
||||
expect(socket.mock.calls[0][0]).not.toBe(options);
|
||||
});
|
||||
|
||||
it('does not call the socket for a disallowed hostname, address or path', () => {
|
||||
const socket = vi.fn();
|
||||
const connect = guardSocketConnect(socket, policy);
|
||||
expect(() => connect(443, 'other.example.com')).toThrow('Not declared');
|
||||
expect(() => connect(443, '127.0.0.1')).toThrow('blocked address');
|
||||
expect(() => connect('/run/app.sock')).toThrow('local socket');
|
||||
expect(socket).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('keeps an opted-in local connection local', () => {
|
||||
const socket = vi.fn();
|
||||
const connect = guardSocketConnect(socket, { ...policy, blockPrivate: false });
|
||||
connect({ path: '/run/app.sock', host: 'api.example.com', port: 443 });
|
||||
expect(socket).toHaveBeenCalledWith({ path: '/run/app.sock' }, undefined);
|
||||
});
|
||||
});
|
||||
|
||||
describe('firstBlockedAddress', () => {
|
||||
it('checks both supported resolver results', () => {
|
||||
expect(firstBlockedAddress('127.0.0.1')).toBe('127.0.0.1');
|
||||
expect(firstBlockedAddress('203.0.113.1')).toBeUndefined();
|
||||
expect(firstBlockedAddress([{ address: '203.0.113.1' }, { address: '127.0.0.1' }])).toBe('127.0.0.1');
|
||||
expect(firstBlockedAddress([{ address: '203.0.113.1' }])).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('guardDnsLookup', () => {
|
||||
it('forwards a vetted answer to the callback-only overload', () => {
|
||||
const lookup = guardDnsLookup((hostname, options, done) => {
|
||||
expect(hostname).toBe('api.example.com');
|
||||
expect(options).toEqual({});
|
||||
done(null, '203.0.113.1', 4);
|
||||
}, true);
|
||||
const done = vi.fn();
|
||||
lookup('api.example.com', done);
|
||||
expect(done).toHaveBeenCalledWith(null, '203.0.113.1', 4);
|
||||
});
|
||||
|
||||
it('rejects a blocked result in an address list', () => {
|
||||
const addresses = [{ address: '127.0.0.1', family: 4 }];
|
||||
const lookup = guardDnsLookup((_host, _options, done) => done(null, addresses, 4), true);
|
||||
const done = vi.fn();
|
||||
lookup('api.example.com', { all: true }, done);
|
||||
expect(done).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining('blocked address') }));
|
||||
});
|
||||
|
||||
it('forwards resolver errors', () => {
|
||||
const error = new Error('Resolver unavailable');
|
||||
const lookup = guardDnsLookup((_host, _options, done) => done(error, '', 0), true);
|
||||
const done = vi.fn();
|
||||
lookup('api.example.com', {}, done);
|
||||
expect(done).toHaveBeenCalledWith(error, '', 0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('guardDatagramCall', () => {
|
||||
it('passes permitted and implicit targets to the socket', () => {
|
||||
const socket = vi.fn();
|
||||
const send = guardDatagramCall(socket, dgramSendTarget, {
|
||||
allowed: makeHostAllow(['api.example.com']),
|
||||
refuse: (host) => new Error(`Not declared: ${host}`),
|
||||
isIp: isIP,
|
||||
blockPrivate: true,
|
||||
});
|
||||
const data = Buffer.from('data');
|
||||
send(data, 443, 'api.example.com');
|
||||
send(data);
|
||||
expect(socket.mock.calls).toEqual([[data, 443, 'api.example.com'], [data]]);
|
||||
});
|
||||
|
||||
it('rejects undeclared and blocked datagram targets before calling the socket', () => {
|
||||
const socket = vi.fn();
|
||||
const connect = guardDatagramCall(socket, dgramConnectTarget, {
|
||||
allowed: makeHostAllow(['127.0.0.1']),
|
||||
refuse: (host) => new Error(`Not declared: ${host}`),
|
||||
isIp: isIP,
|
||||
blockPrivate: true,
|
||||
});
|
||||
expect(() => connect(443, 'other.example.com')).toThrow('Not declared');
|
||||
expect(() => connect(443, '127.0.0.1')).toThrow('blocked address');
|
||||
expect(socket).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('dgramSendTarget', () => {
|
||||
const msg = Buffer.from('x');
|
||||
it('reads the address from send(msg, port, address)', () => {
|
||||
|
||||
Reference in New Issue
Block a user