Skip to content

Commit

Permalink
fix Validate func
Browse files Browse the repository at this point in the history
  • Loading branch information
dogancanbakir committed Mar 5, 2024
1 parent c05f816 commit e6d02a0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ require (
github.com/gorilla/css v1.0.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/microcosm-cc/bluemonday v1.0.24 // indirect
github.com/miekg/dns v1.1.55 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/projectdiscovery/blackrock v0.0.1 // indirect
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20221019170559-20944726eadf // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/tools v0.6.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/microcosm-cc/bluemonday v1.0.24 h1:NGQoPtwGVcbGkKfvyYk1yRqknzBuoMiUrO6R7uFTPlw=
github.com/microcosm-cc/bluemonday v1.0.24/go.mod h1:ArQySAMps0790cHSkdPEJ7bGkF2VePWH773hsJNSHf8=
github.com/miekg/dns v1.1.55 h1:GoQ4hpsj0nFLYe+bWiCToyrBEJXkQfOOIvFGFy0lEgo=
github.com/miekg/dns v1.1.55/go.mod h1:uInx36IzPl7FYnDcMeVWxj9byh7DutNykX4G9Sj60FY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/projectdiscovery/blackrock v0.0.1 h1:lHQqhaaEFjgf5WkuItbpeCZv2DUIE45k0VbGJyft6LQ=
github.com/projectdiscovery/blackrock v0.0.1/go.mod h1:ANUtjDfaVrqB453bzToU+YB4cUbvBRpLvEwoWIwlTss=
github.com/projectdiscovery/utils v0.0.44 h1:F/LNgBw53RNM/3mRZ1ji+prM1yDnehDRBf13TPk3WBM=
github.com/projectdiscovery/utils v0.0.44/go.mod h1:HtUI1pyNCgQUuwZuxDILQ4NSUaFcfBh0TuCK/ZQTS6Q=
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca h1:NugYot0LIVPxTvN8n+Kvkn6TrbMyxQiuvKdEwFdR9vI=
Expand All @@ -28,8 +32,17 @@ github.com/yl2chen/cidranger v1.0.2 h1:lbOWZVCG1tCRX4u24kuM1Tb4nHqWkDxwLdoS+Seva
github.com/yl2chen/cidranger v1.0.2/go.mod h1:9U1yz7WPYDwf0vpNWFaeRh0bjwz5RVgRy/9UEQfHl0g=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/exp v0.0.0-20221019170559-20944726eadf h1:nFVjjKDgNY37+ZSYCJmtYf7tOlfQswHqplG2eosjOMg=
golang.org/x/exp v0.0.0-20221019170559-20944726eadf/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
14 changes: 7 additions & 7 deletions networkpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package networkpolicy

import (
"net"
"net/url"
"regexp"
"strconv"

iputil "github.com/projectdiscovery/utils/ip"
urlutil "github.com/projectdiscovery/utils/url"
"github.com/yl2chen/cidranger"
)

Expand Down Expand Up @@ -144,7 +144,7 @@ func (r NetworkPolicy) Validate(host string) bool {
var scheme string

// check if it's a valid URL
if URL, err := url.Parse(host); err == nil {
if URL, err := urlutil.Parse(host); err == nil {
// parse scheme
scheme := URL.Scheme
hasScheme = scheme != ""
Expand All @@ -158,22 +158,22 @@ func (r NetworkPolicy) Validate(host string) bool {
// check the port
var isPortInDenyList, isPortInAllowedList bool

if r.DenyPortList != nil && hasPort {
if len(r.DenyPortList) > 0 && hasPort {
_, isPortInDenyList = r.DenyPortList[port]
}

if r.AllowPortList != nil && hasPort {
_, isPortInAllowedList = r.DenyPortList[port]
if len(r.AllowPortList) > 0 && hasPort {
_, isPortInAllowedList = r.AllowPortList[port]
} else {
isPortInAllowedList = true
}

var isSchemeInDenyList, isSchemeInAllowedList bool
if r.DenySchemeList != nil && hasScheme {
if len(r.DenySchemeList) > 0 && hasScheme {
_, isSchemeInDenyList = r.DenySchemeList[scheme]
}

if r.AllowSchemeList != nil && hasScheme {
if len(r.AllowSchemeList) > 0 && hasScheme {
_, isSchemeInAllowedList = r.AllowSchemeList[scheme]
} else {
isSchemeInAllowedList = true
Expand Down
30 changes: 30 additions & 0 deletions networkpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,33 @@ func TestValidateAddress(t *testing.T) {
ok = np.ValidateAddress("192.168.1.1")
require.Equal(t, true, ok, "Unexpected negative result")
}

func TestMultipleCases(t *testing.T) {
var testCases = []struct {
address string
expectedValid bool
}{
{"projectdiscovery.io", false},
{"projectdiscovery.io:80", false},
{"http://scanme.sh", false},
{"scanme.sh:8080", true},
}

var npOptions Options
npOptions.DenyList = append(npOptions.DenyList,
"projectdiscovery.io",
"projectdiscovery.io:80",
"http://scanm.\\.sh",
"honey\\.scanme\\.sh",
)

np, err := New(npOptions)
if err != nil {
log.Fatal(err)
}

for _, tc := range testCases {
ok := np.Validate(tc.address)
require.Equal(t, tc.expectedValid, ok, "Unexpected result for address: "+tc.address)
}
}

0 comments on commit e6d02a0

Please sign in to comment.