0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/core/server/services/auth/session/index.js
Fabien 'egg' O'Carroll 244704156c
Updated all Origin header checks to handle 'null' (#12246)
closes #12244

As per RFC 6454 the Origin header MUST be set to the string 'null' when
in a "privacy-sensitive" context. We were not handling this string and
this was causing errors. This commit updates all checks of the 'Origin'
header to treat the value 'null' as if the header was not present.

ref: https://tools.ietf.org/html/rfc6454#section-7.3
2020-10-01 09:37:22 +01:00

49 lines
1.5 KiB
JavaScript

const adapterManager = require('../../adapter-manager');
const createSessionService = require('@tryghost/session-service');
const sessionFromToken = require('@tryghost/mw-session-from-token');
const createSessionMiddleware = require('./middleware');
const expressSession = require('./express-session');
const models = require('../../../models');
const urlUtils = require('../../../../shared/url-utils');
const url = require('url');
function getOriginOfRequest(req) {
const origin = req.get('origin');
const referrer = req.get('referrer') || urlUtils.getAdminUrl() || urlUtils.getSiteUrl();
if (!origin && !referrer || origin === 'null') {
return null;
}
if (origin) {
return origin;
}
const {protocol, host} = url.parse(referrer);
if (protocol && host) {
return `${protocol}//${host}`;
}
return null;
}
const sessionService = createSessionService({
getOriginOfRequest,
getSession: expressSession.getSession,
findUserById({id}) {
return models.User.findOne({id});
}
});
module.exports = createSessionMiddleware({sessionService});
const ssoAdapter = adapterManager.getAdapter('sso');
// Looks funky but this is a "custom" piece of middleware
module.exports.createSessionFromToken = sessionFromToken({
callNextWithError: false,
createSession: sessionService.createSessionForUser,
findUserByLookup: ssoAdapter.getUserForIdentity,
getLookupFromToken: ssoAdapter.getIdentityFromCredentials,
getTokenFromRequest: ssoAdapter.getRequestCredentials
});