Commit e8c9d5f
test: add DNS restriction enforcement tests (#1054)
* feat(api-proxy): add structured logging, metrics, and request tracing
- logging.js: structured JSON logging with request IDs (crypto.randomUUID),
sanitizeForLog utility, zero external dependencies
- metrics.js: in-memory counters (requests_total, bytes), histograms
(request_duration_ms with fixed buckets and percentile calculation),
gauges (active_requests, uptime), memory-bounded
- server.js: replace all console.log/error with structured logger,
instrument proxyRequest() with full metrics, add X-Request-ID header
propagation, enhance /health with metrics_summary, add GET /metrics
endpoint on port 10000
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* feat(api-proxy): add sliding window rate limiter with CLI integration
Implement per-provider rate limiting for the API proxy sidecar:
- rate-limiter.js: Sliding window counter algorithm with 1-second
granularity for RPM/bytes and 1-minute granularity for RPH.
Per-provider independence, memory-bounded, fail-open on errors.
- server.js: Rate limit check before each proxyRequest() call.
Returns 429 with Retry-After, X-RateLimit-* headers and JSON body.
Rate limit status added to /health endpoint.
- CLI flags: --rate-limit-rpm, --rate-limit-rph, --rate-limit-bytes-pm,
--no-rate-limit (all require --enable-api-proxy)
- TypeScript: RateLimitConfig interface in types.ts, env var passthrough
in docker-manager.ts, validation in cli.ts
- Test runner: AwfOptions extended with rate limit fields
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* ci: add API proxy unit tests to build workflow
Add Jest devDependency and test script to api-proxy package.json,
and add a CI step in build.yml to run container-level unit tests.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* test: add integration tests for api-proxy observability
Add two integration test files that verify the observability and rate
limiting features work end-to-end with actual Docker containers.
api-proxy-observability.test.ts:
- /metrics endpoint returns valid JSON with counters, histograms, gauges
- /health endpoint includes metrics_summary
- X-Request-ID header in proxy responses
- Metrics increment after API requests
- rate_limits appear in /health
api-proxy-rate-limit.test.ts:
- 429 response when RPM limit exceeded
- Retry-After header in 429 response
- X-RateLimit-* headers in 429 response
- --no-rate-limit flag disables limiting
- Custom RPM reflected in /health
- Rate limit metrics in /metrics after rejection
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* test: extract buildRateLimitConfig and add coverage tests
Refactor rate limit validation into a standalone exported function
that can be tested independently. Adds 12 unit tests covering
defaults, --no-rate-limit, custom values, and validation errors.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* test: add --block-domains integration tests
Add blockDomains option to AwfRunner test fixture and integration tests
for the --block-domains deny-list feature:
- Block specific subdomain while allowing parent domain
- Block takes precedence over allow
- Wildcard blocking patterns (*.github.com)
- Multiple blocked domains
- Debug output verification
Closes #1041
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* test: add DNS restriction enforcement tests
Add integration tests that verify DNS queries to non-whitelisted servers
are actually blocked by the --dns-servers flag, closing a gap where no
test used the dnsServers option in AwfRunner.
New tests verify:
- DNS queries to non-whitelisted servers are blocked
- DNS queries to whitelisted servers succeed
- The --dns-servers flag is passed through to iptables configuration
- Default DNS (8.8.8.8, 8.8.4.4) works without explicit --dns-servers
- Non-default DNS servers are blocked when using defaults
- Cloudflare DNS works when explicitly whitelisted
Closes #1043
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* fix: address review feedback from Copilot
- Fix api-proxy Dockerfile to copy logging.js, metrics.js, rate-limiter.js
- Remove incomplete X-RateLimit headers test (covered by 429 test)
- Remove loose DNS test assertion that always matched "dns-test"
- Add CLI warning when rate limit flags used without --enable-api-proxy
- Fix rate-limiter.js comment to match actual algorithm (rolling window)
- Fix pre-existing cli.test.ts Commander.js parse failure
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* test: add hasRateLimitOptions coverage to fix coverage regression
Extract rate limit option detection into testable hasRateLimitOptions()
function and add unit tests covering all branches.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* test: add validateApiProxyConfig copilot key coverage
Add tests for hasCopilotKey branch that was previously untested,
improving cli.ts line coverage.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* fix: restore rate limit options in awf-runner test helper
The merge with main incorrectly dropped the rate limit options
(rateLimitRpm, rateLimitRph, rateLimitBytesPm, noRateLimit) from
AwfOptions and both run/runWithSudo methods. These are needed by
api-proxy-rate-limit.test.ts on this branch.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>1 parent f113414 commit e8c9d5f
3 files changed
+140
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
351 | 351 | | |
352 | 352 | | |
353 | 353 | | |
354 | | - | |
| 354 | + | |
355 | 355 | | |
356 | 356 | | |
357 | 357 | | |
| |||
1396 | 1396 | | |
1397 | 1397 | | |
1398 | 1398 | | |
1399 | | - | |
1400 | | - | |
| 1399 | + | |
| 1400 | + | |
1401 | 1401 | | |
1402 | 1402 | | |
1403 | | - | |
| 1403 | + | |
| 1404 | + | |
| 1405 | + | |
| 1406 | + | |
| 1407 | + | |
| 1408 | + | |
| 1409 | + | |
| 1410 | + | |
| 1411 | + | |
1404 | 1412 | | |
1405 | 1413 | | |
| 1414 | + | |
1406 | 1415 | | |
1407 | 1416 | | |
1408 | 1417 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
371 | 371 | | |
372 | 372 | | |
373 | 373 | | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
374 | 387 | | |
375 | 388 | | |
376 | 389 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| |||
113 | 113 | | |
114 | 114 | | |
115 | 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 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
0 commit comments