|
| 1 | +# ADR-023: Wildcard Subdomain Support |
| 2 | + |
| 3 | +## Status |
| 4 | + |
| 5 | +Accepted |
| 6 | + |
| 7 | +## Context |
| 8 | + |
| 9 | +The Claude Nexus Proxy needs to support organizations with many subdomains (e.g., `api.staging.example.com`, `api.prod.example.com`, `api.dev.example.com`) without requiring separate credential files for each subdomain. This is particularly important for: |
| 10 | + |
| 11 | +1. Large organizations with environment-based subdomains |
| 12 | +2. Multi-tenant SaaS applications with customer-specific subdomains |
| 13 | +3. Development teams using feature branch deployments |
| 14 | + |
| 15 | +Currently, each subdomain requires its own credential file, leading to: |
| 16 | + |
| 17 | +- Credential proliferation and management overhead |
| 18 | +- Increased risk of configuration drift |
| 19 | +- Difficulty in rotating API keys across environments |
| 20 | + |
| 21 | +## Decision |
| 22 | + |
| 23 | +We will implement wildcard subdomain support using a prefix-based approach that is cross-platform compatible: |
| 24 | + |
| 25 | +### 1. Wildcard Credential Files |
| 26 | + |
| 27 | +- Use `_wildcard.` prefix for wildcard credential files |
| 28 | +- Example: `_wildcard.example.com.credentials.json` matches `*.example.com` |
| 29 | +- This avoids filesystem issues with `*` character on Windows |
| 30 | + |
| 31 | +### 2. Resolution Order |
| 32 | + |
| 33 | +Credentials are resolved in order of specificity: |
| 34 | + |
| 35 | +1. Exact match: `api.staging.example.com.credentials.json` |
| 36 | +2. Most specific wildcard: `_wildcard.staging.example.com.credentials.json` |
| 37 | +3. Less specific wildcard: `_wildcard.example.com.credentials.json` |
| 38 | +4. No match: fallback to default behavior |
| 39 | + |
| 40 | +### 3. Security Boundaries |
| 41 | + |
| 42 | +- Integrate Public Suffix List (PSL) to prevent privilege escalation |
| 43 | +- Wildcard matching stops at registrable domain boundaries |
| 44 | +- Example: `_wildcard.co.uk.credentials.json` will NOT match `example.co.uk` |
| 45 | + |
| 46 | +### 4. Feature Flag Control |
| 47 | + |
| 48 | +- Environment variable: `CNP_WILDCARD_CREDENTIALS` |
| 49 | +- Values: |
| 50 | + - `true`: Enable wildcard support |
| 51 | + - `false`: Disable (default) |
| 52 | + - `shadow`: Log matches without changing behavior |
| 53 | + |
| 54 | +### 5. Performance Optimization |
| 55 | + |
| 56 | +- TTL-based caching for credential resolution |
| 57 | +- Cache both positive and negative results |
| 58 | +- Environment variable: `CNP_RESOLUTION_CACHE_TTL` (default: 5 minutes) |
| 59 | +- Maximum cache size: 10,000 entries with LRU eviction |
| 60 | + |
| 61 | +### 6. Domain Normalization |
| 62 | + |
| 63 | +- Convert to lowercase |
| 64 | +- Remove ports |
| 65 | +- Handle Internationalized Domain Names (IDN) with punycode |
| 66 | +- Remove trailing dots |
| 67 | +- Collapse consecutive dots |
| 68 | + |
| 69 | +## Consequences |
| 70 | + |
| 71 | +### Positive |
| 72 | + |
| 73 | +- **Simplified credential management**: One file can serve multiple subdomains |
| 74 | +- **Reduced configuration drift**: Centralized credential updates |
| 75 | +- **Better security**: PSL integration prevents unauthorized access |
| 76 | +- **Cross-platform compatibility**: Works on all operating systems |
| 77 | +- **Performance**: Caching reduces filesystem operations |
| 78 | +- **Backward compatible**: Existing configurations continue to work |
| 79 | +- **Safe rollout**: Shadow mode allows testing without risk |
| 80 | + |
| 81 | +### Negative |
| 82 | + |
| 83 | +- **Added complexity**: More code paths to maintain |
| 84 | +- **Memory usage**: Cache requires memory (bounded at 10K entries) |
| 85 | +- **PSL dependency**: Requires maintaining PSL library |
| 86 | +- **Learning curve**: Administrators need to understand wildcard behavior |
| 87 | + |
| 88 | +### Neutral |
| 89 | + |
| 90 | +- **Explicit wildcards**: Requires `_wildcard.` prefix (not automatic) |
| 91 | +- **Cache invalidation**: Manual cache clear may be needed for immediate updates |
| 92 | + |
| 93 | +## Implementation Details |
| 94 | + |
| 95 | +### Key Components |
| 96 | + |
| 97 | +1. **AuthenticationService**: Enhanced with wildcard resolution logic |
| 98 | +2. **Domain normalization**: Handles IDN, ports, and edge cases |
| 99 | +3. **PSL integration**: Prevents privilege escalation attacks |
| 100 | +4. **Resolution cache**: Map with TTL-based expiration |
| 101 | +5. **Feature flags**: Safe rollout with shadow mode |
| 102 | + |
| 103 | +### Configuration Examples |
| 104 | + |
| 105 | +``` |
| 106 | +# Wildcard for all staging subdomains |
| 107 | +_wildcard.staging.example.com.credentials.json |
| 108 | +
|
| 109 | +# Wildcard for all subdomains of example.com |
| 110 | +_wildcard.example.com.credentials.json |
| 111 | +
|
| 112 | +# Exact match (takes precedence) |
| 113 | +api.staging.example.com.credentials.json |
| 114 | +``` |
| 115 | + |
| 116 | +### Environment Variables |
| 117 | + |
| 118 | +- `CNP_WILDCARD_CREDENTIALS`: Enable/disable feature |
| 119 | +- `CNP_RESOLUTION_CACHE_TTL`: Cache TTL in milliseconds |
| 120 | +- `CNP_DEBUG_RESOLUTION`: Enable debug logging |
| 121 | + |
| 122 | +## References |
| 123 | + |
| 124 | +- [Public Suffix List](https://publicsuffix.org/) |
| 125 | +- [RFC 3490: Internationalizing Domain Names in Applications (IDNA)](https://tools.ietf.org/html/rfc3490) |
| 126 | +- Original implementation plan: `/PLAN.md` |
0 commit comments