mirror of
https://github.com/czlonkowski/n8n-mcp.git
synced 2026-02-06 05:23:08 +00:00
fix: add DNS mocking to n8n-api-client tests for SSRF protection
Root Cause:
- SSRF protection added DNS resolution via dns/promises.lookup()
- n8n-api-client.test.ts did not mock DNS module
- Tests failed with "DNS resolution failed" error in CI
Fix:
- Added vi.mock('dns/promises') before imports
- Imported dns module for type safety
- Implemented DNS mock in beforeEach to simulate real behavior:
- localhost → 127.0.0.1
- IP addresses → returned as-is
- Real hostnames → 8.8.8.8 (public IP)
Test Results:
- All 50 n8n-api-client tests now pass ✅
- Type checking passes ✅
- Matches pattern from ssrf-protection.test.ts
🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -12,6 +12,12 @@ import {
|
|||||||
} from '../../../src/utils/n8n-errors';
|
} from '../../../src/utils/n8n-errors';
|
||||||
import * as n8nValidation from '../../../src/services/n8n-validation';
|
import * as n8nValidation from '../../../src/services/n8n-validation';
|
||||||
import { logger } from '../../../src/utils/logger';
|
import { logger } from '../../../src/utils/logger';
|
||||||
|
import * as dns from 'dns/promises';
|
||||||
|
|
||||||
|
// Mock DNS module for SSRF protection
|
||||||
|
vi.mock('dns/promises', () => ({
|
||||||
|
lookup: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
// Mock dependencies
|
// Mock dependencies
|
||||||
vi.mock('axios');
|
vi.mock('axios');
|
||||||
@@ -52,7 +58,22 @@ describe('N8nApiClient', () => {
|
|||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
|
|
||||||
|
// Mock DNS lookup for SSRF protection
|
||||||
|
vi.mocked(dns.lookup).mockImplementation(async (hostname: any) => {
|
||||||
|
// Simulate real DNS behavior for test URLs
|
||||||
|
if (hostname === 'localhost') {
|
||||||
|
return { address: '127.0.0.1', family: 4 } as any;
|
||||||
|
}
|
||||||
|
// For hostnames that look like IPs, return as-is
|
||||||
|
const ipv4Regex = /^(\d{1,3}\.){3}\d{1,3}$/;
|
||||||
|
if (ipv4Regex.test(hostname)) {
|
||||||
|
return { address: hostname, family: 4 } as any;
|
||||||
|
}
|
||||||
|
// For real hostnames (like n8n.example.com), return a public IP
|
||||||
|
return { address: '8.8.8.8', family: 4 } as any;
|
||||||
|
});
|
||||||
|
|
||||||
// Create mock axios instance
|
// Create mock axios instance
|
||||||
mockAxiosInstance = {
|
mockAxiosInstance = {
|
||||||
defaults: { baseURL: 'https://n8n.example.com/api/v1' },
|
defaults: { baseURL: 'https://n8n.example.com/api/v1' },
|
||||||
|
|||||||
Reference in New Issue
Block a user