Giter Club home page Giter Club logo

cors's Introduction

@koa/cors

NPM version Node.js CI Test coverage npm download

Cross-Origin Resource Sharing(CORS) for koa

Installation

$ npm install @koa/cors --save

Quick start

Enable cors with default options:

  • origin: * (v4 and before: the request's Origin header). This means that by default the requests from all origin webpages will be allowed. If you're running a generic API server, this is what you want, but otherwise you should look into changing the default to something more suitable to your application.
  • allowMethods: GET,HEAD,PUT,POST,DELETE,PATCH
const Koa = require('koa');
const cors = require('@koa/cors');

const app = new Koa();
app.use(cors());

cors(options)

/**
 * CORS middleware
 *
 * @param {Object} [options]
 *  - {String|Function(ctx)} origin `Access-Control-Allow-Origin`, default is '*'
 *    If `credentials` set and return `true, the `origin` default value will set to the request `Origin` header
 *  - {String|Array} allowMethods `Access-Control-Allow-Methods`, default is 'GET,HEAD,PUT,POST,DELETE,PATCH'
 *  - {String|Array} exposeHeaders `Access-Control-Expose-Headers`
 *  - {String|Array} allowHeaders `Access-Control-Allow-Headers`
 *  - {String|Number} maxAge `Access-Control-Max-Age` in seconds
 *  - {Boolean|Function(ctx)} credentials `Access-Control-Allow-Credentials`, default is false.
 *  - {Boolean} keepHeadersOnError Add set headers to `err.header` if an error is thrown
 *  - {Boolean} secureContext `Cross-Origin-Opener-Policy` & `Cross-Origin-Embedder-Policy` headers.', default is false
 *  - {Boolean} privateNetworkAccess handle `Access-Control-Request-Private-Network` request by return `Access-Control-Allow-Private-Network`, default to false
 * @return {Function} cors middleware
 * @api public
 */

Breaking change between 4.0 and 5.0

The default origin is set to *, if you want to keep the 4.0 behavior, you can set the origin handler like this:

app.use(cors({
  origin(ctx) {
    return ctx.get('Origin') || '*';
  },
}));

License

MIT

Contributors


fengmk2


dead-horse


omsmith


jonathanong


AlphaWong


cma-skedulo


CleberRossi


erikfried


j-waaang


ltomes


lfreneda


matthewmueller


PlasmaPower


swain


TyrealHu


xg-wang


lishengzxc


mcohen75

This project follows the git-contributor spec, auto updated at Sat Oct 08 2022 21:35:10 GMT+0800.

cors's People

Contributors

alphawong avatar cleberrossi avatar cma-skedulo avatar dead-horse avatar erikfried avatar fengmk2 avatar j-waaang avatar jonathanong avatar julienw avatar lfreneda avatar lishengzxc avatar ltomes avatar matthewmueller avatar mcohen75 avatar omsmith avatar plasmapower avatar swain avatar tyrealhu avatar xg-wang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cors's Issues

Header not set on error response

Hi,

I have a situation where when I receive a response with an error status (in this case status 416), there are no CORS headers present on the response. It works fine with otherwise. Any idea why this could be?

Cheers,

Nick

Make it possible to permit CORS requests for multiple origins

Hi,
Loving the package so far! Well done.

The following code can be used to specify a specific origin for CORS using kcors:

var cors = require('kcors');

// Configure cross-origin requests from localhost:3000
var config = {origin: 'http://localhost:3000'};

app.use(cors(config));

It would be great if we could specify more than one origin via an array:

var config = {origin: ['http://localhost:3000', 'http://mydomain.com']};

app.use(cors(config));

It's possible to specify multiple hosts using separate Access-Control-Allow-Origin headers, e.g:

Access-Control-Allow-Origin: http://localhost:3000
Access-Control-Allow-Origin: http://mydomain.com

Can I submit a PR to add support for this?

Thanks!

breaking change in 1.2.0

In 677833d an unconditional yield next; has been added at the end of the middleware. It is breaking preflight requests on our servers because in earlier versions the middleware chain stopped at this.status = 204.

Allow not returning any access control headers if the Origin is not allowed to access the resource

Describe the feature

At the moment the library does not allow handling the scenario in which the origin is not allowed to access the resource: it expects options. origin to always return a string. That leads to developers having to come up with a "workaround" configuration (for example here or here) which is not ideal:

If the request Origin is not allowed, the middleware should be able to respond to pre-flight requests immediately with no Access-Control headers being returned at all.

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.

Docs: Installation modify

original:
$ npm install @koa/cors@2 --save

new:

v2 latest:
$ npm install @koa/cors@2 --save

v3
$ npm install @koa/cors@3 --save

Should default options allow null origin?

When using the default options, as the docs describe, without defining an origin, the response will set Access-Control-Allow-Origin to the request Origin header.

Enable cors with default options:
  origin: request Origin header

But if hit with a request that sets Origin: null, then the response will be Access-Control-Allow-Origin: null.

There's a lot of sources that say don't do this...
https://w3c.github.io/webappsec-cors-for-developers/#avoid-returning-access-control-allow-origin-null

I'm not really an expert in this area, but would there be a better default behavior than..

// https://github.com/koajs/cors/blob/master/index.js#L66
origin = options.origin || requestOrigin;

... if the requestOrigin were null?

Make spec-compliant

Please make this module spec-compliant :) An example where it is not that I noticed right away is that this module considers any request with an Origin request header to be a CORS request--this is actually not the indicator of what is or isn't a CORS request. You can find more within the spec.

