{"id":19829083,"url":"https://github.com/jub0bs/cors","last_synced_at":"2025-08-04T06:37:25.467Z","repository":{"id":229289444,"uuid":"758456165","full_name":"jub0bs/cors","owner":"jub0bs","description":"perhaps the best CORS middleware library for Go","archived":false,"fork":false,"pushed_at":"2025-06-14T07:51:00.000Z","size":473,"stargazers_count":93,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-06-14T08:35:46.046Z","etag":null,"topics":["cors","go","golang-library","http","middleware","server","web-security"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/jub0bs/cors","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jub0bs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"jub0bs"}},"created_at":"2024-02-16T11:08:51.000Z","updated_at":"2025-06-14T07:51:03.000Z","dependencies_parsed_at":"2024-05-08T10:41:29.317Z","dependency_job_id":"450ab128-3ecc-4f43-b961-8c707eae7afd","html_url":"https://github.com/jub0bs/cors","commit_stats":null,"previous_names":["jub0bs/cors"],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/jub0bs/cors","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jub0bs%2Fcors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jub0bs%2Fcors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jub0bs%2Fcors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jub0bs%2Fcors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jub0bs","download_url":"https://codeload.github.com/jub0bs/cors/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jub0bs%2Fcors/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268658128,"owners_count":24285636,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-04T02:00:09.867Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cors","go","golang-library","http","middleware","server","web-security"],"created_at":"2024-11-12T11:17:42.854Z","updated_at":"2025-08-04T06:37:25.456Z","avatar_url":"https://github.com/jub0bs.png","language":"Go","funding_links":["https://github.com/sponsors/jub0bs"],"categories":["Go","Middlewares"],"sub_categories":["Microsoft Word"],"readme":"# jub0bs/cors\n\n[![tag](https://img.shields.io/github/tag/jub0bs/cors.svg)](https://github.com/jub0bs/cors/tags)\n[![Go Version](https://img.shields.io/badge/Go-%3E%3D%201.23-%23007d9c)][go1.23]\n[![Go Reference](https://pkg.go.dev/badge/github.com/jub0bs/cors.svg)](https://pkg.go.dev/github.com/jub0bs/cors)\n[![license](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat)](https://github.com/jub0bs/cors/raw/main/LICENSE)\n[![build](https://github.com/jub0bs/cors/actions/workflows/cors.yml/badge.svg)](https://github.com/jub0bs/cors/actions/workflows/cors.yml)\n[![codecov](https://codecov.io/gh/jub0bs/cors/branch/main/graph/badge.svg?token=N208BHWQTM)](https://app.codecov.io/gh/jub0bs/cors/tree/main)\n[![goreport](https://goreportcard.com/badge/jub0bs/cors)](https://goreportcard.com/report/jub0bs/cors)\n\nA principled [CORS][mdn-cors] middleware library for [Go][golang],\ndesigned to be both easier to use and harder to misuse\nthan existing alternatives.\n\n## About CORS\n\nThe [Same-Origin Policy (SOP)][mdn-sop] is a security mechanism that\nWeb browsers implement to protect their users.\nIn particular, the SOP places some restrictions on cross-origin network access,\nin terms of both sending and reading.\n\n[Cross-Origin Resource Sharing (CORS)][mdn-cors] is a protocol that\nlets servers instruct browsers to relax those restrictions for select clients.\n\n## Features\n\nThis library allows you to configure and build [net/http][net-http] middleware\nthat implement CORS. It distinguishes itself from other CORS middleware\nlibraries by providing the following features:\n\n- [a simple and coherent API][pkgsite-index];\n- [comprehensive documentation][pkgsite];\n- [extensive configuration validation][validation];\n- [programmatic handling of configuration errors][cfgerrors];\n- [safe-by-default middleware][safe];\n- [a useful debug mode][debug];\n- [on-the-fly, concurrency-safe middleware reconfigurability][reconfigurable];\n- [strong performance guarantees][benchmark-results];\n- full compliance with [the Fetch standard][fetch].\n\nDespite all of this library's goodness, here are some valid reasons\nfor favoring libraries like the more popular [rs/cors][rs-cors]:\n\n- You need more flexibility than that afforded by the\n  [origin patterns supported by this library][origin-patterns];\n  but do bear in mind that\n  [excessive flexibility in this regard implies security risks][danger].\n- You want to log a message for every single request processed\n  by your CORS middleware; [but do you really?][logging]\n\n## Installation\n\n```shell\ngo get github.com/jub0bs/cors\n```\n\nThis library requires [Go 1.23.0][go1.23] or above. Note that it only\n[supports][release-policy] the two most recent minor versions of Go.\n\n## Example\n\nThe following program demonstrates how to create a CORS middleware that\n\n- allows anonymous access from [Web origin][web-origin] `https://example.com`,\n- with requests whose method is either `GET` or `POST` (or `HEAD`), and\n- (optionally) with request header `Authorization`,\n\nand how to apply the middleware in question to all the resources accessible\nunder the `/api/` path:\n\n```go\npackage main\n\nimport (\n  \"io\"\n  \"log\"\n  \"net/http\"\n\n  \"github.com/jub0bs/cors\"\n)\n\nfunc main() {\n  mux := http.NewServeMux()\n  mux.HandleFunc(\"GET /hello\", handleHello) // note: not configured for CORS\n\n  // create CORS middleware\n  corsMw, err := cors.NewMiddleware(cors.Config{\n    Origins:        []string{\"https://example.com\"},\n    Methods:        []string{http.MethodGet, http.MethodPost},\n    RequestHeaders: []string{\"Authorization\"},\n  })\n  if err != nil {\n    log.Fatal(err)\n  }\n  corsMw.SetDebug(true) // turn debug mode on (optional)\n\n  api := http.NewServeMux()\n  api.HandleFunc(\"GET  /users\", handleUsersGet)\n  api.HandleFunc(\"POST /users\", handleUsersPost)\n  mux.Handle(\"/api/\", http.StripPrefix(\"/api\", corsMw.Wrap(api))) // note: method-less pattern here\n\n  if err := http.ListenAndServe(\":8080\", mux); err != http.ErrServerClosed {\n    log.Fatal(err)\n  }\n}\n\nfunc handleHello(w http.ResponseWriter, _ *http.Request) {\n  io.WriteString(w, \"Hello, World!\")\n}\n\nfunc handleUsersGet(_ http.ResponseWriter, _ *http.Request) {\n  // omitted\n}\n\nfunc handleUsersPost(_ http.ResponseWriter, _ *http.Request) {\n  // omitted\n}\n```\n\nTry it out yourself by saving this program to a file named `server.go`.\nYou may need to adjust the port number if port 8080 happens to be unavailable\non your machine. Then build and run your server:\n\n```shell\ngo build server.go\n./server\n```\n\nIf no error occurred, the server is now running on `localhost:8080` and\nthe various resources accessible under the `/api/` path are now configured\nfor CORS as desired.\n\nIf you need to handle CORS-configuration errors programmatically,\nsee [package cfgerrors][cfgerrors].\n\n## A note about testing\n\nBe aware that, for performance reasons, CORS middleware produced by this\nlibrary closely adheres to guarantees (provided by [the Fetch standard][fetch])\nabout the format of some CORS headers. In particular, if you wish to write\ntests that exercise CORS middleware via CORS-preflight requests that include an\n[`Access-Control-Request-Headers` header][acrh], keep in mind that you should\nspecify the comma-separated elements in that header value\n\n- in lower case,\n- in lexicographical order,\n- without repetitions.\n\nOtherwise, the CORS middleware will cause preflight to fail.\n\n## Documentation\n\nThe documentation is available on [pkg.go.dev][pkgsite].\n\nMoreover, guidance on how to use this library with popular third-party routers\ncan be found in [jub0bs/cors-examples][cors-examples].\n\n## Code coverage\n\n![coverage](https://codecov.io/gh/jub0bs/cors/branch/main/graphs/sunburst.svg?token=N208BHWQTM)\n\n## Benchmarks\n\nSome benchmarks pitting this library against [rs/cors][rs-cors]\nare available in [jub0bs/cors-benchmarks][cors-benchmarks].\n\n## License\n\nAll source code is covered by the [MIT License][license].\n\n## Additional resources\n\n- [_Fearless CORS: a design philosophy for CORS middleware libraries (and a Go implementation)_][fearless-cors] (blog post)\n- [_jub0bs/cors: a better CORS middleware library for Go_][a-better-cors-lib] (blog post)\n- [_Reconfigurable CORS middleware with jub0bs/cors_][reconfigurable] (blog post)\n- [_Programmatic handling of CORS-configuration errors with jub0bs/cors_][programmatic] (blog post)\n- [_Useful Functional-Options Tricks for Better Libraries_ (GopherCon Europe 2023)][funcopts] (video)\n- [github.com/jub0bs/fcors][fcors] (this library's predecessor)\n\n[a-better-cors-lib]: https://jub0bs.com/posts/2024-04-27-jub0bs-cors-a-better-cors-middleware-library-for-go/\n[acrh]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Request-Headers\n[benchmark-results]: https://github.com/jub0bs/cors-benchmarks#results\n[cfgerrors]: https://pkg.go.dev/github.com/jub0bs/cors/cfgerrors\n[cors-benchmarks]: https://github.com/jub0bs/cors-benchmarks\n[cors-examples]: https://github.com/jub0bs/cors-examples\n[danger]: https://jub0bs.com/posts/2023-02-08-fearless-cors/#disallow-dangerous-origin-patterns\n[debug]: https://jub0bs.com/posts/2024-04-27-jub0bs-cors-a-better-cors-middleware-library-for-go/#debug-mode\n[fcors]: https://github.com/jub0bs/fcors\n[fearless-cors]: https://jub0bs.com/posts/2023-02-08-fearless-cors/\n[fetch]: https://fetch.spec.whatwg.org\n[funcopts]: https://www.youtube.com/watch?v=5uM6z7RnReE\n[go1.23]: https://tip.golang.org/doc/go1.23\n[golang]: https://go.dev/\n[license]: https://github.com/jub0bs/cors/blob/main/LICENSE\n[logging]: https://jub0bs.com/posts/2024-04-27-jub0bs-cors-a-better-cors-middleware-library-for-go/#debug-mode\n[mdn-cors]: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS\n[mdn-sop]: https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy\n[net-http]: https://pkg.go.dev/net/http\n[origin-patterns]: https://pkg.go.dev/github.com/jub0bs/cors#hdr-Origins-Config\n[pkgsite-index]: https://pkg.go.dev/github.com/jub0bs/cors#pkg-index\n[pkgsite]: https://pkg.go.dev/github.com/jub0bs/cors\n[programmatic]: https://jub0bs.com/posts/2025-01-28-programmatic-handling-of-cors-configuration-errors/\n[reconfigurable]: https://jub0bs.com/posts/2024-05-14-reconfigurable-cors-middleware/\n[release-policy]: https://go.dev/doc/devel/release#policy\n[rs-cors]: https://github.com/rs/cors\n[safe]: https://jub0bs.com/posts/2023-02-08-fearless-cors/#10-render-insecure-configurations-impossible\n[validation]: https://jub0bs.com/posts/2023-02-08-fearless-cors/#5-validate-configuration-and-fail-fast\n[web-origin]: https://developer.mozilla.org/en-US/docs/Glossary/Origin\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjub0bs%2Fcors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjub0bs%2Fcors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjub0bs%2Fcors/lists"}