|
1 | 1 | import { Command } from 'commander'; |
2 | | -import { parseEnvironmentVariables, parseDomains, parseDomainsFile, escapeShellArg, joinShellArgs, parseVolumeMounts, isValidIPv4, isValidIPv6, parseDnsServers, parseDnsOverHttps, validateAgentImage, isAgentImagePreset, AGENT_IMAGE_PRESETS, processAgentImageOption, processLocalhostKeyword, validateSkipPullWithBuildLocal, validateAllowHostPorts, parseMemoryLimit, validateFormat, validateApiProxyConfig, buildRateLimitConfig, validateRateLimitFlags, hasRateLimitOptions, collectRulesetFile, validateApiTargetInAllowedDomains, DEFAULT_OPENAI_API_TARGET, DEFAULT_ANTHROPIC_API_TARGET, DEFAULT_COPILOT_API_TARGET, emitApiProxyTargetWarnings, formatItem, program, parseAgentTimeout, applyAgentTimeout, handlePredownloadAction, resolveApiTargetsToAllowedDomains } from './cli'; |
| 2 | +import { parseEnvironmentVariables, parseDomains, parseDomainsFile, escapeShellArg, joinShellArgs, parseVolumeMounts, isValidIPv4, isValidIPv6, parseDnsServers, parseDnsOverHttps, validateAgentImage, isAgentImagePreset, AGENT_IMAGE_PRESETS, processAgentImageOption, processLocalhostKeyword, validateSkipPullWithBuildLocal, validateAllowHostPorts, parseMemoryLimit, validateFormat, validateApiProxyConfig, buildRateLimitConfig, validateRateLimitFlags, hasRateLimitOptions, collectRulesetFile, validateApiTargetInAllowedDomains, DEFAULT_OPENAI_API_TARGET, DEFAULT_ANTHROPIC_API_TARGET, DEFAULT_COPILOT_API_TARGET, emitApiProxyTargetWarnings, formatItem, program, parseAgentTimeout, applyAgentTimeout, handlePredownloadAction, resolveApiTargetsToAllowedDomains, extractGhesDomainsFromEngineApiTarget } from './cli'; |
3 | 3 | import { redactSecrets } from './redact-secrets'; |
4 | 4 | import * as fs from 'fs'; |
5 | 5 | import * as path from 'path'; |
@@ -2175,4 +2175,87 @@ describe('cli', () => { |
2175 | 2175 | expect(hasRateLimitOptions({ rateLimit: true })).toBe(false); |
2176 | 2176 | }); |
2177 | 2177 | }); |
| 2178 | + |
| 2179 | + describe('extractGhesDomainsFromEngineApiTarget', () => { |
| 2180 | + it('should return empty array when ENGINE_API_TARGET is not set', () => { |
| 2181 | + const domains = extractGhesDomainsFromEngineApiTarget({}); |
| 2182 | + expect(domains).toEqual([]); |
| 2183 | + }); |
| 2184 | + |
| 2185 | + it('should extract GHES domains from api.github.* format', () => { |
| 2186 | + const env = { ENGINE_API_TARGET: 'https://api.github.mycompany.com' }; |
| 2187 | + const domains = extractGhesDomainsFromEngineApiTarget(env); |
| 2188 | + expect(domains).toContain('github.mycompany.com'); |
| 2189 | + expect(domains).toContain('api.github.mycompany.com'); |
| 2190 | + expect(domains).toContain('api.githubcopilot.com'); |
| 2191 | + expect(domains).toContain('api.enterprise.githubcopilot.com'); |
| 2192 | + expect(domains).toContain('telemetry.enterprise.githubcopilot.com'); |
| 2193 | + }); |
| 2194 | + |
| 2195 | + it('should handle non-api.* hostnames', () => { |
| 2196 | + const env = { ENGINE_API_TARGET: 'https://github.mycompany.com' }; |
| 2197 | + const domains = extractGhesDomainsFromEngineApiTarget(env); |
| 2198 | + expect(domains).toContain('github.mycompany.com'); |
| 2199 | + expect(domains).toContain('api.githubcopilot.com'); |
| 2200 | + expect(domains).toContain('api.enterprise.githubcopilot.com'); |
| 2201 | + expect(domains).toContain('telemetry.enterprise.githubcopilot.com'); |
| 2202 | + }); |
| 2203 | + |
| 2204 | + it('should handle invalid URL gracefully', () => { |
| 2205 | + const env = { ENGINE_API_TARGET: 'not-a-valid-url' }; |
| 2206 | + const domains = extractGhesDomainsFromEngineApiTarget(env); |
| 2207 | + expect(domains).toEqual([]); |
| 2208 | + }); |
| 2209 | + |
| 2210 | + it('should always include Copilot API domains for GHES', () => { |
| 2211 | + const env = { ENGINE_API_TARGET: 'https://api.github.enterprise.local' }; |
| 2212 | + const domains = extractGhesDomainsFromEngineApiTarget(env); |
| 2213 | + expect(domains).toContain('api.githubcopilot.com'); |
| 2214 | + expect(domains).toContain('api.enterprise.githubcopilot.com'); |
| 2215 | + expect(domains).toContain('telemetry.enterprise.githubcopilot.com'); |
| 2216 | + }); |
| 2217 | + }); |
| 2218 | + |
| 2219 | + describe('resolveApiTargetsToAllowedDomains with GHES', () => { |
| 2220 | + it('should auto-add GHES domains when ENGINE_API_TARGET is set', () => { |
| 2221 | + const domains: string[] = ['github.com']; |
| 2222 | + const env = { ENGINE_API_TARGET: 'https://api.github.mycompany.com' }; |
| 2223 | + resolveApiTargetsToAllowedDomains({}, domains, env); |
| 2224 | + expect(domains).toContain('github.mycompany.com'); |
| 2225 | + expect(domains).toContain('api.github.mycompany.com'); |
| 2226 | + expect(domains).toContain('api.githubcopilot.com'); |
| 2227 | + expect(domains).toContain('api.enterprise.githubcopilot.com'); |
| 2228 | + expect(domains).toContain('telemetry.enterprise.githubcopilot.com'); |
| 2229 | + }); |
| 2230 | + |
| 2231 | + it('should not duplicate GHES domains if already in allowlist', () => { |
| 2232 | + const domains: string[] = ['github.mycompany.com', 'api.githubcopilot.com']; |
| 2233 | + const env = { ENGINE_API_TARGET: 'https://api.github.mycompany.com' }; |
| 2234 | + resolveApiTargetsToAllowedDomains({}, domains, env); |
| 2235 | + const ghesCount = domains.filter(d => d === 'github.mycompany.com').length; |
| 2236 | + const copilotCount = domains.filter(d => d === 'api.githubcopilot.com').length; |
| 2237 | + expect(ghesCount).toBe(1); |
| 2238 | + expect(copilotCount).toBe(1); |
| 2239 | + }); |
| 2240 | + |
| 2241 | + it('should combine GHES domains with API target domains', () => { |
| 2242 | + const domains: string[] = []; |
| 2243 | + const env = { ENGINE_API_TARGET: 'https://api.github.mycompany.com' }; |
| 2244 | + resolveApiTargetsToAllowedDomains( |
| 2245 | + { copilotApiTarget: 'custom.copilot.com' }, |
| 2246 | + domains, |
| 2247 | + env |
| 2248 | + ); |
| 2249 | + // GHES domains |
| 2250 | + expect(domains).toContain('github.mycompany.com'); |
| 2251 | + expect(domains).toContain('api.github.mycompany.com'); |
| 2252 | + // Copilot API domains |
| 2253 | + expect(domains).toContain('api.githubcopilot.com'); |
| 2254 | + expect(domains).toContain('api.enterprise.githubcopilot.com'); |
| 2255 | + expect(domains).toContain('telemetry.enterprise.githubcopilot.com'); |
| 2256 | + // Custom API target |
| 2257 | + expect(domains).toContain('custom.copilot.com'); |
| 2258 | + expect(domains).toContain('https://custom.copilot.com'); |
| 2259 | + }); |
| 2260 | + }); |
2178 | 2261 | }); |
0 commit comments