|
| 1 | +/** |
| 2 | + * Empty Domains Tests |
| 3 | + * |
| 4 | + * These tests verify the behavior when no domains are allowed: |
| 5 | + * - All network access should be blocked |
| 6 | + * - Commands that don't require network should still work |
| 7 | + * - Debug logs should indicate no domains are configured |
| 8 | + */ |
| 9 | + |
| 10 | +/// <reference path="../jest-custom-matchers.d.ts" /> |
| 11 | + |
| 12 | +import { describe, test, expect, beforeAll, afterAll } from '@jest/globals'; |
| 13 | +import { createRunner, AwfRunner } from '../fixtures/awf-runner'; |
| 14 | +import { cleanup } from '../fixtures/cleanup'; |
| 15 | + |
| 16 | +describe('Empty Domains (No Network Access)', () => { |
| 17 | + let runner: AwfRunner; |
| 18 | + |
| 19 | + beforeAll(async () => { |
| 20 | + await cleanup(false); |
| 21 | + runner = createRunner(); |
| 22 | + }); |
| 23 | + |
| 24 | + afterAll(async () => { |
| 25 | + await cleanup(false); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('Network Blocking', () => { |
| 29 | + test('should block all network access when no domains are specified', async () => { |
| 30 | + // Try to access a website without any allowed domains |
| 31 | + const result = await runner.runWithSudo( |
| 32 | + 'curl -f --max-time 5 https://example.com', |
| 33 | + { |
| 34 | + allowDomains: [], // Empty domains list |
| 35 | + logLevel: 'debug', |
| 36 | + timeout: 60000, |
| 37 | + } |
| 38 | + ); |
| 39 | + |
| 40 | + // Request should fail because no domains are allowed |
| 41 | + expect(result).toFail(); |
| 42 | + }, 120000); |
| 43 | + |
| 44 | + test('should block HTTPS traffic when no domains are specified', async () => { |
| 45 | + const result = await runner.runWithSudo( |
| 46 | + 'curl -f --max-time 5 https://api.github.com/zen', |
| 47 | + { |
| 48 | + allowDomains: [], |
| 49 | + logLevel: 'debug', |
| 50 | + timeout: 60000, |
| 51 | + } |
| 52 | + ); |
| 53 | + |
| 54 | + expect(result).toFail(); |
| 55 | + }, 120000); |
| 56 | + |
| 57 | + test('should block HTTP traffic when no domains are specified', async () => { |
| 58 | + const result = await runner.runWithSudo( |
| 59 | + 'curl -f --max-time 5 http://httpbin.org/get', |
| 60 | + { |
| 61 | + allowDomains: [], |
| 62 | + logLevel: 'debug', |
| 63 | + timeout: 60000, |
| 64 | + } |
| 65 | + ); |
| 66 | + |
| 67 | + expect(result).toFail(); |
| 68 | + }, 120000); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('Offline Commands', () => { |
| 72 | + test('should allow commands that do not require network access', async () => { |
| 73 | + const result = await runner.runWithSudo( |
| 74 | + 'echo "Hello, offline world!"', |
| 75 | + { |
| 76 | + allowDomains: [], |
| 77 | + logLevel: 'debug', |
| 78 | + timeout: 60000, |
| 79 | + } |
| 80 | + ); |
| 81 | + |
| 82 | + expect(result).toSucceed(); |
| 83 | + expect(result.stdout).toContain('Hello, offline world!'); |
| 84 | + }, 120000); |
| 85 | + |
| 86 | + test('should allow file system operations without network', async () => { |
| 87 | + const result = await runner.runWithSudo( |
| 88 | + 'bash -c "echo test > /tmp/test.txt && cat /tmp/test.txt && rm /tmp/test.txt"', |
| 89 | + { |
| 90 | + allowDomains: [], |
| 91 | + logLevel: 'debug', |
| 92 | + timeout: 60000, |
| 93 | + } |
| 94 | + ); |
| 95 | + |
| 96 | + expect(result).toSucceed(); |
| 97 | + expect(result.stdout).toContain('test'); |
| 98 | + }, 120000); |
| 99 | + |
| 100 | + test('should allow local computations without network', async () => { |
| 101 | + const result = await runner.runWithSudo( |
| 102 | + 'bash -c "expr 2 + 2"', |
| 103 | + { |
| 104 | + allowDomains: [], |
| 105 | + logLevel: 'debug', |
| 106 | + timeout: 60000, |
| 107 | + } |
| 108 | + ); |
| 109 | + |
| 110 | + expect(result).toSucceed(); |
| 111 | + expect(result.stdout.trim()).toBe('4'); |
| 112 | + }, 120000); |
| 113 | + }); |
| 114 | + |
| 115 | + describe('Debug Output', () => { |
| 116 | + test('should indicate no domains are configured in debug output', async () => { |
| 117 | + const result = await runner.runWithSudo( |
| 118 | + 'echo "test"', |
| 119 | + { |
| 120 | + allowDomains: [], |
| 121 | + logLevel: 'debug', |
| 122 | + timeout: 60000, |
| 123 | + } |
| 124 | + ); |
| 125 | + |
| 126 | + expect(result).toSucceed(); |
| 127 | + // Should show debug message about no domains |
| 128 | + expect(result.stderr).toMatch(/No allowed domains specified|all network access will be blocked/i); |
| 129 | + }, 120000); |
| 130 | + }); |
| 131 | + |
| 132 | + describe('DNS Behavior', () => { |
| 133 | + test('should block DNS resolution for external domains', async () => { |
| 134 | + // DNS lookups should work (we allow DNS traffic), but connecting should fail |
| 135 | + // because the domain isn't in the allowlist |
| 136 | + const result = await runner.runWithSudo( |
| 137 | + 'bash -c "host example.com > /dev/null 2>&1 && curl -f --max-time 5 https://example.com || echo network_blocked"', |
| 138 | + { |
| 139 | + allowDomains: [], |
| 140 | + logLevel: 'debug', |
| 141 | + timeout: 60000, |
| 142 | + } |
| 143 | + ); |
| 144 | + |
| 145 | + // The network request should be blocked |
| 146 | + expect(result.stdout).toContain('network_blocked'); |
| 147 | + }, 120000); |
| 148 | + }); |
| 149 | +}); |
0 commit comments