A configurable Go net/http handler for handling Cross-Origin Resource Sharing (CORS) requests.
rs/cors is a Go package that provides a net/http handler implementing the Cross-Origin Resource Sharing (CORS) W3 specification. It enables secure cross-origin requests and data transfers between browsers and servers, which is essential for modern web applications that interact with APIs from different domains.
Go developers building HTTP servers or APIs that need to handle cross-origin requests from web browsers, especially those using frameworks like Gin, Chi, Gorilla, or Buffalo.
Developers choose rs/cors for its strict compliance with the W3 CORS specification, security-focused defaults like protection against unsafe wildcard configurations with credentials, and seamless integration with popular Go web frameworks with minimal performance overhead.
Go net/http configurable handler to handle CORS requests
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Strictly adheres to the W3 CORS specification with built-in protections, such as preventing unsafe configurations like allowing credentials with wildcard origins, as highlighted in the security section.
Seamlessly works with popular Go web frameworks like Gin, Chi, and Gorilla, with dedicated code examples provided in the README for easy adoption.
Benchmark results show low-latency operations (e.g., 51.40 ns/op for default setup) with zero allocations, ensuring minimal overhead in high-throughput applications.
Offers extensive parameters including allowed origins, methods, headers, and custom validation functions like AllowOriginVaryRequestFunc for dynamic CORS policies.
The 'AllowOriginRequestFunc' is deprecated in favor of 'AllowOriginVaryRequestFunc', which may cause breaking changes or confusion in legacy codebases, as noted in the parameters section.
Exclusively for Go projects, limiting usability in polyglot environments or for teams needing cross-language CORS solutions.
Setting up advanced features like wildcard origins with custom validation can be intricate and error-prone, as indicated by the performance penalty and security caveats in the documentation.