Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hide secrets from log output #35

Merged
merged 3 commits into from
Oct 17, 2017
Merged

Conversation

kriswep
Copy link
Contributor

@kriswep kriswep commented Oct 4, 2017

I added a log and sanitize function and used that over console.log.

Secret stuff (gh client id, gh client secret, codes and tokens) will be truncated to 3 characters. If the secret is less equal 10 characters or not a string it will be replaced by three dots.

So it logs sthg like this

Configuration
oauth_client_id: ac4...
oauth_client_secret: 2ea...
oauth_host: github.com
oauth_port: 443
oauth_path: /login/oauth/access_token
oauth_method: POST
Gatekeeper, at your service: http://localhost:9999
authenticating code: 439...
bad_code
authenticating code: ...
bad_code
authenticating code: 628...
token ee7...

closes #34

add log and sanitze function and used that over console.log
closes prose#34
@compumike08
Copy link
Contributor

@kriswep: I have a few suggestions to improve your implementation. I can’t go into details right now, but I’ll try to post a more detailed comment later tonight before this gets merged.

Copy link
Contributor

@compumike08 compumike08 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @kriswep for taking the time to address this security issue. I’m sure the entire community which uses gatekeeper is very greatful for your time and effort (I know I am). I did have a few minor suggestions to improve code quality and future maintainablity. Feel free to look at my line comments in this review to see my suggestions, and implement any which you agree with. Thanks again.

server.js Outdated
function log(label, value, sanitized) {
value = value || '';
if (sanitized){
if (typeof(value) === 'string' && value.length > 10){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace the hard-coded 10 with a named constant to avoid introducing “magic numbers”.

server.js Outdated
value = value || '';
if (sanitized){
if (typeof(value) === 'string' && value.length > 10){
console.log(label, value.substring(3,0) + '...');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace hard-coded ’...’ string with a named constant. This will make it easy to change the displayed string everywhere in the future without having to hunt through the code for each hard-coded instance.

server.js Outdated
if (typeof(value) === 'string' && value.length > 10){
console.log(label, value.substring(3,0) + '...');
} else {
console.log(label, '...');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace hard-coded ’...’ string with a named constant. This will make it easy to change the displayed string everywhere in the future without having to hunt through the code for each hard-coded instance.

server.js Outdated
@@ -49,6 +53,19 @@ function authenticate(code, cb) {
req.on('error', function(e) { cb(e.message); });
}

function log(label, value, sanitized) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a JSDoc comment block to this function to document the function, the parameters, what each parameter is for, and what types of data are expected for each parameter.

server.js Outdated
var result = err || !token ? {"error": "bad_code"} : { "token": token };
console.log(result);
if ( err || !token ) {
result = {"error": "bad_code"};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace hard-coded ”error” and ”bad_code” strings with named constants to make it easier to change the values in the future, in case it is ever necessary to do so.

server.js Outdated
result = {"error": "bad_code"};
log(result.error);
} else {
result = {"token": token};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace hard-coded ”token” string with a named constant to make it easier to change the value in the future, in case it is ever necessary to do so.

server.js Outdated
log(result.error);
} else {
result = {"token": token};
log("token", result.token, true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace hard-coded ”token” string with a named constant to make it easier to change the value in the future, in case it is ever necessary to do so.

and document log fn
@kriswep
Copy link
Contributor Author

kriswep commented Oct 5, 2017

Alright, I documented the new log function and replaced the hardcoded strings in there.

I did not change the strings in the authenticate route function. I felt doing so would actually reduce readability by providing very minor benefits. "token", "error" and "bad_code" in there were part of the output users saw and used for years, right? I don't think they would need to be changed anytime soon.

BTW: I was very tempted to use const for the var declarations but decided against that. Main reason being, that the rest of the project is still ES5. What would be your opinion towards using ES6? You declared node 6.x as engine, so you could use ES6 without transpiling for the most part. Just wondering...

Copy link
Contributor

@compumike08 compumike08 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@compumike08
Copy link
Contributor

Regarding const vs var, even though transpiling wouldn’t be an issue here, my personal opinion on the issue is that this is a case where consistency may be more important than correctness. Having const in just a few places in the codebase and still using var everywhere else might be a little confusing. I do think that at some point this project could be refactored to replace all instances of var with let/const, but unless/until that happens, my personal opinion is that it is best to be consistent.

@kriswep
Copy link
Contributor Author

kriswep commented Oct 6, 2017

Sounds reasonable. Would be happy to help moving this project forward in that regard.

server.js Outdated
console.log(result);
if ( err || !token ) {
result = {"error": "bad_code"};
log(result.error);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only indirectly related to what your PR fixes, but since we're touching this code, one thing I'd find useful is, in the case where we are returning err, logging that instead of just logging bad_code. This would make it easier to debug, since it would likely return error messages from whatever Oauth provider this is attached to.

Copy link
Contributor Author

@kriswep kriswep Oct 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a change for this.

However, while testing, I noticed your implementation in the authenticate function swallows quite a lot of errors. I opened issue #36 for that. I would prefer that to be fixed in another pr.

@dereklieu
Copy link
Member

@kriswep thanks for the pr. As far as the logic goes, it looks good. I agree we shouldn't change the format of the returned object. I'll do some testing of the code tomorrow, but in the meantime I left a comment about logging errors. Thanks again for your work on this.

@dereklieu dereklieu merged commit 5169fa9 into prose:master Oct 17, 2017
@kriswep kriswep deleted the sanitize-logs branch October 20, 2017 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants