Skip to content
Merged
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
25 changes: 24 additions & 1 deletion uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,11 @@ func (u *URI) parse(host, uri []byte, isTLS bool) error {
u.SetSchemeBytes(strHTTPS)
}

if n := bytes.IndexByte(host, '@'); n >= 0 {
if n := bytes.LastIndexByte(host, '@'); n >= 0 {
auth := host[:n]
if !validUserinfo(auth) {
return ErrorInvalidURI
}
host = host[n+1:]

if n := bytes.IndexByte(auth, ':'); n >= 0 {
Expand Down Expand Up @@ -356,6 +359,26 @@ func (u *URI) parse(host, uri []byte, isTLS bool) error {
return nil
}

func validUserinfo(userinfo []byte) bool {
for _, c := range userinfo {
switch {
Comment on lines +362 to +364
Copy link
Preview

Copilot AI Sep 28, 2025

Choose a reason for hiding this comment

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

The validUserinfo function lacks documentation. Add a comment explaining what characters are considered valid for userinfo according to the RFC specification and any security considerations.

Suggested change
func validUserinfo(userinfo []byte) bool {
for _, c := range userinfo {
switch {
// validUserinfo checks whether the provided userinfo byte slice contains only valid characters
// as specified by RFC 3986 section 3.2.1. The userinfo component may contain unreserved characters
// (A-Z, a-z, 0-9, '-', '.', '_', '~'), sub-delimiters ('!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '='),
// as well as ':' and '@'. Percent-encoded characters ('%') are also allowed.
//
// Security considerations: Proper validation of userinfo is important to prevent injection attacks
// or misinterpretation of URIs. This function ensures that only valid characters are accepted.
func validUserinfo(userinfo []byte) bool {
for _, c := range userinfo {

Copilot uses AI. Check for mistakes.

case 'A' <= c && c <= 'Z':
continue
case 'a' <= c && c <= 'z':
continue
case '0' <= c && c <= '9':
continue
}
switch c {
case '-', '.', '_', ':', '~', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', '%', '@':
continue
default:
return false
}
}
return true
}

// parseHost parses host as an authority without user
// information. That is, as host[:port].
//
Expand Down
38 changes: 38 additions & 0 deletions uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,44 @@ func testURIUpdate(t *testing.T, base, update, result string) {
}
}

func TestURIRejectInvalidUserinfo(t *testing.T) {
t.Parallel()

bad := []string{
"http://[normal.com@]vulndetector.com/",
"http://normal.com[user@vulndetector].com/",
"http://normal.com[@]vulndetector.com/",
}

for _, raw := range bad {
var u URI
if err := u.Parse(nil, []byte(raw)); err == nil {
t.Fatalf("expected error parsing %q", raw)
}
}
}

func TestURIAllowAtInUserinfo(t *testing.T) {
t.Parallel()

var u URI
if err := u.Parse(nil, []byte("http://user:p@[email protected]/")); err != nil {
t.Fatalf("unexpected error: %v", err)
}

if got := string(u.Host()); got != "example.com" {
t.Fatalf("unexpected host %q", got)
}

if got := string(u.Username()); got != "user" {
t.Fatalf("unexpected username %q", got)
}

if got := string(u.Password()); got != "p@ss" {
t.Fatalf("unexpected password %q", got)
}
}

func TestURIPathNormalize(t *testing.T) {
if runtime.GOOS == "windows" {
t.SkipNow()
Expand Down
Loading