-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
fix(httpext): remove user credentials from metric tags #1132
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1132 +/- ##
==========================================
+ Coverage 73.59% 73.65% +0.05%
==========================================
Files 145 144 -1
Lines 10575 10544 -31
==========================================
- Hits 7783 7766 -17
+ Misses 2333 2321 -12
+ Partials 459 457 -2
Continue to review full report at Codecov.
|
5bf47fe
to
599112f
Compare
lib/netext/httpext/request.go
Outdated
} | ||
clean := strings.Replace(u.String(), old, new, 1) | ||
// Replace back any literal `${}` placeholders escaped by `u.String()` | ||
return strings.Replace(clean, `$%7B%7D`, `${}`, -1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a bit of a nasty hack 😄 In general, I slightly dislike that we're having to parse the previously parsed URL here and do a bunch of shifty string operations to sanitize it. This shouldn't noticeably affect performance, even when we do it twice for each URL in the script, bit it just seems like a waste, especially given the fact that most URLs probably won't have credentials...
Why are you reluctant to mess with the NewURL()
constructor https://github.com/loadimpact/k6/blob/d385166a821a8cd98a4ab2c158b3982d87ee61a5/lib/netext/httpext/request.go#L59-L62
It seems like it would be relatively easy to just add another SanitizedURL
property or something like that, which can just be the same string as the URL if there's no authentication, and sanitized from the already parsed URL if there is. In my head that won't be more difficult to trace than the current implementation, so I don't think it's a case of premature optimization, but I might be wrong 😄 @mstoykov, thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am of the opinion that given the amount of strings that the above code will generate I am pretty sure it will have performance impact if we do it for ALL urls. IMHO we should at least check if we need to do anything at all before we even parse it back from string to URL ... given that we already have it as a URL.
Also given that .. I am pretty sure it would be better if we just copy the URL , change the user and password and string it back ...
And ... isn't Name supposed to stay if it's set by the user and if it's not it's the same as the URL so we don't need to do the work twice ?
As a whole I think that maybe taking a step back and working directly with the URLs will be better than what you are currently trying to do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you reluctant to mess with the NewURL() constructor
I guess I was trying to introduce the least amount of changes while I'm getting familiar, but you're both right that it would be more efficient to create a clean URL in the constructor once and avoid parsing it every time the tags are set.
I'll refactor this again, thanks for the feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@na-- @mstoykov Take a look at the latest commit 6ab3603. I suppose it's cleaner and more efficient, but I don't like that httpext.URL
now wraps 4 representations of potentially the same URL. It's a bit messy to me, but I'm not familiar enough with the codebase to make a concrete suggestion for fixing it.
599112f
to
6ab3603
Compare
newURL := URL{u: u, Name: name, URL: urlString} | ||
newURL.CleanURL = newURL.Clean() | ||
if urlString == name { | ||
newURL.Name = newURL.CleanURL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This handles this use case.
Again I did some benchmarking:
Here
|
Thanks for the benchmark @mstoykov. It does make sense to consider setting |
@mstoykov Unfortunately, the implementation can't be that simple. We can use
And dealing with that issue also gets messy and expensive:
There might be a more performant way of doing the above, but on my machine it's only a marginal improvement over the original stringified approach (BenchmarkC):
... and more things could go wrong in this version, since it's re-assembling the URL. I'm not sure. Let me know if you have any suggestions. |
@imiric I published a new version which is even faster ;) :
|
@imiric but the first one will be the one we care about ;) (also not sure if you can have an unescaped one but am too lazy to go and read the RFC to check, but it doesn't matter) Also I happen to like my code ...more ;) |
I would agree with @mstoykov, |
This version performs ~8x faster with ~4x less memory usage than the previous version. Thanks @mstoykov! See #1132 (comment)
6ab3603
to
ed6e0e7
Compare
This fixes the credential leak in HTTP metric tags.
Closes #1103