Skip to content

Commit

Permalink
Add test to make sure HTTP basic auth doesn't leak
Browse files Browse the repository at this point in the history
This commit improves the tests to make sure that HTTP basic auth
credentials don't leak into tracing data.

See olivere#1459
  • Loading branch information
olivere authored and dungnx-teko committed Sep 16, 2021
1 parent 6d38af9 commit 4eb8ef6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
11 changes: 11 additions & 0 deletions trace/opencensus/transport_test.go
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"

"go.opencensus.io/trace"
Expand All @@ -31,6 +32,12 @@ func (t *testExporter) ExportSpan(s *trace.SpanData) {

func TestTransport(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
if !ok || username != "alice" || password != "secret" {
w.WriteHeader(http.StatusForbidden)
return
}

switch r.URL.Path {
case "/":
w.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -79,6 +86,7 @@ func TestTransport(t *testing.T) {
elastic.SetHttpClient(httpClient),
elastic.SetHealthcheck(false),
elastic.SetSniff(false),
elastic.SetBasicAuth("alice", "secret"),
)
if err != nil {
t.Fatal(err)
Expand All @@ -101,6 +109,7 @@ func TestTransport(t *testing.T) {
t.Fatalf("want %d finished spans, have %d", want, have)
}
span := spans[0]

if want, have := "elastic:PerformRequest", span.Name; want != have {
t.Fatalf("want Span.Name=%q, have %q", want, have)
}
Expand All @@ -116,6 +125,8 @@ func TestTransport(t *testing.T) {
}
if attr, ok := span.Attributes["URL"].(string); !ok || attr == "" {
t.Fatalf("attribute %q not found", "URL")
} else if strings.Contains(attr, "alice") || strings.Contains(attr, "password") {
t.Fatalf("attribute %q contains username and/or password: %s", "URL", attr)
}
if attr, ok := span.Attributes["Hostname"].(string); !ok || attr == "" {
t.Fatalf("attribute %q not found", "Hostname")
Expand Down
18 changes: 17 additions & 1 deletion trace/opentracing/transport_test.go
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/opentracing/opentracing-go"
Expand All @@ -19,6 +20,12 @@ import (

func TestTransport(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
if !ok || username != "alice" || password != "secret" {
w.WriteHeader(http.StatusForbidden)
return
}

switch r.URL.Path {
case "/":
w.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -63,6 +70,7 @@ func TestTransport(t *testing.T) {
elastic.SetHttpClient(httpClient),
elastic.SetHealthcheck(false),
elastic.SetSniff(false),
elastic.SetBasicAuth("alice", "secret"),
)
if err != nil {
t.Fatal(err)
Expand All @@ -84,15 +92,23 @@ func TestTransport(t *testing.T) {
t.Fatalf("want %d finished spans, have %d", want, have)
}
span := spans[0]

if want, have := "PerformRequest", span.OperationName; want != have {
t.Fatalf("want Span.OperationName=%q, have %q", want, have)
}
if want, have := "github.com/olivere/elastic/v7", span.Tag("component"); want != have {
t.Fatalf("want component tag=%q, have %q", want, have)
}
if want, have := ts.URL+"/", span.Tag("http.url"); want != have {
httpURL, ok := span.Tag("http.url").(string)
if !ok || httpURL == "" {
t.Fatalf("want http.url tag=%q to be a non-empty string (found type %T)", "http.url", span.Tag("http.url"))
}
if want, have := ts.URL+"/", httpURL; want != have {
t.Fatalf("want http.url tag=%q, have %q", want, have)
}
if strings.Contains(httpURL, "alice") || strings.Contains(httpURL, "password") {
t.Fatalf("want http.url tag %q to not contain username and/or password: %s", "URL", span.Tag("http.url"))
}
if want, have := "GET", span.Tag("http.method"); want != have {
t.Fatalf("want http.method tag=%q, have %q", want, have)
}
Expand Down

0 comments on commit 4eb8ef6

Please sign in to comment.