Ghost/core/frontend/apps/private-blogging/lib/middleware.js

154 lines
5.2 KiB
JavaScript
Raw Normal View History

const fs = require('fs-extra');
const url = require('url');
const session = require('cookie-session');
const crypto = require('crypto');
const path = require('path');
const config = require('../../../../shared/config');
const urlUtils = require('../../../../shared/url-utils');
const constants = require('../../../../server/lib/constants');
Refactored `common` lib import to use destructuring (#11835) * refactored `core/frontend/apps` to destructure common imports * refactored `core/frontend/services/{apps, redirects, routing}` to destructure common imports * refactored `core/frontend/services/settings` to destructure common imports * refactored remaining `core/frontend/services` to destructure common imports * refactored `core/server/adapters` to destructure common imports * refactored `core/server/data/{db, exporter, schema, validation}` to destructure common imports * refactored `core/server/data/importer` to destructure common imports * refactored `core/server/models/{base, plugins, relations}` to destructure common imports * refactored remaining `core/server/models` to destructure common imports * refactored `core/server/api/canary/utils/serializers/output` to destructure common imports * refactored remaining `core/server/api/canary/utils` to destructure common imports * refactored remaining `core/server/api/canary` to destructure common imports * refactored `core/server/api/shared` to destructure common imports * refactored `core/server/api/v2/utils` to destructure common imports * refactored remaining `core/server/api/v2` to destructure common imports * refactored `core/frontend/meta` to destructure common imports * fixed some tests referencing `common.errors` instead of `@tryghost/errors` - Not all of them need to be updated; only updating the ones that are causing failures * fixed errors import being shadowed by local scope
2020-05-22 21:22:20 +03:00
const {i18n} = require('../../../../server/lib/common');
const errors = require('@tryghost/errors');
const settingsCache = require('../../../../server/services/settings/cache');
// routeKeywords.private: 'private'
const privateRoute = '/private/';
function verifySessionHash(salt, hash) {
if (!salt || !hash) {
return false;
}
let hasher = crypto.createHash('sha256');
hasher.update(settingsCache.get('password') + salt, 'utf8');
return hasher.digest('hex') === hash;
}
function getRedirectUrl(query) {
try {
const redirect = decodeURIComponent(query.r || '/');
return url.parse(redirect).pathname;
} catch (e) {
return '/';
}
}
const privateBlogging = {
checkIsPrivate: function checkIsPrivate(req, res, next) {
let isPrivateBlog = settingsCache.get('is_private');
if (!isPrivateBlog) {
res.isPrivateBlog = false;
return next();
}
res.isPrivateBlog = true;
return session({
maxAge: constants.ONE_MONTH_MS,
signed: false,
sameSite: 'none'
})(req, res, next);
},
filterPrivateRoutes: function filterPrivateRoutes(req, res, next) {
if (!res.isPrivateBlog || req.url.lastIndexOf(privateRoute, 0) === 0) {
return next();
}
if (req.url.lastIndexOf('/robots.txt', 0) === 0) {
return fs.readFile(path.resolve(__dirname, '../', 'robots.txt'), function readFile(err, buf) {
if (err) {
return next(err);
}
res.writeHead(200, {
'Content-Type': 'text/plain',
'Content-Length': buf.length,
'Cache-Control': 'public, max-age=' + config.get('caching:robotstxt:maxAge')
});
res.end(buf);
});
}
// CASE: Allow private RSS feed urls.
// Any url which contains the hash and the postfix /rss is allowed to access a private rss feed without
// a session. As soon as a path matches, we rewrite the url. Even Express uses rewriting when using `app.use()`.
if (req.url.indexOf(settingsCache.get('public_hash') + '/rss') !== -1) {
req.url = req.url.replace(settingsCache.get('public_hash') + '/', '');
return next();
}
// NOTE: Redirect to /private if the session does not exist.
privateBlogging.authenticatePrivateSession(req, res, function onSessionVerified() {
// CASE: RSS is disabled for private blogging e.g. they create overhead
if (req.path.match(/\/rss\/$/)) {
Refactored `common` lib import to use destructuring (#11835) * refactored `core/frontend/apps` to destructure common imports * refactored `core/frontend/services/{apps, redirects, routing}` to destructure common imports * refactored `core/frontend/services/settings` to destructure common imports * refactored remaining `core/frontend/services` to destructure common imports * refactored `core/server/adapters` to destructure common imports * refactored `core/server/data/{db, exporter, schema, validation}` to destructure common imports * refactored `core/server/data/importer` to destructure common imports * refactored `core/server/models/{base, plugins, relations}` to destructure common imports * refactored remaining `core/server/models` to destructure common imports * refactored `core/server/api/canary/utils/serializers/output` to destructure common imports * refactored remaining `core/server/api/canary/utils` to destructure common imports * refactored remaining `core/server/api/canary` to destructure common imports * refactored `core/server/api/shared` to destructure common imports * refactored `core/server/api/v2/utils` to destructure common imports * refactored remaining `core/server/api/v2` to destructure common imports * refactored `core/frontend/meta` to destructure common imports * fixed some tests referencing `common.errors` instead of `@tryghost/errors` - Not all of them need to be updated; only updating the ones that are causing failures * fixed errors import being shadowed by local scope
2020-05-22 21:22:20 +03:00
return next(new errors.NotFoundError({
message: i18n.t('errors.errors.pageNotFound')
}));
}
next();
});
},
authenticatePrivateSession: function authenticatePrivateSession(req, res, next) {
const hash = req.session.token || '';
const salt = req.session.salt || '';
const isVerified = verifySessionHash(salt, hash);
if (isVerified) {
return next();
} else {
let redirectUrl = urlUtils.urlFor({relativeUrl: privateRoute});
redirectUrl += '?r=' + encodeURIComponent(req.url);
return res.redirect(redirectUrl);
}
},
// This is here so a call to /private/ after a session is verified will redirect to home;
redirectPrivateToHomeIfLoggedIn: function redirectPrivateToHomeIfLoggedIn(req, res, next) {
if (!res.isPrivateBlog) {
return res.redirect(urlUtils.urlFor('home', true));
}
const hash = req.session.token || '';
const salt = req.session.salt || '';
const isVerified = verifySessionHash(salt, hash);
if (isVerified) {
// redirect to home if user is already authenticated
return res.redirect(urlUtils.urlFor('home', true));
} else {
return next();
}
},
doLoginToPrivateSite: function doLoginToPrivateSite(req, res, next) {
// if errors have been generated from the previous call
if (res.error) {
return next();
}
const bodyPass = req.body.password;
const pass = settingsCache.get('password');
const hasher = crypto.createHash('sha256');
const salt = Date.now().toString();
const forward = getRedirectUrl(req.query);
if (pass === bodyPass) {
hasher.update(bodyPass + salt, 'utf8');
req.session.token = hasher.digest('hex');
req.session.salt = salt;
return res.redirect(urlUtils.urlFor({relativeUrl: forward}));
} else {
res.error = {
Refactored `common` lib import to use destructuring (#11835) * refactored `core/frontend/apps` to destructure common imports * refactored `core/frontend/services/{apps, redirects, routing}` to destructure common imports * refactored `core/frontend/services/settings` to destructure common imports * refactored remaining `core/frontend/services` to destructure common imports * refactored `core/server/adapters` to destructure common imports * refactored `core/server/data/{db, exporter, schema, validation}` to destructure common imports * refactored `core/server/data/importer` to destructure common imports * refactored `core/server/models/{base, plugins, relations}` to destructure common imports * refactored remaining `core/server/models` to destructure common imports * refactored `core/server/api/canary/utils/serializers/output` to destructure common imports * refactored remaining `core/server/api/canary/utils` to destructure common imports * refactored remaining `core/server/api/canary` to destructure common imports * refactored `core/server/api/shared` to destructure common imports * refactored `core/server/api/v2/utils` to destructure common imports * refactored remaining `core/server/api/v2` to destructure common imports * refactored `core/frontend/meta` to destructure common imports * fixed some tests referencing `common.errors` instead of `@tryghost/errors` - Not all of them need to be updated; only updating the ones that are causing failures * fixed errors import being shadowed by local scope
2020-05-22 21:22:20 +03:00
message: i18n.t('errors.middleware.privateblogging.wrongPassword')
};
return next();
}
}
};
module.exports = privateBlogging;