From 354213c5bce2c58d69c051f3e20d41759a66a9a6 Mon Sep 17 00:00:00 2001 From: Jeremy Cocks Date: Fri, 13 Nov 2026 18:29:47 +0000 Subject: [PATCH 1/2] Normalize @authority according to RFC 9421 --- httpparse.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/httpparse.go b/httpparse.go index 6807468..6ff5b2b 100644 --- a/httpparse.go +++ b/httpparse.go @@ -105,10 +105,18 @@ func derivedComponent(name, v string, components components) { } func generateReqDerivedComponents(method string, u *url.URL, authority string, components components) { + canonicalAuthority := strings.ToLower(authority) + switch strings.ToLower(u.Scheme) { + case "https": + canonicalAuthority = strings.TrimSuffix(canonicalAuthority, ":443") + case "http": + canonicalAuthority = strings.TrimSuffix(canonicalAuthority, ":80") + } + derivedComponent("@method", method, components) derivedComponent("@target-uri", scTargetURI(u), components) derivedComponent("@path", scPath(u), components) - derivedComponent("@authority", authority, components) + derivedComponent("@authority", canonicalAuthority, components) derivedComponent("@scheme", scScheme(u), components) derivedComponent("@request-target", scRequestTarget(u), components) derivedComponent("@query", scQuery(u), components) From ae5b6d69bddd41188a15ff3cc82b98c1fbd29658 Mon Sep 17 00:00:00 2001 From: Yaron Sheffer Date: Sat, 14 Nov 2026 08:20:21 -0500 Subject: [PATCH 2/2] Normalize @authority via SplitHostPort; add tests and upgrade notes. Co-authored-by: Cursor --- httpparse.go | 37 ++++++++--- httpparse_test.go | 108 ++++++++++++++++++++++++++++++++ internal-docs/RELEASE-v0.6.3.md | 19 ++++++ 3 files changed, 155 insertions(+), 9 deletions(-) create mode 100644 httpparse_test.go create mode 100644 internal-docs/RELEASE-v0.6.3.md diff --git a/httpparse.go b/httpparse.go index 6ff5b2b..d1d274d 100644 --- a/httpparse.go +++ b/httpparse.go @@ -2,6 +2,7 @@ package httpsign import ( "fmt" + "net" "net/http" "net/url" "strconv" @@ -105,23 +106,41 @@ func derivedComponent(name, v string, components components) { } func generateReqDerivedComponents(method string, u *url.URL, authority string, components components) { - canonicalAuthority := strings.ToLower(authority) - switch strings.ToLower(u.Scheme) { - case "https": - canonicalAuthority = strings.TrimSuffix(canonicalAuthority, ":443") - case "http": - canonicalAuthority = strings.TrimSuffix(canonicalAuthority, ":80") - } - derivedComponent("@method", method, components) derivedComponent("@target-uri", scTargetURI(u), components) derivedComponent("@path", scPath(u), components) - derivedComponent("@authority", canonicalAuthority, components) + derivedComponent("@authority", canonicalAuthority(u.Scheme, authority), components) derivedComponent("@scheme", scScheme(u), components) derivedComponent("@request-target", scRequestTarget(u), components) derivedComponent("@query", scQuery(u), components) } +// canonicalAuthority normalizes @authority per RFC 9421 §2.2.3 / HTTP §4.2.3: +// lowercase hostname, omit default port (443 for https, 80 for http). +func canonicalAuthority(scheme, authority string) string { + scheme = strings.ToLower(scheme) + host, port, err := net.SplitHostPort(authority) + if err != nil { + // No port present (or not host:port form). Case-fold only. + return strings.ToLower(authority) + } + host = strings.ToLower(host) + if (scheme == "https" && port == "443") || (scheme == "http" && port == "80") { + // SplitHostPort returns an unbracketed IPv6 literal; Host-form @authority needs brackets. + return formatHostPortless(host) + } + return net.JoinHostPort(host, port) +} + +// formatHostPortless returns host for an authority with no port. +// IPv6 literals are wrapped in brackets; names and IPv4 are unchanged. +func formatHostPortless(host string) string { + if ip := net.ParseIP(host); ip != nil && ip.To4() == nil { + return "[" + host + "]" + } + return host +} + func scPath(theURL *url.URL) string { return theURL.EscapedPath() } diff --git a/httpparse_test.go b/httpparse_test.go new file mode 100644 index 0000000..fc9fd85 --- /dev/null +++ b/httpparse_test.go @@ -0,0 +1,108 @@ +package httpsign + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestCanonicalAuthority(t *testing.T) { + t.Parallel() + + cases := []struct { + name string + scheme string + authority string + want string + }{ + {name: "lowercase host", scheme: "https", authority: "EXAMPLE.com", want: "example.com"}, + {name: "https default port omitted", scheme: "https", authority: "EXAMPLE.com:443", want: "example.com"}, + {name: "http default port omitted", scheme: "http", authority: "EXAMPLE.com:80", want: "example.com"}, + {name: "https non-default port kept", scheme: "https", authority: "example.com:8443", want: "example.com:8443"}, + {name: "http non-default port kept", scheme: "http", authority: "example.com:8080", want: "example.com:8080"}, + {name: "https non-default port case fold", scheme: "https", authority: "EXAMPLE.com:8443", want: "example.com:8443"}, + {name: "http non-default port case fold", scheme: "http", authority: "EXAMPLE.com:8080", want: "example.com:8080"}, + {name: "http with 443 kept", scheme: "http", authority: "example.com:443", want: "example.com:443"}, + {name: "https with 80 kept", scheme: "https", authority: "example.com:80", want: "example.com:80"}, + {name: "scheme case insensitive", scheme: "HTTPS", authority: "Example.COM:443", want: "example.com"}, + {name: "ipv4 no port", scheme: "https", authority: "192.0.2.10", want: "192.0.2.10"}, + {name: "ipv4 https default port omitted", scheme: "https", authority: "192.0.2.10:443", want: "192.0.2.10"}, + {name: "ipv4 http default port omitted", scheme: "http", authority: "192.0.2.10:80", want: "192.0.2.10"}, + {name: "ipv4 https non-default port", scheme: "https", authority: "192.0.2.10:8443", want: "192.0.2.10:8443"}, + {name: "ipv4 http non-default port", scheme: "http", authority: "192.0.2.10:8080", want: "192.0.2.10:8080"}, + {name: "ipv6 default https port", scheme: "https", authority: "[2001:db8::1]:443", want: "[2001:db8::1]"}, + {name: "ipv6 default http port", scheme: "http", authority: "[2001:db8::1]:80", want: "[2001:db8::1]"}, + {name: "ipv6 hextet 443 unchanged", scheme: "https", authority: "[2001:db8::443]", want: "[2001:db8::443]"}, + {name: "ipv6 hextet 80 unchanged", scheme: "http", authority: "[2001:db8::80]", want: "[2001:db8::80]"}, + {name: "ipv6 https non-default port", scheme: "https", authority: "[2001:db8::1]:8443", want: "[2001:db8::1]:8443"}, + {name: "ipv6 http non-default port", scheme: "http", authority: "[2001:db8::1]:8080", want: "[2001:db8::1]:8080"}, + {name: "empty", scheme: "https", authority: "", want: ""}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.want, canonicalAuthority(tc.scheme, tc.authority)) + }) + } +} + +func TestDerivedAuthorityNonDefaultPort(t *testing.T) { + t.Parallel() + + cases := []struct { + name string + url string + host string + scheme string + wantAuth string + }{ + { + name: "https custom port", + url: "https://example.com:8443/path", + host: "EXAMPLE.com:8443", + scheme: "https", + wantAuth: "example.com:8443", + }, + { + name: "http custom port", + url: "http://example.com:8080/path", + host: "EXAMPLE.com:8080", + scheme: "http", + wantAuth: "example.com:8080", + }, + { + name: "https default port still omitted", + url: "https://example.com/path", + host: "EXAMPLE.com:443", + scheme: "https", + wantAuth: "example.com", + }, + { + name: "ipv4 https custom port", + url: "https://192.0.2.10:8443/path", + host: "192.0.2.10:8443", + scheme: "https", + wantAuth: "192.0.2.10:8443", + }, + { + name: "ipv4 https default port omitted", + url: "https://192.0.2.10/path", + host: "192.0.2.10:443", + scheme: "https", + wantAuth: "192.0.2.10", + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + req, err := http.NewRequest("GET", tc.url, nil) + require.NoError(t, err) + req.Host = tc.host + msg, err := parseRequest(req, false, 0, tc.scheme) + require.NoError(t, err) + assert.Equal(t, tc.wantAuth, msg.derived["@authority"]) + }) + } +} diff --git a/internal-docs/RELEASE-v0.6.3.md b/internal-docs/RELEASE-v0.6.3.md new file mode 100644 index 0000000..ce0a072 --- /dev/null +++ b/internal-docs/RELEASE-v0.6.3.md @@ -0,0 +1,19 @@ +# Release notes: httpsign v0.6.3 (draft) + +Draft for the next release after v0.6.2. Update when cutting the tag. + +--- + +## Summary + +### `@authority` normalization (RFC 9421 §2.2.3) + +`@authority` is now normalized before it enters the signature base: the hostname is lowercased, and the default port is omitted (`443` for `https`, `80` for `http`). See [#44](https://github.com/yaronf/httpsign/pull/44). + +### Upgrade notes / behavioral change + +Sign and verify both use the same normalization, so pairs that upgrade together stay consistent. + +**Cross-version mismatch:** if one side still emits a non-normalized authority (for example `EXAMPLE.com:443`) and the other side verifies with this release (`example.com`), signatures that cover `@authority` will fail. Align both peers on a build that includes this change, or ensure the Host / authority value is already RFC-normalized before signing. + +Hosts that were already lowercase and omitted the default port are unaffected.