github.com/newrelic/go-agent@v3.26.0+incompatible/internal/url.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package internal
     5  
     6  import "net/url"
     7  
     8  // SafeURL removes sensitive information from a URL.
     9  func SafeURL(u *url.URL) string {
    10  	if nil == u {
    11  		return ""
    12  	}
    13  	if "" != u.Opaque {
    14  		// If the URL is opaque, we cannot be sure if it contains
    15  		// sensitive information.
    16  		return ""
    17  	}
    18  
    19  	// Omit user, query, and fragment information for security.
    20  	ur := url.URL{
    21  		Scheme: u.Scheme,
    22  		Host:   u.Host,
    23  		Path:   u.Path,
    24  	}
    25  	return ur.String()
    26  }
    27  
    28  // SafeURLFromString removes sensitive information from a URL.
    29  func SafeURLFromString(rawurl string) string {
    30  	u, err := url.Parse(rawurl)
    31  	if nil != err {
    32  		return ""
    33  	}
    34  	return SafeURL(u)
    35  }
    36  
    37  // HostFromURL returns the URL's host.
    38  func HostFromURL(u *url.URL) string {
    39  	if nil == u {
    40  		return ""
    41  	}
    42  	if "" != u.Opaque {
    43  		return "opaque"
    44  	}
    45  	return u.Host
    46  }