Skip to content

Commit

Permalink
perf(httpext): more efficient credential masking implementation
Browse files Browse the repository at this point in the history
This version performs ~8x faster with ~4x less memory usage than the previous version.
Thanks @mstoykov!

See grafana#1132 (comment)
  • Loading branch information
Ivan Mirić authored and srguglielmo committed Nov 3, 2019
1 parent 6dbc2ed commit 9fd5976
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions lib/netext/httpext/request.go
Expand Up @@ -23,7 +23,6 @@ package httpext
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"net"
Expand Down Expand Up @@ -77,14 +76,13 @@ func (u URL) Clean() string {
out := u.URL

if u.u != nil && u.u.User != nil {
// mask user credentials
creds := fmt.Sprintf("%s@", u.u.User.String())
mask := "****@"
_, passSet := u.u.User.Password()
if passSet {
mask = fmt.Sprintf("****:%s", mask)
schemeIndex := strings.Index(out, "://")
atIndex := strings.Index(out, "@")
if _, passwordOk := u.u.User.Password(); passwordOk {
out = out[:schemeIndex+3] + "****:****" + out[atIndex:]
} else {
out = out[:schemeIndex+3] + "****" + out[atIndex:]
}
return strings.Replace(out, creds, mask, 1)
}

return out
Expand Down

0 comments on commit 9fd5976

Please sign in to comment.