Ghost/core/frontend/apps/private-blogging/index.js
Hannah Wolfe a9759736d6
🐛 Fixed private blogging exposing 404 and robots (#11922)
- There were various cases where it was possible to trigger a private site to display a 404 instead of redirecting to /private/
- Private mode was also not always displaying the correct robots.txt
- This PR includes tests for all cases in test/frontend-acceptance/default_routes_spec.js & where possible the unit tests have also been updated for completeness
- Fixing the 404 issues required
    - Better handling of paths using req.path instead of req.url in filterPrivateRoutes
    - Additional error handling, to cover the case that a tag/author RSS feed does not exist
 - Fixing the robots.txt required the order of middleware to be changed, so that private blogging gets a chance to render first
    - NOTE private blogging is the only app with a setupMiddleware function so nothing else is affected
2020-06-16 11:42:32 +01:00

51 lines
1.5 KiB
JavaScript

const {i18n} = require('../../../server/lib/common');
const urlUtils = require('../../../shared/url-utils');
const logging = require('../../../shared/logging');
const errors = require('@tryghost/errors');
const middleware = require('./lib/middleware');
const router = require('./lib/router');
const registerHelpers = require('./lib/helpers');
// routeKeywords.private: 'private'
const PRIVATE_KEYWORD = 'private';
let checkSubdir = function checkSubdir() {
let paths = '';
if (urlUtils.getSubdir()) {
paths = urlUtils.getSubdir().split('/');
if (paths.pop() === PRIVATE_KEYWORD) {
logging.error(new errors.GhostError({
message: i18n.t('errors.config.urlCannotContainPrivateSubdir.error'),
context: i18n.t('errors.config.urlCannotContainPrivateSubdir.description'),
help: i18n.t('errors.config.urlCannotContainPrivateSubdir.help')
}));
// @TODO: why
process.exit(0);
}
}
};
module.exports = {
activate: function activate(ghost) {
let privateRoute = `/${PRIVATE_KEYWORD}/`;
checkSubdir();
ghost.routeService.registerRouter(privateRoute, router);
registerHelpers(ghost);
},
setupMiddleware: function setupMiddleware(siteApp) {
siteApp.use(middleware.checkIsPrivate);
siteApp.use(middleware.filterPrivateRoutes);
},
setupErrorHandling: function setupErrorHandling(siteApp) {
siteApp.use(middleware.handle404);
}
};