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
41 changes: 40 additions & 1 deletion policy/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import (
"fmt"
"io/fs"
"maps"
"net"
"net/netip"
"net/url"
"path"
"slices"
"strings"
"sync"
"time"
"unicode/utf8"

"github.com/containerd/platforms"
"github.com/distribution/reference"
Expand Down Expand Up @@ -532,6 +535,41 @@ func (p *Policy) Print(ctx print.Context, msg string) error {
return nil
}

func normalizeHTTPHost(u *url.URL) string {
host := u.Hostname()
addr, err := netip.ParseAddr(host)
if err == nil {
host = addr.String()
} else if utf8.ValidString(host) {
// Leave invalid UTF-8 hosts accepted by url.Parse unchanged, since
// strings.Map would replace invalid bytes with U+FFFD.
// Unicode case folding can change the IDNA destination (for example, İ to i).
host = strings.Map(func(r rune) rune {
if 'A' <= r && r <= 'Z' {
return r + ('a' - 'A')
}
return r
}, host)
}
port := u.Port()
if port != "" {
port = strings.TrimLeft(port, "0")
if port == "" {
port = "0"
}
}
if u.Scheme == "http" && port == "80" || u.Scheme == "https" && port == "443" {
port = ""
}
if port != "" || strings.HasSuffix(u.Host, ":") {
return net.JoinHostPort(host, port)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this join the normalised port? i.e., 000123 should likely be normalised to 123 to get a normalised reference.

}
if addr.Is6() {
return "[" + host + "]"
}
return host
}

func sourceToInput(ctx context.Context, getVerifier PolicyVerifierProvider, src *gwpb.ResolveSourceMetaResponse, platform *ocispecs.Platform, logf func(logrus.Level, string)) (Input, []string, error) {
var inp Input
var unknowns []string
Expand All @@ -544,6 +582,7 @@ func sourceToInput(ctx context.Context, getVerifier PolicyVerifierProvider, src
if !ok {
return inp, nil, errors.Errorf("invalid source identifier: %s", src.Source.Identifier)
}
scheme = strings.ToLower(scheme)

switch scheme {
case "http", "https":
Expand All @@ -554,7 +593,7 @@ func sourceToInput(ctx context.Context, getVerifier PolicyVerifierProvider, src
inp.HTTP = &HTTP{
URL: src.Source.Identifier,
Schema: scheme,
Host: u.Host,
Host: normalizeHTTPHost(u),
Path: u.Path,
Query: u.Query(),
}
Expand Down
Loading
Loading