Skip to content

Instantly share code, notes, and snippets.

@ChrisBAshton
Last active October 2, 2016 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisBAshton/ad772f616a64fa7d33b1ea875c844aea to your computer and use it in GitHub Desktop.
Save ChrisBAshton/ad772f616a64fa7d33b1ea875c844aea to your computer and use it in GitHub Desktop.
Security in JavaScript
var customRequest;
(function () {
function CustomRequestModule () {
function isSafeUrl (url) {
return url === 'https://safe-site.com';
}
function getAccessToken () {
return 'my top secret access token';
}
return {
post: function (params, callback) {
var paramsToSend = JSON.parse(JSON.stringify(params)); // copy the JSON
if (isSafeUrl(paramsToSend.url)) {
paramsToSend.headers.Authorization = 'token ' + getAccessToken();
}
request(paramsToSend, callback);
}
}
};
customRequest = new CustomRequestModule();
})();
require('./your-custom-module.js')(customRequest);
@tmaslen
Copy link

tmaslen commented Oct 2, 2016

And put the secret token in a variable that is outside the closure so if you cast the closured function to a string all it outputs is the variable name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment