-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This funnels into the socket.io `origins` config, but only supports using an array of strings. It's also stricter in how it matches the origin against the whitelist.
- Loading branch information
Showing
4 changed files
with
86 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Module dependencies | ||
*/ | ||
|
||
var _ = require('lodash'); | ||
var url = require('url'); | ||
var flaverr = require('flaverr'); | ||
|
||
|
||
/** | ||
* checkOriginUrl() | ||
* | ||
* @param {String} originUrl | ||
* The origin URL to check. | ||
* (This is used when parsing the relevant config from within `sails.config.security` | ||
* or `sails.config.sockets`.) | ||
* | ||
* @throws {Error} if not valid | ||
* @property {String} code (==='E_INVALID') | ||
*/ | ||
|
||
module.exports = function checkOriginUrl(originUrl) { | ||
|
||
if (!_.isString(originUrl) || originUrl === '') { | ||
throw flaverr('E_INVALID', new Error('Must specify a non-empty string, but instead got: '+util.inspect(originUrl, {depth: null}))); | ||
} | ||
|
||
if (!originUrl.match(/^https?:\/\//)) { | ||
throw flaverr('E_INVALID', new Error('Must specify a protocol like http:// or https://, but instead got: '+originUrl)); | ||
} | ||
|
||
// Now do a mostly-correct parse of the URL. | ||
var parsedOriginUrl = url.parse(originUrl); | ||
|
||
var isHttps = parsedOriginUrl.protocol === 'https:'; | ||
|
||
if (isHttps && parsedOriginUrl.port === '443') { | ||
throw flaverr('E_INVALID', new Error('Should not explicitly specify port 443 with https:// (it is implied). But instead got: '+originUrl)); | ||
} | ||
if (!isHttps && parsedOriginUrl.port === '80') { | ||
throw flaverr('E_INVALID', new Error('Should not explicitly specify port 80 with https:// (it is implied). But instead got: '+originUrl)); | ||
} | ||
|
||
// Ensure there is no path or query string or fragment or anything like that. | ||
if (parsedOriginUrl.pathname !== '/' || parsedOriginUrl.path !== '/') { | ||
throw flaverr('E_INVALID', new Error('Should not specify a path, query string, URL fragment, or anything like that (but instead, got `'+originUrl+'`)')); | ||
} | ||
|
||
// Ensure there is no trailing slice | ||
var lastCharacter = originUrl.slice(-1); | ||
if (lastCharacter === '/') { | ||
throw flaverr('E_INVALID', new Error('Should not specify a trailing slash, but instead got: '+originUrl)); | ||
} | ||
|
||
}; |
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