Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/commands/preflight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ describe('resolveAllowedDomains', () => {
mockedOptionParsers.processLocalhostKeyword.mockReturnValue({
allowedDomains: [],
localhostDetected: false,
hostGatewayDetected: false,
shouldEnableHostAccess: false,
});
mockedCopilotResolver.resolveCopilotApiRouting.mockReturnValue({
Expand All @@ -160,6 +161,7 @@ describe('resolveAllowedDomains', () => {
mockedOptionParsers.processLocalhostKeyword.mockReturnValue({
allowedDomains: ['example.com'],
localhostDetected: false,
hostGatewayDetected: false,
shouldEnableHostAccess: false,
});

Expand All @@ -173,6 +175,7 @@ describe('resolveAllowedDomains', () => {
mockedOptionParsers.processLocalhostKeyword.mockReturnValue({
allowedDomains: ['file-domain.com'],
localhostDetected: false,
hostGatewayDetected: false,
shouldEnableHostAccess: false,
});

Expand All @@ -195,6 +198,7 @@ describe('resolveAllowedDomains', () => {
mockedOptionParsers.processLocalhostKeyword.mockReturnValue({
allowedDomains: ['ruleset-domain.com'],
localhostDetected: false,
hostGatewayDetected: false,
shouldEnableHostAccess: false,
});

Expand All @@ -221,6 +225,7 @@ describe('resolveAllowedDomains', () => {
mockedOptionParsers.processLocalhostKeyword.mockReturnValue({
allowedDomains: ['bad domain!'],
localhostDetected: false,
hostGatewayDetected: false,
shouldEnableHostAccess: false,
});
mockedApiProxyConfig.resolveApiTargetsToAllowedDomains.mockReturnValue(['bad domain!']);
Expand All @@ -238,6 +243,7 @@ describe('resolveAllowedDomains', () => {
mockedOptionParsers.processLocalhostKeyword.mockReturnValue({
allowedDomains: ['host.docker.internal'],
localhostDetected: true,
hostGatewayDetected: true,
shouldEnableHostAccess: true,
defaultPorts: '3000,8080',
});
Expand All @@ -252,6 +258,26 @@ describe('resolveAllowedDomains', () => {
expect(mockedLogger.warn).toHaveBeenCalledWith(expect.stringContaining('localhost keyword enables host access'));
});

it('enables host access for explicit host gateway domains without treating them as localhost', () => {
mockedOptionParsers.processLocalhostKeyword.mockReturnValue({
allowedDomains: ['host.docker.internal'],
localhostDetected: false,
hostGatewayDetected: true,
shouldEnableHostAccess: true,
defaultPorts: undefined,
});
mockedDomainUtils.parseDomains.mockReturnValue(['host.docker.internal']);

const options: Record<string, unknown> = { allowDomains: 'host.docker.internal', allowHostPorts: '11434' };
const result = resolveAllowedDomains(options);

expect(result.localhostResult.localhostDetected).toBe(false);
expect(result.localhostResult.hostGatewayDetected).toBe(true);
expect(options.enableHostAccess).toBe(true);
expect(options.allowHostPorts).toBe('11434');
expect(mockedLogger.warn).not.toHaveBeenCalledWith(expect.stringContaining('localhost keyword enables host access'));
});

it('returns resolved Copilot API target from resolver', () => {
mockedCopilotResolver.resolveCopilotApiRouting.mockReturnValue({
copilotApiTarget: 'custom.copilot.com',
Expand All @@ -267,6 +293,7 @@ describe('resolveAllowedDomains', () => {
mockedOptionParsers.processLocalhostKeyword.mockReturnValue({
allowedDomains: ['localhost'],
localhostDetected: true,
hostGatewayDetected: true,
shouldEnableHostAccess: false,
});
mockedDomainUtils.parseDomains.mockReturnValue(['localhost']);
Expand All @@ -286,6 +313,7 @@ describe('resolveAllowedDomains', () => {
mockedOptionParsers.processLocalhostKeyword.mockReturnValue({
allowedDomains: ['localhost'],
localhostDetected: true,
hostGatewayDetected: true,
shouldEnableHostAccess: false,
defaultPorts: undefined,
});
Expand Down Expand Up @@ -332,6 +360,7 @@ describe('resolveAllowedDomains', () => {
mockedOptionParsers.processLocalhostKeyword.mockReturnValue({
allowedDomains: ['awmg-mcpg'],
localhostDetected: false,
hostGatewayDetected: false,
shouldEnableHostAccess: false,
});

Expand Down
8 changes: 5 additions & 3 deletions src/commands/preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,16 @@ export function resolveAllowedDomains(options: Record<string, unknown>): Allowed
options.allowHostPorts as string | undefined
);

if (localhostResult.localhostDetected) {
if (localhostResult.hostGatewayDetected) {
allowedDomains = localhostResult.allowedDomains;

// Auto-enable host access
if (localhostResult.shouldEnableHostAccess) {
options.enableHostAccess = true;
logger.warn('⚠️ Security warning: localhost keyword enables host access - agent can reach services on your machine');
logger.info('ℹ️ localhost keyword detected - automatically enabling host access');
if (localhostResult.localhostDetected) {
logger.warn('⚠️ Security warning: localhost keyword enables host access - agent can reach services on your machine');
logger.info('ℹ️ localhost keyword detected - automatically enabling host access');
}
}

// Auto-configure common dev ports if not already specified
Expand Down
2 changes: 2 additions & 0 deletions src/commands/validate-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ describe('validateOptions', () => {
sensitiveAllowedDomains: [],
localhostResult: {
localhostDetected: false,
hostGatewayDetected: false,
allowedDomains: ['github.com'],
shouldEnableHostAccess: false,
},
Expand Down Expand Up @@ -579,6 +580,7 @@ describe('validateOptions', () => {
sensitiveAllowedDomains: [],
localhostResult: {
localhostDetected: false,
hostGatewayDetected: true,
allowedDomains: ['host.docker.internal'],
shouldEnableHostAccess: false,
},
Expand Down
1 change: 1 addition & 0 deletions src/commands/validators/config-assembly.test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export const createMinimalNetworkOptions = (): NetworkOptionsResult => ({
localhostResult: {
allowedDomains: ['example.com'],
localhostDetected: false,
hostGatewayDetected: false,
shouldEnableHostAccess: false,
},
upstreamProxy: undefined,
Expand Down
6 changes: 6 additions & 0 deletions src/option-parsers-network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ describe('processLocalhostKeyword', () => {
);

expect(result.localhostDetected).toBe(false);
expect(result.hostGatewayDetected).toBe(false);
expect(result.allowedDomains).toEqual(['github.com', 'example.com']);
expect(result.shouldEnableHostAccess).toBe(false);
expect(result.defaultPorts).toBeUndefined();
Expand All @@ -100,6 +101,7 @@ describe('processLocalhostKeyword', () => {
);

expect(result.localhostDetected).toBe(true);
expect(result.hostGatewayDetected).toBe(true);
expect(result.allowedDomains).toEqual(['github.com', 'host.docker.internal']);
expect(result.shouldEnableHostAccess).toBe(true);
expect(result.defaultPorts).toBe('3000,3001,4000,4200,5000,5173,8000,8080,8081,8888,9000,9090');
Expand All @@ -113,6 +115,7 @@ describe('processLocalhostKeyword', () => {
);

expect(result.localhostDetected).toBe(true);
expect(result.hostGatewayDetected).toBe(true);
expect(result.allowedDomains).toEqual(['host.docker.internal']);
expect(result.shouldEnableHostAccess).toBe(true);
});
Expand All @@ -127,6 +130,7 @@ describe('processLocalhostKeyword', () => {
);

expect(result.localhostDetected).toBe(true);
expect(result.hostGatewayDetected).toBe(true);
expect(result.allowedDomains).toEqual(['github.com', 'http://host.docker.internal']);
expect(result.shouldEnableHostAccess).toBe(true);
expect(result.defaultPorts).toBe('3000,3001,4000,4200,5000,5173,8000,8080,8081,8888,9000,9090');
Expand All @@ -142,6 +146,7 @@ describe('processLocalhostKeyword', () => {
);

expect(result.localhostDetected).toBe(true);
expect(result.hostGatewayDetected).toBe(true);
expect(result.allowedDomains).toEqual(['github.com', 'https://host.docker.internal']);
expect(result.shouldEnableHostAccess).toBe(true);
expect(result.defaultPorts).toBe('3000,3001,4000,4200,5000,5173,8000,8080,8081,8888,9000,9090');
Expand Down Expand Up @@ -222,6 +227,7 @@ describe('processLocalhostKeyword', () => {
);

expect(result.localhostDetected).toBe(false);
expect(result.hostGatewayDetected).toBe(false);
expect(result.allowedDomains).toEqual([]);
});
});
Expand Down
23 changes: 23 additions & 0 deletions src/parsers/dns-parsers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ describe('processLocalhostKeyword', () => {
it('returns domains unchanged when localhost is not present', () => {
const result = processLocalhostKeyword(['github.com', 'api.github.com'], false, undefined);
expect(result.localhostDetected).toBe(false);
expect(result.hostGatewayDetected).toBe(false);
expect(result.shouldEnableHostAccess).toBe(false);
expect(result.allowedDomains).toEqual(['github.com', 'api.github.com']);
expect(result.defaultPorts).toBeUndefined();
Expand All @@ -89,11 +90,33 @@ describe('processLocalhostKeyword', () => {
it('replaces bare localhost with host.docker.internal', () => {
const result = processLocalhostKeyword(['localhost', 'github.com'], false, undefined);
expect(result.localhostDetected).toBe(true);
expect(result.hostGatewayDetected).toBe(true);
expect(result.allowedDomains).toContain('host.docker.internal');
expect(result.allowedDomains).not.toContain('localhost');
expect(result.allowedDomains).toContain('github.com');
});

it('enables host access when host.docker.internal is explicitly allowed', () => {
const result = processLocalhostKeyword(['host.docker.internal'], false, '11434');
expect(result.localhostDetected).toBe(false);
expect(result.hostGatewayDetected).toBe(true);
expect(result.shouldEnableHostAccess).toBe(true);
expect(result.allowedDomains).toEqual(['host.docker.internal']);
expect(result.defaultPorts).toBeUndefined();
});

it.each([
['http://host.docker.internal'],
['https://host.docker.internal'],
])('preserves explicit %s entries without detecting localhost', (domain) => {
const result = processLocalhostKeyword([domain], false, '11434');
expect(result.localhostDetected).toBe(false);
expect(result.hostGatewayDetected).toBe(true);
expect(result.shouldEnableHostAccess).toBe(true);
expect(result.allowedDomains).toEqual([domain]);
expect(result.defaultPorts).toBeUndefined();
});

it('preserves http:// protocol when replacing localhost', () => {
const result = processLocalhostKeyword(['http://localhost'], false, undefined);
expect(result.allowedDomains).toContain('http://host.docker.internal');
Expand Down
33 changes: 24 additions & 9 deletions src/parsers/dns-parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const DEFAULT_DOH_RESOLVER = 'https://dns.google/dns-query';
interface LocalhostProcessingResult {
allowedDomains: string[];
localhostDetected: boolean;
hostGatewayDetected: boolean;
shouldEnableHostAccess: boolean;
defaultPorts?: string;
}
Expand Down Expand Up @@ -48,32 +49,45 @@ export function parseDnsOverHttps(
}

/**
* Processes the localhost keyword in the allowed domains list.
* Processes host-gateway keywords in the allowed domains list.
*/
export function processLocalhostKeyword(
allowedDomains: string[],
enableHostAccess: boolean,
allowHostPorts: string | undefined
): LocalhostProcessingResult {
const localhostIndex = allowedDomains.findIndex(d =>
d === 'localhost' || d === 'http://localhost' || d === 'https://localhost'
d === 'localhost' ||
d === 'http://localhost' ||
d === 'https://localhost'
);
const hostGatewayIndex = localhostIndex === -1 ? allowedDomains.findIndex(d =>
d === 'host.docker.internal' ||
d === 'http://host.docker.internal' ||
d === 'https://host.docker.internal'
Comment on lines +65 to +67
) : localhostIndex;

if (localhostIndex === -1) {
if (hostGatewayIndex === -1) {
return {
allowedDomains,
localhostDetected: false,
hostGatewayDetected: false,
shouldEnableHostAccess: false,
};
}

// Remove localhost and replace with host.docker.internal
const localhostValue = allowedDomains[localhostIndex];
const localhostDetected = localhostIndex !== -1;

// Normalize localhost to host.docker.internal. An explicit
// host.docker.internal entry is already normalized.
const localhostValue = allowedDomains[hostGatewayIndex];
const updatedDomains = [...allowedDomains];
updatedDomains.splice(localhostIndex, 1);
updatedDomains.splice(hostGatewayIndex, 1);

// Preserve protocol if specified
if (localhostValue.startsWith('http://')) {
if (localhostValue === 'host.docker.internal') {
updatedDomains.push(localhostValue);
} else if (localhostValue.startsWith('http://')) {
updatedDomains.push('http://host.docker.internal');
} else if (localhostValue.startsWith('https://')) {
updatedDomains.push('https://host.docker.internal');
Expand All @@ -83,8 +97,9 @@ export function processLocalhostKeyword(

return {
allowedDomains: updatedDomains,
localhostDetected: true,
localhostDetected,
hostGatewayDetected: true,
shouldEnableHostAccess: !enableHostAccess,
defaultPorts: allowHostPorts ? undefined : '3000,3001,4000,4200,5000,5173,8000,8080,8081,8888,9000,9090',
defaultPorts: localhostDetected && !allowHostPorts ? '3000,3001,4000,4200,5000,5173,8000,8080,8081,8888,9000,9090' : undefined,
};
}
Loading