Skip to content

Commit

Permalink
Added Vary value for CORS in Frontend
Browse files Browse the repository at this point in the history
refs https:/TryGhost/Toolbox/issues/461

- Having a 'Origin' in vary header value present on each `OPTIONS` allows to correctly bucket "allowed CORS" and "disallowed CORS" responses in shared caches
  • Loading branch information
naz committed Nov 2, 2022
1 parent f581e33 commit a8ba8cc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
17 changes: 16 additions & 1 deletion ghost/core/core/frontend/web/middleware/cors.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,19 @@ function corsOptionsDelegate(req, callback) {
callback(null, corsOptions);
}

module.exports = cors(corsOptionsDelegate);
/**
*
* @param {Express.Request} req
* @param {Express.Response} res
* @param {Function} next
*/
const handleCaching = (req, res, next) => {
// @NOTE: try to add native support for dynamic 'vary' header value in 'cors' module
res.vary('Origin');
next();
};

module.exports = [
cors(corsOptionsDelegate),
handleCaching
];
2 changes: 1 addition & 1 deletion ghost/core/core/server/web/api/middleware/cors.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function corsOptionsDelegate(req, cb) {
}

/**
*
*
* @param {Express.Request} req
* @param {Express.Response} res
* @param {Function} next
Expand Down
4 changes: 2 additions & 2 deletions ghost/core/test/e2e-server/1-options-requests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('OPTIONS requests', function () {
.expect(200);

assert.equal(res.headers['cache-control'], 'public, max-age=0');
assert.equal(res.headers.vary, 'Accept-Encoding');
assert.equal(res.headers.vary, 'Origin, Accept-Encoding');
assert.equal(res.headers.allow, 'POST,GET,HEAD');
});

Expand All @@ -119,7 +119,7 @@ describe('OPTIONS requests', function () {
.expect(200);

assert.equal(res.headers['cache-control'], 'public, max-age=0');
assert.equal(res.headers.vary, 'Accept-Encoding');
assert.equal(res.headers.vary, 'Origin, Accept-Encoding');
assert.equal(res.headers.allow, 'POST,GET,HEAD');
});
});
Expand Down

0 comments on commit a8ba8cc

Please sign in to comment.