Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"net"
"net/http"
"net/url"
"regexp"
"strings"

"github.com/opentdf/platform/lib/ocrypto"
"github.com/opentdf/platform/protocol/go/authorization"
Expand Down Expand Up @@ -207,17 +207,24 @@ func New(platformEndpoint string, opts ...Option) (*SDK, error) {

func SanitizePlatformEndpoint(e string) (string, error) {
// check if there's a scheme, if not, add https
if !regexp.MustCompile(`^https?://`).MatchString(e) {
e = "https://" + e
u, err := url.ParseRequestURI(e)
if err != nil {
return "", errors.Join(fmt.Errorf("cannot parse platform endpoint [%s]", e), err)
}

if !regexp.MustCompile(`^(https?:\/\/)?(([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(:\d+)?|(localhost)(:\d+)?)\/?$`).MatchString(e) {
return "", errors.New("platform endpoint is not valid")
if u.Host == "" {
// if the schema is missing add https. when the schema is missing the host is parsed as the scheme
newE := "https://" + e
u, err = url.ParseRequestURI(newE)
if err != nil {
return "", errors.Join(fmt.Errorf("cannot parse platform endpoint [%s]", newE), err)
}
if u.Host == "" {
return "", fmt.Errorf("invalid URL [%s], got empty hostname", newE)
}
}

u, err := url.ParseRequestURI(e)
if err != nil {
return "", errors.Join(fmt.Errorf("cannot parse platform endpoint(%s)", e), err)
if strings.Contains(u.Hostname(), ":") {
return "", fmt.Errorf("invalid hostname [%s]. IPv6 addresses are not supported", u.Hostname())
}

p := u.Port()
Expand Down
10 changes: 10 additions & 0 deletions sdk/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,16 @@ func Test_ShouldSanitizePlatformEndpoint(t *testing.T) {
endpoint: "https://localhost",
expected: "localhost:443",
},
{
name: "HTTPS scheme port (IP)",
endpoint: "https://192.168.1.1:8080",
expected: "192.168.1.1:8080",
},
{
name: "HTTPS scheme no port (IP)",
endpoint: "https://192.168.1.1",
expected: "192.168.1.1:443",
},
{
name: "Malformed url",
endpoint: "http://localhost:8080:8080",
Expand Down
Loading