The biggest failure right now is this module doesn't correctly handle pre flight requests, as it makes a mistakes in the processing of section 6.2 (http://www.w3.org/TR/cors/#resource-preflight-requests), step 3:

If there is no Access-Control-Request-Method header or if parsing failed, do not set any additional headers and terminate this set of steps. The request is outside the scope of this specification.

If I send an OPTIONS request with an Origin request header and no Access-Control-Request-Method request header, this module still adds CORS response headers.

You can find the spec here: http://www.w3.org/TR/cors/

The default CORS configuration poses a security risk.

I don't understand why the latest commit sets the default Access-Control-Allow-Origin to *. This makes no difference to setting it as the request Origin, as both will break the browser's same-origin policy restrictions. The safest option is to default to empty, letting users specify the origin value themselves.

f31dac9

How to return dynamic origin?

String|Function(ctx)} origin Access-Control-Allow-Origin, default is request Origin header
what is Function(ctx) expect return value?
I tried
origin:ctx=>ctx.request.origin
It doesn't work.

Overly permissive origin policy

Describe the bug
While scanning my React.js application's manifest file using Vulert for vulnerability checks, I identified an issue associated with your package.

Reference
Upon conducting a vulnerability scan, the following references were identified:
Vulert Scan Report: Vulert Report
CVE Reference: CVE-2023-49803

should preflight request return 204 instead of 404 when origin not match?

Background

If a browser sends an OPTIONS request, but origin it not match, now koajs/cors will return 404 status code, due to https://github.com/koajs/cors/blob/master/index.js#L60

then the browser will warn 404, some user will confuse if they need to register an OPTION router.

Discuss

should it return 204 here?

I don't find any clear description at SPEC:

https://www.w3.org/TR/cors 7.1.5 Cross-Origin Request with Preflight

and according to spec below, OPTIONS should never return 404.

and Express will return 204, https://github.com/expressjs/cors/blob/master/lib/index.js#L178

Vary: Origin header breaks CDN

I am using koa/cors for a REST API I'm developing. I noticed that setting up a CDN didn't reduce the number of requests, so I started checking everything and eventually found that cors adds the Vary: Origin header, which was preventing the CDN from working.

I don't know a lot about http headers and CORS internal workings so I just solved it by adding ctx.remove('Vary') when setting Cache-Control header. Maybe I'm doing something wrong but with that everything seems to work fine. I'd appreciate any guidance.

