|
1 | 1 | package connector |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "net" |
| 6 | + "net/url" |
| 7 | + "strings" |
| 8 | + |
4 | 9 | v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" |
5 | 10 | "github.com/conductorone/baton-sdk/pkg/annotations" |
6 | 11 | "github.com/conductorone/baton-sdk/pkg/pagination" |
@@ -30,3 +35,107 @@ func parsePageToken(i string, resourceID *v2.ResourceId) (*pagination.Bag, strin |
30 | 35 |
|
31 | 36 | return b, b.PageToken(), nil |
32 | 37 | } |
| 38 | + |
| 39 | +var disallowedSchemesAsHostnames = map[string]bool{ |
| 40 | + "javascript": true, |
| 41 | + "mailto": true, |
| 42 | + "ftp": true, |
| 43 | + "file": true, |
| 44 | +} |
| 45 | + |
| 46 | +const suffix = "onelogin.com" |
| 47 | + |
| 48 | +func sanitizeDomainInput(domain string) (string, error) { |
| 49 | + var host string |
| 50 | + var port string |
| 51 | + |
| 52 | + if strings.Contains(domain, "//") { |
| 53 | + u, err := url.Parse(domain) |
| 54 | + if err != nil { |
| 55 | + return "", fmt.Errorf("invalid URL format: %w", err) |
| 56 | + } |
| 57 | + scheme := strings.ToLower(u.Scheme) |
| 58 | + |
| 59 | + if scheme != "http" && scheme != "https" && scheme != "htp" && scheme != "htps" { |
| 60 | + return "", fmt.Errorf("unsupported URL scheme: '%s'", u.Scheme) |
| 61 | + } |
| 62 | + host = u.Hostname() |
| 63 | + port = u.Port() |
| 64 | + if host == "" { |
| 65 | + return "", fmt.Errorf("could not extract hostname from URL: '%s'", domain) |
| 66 | + } |
| 67 | + } else { |
| 68 | + // No "://" found, try to parse as host or host:port |
| 69 | + tempHost, tempPort, err := net.SplitHostPort(domain) |
| 70 | + if err == nil { |
| 71 | + // Successfully split host and port |
| 72 | + // Check if the extracted 'host' part looks like a disallowed scheme |
| 73 | + if disallowedSchemesAsHostnames[strings.ToLower(tempHost)] { |
| 74 | + return "", fmt.Errorf("invalid hostname part, looks like a scheme: '%s'", tempHost) |
| 75 | + } |
| 76 | + host = tempHost |
| 77 | + port = tempPort |
| 78 | + } else { |
| 79 | + // Could not split host:port, assume the whole input is the host |
| 80 | + // But first, check if it contains a colon, which might indicate an invalid scheme attempt without :// |
| 81 | + if strings.Contains(domain, ":") { |
| 82 | + firstPart := strings.SplitN(domain, ":", 2)[0] |
| 83 | + if disallowedSchemesAsHostnames[strings.ToLower(firstPart)] { |
| 84 | + return "", fmt.Errorf("invalid input, looks like a scheme without '://': '%s'", firstPart) |
| 85 | + } |
| 86 | + } |
| 87 | + host = domain |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + host = strings.TrimSpace(host) |
| 92 | + if host == "" { |
| 93 | + return "", fmt.Errorf("hostname could not be determined from input: '%s'", domain) |
| 94 | + } |
| 95 | + |
| 96 | + host = strings.ToLower(host) |
| 97 | + parts := strings.Split(host, ".") |
| 98 | + |
| 99 | + didAppendSuffix := false |
| 100 | + // Handle single component expansion (e.g., "acme" -> "acme.okta.com") |
| 101 | + if len(parts) == 1 && parts[0] != "" { |
| 102 | + host = strings.Join(append(parts, suffix), ".") // Update host for suffix check |
| 103 | + didAppendSuffix = true |
| 104 | + } |
| 105 | + |
| 106 | + // Suffix validation |
| 107 | + |
| 108 | + matched := false |
| 109 | + |
| 110 | + if host == suffix { |
| 111 | + if didAppendSuffix { // Case: single input component + appendSuffix resulted in exact match |
| 112 | + matched = true |
| 113 | + } else if len(parts) > strings.Count(suffix, ".")+1 { |
| 114 | + // Case: multi-component input (or no appendSuffix) was an exact match for suffix |
| 115 | + // 'parts' here refers to the components of the original host before any appendSuffix. |
| 116 | + matched = true |
| 117 | + } |
| 118 | + } else if strings.HasSuffix(host, "."+suffix) { |
| 119 | + // Check that there's something before the suffix |
| 120 | + if len(strings.TrimSuffix(host, "."+suffix)) > 0 { |
| 121 | + matched = true |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if !matched { |
| 126 | + return "", fmt.Errorf("domain '%s' does not have an expected suffix (e.g., %s)", host, suffix) |
| 127 | + } |
| 128 | + |
| 129 | + // Re-split parts after potential single-component expansion for remapping/validation |
| 130 | + parts = strings.Split(host, ".") |
| 131 | + |
| 132 | + normalizedHost := strings.Join(parts, ".") |
| 133 | + |
| 134 | + if port != "" && port != "80" && port != "443" { |
| 135 | + normalizedHost = net.JoinHostPort(normalizedHost, port) |
| 136 | + } |
| 137 | + |
| 138 | + subdomain := strings.TrimSuffix(normalizedHost, ".onelogin.com") |
| 139 | + |
| 140 | + return subdomain, nil |
| 141 | +} |
0 commit comments