Skip to content
Closed
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
17 changes: 13 additions & 4 deletions purell.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,10 @@ func decodeDWORDHost(u *url.URL) {
if matches := rxDWORDHost.FindStringSubmatch(u.Host); len(matches) > 2 {
var parts [4]int64

dword, _ := strconv.ParseInt(matches[1], 10, 0)
dword, err := strconv.ParseInt(matches[1], 10, 64)
if err != nil || dword < 0 || dword > 0xFFFFFFFF {
return
}
for i, shift := range []uint{24, 16, 8, 0} {
parts[i] = dword >> shift & 0xFF
}
Expand All @@ -360,7 +363,11 @@ func decodeOctalHost(u *url.URL) {
var parts [4]int64

for i := 1; i <= 4; i++ {
parts[i-1], _ = strconv.ParseInt(matches[i], 8, 0)
n, err := strconv.ParseInt(matches[i], 8, 64)
if err != nil || n < 0 || n > 255 {
return
}
parts[i-1] = n
}
u.Host = fmt.Sprintf("%d.%d.%d.%d%s", parts[0], parts[1], parts[2], parts[3], matches[5])
}
Expand All @@ -370,8 +377,10 @@ func decodeOctalHost(u *url.URL) {
func decodeHexHost(u *url.URL) {
if len(u.Host) > 0 {
if matches := rxHexHost.FindStringSubmatch(u.Host); len(matches) > 2 {
// Conversion is safe because of regex validation
parsed, _ := strconv.ParseInt(matches[1], 16, 0)
parsed, err := strconv.ParseInt(matches[1], 16, 64)
if err != nil || parsed < 0 || parsed > 0xFFFFFFFF {
return
}
// Set host as DWORD (base 10) encoded host
u.Host = fmt.Sprintf("%d%s", parsed, matches[2])
// The rest is the same as decoding a DWORD host
Expand Down
42 changes: 42 additions & 0 deletions purell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,48 @@ var (
"/foo/bar",
false,
},
{
"OctalIP-invalid-digit",
"http://08.00.00.01/",
FlagsSafe | FlagDecodeOctalHost,
"http://08.00.00.01/",
false,
},
{
"OctalIP-octet-overflow",
"http://0400.0000.0000.0001/",
FlagsSafe | FlagDecodeOctalHost,
"http://0400.0000.0000.0001/",
false,
},
{
"DWORDIP-above-uint32",
"http://4294967296/",
FlagsSafe | FlagDecodeDWORDHost,
"http://4294967296/",
false,
},
{
"DWORDIP-int-overflow",
"http://999999999999999999999/",
FlagsSafe | FlagDecodeDWORDHost,
"http://999999999999999999999/",
false,
},
{
"DWORDIP-uint32-max",
"http://4294967295/",
FlagsSafe | FlagDecodeDWORDHost,
"http://255.255.255.255/",
false,
},
{
"HexIP-above-uint32",
"http://0x100000000/",
FlagsSafe | FlagDecodeHexHost,
"http://0x100000000/",
false,
},
/*&testCase{
"UrlNorm-5",
"http://ja.wikipedia.org/wiki/%E3%82%AD%E3%83%A3%E3%82%BF%E3%83%94%E3%83%A9%E3%83%BC%E3%82%B8%E3%83%A3%E3%83%91%E3%83%B3",
Expand Down