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
46 changes: 11 additions & 35 deletions src/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,36 @@
import { Command } from 'commander';
import { parseDomains } from './cli';

describe('cli', () => {
describe('domain parsing', () => {
it('should split comma-separated domains correctly', () => {
const allowDomainsInput = 'github.com, api.github.com, npmjs.org';
const result = parseDomains('github.com, api.github.com, npmjs.org');

const domains = allowDomainsInput
.split(',')
.map(d => d.trim())
.filter(d => d.length > 0);

expect(domains).toEqual(['github.com', 'api.github.com', 'npmjs.org']);
expect(result).toEqual(['github.com', 'api.github.com', 'npmjs.org']);
});

it('should handle domains without spaces', () => {
const allowDomainsInput = 'github.com,api.github.com,npmjs.org';

const domains = allowDomainsInput
.split(',')
.map(d => d.trim())
.filter(d => d.length > 0);
const result = parseDomains('github.com,api.github.com,npmjs.org');

expect(domains).toEqual(['github.com', 'api.github.com', 'npmjs.org']);
expect(result).toEqual(['github.com', 'api.github.com', 'npmjs.org']);
});

it('should filter out empty domains', () => {
const allowDomainsInput = 'github.com,,, api.github.com, ,npmjs.org';

const domains = allowDomainsInput
.split(',')
.map(d => d.trim())
.filter(d => d.length > 0);
const result = parseDomains('github.com,,, api.github.com, ,npmjs.org');

expect(domains).toEqual(['github.com', 'api.github.com', 'npmjs.org']);
expect(result).toEqual(['github.com', 'api.github.com', 'npmjs.org']);
});

it('should return empty array for whitespace-only input', () => {
const allowDomainsInput = ' , , ';
const result = parseDomains(' , , ');

const domains = allowDomainsInput
.split(',')
.map(d => d.trim())
.filter(d => d.length > 0);

expect(domains).toEqual([]);
expect(result).toEqual([]);
});

it('should handle single domain', () => {
const allowDomainsInput = 'github.com';

const domains = allowDomainsInput
.split(',')
.map(d => d.trim())
.filter(d => d.length > 0);
const result = parseDomains('github.com');

expect(domains).toEqual(['github.com']);
expect(result).toEqual(['github.com']);
});
});

Expand Down
22 changes: 17 additions & 5 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ import {
cleanupFirewallNetwork,
} from './host-iptables';

/**
* Parses a comma-separated list of domains into an array of trimmed, non-empty domain strings
* @param input - Comma-separated domain string (e.g., "github.com, api.github.com, npmjs.org")
* @returns Array of trimmed domain strings with empty entries filtered out
*/
export function parseDomains(input: string): string[] {
return input
.split(',')
.map(d => d.trim())
.filter(d => d.length > 0);
}

/**
* Redacts sensitive information from command strings
*/
Expand Down Expand Up @@ -97,10 +109,7 @@ program

logger.setLevel(logLevel);

const allowedDomains = options.allowDomains
.split(',')
.map((d: string) => d.trim())
.filter((d: string) => d.length > 0);
const allowedDomains = parseDomains(options.allowDomains);

if (allowedDomains.length === 0) {
logger.error('At least one domain must be specified with --allow-domains');
Expand Down Expand Up @@ -224,4 +233,7 @@ program
}
});

program.parse();
// Only parse arguments if this file is run directly (not imported as a module)
if (require.main === module) {
program.parse();
}
Loading