gitlab.com/gitlab-org/labkit@v1.21.0/mask/url_test.go (about)

     1  package mask
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func BenchmarkURL(b *testing.B) {
    10  	for n := 0; n < b.N; n++ {
    11  		URL(`http://localhost:8000?token=123&something_else=92384&secret=sdmalaksjdasd&hook=123901283019238&trace=12312312312123`)
    12  	}
    13  }
    14  
    15  func TestURL(t *testing.T) {
    16  	tests := map[string]string{
    17  		"http://localhost:8000":                                             "http://localhost:8000",
    18  		"https://gitlab.com/":                                               "https://gitlab.com/",
    19  		"custom://gitlab.com?secret=x":                                      "custom://gitlab.com?secret=[FILTERED]",
    20  		"gitlab.com?secret=x":                                               "gitlab.com?secret=[FILTERED]",
    21  		":":                                                                 "<invalid URL>",
    22  		"http://user@example.com":                                           "http://FILTERED@example.com",
    23  		"http://user:password@example.com":                                  "http://FILTERED:FILTERED@example.com",
    24  		"http://example.com":                                                "http://example.com",
    25  		"http://example.com?foo=1":                                          "http://example.com?foo=1",
    26  		"http://example.com?foo=token":                                      "http://example.com?foo=token",
    27  		"http://example.com?title=token":                                    "http://example.com?title=[FILTERED]",
    28  		"http://example.com?authenticity_token=1":                           "http://example.com?authenticity_token=[FILTERED]",
    29  		"http://example.com?private_token=1":                                "http://example.com?private_token=[FILTERED]",
    30  		"http://example.com?rss_token=1":                                    "http://example.com?rss_token=[FILTERED]",
    31  		"http://example.com?access_token=1":                                 "http://example.com?access_token=[FILTERED]",
    32  		"http://example.com?refresh_token=1":                                "http://example.com?refresh_token=[FILTERED]",
    33  		"http://example.com?foo&authenticity_token=blahblah&bar":            "http://example.com?foo&authenticity_token=[FILTERED]&bar",
    34  		"http://example.com?private-token=1":                                "http://example.com?private-token=[FILTERED]",
    35  		"http://example.com?foo&private-token=blahblah&bar":                 "http://example.com?foo&private-token=[FILTERED]&bar",
    36  		"http://example.com?private-token=foo&authenticity_token=bar":       "http://example.com?private-token=[FILTERED]&authenticity_token=[FILTERED]",
    37  		"https://example.com:8080?private-token=foo&authenticity_token=bar": "https://example.com:8080?private-token=[FILTERED]&authenticity_token=[FILTERED]",
    38  		"/?private-token=foo&authenticity_token=bar":                        "/?private-token=[FILTERED]&authenticity_token=[FILTERED]",
    39  		"?private-token=&authenticity_token=&bar":                           "?private-token=[FILTERED]&authenticity_token=[FILTERED]&bar",
    40  		"?private-token=foo&authenticity_token=bar":                         "?private-token=[FILTERED]&authenticity_token=[FILTERED]",
    41  		"?private_token=foo&authenticity-token=bar":                         "?private_token=[FILTERED]&authenticity-token=[FILTERED]",
    42  		"?X-AMZ-Signature=foo":                                              "?X-AMZ-Signature=[FILTERED]",
    43  		"?x-amz-signature=foo":                                              "?x-amz-signature=[FILTERED]",
    44  		"?Signature=foo":                                                    "?Signature=[FILTERED]",
    45  		"?confirmation_password=foo":                                        "?confirmation_password=[FILTERED]",
    46  		"?pos_secret_number=foo":                                            "?pos_secret_number=[FILTERED]",
    47  		"?sharedSecret=foo":                                                 "?sharedSecret=[FILTERED]",
    48  		"?book_key=foo":                                                     "?book_key=[FILTERED]",
    49  		"?certificate=foo":                                                  "?certificate=[FILTERED]",
    50  		"?hook=foo":                                                         "?hook=[FILTERED]",
    51  		"?import_url=foo":                                                   "?import_url=[FILTERED]",
    52  		"?elasticsearch_url=foo":                                            "?elasticsearch_url=[FILTERED]",
    53  		"?otp_attempt=foo":                                                  "?otp_attempt=[FILTERED]",
    54  		"?sentry_dsn=foo":                                                   "?sentry_dsn=[FILTERED]",
    55  		"?trace=foo":                                                        "?trace=[FILTERED]",
    56  		"?variables=foo":                                                    "?variables=[FILTERED]",
    57  		"?content=foo":                                                      "?content=[FILTERED]",
    58  		"?content=e=mc2":                                                    "?content=[FILTERED]",
    59  		"?formula=e=mc2":                                                    "?formula=e=mc2",
    60  		"http://%41:8080/":                                                  "<invalid URL>",
    61  		"?redirect=http://example.com/":                                     "?redirect=[FILTERED]",
    62  		"https://gitlab.com?name=andrew&password=1&secret=1&key=1&signature=1&authorization=1&note=1&certificate=1&encrypted_key=1&hook=1&import_url=1&otp_attempt=1&sentry_dsn=1&trace=1&variables=1&content=1&sharedsecret=1&real=1": "https://gitlab.com?name=andrew&password=[FILTERED]&secret=[FILTERED]&key=[FILTERED]&signature=[FILTERED]&authorization=[FILTERED]&note=[FILTERED]&certificate=[FILTERED]&encrypted_key=[FILTERED]&hook=[FILTERED]&import_url=[FILTERED]&otp_attempt=[FILTERED]&sentry_dsn=[FILTERED]&trace=[FILTERED]&variables=[FILTERED]&content=[FILTERED]&sharedsecret=[FILTERED]&real=1", // nolint:lll
    63  	}
    64  
    65  	for url, want := range tests {
    66  		t.Run(url, func(t *testing.T) {
    67  			got := URL(url)
    68  			require.Equal(t, want, got)
    69  		})
    70  	}
    71  }