Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 37 additions & 0 deletions middleware/hostauthorization/hostauthorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,46 @@ func parseNormalizedAuthority(authority string) (string, bool) {
return "", false
}

if !isValidHostSyntax(host) {
return "", false
}

return host, true
}

func isValidHostSyntax(host string) bool {
if host == "" {
return false
}

// IPv4 / IPv6 literals.
if net.ParseIP(host) != nil {
return true
}

if strings.HasPrefix(host, ".") || strings.HasSuffix(host, ".") {
return false
}

for label := range strings.SplitSeq(host, ".") {
if label == "" || len(label) > maxLabelLength {
return false
}
if label[0] == '-' || label[len(label)-1] == '-' {
return false
}
for i := 0; i < len(label); i++ {
ch := label[i]
if (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '-' {
continue
}
return false
}
}

return true
}
Comment on lines +194 to +225
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The current implementation of isValidHostSyntax uses strings.SplitSeq to split the host into labels. Since this middleware runs on the hot path of every incoming HTTP request, avoiding heap allocations and iterator overhead is crucial for performance. We can validate the host syntax in a single pass with zero allocations by iterating over the string characters directly.

func isValidHostSyntax(host string) bool {
	if host == "" {
		return false
	}

	// IPv4 / IPv6 literals.
	if net.ParseIP(host) != nil {
		return true
	}

	if strings.HasPrefix(host, ".") || strings.HasSuffix(host, ".") {
		return false
	}

	labelLen := 0
	for i := 0; i < len(host); i++ {
		ch := host[i]
		if ch == '.' {
			if labelLen == 0 || labelLen > maxLabelLength {
				return false
			}
			if host[i-labelLen] == '-' || host[i-1] == '-' {
				return false
			}
			labelLen = 0
			continue
		}

		if (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '-' {
			labelLen++
			continue
		}
		return false
	}

	if labelLen == 0 || labelLen > maxLabelLength {
		return false
	}
	if host[len(host)-labelLen] == '-' || host[len(host)-1] == '-' {
		return false
	}

	return true
}


func isValidPort(raw string) bool {
if raw == "" || len(raw) > 5 {
return false
Expand Down
3 changes: 3 additions & 0 deletions middleware/hostauthorization/hostauthorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ func Test_ParseNormalizedAuthority(t *testing.T) {
{name: "bare ipv6", input: "::1", expectOK: false},
{name: "malformed bracket", input: "[::1", expectOK: false},
{name: "extra data after bracket", input: "[::1]extra", expectOK: false},
{name: "path delimiter in host", input: "attacker.test/.example.com", expectOK: false},
{name: "query delimiter in host", input: "attacker.test?.example.com", expectOK: false},
{name: "space in host", input: "bad host.example.com", expectOK: false},
}

for _, tt := range tests {
Expand Down
Loading