CVE-2023-49803 (published 2023-12-11T23:15Z)

@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.

CVSS v3 base score: 7.5, HIGH (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:H/A:N)

Weakness information:
cwe_id: CWE-346
cwe_name: Origin Validation Error
description: The product does not properly verify that the source of data or communication is valid.
extended_description: 
            
            
            
         
         

Fix information:
repo_url: https://github.com/koajs/cors
commit_details: repo_url: https://github.com/koajs/cors
author: fengmk2
author_date: 2023-12-11 09:46:27+08:00
msg: 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
repository: repo_url: https://github.com/koajs/cors
repo_name: koajs/cors
description: Cross-Origin Resource Sharing(CORS) for koa
repo_language: JavaScript
owner: koajs
file_changes: filename: nodejs.yml
change_type: ModificationType.MODIFY
diff: @@ -12,8 +12,6 @@ on:
     branches:
       - main
       - master
-  schedule:
-    - cron: '0 2 * * *'
 
 jobs:
   build:
@@ -22,7 +20,7 @@ jobs:
     strategy:
       fail-fast: false
       matrix:
-        node-version: [14, 16, 18]
+        node-version: [14, 16, 18, 20]
         os: [ubuntu-latest]
 
     steps:

programming_language: Markdown
filename: README.md
change_type: ModificationType.MODIFY
diff: @@ -1,5 +1,4 @@
-@koa/cors
-=======
+# @koa/cors
 
 [![NPM version][npm-image]][npm-url]
 [![Node.js CI](https://github.com/koajs/cors/actions/workflows/nodejs.yml/badge.svg)](https://github.com/koajs/cors/actions/workflows/nodejs.yml)
@@ -43,7 +42,8 @@ app.use(cors());
  * CORS middleware
  *
  * @param {Object} [options]
- *  - {String|Function(ctx)} origin `Access-Control-Allow-Origin`, default is request Origin header
+ *  - {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`
@@ -57,6 +57,18 @@ app.use(cors());
  */
 ```
 
+## 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') || '*';
+  },
+}));
+```
+
 ## License
 
 [MIT](./LICENSE)

programming_language: Markdown
filename: index.js
change_type: ModificationType.MODIFY
diff: @@ -1,12 +1,11 @@
-'use strict';
-
 const vary = require('vary');
 
 /**
  * CORS middleware
  *
  * @param {Object} [options]
- *  - {String|Function(ctx)} origin `Access-Control-Allow-Origin`, default is request Origin header
+ *  - {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`
@@ -61,9 +60,11 @@ module.exports = function(options) {
     let origin;
     if (typeof options.origin === 'function') {
       origin = await options.origin(ctx);
-      if (!origin) return await next();
+      if (!origin) {
+        return await next();
+      }
     } else {
-      origin = options.origin || requestOrigin;
+      origin = options.origin || '*';
     }
 
     let credentials;

programming_language: JavaScript
filename: package.json
change_type: ModificationType.MODIFY
diff: @@ -46,7 +46,7 @@
     "node": ">= 14.0.0"
   },
   "ci": {
-    "version": "14, 16, 18",
+    "version": "14, 16, 18, 20",
     "os": "linux"
   },
   "author": "fengmk2 <fengmk2@gmail.com> (http://github.com/fengmk2)",

programming_language: JavaScript
filename: cors.test.js
change_type: ModificationType.MODIFY
diff: @@ -1,5 +1,3 @@
-'use strict';
-
 const assert = require('assert');
 const Koa = require('koa');
 const request = require('supertest');
@@ -13,22 +11,19 @@ describe('cors.test.js', function() {
       ctx.body = { foo: 'bar' };
     });
 
-    it('should not set `Access-Control-Allow-Origin` when request Origin header missing', function(done) {
+    it('should set `Access-Control-Allow-Origin` to `*` when request Origin header missing', function(done) {
       request(app.listen())
         .get('/')
         .expect({ foo: 'bar' })
-        .expect(200, function(err, res) {
-          assert(!err);
-          assert(!res.headers['access-control-allow-origin']);
-          done();
-        });
+        .expect('access-control-allow-origin', '*')
+        .expect(200, done);
     });
 
-    it('should set `Access-Control-Allow-Origin` to request origin header', function(done) {
+    it('should set `Access-Control-Allow-Origin` to `*`', function(done) {
       request(app.listen())
         .get('/')
         .set('Origin', 'http://koajs.com')
-        .expect('Access-Control-Allow-Origin', 'http://koajs.com')
+        .expect('Access-Control-Allow-Origin', '*')
         .expect({ foo: 'bar' })
         .expect(200, done);
     });
@@ -38,7 +33,7 @@ describe('cors.test.js', function() {
         .options('/')
         .set('Origin', 'http://koajs.com')
         .set('Access-Control-Request-Method', 'PUT')
-        .expect('Access-Control-Allow-Origin', 'http://koajs.com')
+        .expect('Access-Control-Allow-Origin', '*')
         .expect('Access-Control-Allow-Methods', 'GET,HEAD,PUT,POST,DELETE,PATCH')
         .expect(204, done);
     });
@@ -87,6 +82,44 @@ describe('cors.test.js', function() {
     });
   });
 
+  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);
+    });
+  });
+
   describe('options.secureContext=true', function() {
     const app = new Koa();
     app.use(cors({
@@ -651,7 +684,11 @@ describe('cors.test.js', function() {
   describe('options.headersKeptOnError', function() {
     it('should keep CORS headers after an error', function(done) {
       const app = new Koa();
-      app.use(cors());
+      app.use(cors({
+        origin(ctx) {
+          return ctx.get('Origin') || '*';
+        },
+      }));
       app.use(function(ctx) {
         ctx.body = { foo: 'bar' };
         throw new Error('Whoops!');
@@ -668,7 +705,11 @@ describe('cors.test.js', function() {
 
     it('should not affect OPTIONS requests', function(done) {
       const app = new Koa();
-      app.use(cors());
+      app.use(cors({
+        origin(ctx) {
+          return ctx.get('Origin') || '*';
+        },
+      }));
       app.use(function(ctx) {
         ctx.body = { foo: 'bar' };
         throw new Error('Whoops!');
@@ -684,7 +725,11 @@ describe('cors.test.js', function() {
 
     it('should not keep unrelated headers', function(done) {
       const app = new Koa();
-      app.use(cors());
+      app.use(cors({
+        origin(ctx) {
+          return ctx.get('Origin') || '*';
+        },
+      }));
       app.use(function(ctx) {
         ctx.body = { foo: 'bar' };
         ctx.set('X-Example', 'Value');
@@ -752,6 +797,7 @@ describe('cors.test.js', function() {
         .expect(200, done);
     });
   });
+
   describe('other middleware has set vary header on Error', function() {
     it('should append `Origin to other `Vary` header', function(done) {
       const app = new Koa();

programming_language: JavaScript