diff --git a/httpparse.go b/httpparse.go index 6807468..c65c38d 100644 --- a/httpparse.go +++ b/httpparse.go @@ -137,7 +137,8 @@ func scScheme(url *url.URL) string { if url.Scheme == "" { return "http" } - return url.Scheme + // RFC 9421 §2.2.4: scheme MUST be lowercase in the signature base. + return strings.ToLower(url.Scheme) } func scTargetURI(url *url.URL) string { diff --git a/httpparse_test.go b/httpparse_test.go new file mode 100644 index 0000000..30ab73b --- /dev/null +++ b/httpparse_test.go @@ -0,0 +1,86 @@ +package httpsign + +import ( + "encoding/base64" + "net/http" + "net/url" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestScScheme(t *testing.T) { + t.Parallel() + + cases := []struct { + name string + u *url.URL + want string + }{ + {name: "empty defaults to http", u: &url.URL{}, want: "http"}, + {name: "http lowercase", u: &url.URL{Scheme: "http"}, want: "http"}, + {name: "https lowercase", u: &url.URL{Scheme: "https"}, want: "https"}, + {name: "HTTP upper", u: &url.URL{Scheme: "HTTP"}, want: "http"}, + {name: "HTTPS upper", u: &url.URL{Scheme: "HTTPS"}, want: "https"}, + {name: "Https mixed", u: &url.URL{Scheme: "Https"}, want: "https"}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.want, scScheme(tc.u)) + }) + } +} + +func TestDerivedSchemeLowercase(t *testing.T) { + t.Parallel() + + cases := []struct { + name string + url string + schemeOverride string + want string + }{ + {name: "https from URL", url: "https://example.com/x", schemeOverride: "", want: "https"}, + {name: "override HTTPS folded", url: "http://example.com/x", schemeOverride: "HTTPS", want: "https"}, + {name: "override HTTP folded", url: "https://example.com/x", schemeOverride: "HTTP", want: "http"}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + req, err := http.NewRequest("GET", tc.url, nil) + require.NoError(t, err) + if tc.schemeOverride != "" { + req.URL.Scheme = "" + } + msg, err := parseRequest(req, false, 0, tc.schemeOverride) + require.NoError(t, err) + assert.Equal(t, tc.want, msg.derived["@scheme"]) + }) + } +} + +func TestSchemeFromRequestFoldedInSignatureBase(t *testing.T) { + t.Parallel() + + req, err := http.NewRequest("GET", "http://example.com/x", nil) + require.NoError(t, err) + req.URL.Scheme = "" + + key, err := base64.StdEncoding.DecodeString("uzvJfB4u3N0Jy4T7NZ75MDVcr8zSTInedJtkgcu46YW4XByzNJjxBdtjUkdJPBtbmHhIDi6pcl8jsasjlTMtDQ==") + require.NoError(t, err) + + fields := Headers("@scheme") + signer, err := NewHMACSHA256Signer(key, + NewSignConfig().SignCreated(false).SignAlg(false).SetSchemeFromRequest(func(*http.Request) string { + return "HTTPS" + }), + fields) + require.NoError(t, err) + + _, _, base, err := signRequestDebug("sig1", *signer, req) + require.NoError(t, err) + assert.Contains(t, base, `"@scheme": https`) + assert.NotContains(t, base, `"@scheme": HTTPS`) +} diff --git a/internal-docs/RELEASE-v0.6.3.md b/internal-docs/RELEASE-v0.6.3.md new file mode 100644 index 0000000..26f3e62 --- /dev/null +++ b/internal-docs/RELEASE-v0.6.3.md @@ -0,0 +1,37 @@ +# Release notes: httpsign v0.6.3 (draft) + +Draft for the next release after v0.6.2. Update when cutting the tag. + +--- + +## Summary + +Derived-component normalization fixes for RFC 9421. Sign and verify use the same rules, so pairs that upgrade together stay consistent. + +### `@authority` normalization (RFC 9421 §2.2.3) + +Hostname is lowercased; default port is omitted (`443` for `https`, `80` for `http`). See [#44](https://github.com/yaronf/httpsign/pull/44). + +### `@request-target` / `@path` encoding (RFC 9421 §2.2.5–2.2.6) + +`@request-target` keeps percent-encoded path octets (`EscapedPath`) and a trailing empty query delimiter (`ForceQuery`). Empty `@path` normalizes to `/`. See [#43](https://github.com/yaronf/httpsign/pull/43). + +### `@scheme` lowercase (RFC 9421 §2.2.4) + +`@scheme` is lowercased before it enters the signature base, including values from `WithScheme` / `SetSchemeFromRequest`. See [#45](https://github.com/yaronf/httpsign/issues/45). + +### Upgrade notes / behavioral change + +**Cross-version mismatch:** if one peer still emits a non-normalized component and the other verifies with this release, signatures that cover that component will fail. Examples: + +| Component | Old value (example) | This release | +|-----------|---------------------|--------------| +| `@authority` | `EXAMPLE.com:443` | `example.com` | +| `@request-target` | `/a/b` from `/a%2Fb` | `/a%2Fb` | +| `@request-target` | `/path` from `/path?` | `/path?` | +| `@path` | empty string | `/` | +| `@scheme` | `HTTPS` | `https` | + +Align both peers on a build that includes these changes, or ensure inputs are already RFC-normalized before signing. + +Values that were already compliant are unaffected.