Skip to content

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

Merged
trusche merged 5 commits intotrusche:masterfrom
Meat-Chopper:mask_json
Jan 19, 2020
Merged

Allow deep mask json and custom JSON serializer. Fix non UTF-8 for js…#85
trusche merged 5 commits intotrusche:masterfrom
Meat-Chopper:mask_json

Conversation

@Meat-Chopper
Copy link
Copy Markdown
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.

Comment thread spec/lib/http_log_spec.rb
end
end

describe 'Non UTF-8 with JSON log' do
Copy link
Copy Markdown
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

Comment thread lib/httplog/http_log.rb
# 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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!

Comment thread lib/httplog/configuration.rb Outdated
@prefix_data_lines = false
@prefix_response_lines = false
@prefix_line_numbers = false
@mask_json = false
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread lib/httplog/http_log.rb Outdated

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
Copy Markdown
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 == /.*/

Comment thread lib/httplog/http_log.rb Outdated
begin
result = masked_data(config.json_parser.load(result))
rescue => e
e.message + ': ' + result
Copy link
Copy Markdown
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?

Comment thread lib/httplog/http_log.rb
# 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
Copy Markdown
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).

Comment thread lib/httplog/http_log.rb
# 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
Copy Markdown
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.

Comment thread spec/lib/http_log_spec.rb
let(:mask_json) { HttpLog.configuration.mask_json }
let(:json_parser) { HttpLog.configuration.json_parser }
let(:filter_parameters) { HttpLog.configuration.filter_parameters }
let(:url_masked_body_pattern) { HttpLog.configuration.url_masked_body_pattern }
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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