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
6 changes: 6 additions & 0 deletions urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import (
"strings"
)

// Max length of the scpUrl to prevent reDOS attacks
Copy link
Copy Markdown
Contributor

@alokmenghrajani alokmenghrajani Jan 11, 2024

Choose a reason for hiding this comment

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

You should link to https://nvd.nist.gov/vuln/detail/CVE-2023-46402 and https://gist.github.com/6en6ar/7c2424c93e7fbf2b6fc44e7fb9acb95d in this comment so people reading the code in the future have additional context.

I'm also suspicious that Go's regexp library would be vulnerable to a ReDOS in the first place. The underlying code implements re2 and the package comment says: The regexp implementation provided by this package is guaranteed to run in time linear in the size of the input. (This is a property not guaranteed by most open source implementations of regular expressions.) . Something seems off with this CVE.

const maxLen = 1000

var (
// scpSyntax was modified from https://golang.org/src/cmd/go/vcs.go.
scpSyntax = regexp.MustCompile(`^([a-zA-Z0-9-._~]+@)?([a-zA-Z0-9._-]+):([a-zA-Z0-9./._-]+)(?:\?||$)(.*)$`)
Expand Down Expand Up @@ -90,6 +93,9 @@ func ParseTransport(rawurl string) (*url.URL, error) {
// ParseScp parses rawurl into a URL object. The rawurl must be
// an SCP-like URL, otherwise ParseScp returns an error.
func ParseScp(rawurl string) (*url.URL, error) {
if len(rawurl) > maxLen {
return nil, fmt.Errorf("URL too long: %q", rawurl)
}
match := scpSyntax.FindAllStringSubmatch(rawurl, -1)
if len(match) == 0 {
return nil, fmt.Errorf("no scp URL found in %q", rawurl)
Expand Down
8 changes: 8 additions & 0 deletions urls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,11 @@ func TestParse(t *testing.T) {
}
}
}

func TestTooLong(t *testing.T) {
longUrl := "https://example.com/" + strings.Repeat("a", 2048)
_, err := ParseScp(longUrl)
if err == nil {
t.Errorf("Parse(%q) = nil, want error", longUrl)
}
}