-
Config Layer (
internal/config/config.go)- Add
AllowedOriginsfield to Config struct - Implement
validateAllowedOrigins()function - Add validation to
validate()method - Handle wildcard blocking in production/staging
- Enforce HTTPS in production/staging
- Validate origin format (scheme, host, no path/query/fragment)
- Add
-
CORS Package (
internal/cors/cors.go)- Add
Profile.Validate()method - Add
validateOriginFormat()helper - Enhance
ProfileForEnv()with validation - Improve
Middleware()with malformed origin detection - Add comprehensive package documentation
- Implement fail-closed behavior
- Add
-
Test Suite (
internal/cors/cors_test.go)- Add profile validation tests (5 tests)
- Add malformed origin tests (3 tests)
- Add edge case tests (7 tests)
- Add security scenario tests (10+ tests)
- Test case sensitivity
- Test port handling
- Test Vary header behavior
- Test fail-closed behavior
- Achieve >95% coverage target
-
Security Documentation (
internal/cors/SECURITY.md)- Security guarantees section
- Configuration guide
- Attack prevention strategies
- Testing requirements
- Monitoring guidance
- Compliance information
- Migration guide
- Troubleshooting section
-
Developer Guide (
internal/cors/README.md)- Quick start guide
- Configuration examples
- API reference
- Usage examples
- Troubleshooting guide
- Best practices
- Security considerations
-
Implementation Summary (
CORS_HARDENING_SUMMARY.md)- Overview of changes
- Security improvements
- Testing strategy
- Configuration examples
- Migration checklist
- Compliance information
-
Commit Message (
CORS_COMMIT_MESSAGE.txt)- Clear description of changes
- Breaking change notice
- Security improvements list
- Testing details
- Files changed
- Block wildcard (*) in production/staging
- Prevent wildcard + credentials combination
- Prevent wildcard mixed with other origins
- Allow wildcard only in development
- Require scheme (https:// or http://)
- Require host
- Reject origins with paths
- Reject origins with query parameters
- Reject origins with fragments
- Enforce HTTPS in production/staging
- Case-sensitive matching
- Port-specific matching
- Validate origin format before processing
- Reject malformed origins without CORS headers
- Return 403 for disallowed preflight requests
- Only reflect allowlisted origins
- Always set Vary: Origin header
- Handle missing Origin header correctly
- Fail-closed on missing configuration
- Fail-closed on invalid configuration
- Validation errors in config error list
- Environment-specific profiles
- Duplicate origin detection
-
TestProfile_ValidateWildcardWithCredentials -
TestProfile_ValidateDuplicateOrigins -
TestProfile_ValidateInvalidOriginFormat -
TestProfile_ValidateNilProfile -
TestProfile_ValidateValidProfile
-
TestMalformedOrigin_MissingScheme -
TestMalformedOrigin_WithPath -
TestMalformedOrigin_PreflightForbidden
-
TestOrigin_CaseSensitive -
TestOrigin_WithExplicitPort -
TestOrigin_PortMismatch -
TestProd_AllMethodsAllowed -
TestVaryHeader_AlwaysSetEvenForDisallowedOrigin -
TestVaryHeader_SetForNoOrigin -
TestProfileForEnv_InvalidOriginFailsClosed
- Development profile tests (4 tests)
- Production profile tests (6 tests)
- ProfileForEnv tests (4 tests)
- Multiple origins test
- Custom MaxAge test
Total Tests: 30+ tests Expected Coverage: >95%
- Origin Reflection Attack: Only allowlisted origins reflected
- Wildcard + Credentials: Validation prevents combination
- Subdomain Takeover: No wildcard patterns, exact matches only
- Cache Poisoning: Vary: Origin always set
- Path Traversal: Origins with paths rejected
- Case Manipulation: Case-sensitive matching enforced
- Port Confusion: Port-specific matching enforced
- Malformed Origins: Format validation before processing
- CORS Specification: Fetch Standard compliant
- RFC 6454: Web Origin Concept compliant
- OWASP: CORS Security Cheat Sheet aligned
- Credentials + Wildcard: Prohibition enforced
- Preflight Caching: Proper MaxAge handling
- Clear security guarantees documented
- Configuration examples provided
- Attack prevention explained
- Troubleshooting guide included
- Migration guide provided
- API reference complete
- Best practices documented
- Monitoring guidance included
- No syntax errors
- No linting issues
- Comprehensive error handling
- Clear function documentation
- Consistent naming conventions
- Proper error messages
- Type safety maintained
- Run full test suite:
go test ./internal/cors/... -v -cover - Verify >95% coverage
- Run race detector:
go test ./internal/cors/... -race - Run integration tests
- Test with real client applications
- Set
ALLOWED_ORIGINSin staging environment - Set
ALLOWED_ORIGINSin production environment - Verify origin format (HTTPS, no paths)
- Test configuration validation
- Verify fail-closed behavior
- Set up metrics for rejected origins
- Set up alerts for validation failures
- Set up alerts for wildcard in production
- Configure logging for CORS errors
- Test monitoring dashboards
- Update deployment runbooks
- Update operations documentation
- Notify client teams of changes
- Update API documentation
- Create rollback plan
- Review with security team
- Verify attack prevention mechanisms
- Test fail-closed scenarios
- Validate CORS spec compliance
- Review monitoring and alerting
-
Staging Deployment
- Deploy code to staging
- Set
ALLOWED_ORIGINSenvironment variable - Test with staging clients
- Monitor for errors
- Verify CORS headers
-
Production Deployment
- Review staging results
- Set
ALLOWED_ORIGINSin production - Deploy during maintenance window
- Monitor metrics closely
- Verify client functionality
-
Post-Deployment
- Monitor rejected origins
- Check error rates
- Verify client applications work
- Review logs for issues
- Update documentation
If issues occur:
- Revert code changes
- Restore previous CORS configuration
- Monitor for resolution
- Investigate root cause
- Fix and redeploy
- All tests pass
- Coverage >95%
- No syntax errors
- Documentation complete
- Staging tests successful
- Production deployment successful
- No client disruptions
- Monitoring operational
- Breaking change: Requires
ALLOWED_ORIGINSin production/staging - Wildcard blocked in production/staging (security improvement)
- Fail-closed behavior protects against misconfigurations
- Comprehensive test suite ensures reliability
- Documentation supports operations and troubleshooting
- Development: Implementation complete
- Testing: Test suite complete
- Documentation: All docs created
- Security Review: Pending
- Staging: Pending deployment
- Production: Pending deployment