Thanks

warning using koa@next

Mon, 26 Dec 2016 18:51:50 GMT koa deprecated Support for generators will been removed in v3. See the documentation for examples of how to convert old middleware https://github.com/koajs/koa/tree/v2.x#old-signature-middleware-v1x at src/app.js:18:4

How to set CORS of static resources

this my config

app.use(convert(require('koa-static')(__dirname + '/public')))
app.use(cors({
  origin: function (ctx) {
    return '*'; 
  },
  exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
  maxAge: 5,
  credentials: true,
  allowMethods: ['GET', 'POST', 'DELETE'],
  allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
}))

Error reporting when I access files under public
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

why set status of `options` for Preflight Request 204

I read the code and felt puzzle about setting the status 204 forever for the result of optionsrequest for Preflight Request. I thought it should be controlled by other components like a real backend( my one is django)

It would be greate if you can reply to give a hand. πŸ‘

ctx.throw || ctx.assert doesn't send the headers back

When using ctx.throw or ctx.assert the cors headers are not sent back. Maybe I'm using it wrong but any help would be welcome :).

example:

router.post('/', (ctx) => {
  let {name, jurisdiction, parentTitle} = ctx.request.body;
  const project = ctx.request.body;

  ctx.assert(name && jurisdiction && parentTitle, 400);

Thanks!

Access-Control-Allow-Origin set to '*' even when 'Origin' is supplied

The recent breaking change to fix this advisory fixes the scenario where an origin is not specified but it breaks the scenario where it is.

If an Origin is supplied in the request, the spec states the server "must return the origin for the specific client making the request" (i.e. the origin passed in). All my apps and test suites are configured to expect this behaviour, however this library now returns * instead.

Could you confirm that was the intended behaviour, to ignore the incoming request origin in all scenarios?

Integrate with @types/koa__cors

Hi

I want this project doesn't require additional typing library(which is @types/koa__cors) when coding in TypeScript. This could be solved by adding .d.ts file. Any ideas?

npm audit reveals security vulnerabilities

Β» npm audit

                       === npm audit security report ===

# Run  npm install --save-dev [email protected]  to resolve 2 vulnerabilities
SEMVER WARNING: Recommended action is a potentially breaking change
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Low           β”‚ Regular Expression Denial of Service                         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Package       β”‚ debug                                                        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Dependency of β”‚ mocha [dev]                                                  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Path          β”‚ mocha > debug                                                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ More info     β”‚ https://nodesecurity.io/advisories/534                       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜


β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Critical      β”‚ Command Injection                                            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Package       β”‚ growl                                                        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Dependency of β”‚ mocha [dev]                                                  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Path          β”‚ mocha > growl                                                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ More info     β”‚ https://nodesecurity.io/advisories/146                       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜


found 2 vulnerabilities (1 low, 1 critical) in 3653 scanned packages
  2 vulnerabilities require semver-major dependency updates.

How to set multiple domains ?

I tried

app.use(cors({
  origin: function (ctx) {
    if (['https://www.example1.com', 'https://www.example2.com'].includes(ctx.get('Origin'))) {
      return ctx.get('Origin');
    }
    return null;
  }
}));

But it doesn't work, any solution ?

Incorrect version tagged as latest

This morning I ran npm outdated this morning and noticed something funny with kcors:

screen shot 2017-09-14 at 12 27 24 pm

An older version has been published as @latest on npm:

screen shot 2017-09-14 at 12 25 41 pm

please bump version

in https://www.npmjs.com/package/@koa/cors , it README shows

{String|Function(ctx)} origin Access-Control-Allow-Origin, default is '*'

But in github, it README shows

- {String|Function(ctx)} origin Access-Control-Allow-Origin, default is request Origin header

github version is actual behavior

republish as @koa/cors?

might be better than kcors, especially since there's a koa-cors that is no longer maintained

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.