-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔥✨ remove forceAdminSSL and urlSSL, add admin url (#7937)
* 🔥 kill apiUrl helper, use urlFor helper instead More consistency of creating urls. Creates an easier ability to add config changes. Attention: urlFor function is getting a little nesty, BUT that is for now wanted to make easier and centralised changes to the configs. The url util need's refactoring anyway. * 🔥 urlSSL Remove all urlSSL usages. Add TODO's for the next commit to re-add logic for deleted logic. e.g. - cors helper generated an array of url's to allow requests from the defined config url's -> will be replaced by the admin url if available - theme handler prefered the urlSSL in case it was defined -> will be replaced by using the urlFor helper to get the blog url (based on the request secure flag) The changes in this commit doesn't have to be right, but it helped going step by step. The next commit is the more interesting one. * 🔥 ✨ remove forceAdminSSL, add new admin url and adapt logic I wanted to remove the forceAdminSSL as separate commit, but was hard to realise. That's why both changes are in one commit: 1. remove forceAdminSSL 2. add admin.url option - fix TODO's from last commits - rewrite the ssl middleware! - create some private helper functions in the url helper to realise the changes - rename some wordings and functions e.g. base === blog (we have so much different wordings) - i would like to do more, but this would end in a non readable PR - this commit contains the most important changes to offer admin.url option * 🤖 adapt tests IMPORTANT - all changes in the routing tests were needed, because each routing test did not start the ghost server - they just required the ghost application, which resulted in a random server port - having a random server port results in a redirect, caused by the ssl/redirect middleware * 😎 rename check-ssl middleware * 🎨 fix theme-handler because of master rebase
- Loading branch information
Showing
37 changed files
with
1,464 additions
and
1,399 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
var url = require('url'), | ||
debug = require('debug')('ghost:redirects'), | ||
utils = require('../utils'), | ||
urlRedirects; | ||
|
||
function redirectUrl(options) { | ||
var redirectTo = options.redirectTo, | ||
path = options.path, | ||
query = options.query, | ||
parts = url.parse(redirectTo); | ||
|
||
return url.format({ | ||
protocol: parts.protocol, | ||
hostname: parts.hostname, | ||
port: parts.port, | ||
pathname: path, | ||
query: query | ||
}); | ||
} | ||
|
||
/** | ||
* SSL AND REDIRECTS | ||
*/ | ||
urlRedirects = function urlRedirects(req, res, next) { | ||
var requestedUrl = req.originalUrl || req.url, | ||
requestedHost = req.get('host'), | ||
targetHostWithProtocol, | ||
targetHostWithoutProtocol; | ||
|
||
if (res.isAdmin) { | ||
targetHostWithProtocol = utils.url.urlFor('admin', true); | ||
targetHostWithoutProtocol = utils.url.urlFor('admin', {cors: true}, true); | ||
} else { | ||
targetHostWithProtocol = utils.url.urlFor('home', true); | ||
targetHostWithoutProtocol = utils.url.urlFor('home', {cors: true}, true); | ||
} | ||
|
||
debug('requestedUrl', requestedUrl); | ||
debug('requestedHost', requestedHost); | ||
debug('targetHost', targetHostWithoutProtocol); | ||
|
||
// CASE: custom admin url is configured, but user requests blog domain | ||
// CASE: exception: localhost is always allowed | ||
if (!targetHostWithoutProtocol.match(new RegExp(requestedHost))) { | ||
debug('redirect because host does not match'); | ||
|
||
return res.redirect(301, redirectUrl({ | ||
redirectTo: targetHostWithProtocol, | ||
path: requestedUrl, | ||
query: req.query | ||
})); | ||
} | ||
|
||
// CASE: correct admin url, but not the correct protocol | ||
if (utils.url.isSSL(targetHostWithProtocol) && !req.secure) { | ||
debug('redirect because protocol does not match'); | ||
|
||
return res.redirect(301, redirectUrl({ | ||
redirectTo: targetHostWithProtocol, | ||
path: requestedUrl, | ||
query: req.query | ||
})); | ||
} | ||
|
||
next(); | ||
}; | ||
|
||
module.exports = urlRedirects; |
Oops, something went wrong.