-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathpreflight.ts
More file actions
209 lines (186 loc) · 7.55 KB
/
preflight.ts
File metadata and controls
209 lines (186 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { logger } from '../logger';
import {
loadAwfFileConfig,
mapAwfFileConfigToCliOptions,
applyConfigOptionsInPlaceWithCliPrecedence,
} from '../config-file';
import { validateDomainOrPattern } from '../domain-patterns';
import { loadAndMergeDomains } from '../rules';
import { parseDomains, parseDomainsFile } from '../domain-utils';
import { processLocalhostKeyword } from '../option-parsers';
import { resolveCopilotApiRouting } from '../copilot-api-resolver';
import { resolveApiTargetsToAllowedDomains } from '../api-proxy-config';
/**
* Resolves the Commander option-value source for a given option name.
* Injected to decouple the action handler from the global program instance.
*/
type OptionSourceResolver = (optionName: string) => string | undefined;
/**
* The result produced by {@link resolveAllowedDomains}.
*/
interface AllowedDomainsResult {
allowedDomains: string[];
localhostResult: ReturnType<typeof processLocalhostKeyword>;
resolvedCopilotApiTarget: string | undefined;
resolvedCopilotApiBasePath: string | undefined;
}
/**
* Loads the AWF config file and applies its values to `options` in-place,
* giving CLI flags precedence over config-file values.
*
* Does nothing when `options.config` is not set.
* Calls `process.exit(1)` on parse errors so the caller need not handle them.
*/
export function applyConfigFilePrecedence(
options: Record<string, unknown>,
getOptionValueSource: OptionSourceResolver
): void {
if (!options.config) return;
try {
const fileConfig = loadAwfFileConfig(options.config as string);
const fileDerivedOptions = mapAwfFileConfigToCliOptions(fileConfig);
applyConfigOptionsInPlaceWithCliPrecedence(
options,
fileDerivedOptions,
// Commander marks explicit user flags with source "cli".
// We only apply config values when a flag was not explicitly provided.
(optionName: string) => getOptionValueSource(optionName) === 'cli'
);
} catch (error) {
logger.error(`Error loading --config: ${error instanceof Error ? error.message : String(error)}`);
process.exit(1);
}
}
/**
* Resolves the final set of allowed domains by:
* 1. Parsing `--allow-domains` and `--allow-domains-file` flags
* 2. Merging `--ruleset-file` YAML domains
* 3. Processing the `localhost` keyword
* 4. Resolving Copilot / OpenAI / Anthropic / Gemini API-target domains
* 5. Validating each domain pattern
*
* Side-effects: may mutate `options.enableHostAccess` and `options.allowHostPorts`
* when the `localhost` keyword is detected.
*
* Calls `process.exit(1)` on any validation failure.
*/
export function resolveAllowedDomains(options: Record<string, unknown>): AllowedDomainsResult {
let allowedDomains: string[] = [];
// Parse domains from command-line flag if provided
if (options.allowDomains) {
allowedDomains = parseDomains(options.allowDomains as string);
}
// Parse domains from file if provided
if (options.allowDomainsFile) {
try {
const fileDomainsArray = parseDomainsFile(options.allowDomainsFile as string);
allowedDomains.push(...fileDomainsArray);
} catch (error) {
logger.error(`Failed to read domains file: ${error instanceof Error ? error.message : error}`);
process.exit(1);
}
}
// Merge domains from --ruleset-file YAML files
if (options.rulesetFile && Array.isArray(options.rulesetFile) && options.rulesetFile.length > 0) {
try {
allowedDomains = loadAndMergeDomains(options.rulesetFile as string[], allowedDomains);
} catch (error) {
logger.error(`Failed to load ruleset file: ${error instanceof Error ? error.message : error}`);
process.exit(1);
}
}
// Log when no domains are specified (all network access will be blocked)
if (allowedDomains.length === 0) {
logger.debug('No allowed domains specified - all network access will be blocked');
}
// Remove duplicates (in case domains appear in both sources)
allowedDomains = [...new Set(allowedDomains)];
// Handle special "localhost" keyword for Playwright testing
// This makes localhost testing work out of the box without requiring manual configuration
const localhostResult = processLocalhostKeyword(
allowedDomains,
(options.enableHostAccess as boolean) || false,
options.allowHostPorts as string | undefined
);
if (localhostResult.localhostDetected) {
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');
}
// Auto-configure common dev ports if not already specified
if (localhostResult.defaultPorts) {
options.allowHostPorts = localhostResult.defaultPorts;
logger.info('ℹ️ localhost keyword detected - allowing common development ports (3000, 4200, 5173, 8080, etc.)');
logger.info(' Use --allow-host-ports to customize the port list');
}
}
const {
copilotApiTarget: resolvedCopilotApiTarget,
copilotApiBasePath: resolvedCopilotApiBasePath,
} = resolveCopilotApiRouting(
{ copilotApiTarget: options.copilotApiTarget as string | undefined },
process.env
);
// Automatically add API target values to allowlist when specified
// This ensures that when engine.api-target is set in GitHub Agentic Workflows,
// the target domain is automatically accessible through the firewall
resolveApiTargetsToAllowedDomains(
{
copilotApiTarget: resolvedCopilotApiTarget,
openaiApiTarget: options.openaiApiTarget as string | undefined,
anthropicApiTarget: options.anthropicApiTarget as string | undefined,
geminiApiTarget: options.geminiApiTarget as string | undefined,
},
allowedDomains,
process.env,
logger.debug.bind(logger)
);
// Validate all domains and patterns
for (const domain of allowedDomains) {
try {
validateDomainOrPattern(domain);
} catch (error) {
logger.error(`Invalid domain or pattern: ${error instanceof Error ? error.message : error}`);
process.exit(1);
}
}
return { allowedDomains, localhostResult, resolvedCopilotApiTarget, resolvedCopilotApiBasePath };
}
/**
* Resolves the final set of blocked domains by parsing `--block-domains`
* and `--block-domains-file` flags and validating each pattern.
*
* Calls `process.exit(1)` on any validation failure.
*/
export function resolveBlockedDomains(options: Record<string, unknown>): string[] {
let blockedDomains: string[] = [];
// Parse blocked domains from command-line flag if provided
if (options.blockDomains) {
blockedDomains = parseDomains(options.blockDomains as string);
}
// Parse blocked domains from file if provided
if (options.blockDomainsFile) {
try {
const fileBlockedDomainsArray = parseDomainsFile(options.blockDomainsFile as string);
blockedDomains.push(...fileBlockedDomainsArray);
} catch (error) {
logger.error(`Failed to read blocked domains file: ${error instanceof Error ? error.message : error}`);
process.exit(1);
}
}
// Remove duplicates from blocked domains
blockedDomains = [...new Set(blockedDomains)];
// Validate all blocked domains and patterns
for (const domain of blockedDomains) {
try {
validateDomainOrPattern(domain);
} catch (error) {
logger.error(`Invalid blocked domain or pattern: ${error instanceof Error ? error.message : error}`);
process.exit(1);
}
}
return blockedDomains;
}