Skip to content

Commit 65d3b33

Browse files
AnkanMisracodex
andcommitted
fix: canonicalize payment audience default ports
Co-authored-by: codex <codex@users.noreply.github.com>
1 parent 2d31b85 commit 65d3b33

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

gateway/config.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"log"
6+
"net"
67
"net/url"
78
"os"
89
"strings"
@@ -79,7 +80,18 @@ func normalizePaygateAudience() error {
7980
return fmt.Errorf("PAYGATE_AUDIENCE must contain an origin only")
8081
}
8182

82-
canonical := parsed.Scheme + "://" + strings.ToLower(parsed.Host)
83+
hostname := strings.ToLower(parsed.Hostname())
84+
port := parsed.Port()
85+
if (parsed.Scheme == "https" && port == "443") || (parsed.Scheme == "http" && port == "80") {
86+
port = ""
87+
}
88+
canonicalHost := hostname
89+
if port != "" {
90+
canonicalHost = net.JoinHostPort(hostname, port)
91+
} else if strings.Contains(hostname, ":") {
92+
canonicalHost = "[" + hostname + "]"
93+
}
94+
canonical := parsed.Scheme + "://" + canonicalHost
8395
if err := os.Setenv("PAYGATE_AUDIENCE", canonical); err != nil {
8496
return fmt.Errorf("failed to normalize PAYGATE_AUDIENCE: %w", err)
8597
}

gateway/config_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ func TestNormalizePaygateAudience(t *testing.T) {
4040
}{
4141
{name: "canonical origin", value: "https://gateway.example.com", want: "https://gateway.example.com"},
4242
{name: "normalizes case and trailing slash", value: " HTTPS://Gateway.Example.COM/ ", want: "https://gateway.example.com"},
43+
{name: "strips default HTTPS port", value: "https://gateway.example.com:443", want: "https://gateway.example.com"},
44+
{name: "strips default HTTP port", value: "http://localhost:80", want: "http://localhost"},
45+
{name: "preserves non-default port", value: "https://gateway.example.com:8443", want: "https://gateway.example.com:8443"},
46+
{name: "normalizes IPv6 default port", value: "https://[::1]:443", want: "https://[::1]"},
47+
{name: "preserves IPv6 non-default port", value: "https://[::1]:8443", want: "https://[::1]:8443"},
4348
{name: "rejects path", value: "https://gateway.example.com/api", wantErr: "origin only"},
4449
{name: "rejects query", value: "https://gateway.example.com?tenant=a", wantErr: "origin only"},
4550
{name: "rejects credentials", value: "https://user@gateway.example.com", wantErr: "credentials"},

0 commit comments

Comments
 (0)