forked from TryGhost/Ghost
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved tokens, url safe and safe string utility to lib/security
refs TryGhost#9178 - we could now also move any crypto usages to lib/security, but no priority - the main goal is to tidy up our utils folder
- Loading branch information
Showing
15 changed files
with
128 additions
and
112 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
get url() { | ||
return require('./url'); | ||
}, | ||
|
||
get tokens() { | ||
return require('./tokens'); | ||
}, | ||
|
||
get string() { | ||
return require('./string'); | ||
} | ||
}; |
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,40 @@ | ||
'use strict'; | ||
|
||
const unidecode = require('unidecode'), | ||
_ = require('lodash'); | ||
|
||
module.exports.safe = function safe(string, options) { | ||
options = options || {}; | ||
|
||
if (string === null) { | ||
string = ''; | ||
} | ||
|
||
// Handle the £ symbol separately, since it needs to be removed before the unicode conversion. | ||
string = string.replace(/£/g, '-'); | ||
|
||
// Remove non ascii characters | ||
string = unidecode(string); | ||
|
||
// Replace URL reserved chars: `@:/?#[]!$&()*+,;=` as well as `\%<>|^~£"{}` and \` | ||
string = string.replace(/(\s|\.|@|:|\/|\?|#|\[|\]|!|\$|&|\(|\)|\*|\+|,|;|=|\\|%|<|>|\||\^|~|"|\{|\}|`|–|—)/g, '-') | ||
// Remove apostrophes | ||
.replace(/'/g, '') | ||
// Make the whole thing lowercase | ||
.toLowerCase(); | ||
|
||
// We do not need to make the following changes when importing data | ||
if (!_.has(options, 'importing') || !options.importing) { | ||
// Convert 2 or more dashes into a single dash | ||
string = string.replace(/-+/g, '-') | ||
// Remove trailing dash | ||
.replace(/-$/, '') | ||
// Remove any dashes at the beginning | ||
.replace(/^-/, ''); | ||
} | ||
|
||
// Handle whitespace at the beginning or end. | ||
string = string.trim(); | ||
|
||
return string; | ||
}; |
4 changes: 3 additions & 1 deletion
4
core/server/utils/tokens.js → core/server/lib/security/tokens.js
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,14 @@ | ||
// The token is encoded URL safe by replacing '+' with '-', '\' with '_' and removing '=' | ||
// NOTE: the token is not encoded using valid base64 anymore | ||
module.exports.encodeBase64 = function encodeBase64(base64String) { | ||
return base64String.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); | ||
}; | ||
|
||
// Decode url safe base64 encoding and add padding ('=') | ||
module.exports.decodeBase64 = function decodeBase64(base64String) { | ||
base64String = base64String.replace(/-/g, '+').replace(/_/g, '/'); | ||
while (base64String.length % 4) { | ||
base64String += '='; | ||
} | ||
return base64String; | ||
}; |
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
Oops, something went wrong.