{"id":19932742,"url":"https://github.com/mixmaxhq/cors-gate","last_synced_at":"2025-04-10T05:00:34.745Z","repository":{"id":20671432,"uuid":"90573780","full_name":"mixmaxhq/cors-gate","owner":"mixmaxhq","description":"Enforce CORS server-side.","archived":false,"fork":false,"pushed_at":"2023-11-22T16:05:28.000Z","size":194,"stargazers_count":45,"open_issues_count":4,"forks_count":8,"subscribers_count":27,"default_branch":"master","last_synced_at":"2025-04-03T03:12:50.178Z","etag":null,"topics":["connect","corgi-tag","cors","origin"],"latest_commit_sha":null,"homepage":"https://www.mixmax.com/careers","language":"JavaScript","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/mixmaxhq.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-08T01:23:21.000Z","updated_at":"2024-06-21T21:06:07.000Z","dependencies_parsed_at":"2023-11-22T17:38:14.435Z","dependency_job_id":null,"html_url":"https://github.com/mixmaxhq/cors-gate","commit_stats":{"total_commits":42,"total_committers":11,"mean_commits":"3.8181818181818183","dds":0.5952380952380952,"last_synced_commit":"f7e4829fa501b20626f969e9b409fd9037d7b646"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mixmaxhq%2Fcors-gate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mixmaxhq%2Fcors-gate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mixmaxhq%2Fcors-gate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mixmaxhq%2Fcors-gate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mixmaxhq","download_url":"https://codeload.github.com/mixmaxhq/cors-gate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247664593,"owners_count":20975606,"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","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":["connect","corgi-tag","cors","origin"],"created_at":"2024-11-12T23:11:24.862Z","updated_at":"2025-04-10T05:00:34.656Z","avatar_url":"https://github.com/mixmaxhq.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"cors-gate\n=========\n\n[![Build Status](https://travis-ci.org/mixmaxhq/cors-gate.svg?branch=master)](https://travis-ci.org/mixmaxhq/cors-gate)\n\nConnect-compatible middleware to selectively reject requests based on CORS rules.\n\nThis lets you implement an elegant alternative to CSRF tokens if you only need to support\nmodern browsers. For more information, see [our blog post](https://mixmax.com/blog/modern-csrf).\n\nInstall\n-------\n\nRun this in your project:\n\n```sh\n$ npm install cors-gate\n```\n\nTest\n----\n\n```sh\n$ npm test\n```\n\nUsage\n-----\n\n```js\nconst express = require('express');\nconst cors = require('cors');\nconst corsGate = require('cors-gate');\n\nconst app = express();\n\napp.use(cors({\n  origin: ['https://app.mixmax.com', 'https://other-app.mixmax.com'],\n  credentials: true\n}));\n\n// prevent cross-origin requests from domains not permitted by the preceeding cors rules\napp.use(corsGate({\n  // require an Origin header, and reject request if missing\n  strict: true,\n  // permit GET and HEAD requests, even without an Origin header\n  allowSafe: true,\n  // the origin of the server\n  origin: 'https://api.mixmax.com'\n}));\n\n// add a new contact\napp.post('/api/contacts', function(req, res) {\n  // ...\n  res.status(200).json({id: id});\n});\n```\n\n### Alternative failure handling\n\nBy default, `cors-gate` will return `403 Unauthorized` to any requests that aren't permitted by the specified options.\n\nThe `failure` option offers a means to change this behavior. This way, unauthorized cross-origin requests can be permitted in a restricted manner - perhaps by requiring an explicit authentication mechanism rather than cookie-based authentication to prevent cross-site scripting. As such, `cors-gate` can serve as a CSRF mechanism without the need for a token, while still allowing limited forms of third-party cross-origin API requests.\n\n```js\napp.use(corsGate({\n  origin: 'https://api.mixmax.com',\n  failure: function(req, res, next) {\n    // requests from other origins will have this flag set.\n    req.requireExplicitAuthentication = true;\n    next();\n  }\n}));\n```\n\n### Firefox and the Origin header\n\nFirefox does not set the `Origin` header [on same-origin requests](http://stackoverflow.com/a/15514049/495611) (see also [csrf-request-tester](https://github.com/mixmaxhq/csrf-request-tester)) for same-origin requests, as of version 53. The `corsGate.originFallbackToReferrer` middleware will, if the `Origin` header is missing, fill it with the origin part of the `Referer`. This middleware thus enables verification of the `Origin` for same-origin requests.\n\nAdditionally, no browser sends the `Origin` header when sending a `GET` request to load an image. We could simply allow all `GET` requests - `GET` requests are safe, per `HTTP` - but we'd rather reject unauthorized cross-origin `GET` requests wholesale.\n\nAt present, Chrome and Safari do not support the `strict-origin` `Referrer-Policy`, so we can only patch the `Origin` from the `Referer` on Firefox. In patching it, however, we can reject unauthorized cross-origin `GET` requests from images, and once Chrome and Safari support `strict-origin`, we'll be able to do so on all three platforms.\n\nIn order to actually reject these requests, however, the patched `Origin` data must be visible to the `cors` middleware. This middleware is distinct because it must appear before `cors` and `corsGate` to perform all the described tasks.\n\n```js\napp.use(corsGate.originFallbackToReferrer());\napp.use(cors({ ... }));\napp.use(corsGate({ ... }));\n```\n\n### Language ports\n\n- Go: [Roemerb/corsgate](https://github.com/Roemerb/corsgate)\n\nLicense\n-------\n\nThe [MIT License](https://github.com/mixmaxhq/cors-gate/blob/master/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmixmaxhq%2Fcors-gate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmixmaxhq%2Fcors-gate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmixmaxhq%2Fcors-gate/lists"}