CVE-2023-49803 (JavaScript, published 2023-12-11T23:15Z)
Description: value: @koa/cors npm provides Cross-Origin Resource Sharing (CORS) for koa, a web framework for Node.js. Prior to version 5.0.0, the middleware operates in a way that if an allowed origin is not provided, it will return an `Access-Control-Allow-Origin` header with the value of the origin from the request. This behavior completely disables one of the most crucial elements of browsers - the Same Origin Policy (SOP), this could cause a very serious security threat to the users of this middleware. If such behavior is expected, for instance, when middleware is used exclusively for prototypes and not for production applications, it should be heavily emphasized in the documentation along with an indication of the risks associated with such behavior, as many users may not be aware of it. Version 5.0.0 fixes this vulnerability.
Severity: nan (CVSS v3 base score: 7.5)
Weakness: CWE-346 \u2014 Origin Validation Error
The product does not properly verify that the source of data or communication is valid.
Repository: https://github.com/koajs/cors
Files changed:
.github/workflows/nodejs.yml
README.md
index.js
package.json
test/cors.test.js
Commit message: Merge pull request from GHSA-qxrj-hx23-xp82

This is a security update for the Same Origin Policy (SOP),
and also a BREAKING CHANGE.

closes https://github.com/koajs/cors/security/advisories/GHSA-qxrj-hx23-xp82

Vulnerable code:
  schedule:
    - cron: '0 2 * * *'
        node-version: [14, 16, 18]
@koa/cors
=======
 *  - {String|Function(ctx)} origin `Access-Control-Allow-Origin`, default is request Origin header
'use strict';

 *  - {String|Function(ctx)} origin `Access-Control-Allow-Origin`, default is request Origin header
      if (!origin) return await next();
      origin = options.origin || requestOrigin;
    "version": "14, 16, 18",
'use strict';

    it('should not set `Access-Control-Allow-Origin` when request Origin header missing', function(done) {
        .expect(200, function(err, res) {
          assert(!err);
          assert(!res.headers['access-control-allow-origin']);
          done();
        });
    it('should set `Access-Control-Allow-Origin` to request origin header', function(done) {
        .expect('Access-Control-Allow-Origin', 'http://koajs.com')
        .expect('Access-Control-Allow-Origin', 'http://koajs.com')
      app.use(cors());
      app.use(cors());
      app.use(cors());

Fixed code:
        node-version: [14, 16, 18, 20]
# @koa/cors
 *  - {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
## Breaking change between 5.0 and 4.0

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

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

 *  - {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
      if (!origin) {
        return await next();
      }
      origin = options.origin || '*';
    "version": "14, 16, 18, 20",
    it('should set `Access-Control-Allow-Origin` to `*` when request Origin header missing', function(done) {
        .expect('access-control-allow-origin', '*')
        .expect(200, done);
    it('should set `Access-Control-Allow-Origin` to `*`', function(done) {
        .expect('Access-Control-Allow-Origin', '*')
        .expect('Access-Control-Allow-Origin', '*')
  describe('options.origin set the request Origin header', function() {
    const app = new Koa();
    app.use(cors({
      origin(ctx) {
        return ctx.get('Origin') || '*';
      },
    }));
    app.use(function(ctx) {
      ctx.body = { foo: 'bar' };
    });

    it('should set `Access-Control-Allow-Origin` to request `Origin` header', function(done) {
      request(app.listen())
        .get('/')
        .set('Origin', 'http://koajs.com')
        .expect('Access-Control-Allow-Origin', 'http://koajs.com')
        .expect({ foo: 'bar' })
        .expect(200, done);
    });

    it('should set `Access-Control-Allow-Origin` to request `origin` header', function(done) {
      request(app.listen())
        .get('/')
        .set('origin', 'http://origin.koajs.com')
        .expect('Access-Control-Allow-Origin', 'http://origin.koajs.com')
        .expect({ foo: 'bar' })
        .expect(200, done);
    });

    it('should set `Access-Control-Allow-Origin` to `*`, even if no Origin is passed on request', function(done) {
      request(app.listen())
        .get('/')
        .expect('Access-Control-Allow-Origin', '*')
        .expect({ foo: 'bar' })
        .expect(200, done);
    });
  });

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