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
9 changes: 9 additions & 0 deletions cmd/client_targets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -64,6 +65,11 @@ example.org:8443 # inline comment
args: []string{"example.com:abc"},
wantErr: true,
},
{
name: "reject URL input",
args: []string{"https://example.com"},
wantErr: true,
},
}

for _, tt := range tests {
Expand All @@ -73,6 +79,9 @@ example.org:8443 # inline comment
if err == nil {
t.Fatalf("expected error, got nil")
}
if len(tt.args) == 1 && strings.Contains(tt.args[0], "://") && !strings.Contains(err.Error(), "expected host[:port], not a URL") {
t.Fatalf("expected concise URL error, got %v", err)
}
return
}
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions internal/cli/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net"
"strconv"
"strings"

"github.com/catay/tlsctl/internal/tlsquery"
)
Expand All @@ -12,6 +13,10 @@ import (
// An optional startTLSProto can be provided to select the default port
// for STARTTLS protocols (smtp=587, imap=143, pop3=110, ldap=389).
func NormalizeEndpoint(endpoint string, startTLSProto ...string) (string, error) {
if looksLikeURL(endpoint) {
return "", fmt.Errorf("expected host[:port], not a URL")
}

host, port, err := net.SplitHostPort(endpoint)
if err != nil {
host = endpoint
Expand All @@ -34,6 +39,13 @@ func NormalizeEndpoint(endpoint string, startTLSProto ...string) (string, error)
return net.JoinHostPort(host, port), nil
}

func looksLikeURL(endpoint string) bool {
if strings.Contains(endpoint, "://") {
return true
}
return strings.ContainsAny(endpoint, "/?#@")
}

func defaultPort(startTLSProto ...string) string {
if len(startTLSProto) > 0 {
if port, ok := tlsquery.StartTLSPort(startTLSProto[0]); ok {
Expand Down
30 changes: 30 additions & 0 deletions internal/cli/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,36 @@ func TestNormalizeEndpoint(t *testing.T) {
endpoint: "[2001:db8::1]:8443",
want: "[2001:db8::1]:8443",
},
{
name: "reject URL scheme",
endpoint: "https://example.com",
wantError: true,
errorMsg: "expected host[:port], not a URL",
},
{
name: "reject path",
endpoint: "example.com/path",
wantError: true,
errorMsg: "expected host[:port], not a URL",
},
{
name: "reject query",
endpoint: "example.com?foo=bar",
wantError: true,
errorMsg: "expected host[:port], not a URL",
},
{
name: "reject fragment",
endpoint: "example.com#anchor",
wantError: true,
errorMsg: "expected host[:port], not a URL",
},
{
name: "reject userinfo",
endpoint: "user@example.com",
wantError: true,
errorMsg: "expected host[:port], not a URL",
},
}

for _, tt := range tests {
Expand Down
Loading