Skip to content

Node.js based forum software built for the modern web

fix: prevent caching of responses carrying a CSRF token (#14787)

palmoni547e385837b2d
diff --git a/src/controllers/api.js b/src/controllers/api.js
index e000d9db2e..332573a375 100644
--- a/src/controllers/api.js
+++ b/src/controllers/api.js
@@ -152,6 +152,7 @@ apiController.loadConfig = async function (req) {
apiController.getConfig = async function (req, res) {
const config = await apiController.loadConfig(req);
+ res.set('cache-control', 'private, no-cache');
res.json(config);
};
diff --git a/src/middleware/render.js b/src/middleware/render.js
index 41e72b70cb..5ec01fd369 100644
--- a/src/middleware/render.js
+++ b/src/middleware/render.js
@@ -47,9 +47,7 @@ module.exports = function (middleware) {
options.url = options.url || (req.baseUrl + req.path.replace(/^\/api/, ''));
options.bodyClass = helpers.buildBodyClass(req, res, options);
- if (req.loggedIn) {
- res.set('cache-control', 'private');
- }
+ res.set('cache-control', 'private, no-cache');
const buildResult = await plugins.hooks.fire(`filter:${template}.build`, {
req: req,
diff --git a/test/middleware.js b/test/middleware.js
index 5941488d94..624fd884f7 100644
--- a/test/middleware.js
+++ b/test/middleware.js
@@ -122,7 +122,7 @@ describe('Middlewares', () => {
assert(!Object.keys(response.headers).includes('cache-control'));
});
- it('should be set to "private" on non-existent routes, for logged in users', async () => {
+ it('should be set to "private, no-cache" on non-existent routes, for logged in users', async () => {
const { response } = await request.get(`${nconf.get('url')}/${utils.generateUUID()}`, {
jar,
headers: {
@@ -131,38 +131,42 @@ describe('Middlewares', () => {
});
assert.strictEqual(response.statusCode, 404);
- assert(Object.keys(response.headers).includes('cache-control'));
- assert.strictEqual(response.headers['cache-control'], 'private');
+ assert.strictEqual(response.headers['cache-control'], 'private, no-cache');
});
- it('should be absent on regular routes, for guests', async () => {
+ it('should be set to "private, no-cache" on regular routes, for guests', async () => {
const { response } = await request.get(nconf.get('url'));
assert.strictEqual(response.statusCode, 200);
- assert(!Object.keys(response.headers).includes('cache-control'));
+ assert.strictEqual(response.headers['cache-control'], 'private, no-cache');
});
- it('should be absent on api routes, for guests', async () => {
+ it('should be set to "private, no-cache" on api routes, for guests', async () => {
const { response } = await request.get(`${nconf.get('url')}/api`);
assert.strictEqual(response.statusCode, 200);
- assert(!Object.keys(response.headers).includes('cache-control'));
+ assert.strictEqual(response.headers['cache-control'], 'private, no-cache');
+ });
+
+ it('should be set to "private, no-cache" on the config route, for guests', async () => {
+ const { response } = await request.get(`${nconf.get('url')}/api/config`);
+
+ assert.strictEqual(response.statusCode, 200);
+ assert.strictEqual(response.headers['cache-control'], 'private, no-cache');
});
- it('should be set to "private" on regular routes, for logged-in users', async () => {
+ it('should be set to "private, no-cache" on regular routes, for logged-in users', async () => {
const { response } = await request.get(nconf.get('url'), { jar });
assert.strictEqual(response.statusCode, 200);
- assert(Object.keys(response.headers).includes('cache-control'));
- assert.strictEqual(response.headers['cache-control'], 'private');
+ assert.strictEqual(response.headers['cache-control'], 'private, no-cache');
});
- it('should be set to "private" on api routes, for logged-in users', async () => {
+ it('should be set to "private, no-cache" on api routes, for logged-in users', async () => {
const { response } = await request.get(`${nconf.get('url')}/api`, { jar });
assert.strictEqual(response.statusCode, 200);
- assert(Object.keys(response.headers).includes('cache-control'));
- assert.strictEqual(response.headers['cache-control'], 'private');
+ assert.strictEqual(response.headers['cache-control'], 'private, no-cache');
});
it('should be set to "private" on apiv3 routes, for logged-in users', async () => {