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

Allow deep mask json and custom JSON serializer. Fix non UTF-8 for js… #85

Merged
merged 5 commits into from
Jan 19, 2020

Conversation

Meat-Chopper
Copy link
Contributor

Hi,
I see the implementation of JSON data masking in the "develop" branch. But since it has not yet been merged with the master, I have decided to try with my own implementation.
Also there is a bug when JSON is unable to dump non UTF-8 symbols.

end
end

describe 'Non UTF-8 with JSON log' do
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It fails in version 1.3.2

# Capitalized for ::HTTP
%w[content-type Content-Type content-encoding Content-Encoding].include? k
end.to_h.each_with_object({}) { |(k, v), h| h[k.downcase] = v }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe it is better to move this logic to the adapters?

Copy link
Owner

Choose a reason for hiding this comment

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

Firstly, this reads a little hard. I'd replace k with header on the first block - I tend to reserve single character variable names for single line blocks (as in 276).

Copy link
Owner

Choose a reason for hiding this comment

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

Secondly, it's not at all clear to me what this block does. Can you add a comment, or perhaps use clearer parameter names? The result of this block is not the request, it's just a bunch of headers.

But leaving the logic here is fine, to deal with differently capitalized headers in one place. It just needs to be clearer that that's what we're doing here. Perhaps move it to a dedicated class method.

Copy link
Owner

@trusche trusche left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, looks good in principle. Just a few small tweaks please. Lots of tests is great - thanks!

@prefix_data_lines = false
@prefix_response_lines = false
@prefix_line_numbers = false
@mask_json = false
Copy link
Owner

Choose a reason for hiding this comment

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

This parameter seems redundant. One could just enable global JSON masking by setting the URL parameter to /.*/. The regex is checked every time anyway.

Copy link
Owner

Choose a reason for hiding this comment

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

If filter_parameters is set, we should just mask JSON as well by default.

@@ -52,10 +53,15 @@ def url_approved?(url)
!config.url_whitelist_pattern || url.to_s.match(config.url_whitelist_pattern)
end

def masked_body_url?(url)
config.filter_parameters.any? &&
(config.url_masked_body_pattern && url.to_s.match(config.url_masked_body_pattern) || config.mask_json)
Copy link
Owner

Choose a reason for hiding this comment

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

See above - the final condition wouldn't be necessary if url_masked_body_pattern == /.*/

begin
result = masked_data(config.json_parser.load(result))
rescue => e
e.message + ': ' + result
Copy link
Owner

Choose a reason for hiding this comment

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

This silently swallow a failure. Perhaps we should just log an error message and return an empty string so we're not accidentally leaking sensitive data into the logfile. Or looking further down, perhaps that's what's indented already and you forgot to re-raise the exception here?

# Capitalized for ::HTTP
%w[content-type Content-Type content-encoding Content-Encoding].include? k
end.to_h.each_with_object({}) { |(k, v), h| h[k.downcase] = v }

Copy link
Owner

Choose a reason for hiding this comment

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

Firstly, this reads a little hard. I'd replace k with header on the first block - I tend to reserve single character variable names for single line blocks (as in 276).

# Capitalized for ::HTTP
%w[content-type Content-Type content-encoding Content-Encoding].include? k
end.to_h.each_with_object({}) { |(k, v), h| h[k.downcase] = v }

Copy link
Owner

Choose a reason for hiding this comment

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

Secondly, it's not at all clear to me what this block does. Can you add a comment, or perhaps use clearer parameter names? The result of this block is not the request, it's just a bunch of headers.

But leaving the logic here is fine, to deal with differently capitalized headers in one place. It just needs to be clearer that that's what we're doing here. Perhaps move it to a dedicated class method.

let(:filter_parameters) { HttpLog.configuration.filter_parameters }
let(:url_masked_body_pattern) { HttpLog.configuration.url_masked_body_pattern }
Copy link
Owner

Choose a reason for hiding this comment

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

Align the block indentation above please.

@Meat-Chopper
Copy link
Contributor Author

Hello @trusche,
Thank you for review! The comments have been applied.

@Meat-Chopper Meat-Chopper force-pushed the mask_json branch 2 times, most recently from cff4bfc to 79a1678 Compare January 13, 2020 11:32
@Meat-Chopper Meat-Chopper force-pushed the mask_json branch 2 times, most recently from 34eb49a to 3a9afe5 Compare January 16, 2020 11:43
@trusche trusche merged commit 820c400 into trusche:master Jan 19, 2020
@trusche
Copy link
Owner

trusche commented Jan 19, 2020

Released as v1.4.0. Thanks again!

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.

2 participants