Skip to content
Open
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
29 changes: 28 additions & 1 deletion httpparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package httpsign

import (
"fmt"
"net"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -108,12 +109,38 @@ func generateReqDerivedComponents(method string, u *url.URL, authority string, c
derivedComponent("@method", method, components)
derivedComponent("@target-uri", scTargetURI(u), components)
derivedComponent("@path", scPath(u), components)
derivedComponent("@authority", authority, 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()
}
Expand Down
108 changes: 108 additions & 0 deletions httpparse_test.go
Original file line number Diff line number Diff line change
@@ -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"])
})
}
}
19 changes: 19 additions & 0 deletions internal-docs/RELEASE-v0.6.3.md
Original file line number Diff line number Diff line change
@@ -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.
Loading