Skip to content

Commit fad8266

Browse files
authored
Merge pull request #72 from projectdiscovery/fix_validate_func
fix Validate func
2 parents b4610cb + da55cdf commit fad8266

File tree

6 files changed

+71
-10
lines changed

6 files changed

+71
-10
lines changed

.github/workflows/build-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
- name: Set up Go
1313
uses: actions/setup-go@v5
1414
with:
15-
go-version: 1.19
15+
go-version: 1.21
1616

1717
- name: Check out code
1818
uses: actions/checkout@v4

.github/workflows/lint-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- name: Set up Go
1414
uses: actions/setup-go@v5
1515
with:
16-
go-version: 1.19
16+
go-version: 1.21
1717
- name: Run golangci-lint
1818
uses: golangci/[email protected]
1919
with:

go.mod

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/projectdiscovery/networkpolicy
22

3-
go 1.19
3+
go 1.21
44

55
require (
66
github.com/projectdiscovery/utils v0.0.82
@@ -14,10 +14,19 @@ require (
1414
github.com/gorilla/css v1.0.0 // indirect
1515
github.com/kr/text v0.2.0 // indirect
1616
github.com/microcosm-cc/bluemonday v1.0.25 // indirect
17+
github.com/miekg/dns v1.1.56 // indirect
1718
github.com/pmezard/go-difflib v1.0.0 // indirect
19+
github.com/projectdiscovery/blackrock v0.0.1 // indirect
1820
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d // indirect
21+
github.com/tidwall/gjson v1.14.3 // indirect
22+
github.com/tidwall/match v1.1.1 // indirect
23+
github.com/tidwall/pretty v1.2.0 // indirect
1924
go.uber.org/multierr v1.11.0 // indirect
25+
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db // indirect
26+
golang.org/x/mod v0.12.0 // indirect
2027
golang.org/x/net v0.17.0 // indirect
28+
golang.org/x/sys v0.16.0 // indirect
29+
golang.org/x/tools v0.13.0 // indirect
2130
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
2231
gopkg.in/yaml.v3 v3.0.1 // indirect
2332
)

go.sum

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
66
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
77
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
88
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
9+
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
10+
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
911
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
1012
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
1113
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -14,8 +16,12 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
1416
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
1517
github.com/microcosm-cc/bluemonday v1.0.25 h1:4NEwSfiJ+Wva0VxN5B8OwMicaJvD8r9tlJWm9rtloEg=
1618
github.com/microcosm-cc/bluemonday v1.0.25/go.mod h1:ZIOjCQp1OrzBBPIJmfX4qDYFuhU02nx4bn030ixfHLE=
19+
github.com/miekg/dns v1.1.56 h1:5imZaSeoRNvpM9SzWNhEcP9QliKiz20/dA2QabIGVnE=
20+
github.com/miekg/dns v1.1.56/go.mod h1:cRm6Oo2C8TY9ZS/TqsSrseAcncm74lfK5G+ikN2SWWY=
1721
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1822
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
23+
github.com/projectdiscovery/blackrock v0.0.1 h1:lHQqhaaEFjgf5WkuItbpeCZv2DUIE45k0VbGJyft6LQ=
24+
github.com/projectdiscovery/blackrock v0.0.1/go.mod h1:ANUtjDfaVrqB453bzToU+YB4cUbvBRpLvEwoWIwlTss=
1925
github.com/projectdiscovery/utils v0.0.82 h1:U//02floCSFxJluN7MP+rJSwI4Px7o454JL7ukERArI=
2026
github.com/projectdiscovery/utils v0.0.82/go.mod h1:AbmIvy0TTlsfXxPDEMaNPVrxmqDmYiCnbGqh0TTthE4=
2127
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d h1:hrujxIzL1woJ7AwssoOcM/tq5JjjG2yYOc8odClEiXA=
@@ -24,12 +30,28 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
2430
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
2531
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
2632
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
33+
github.com/tidwall/gjson v1.14.3 h1:9jvXn7olKEHU1S9vwoMGliaT8jq1vJ7IH/n9zD9Dnlw=
34+
github.com/tidwall/gjson v1.14.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
35+
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
36+
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
37+
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
38+
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
2739
github.com/yl2chen/cidranger v1.0.2 h1:lbOWZVCG1tCRX4u24kuM1Tb4nHqWkDxwLdoS+SevawU=
2840
github.com/yl2chen/cidranger v1.0.2/go.mod h1:9U1yz7WPYDwf0vpNWFaeRh0bjwz5RVgRy/9UEQfHl0g=
2941
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
3042
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
43+
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db h1:D/cFflL63o2KSLJIwjlcIt8PR064j/xsmdEJL/YvY/o=
44+
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
45+
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
46+
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
3147
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
3248
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
49+
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
50+
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
51+
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
52+
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
53+
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
54+
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
3355
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3456
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
3557
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

networkpolicy.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package networkpolicy
22

33
import (
44
"net"
5-
"net/url"
65
"regexp"
76
"strconv"
87

98
iputil "github.com/projectdiscovery/utils/ip"
9+
urlutil "github.com/projectdiscovery/utils/url"
1010
"github.com/yl2chen/cidranger"
1111
)
1212

@@ -144,7 +144,7 @@ func (r NetworkPolicy) Validate(host string) bool {
144144
var scheme string
145145

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

161-
if r.DenyPortList != nil && hasPort {
161+
if len(r.DenyPortList) > 0 && hasPort {
162162
_, isPortInDenyList = r.DenyPortList[port]
163163
}
164164

165-
if r.AllowPortList != nil && hasPort {
166-
_, isPortInAllowedList = r.DenyPortList[port]
165+
if len(r.AllowPortList) > 0 && hasPort {
166+
_, isPortInAllowedList = r.AllowPortList[port]
167167
} else {
168168
isPortInAllowedList = true
169169
}
170170

171171
var isSchemeInDenyList, isSchemeInAllowedList bool
172-
if r.DenySchemeList != nil && hasScheme {
172+
if len(r.DenySchemeList) > 0 && hasScheme {
173173
_, isSchemeInDenyList = r.DenySchemeList[scheme]
174174
}
175175

176-
if r.AllowSchemeList != nil && hasScheme {
176+
if len(r.AllowSchemeList) > 0 && hasScheme {
177177
_, isSchemeInAllowedList = r.AllowSchemeList[scheme]
178178
} else {
179179
isSchemeInAllowedList = true

networkpolicy_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,33 @@ func TestValidateAddress(t *testing.T) {
1919
ok = np.ValidateAddress("192.168.1.1")
2020
require.Equal(t, true, ok, "Unexpected negative result")
2121
}
22+
23+
func TestMultipleCases(t *testing.T) {
24+
var testCases = []struct {
25+
address string
26+
expectedValid bool
27+
}{
28+
{"projectdiscovery.io", false},
29+
{"projectdiscovery.io:80", false},
30+
{"http://scanme.sh", false},
31+
{"scanme.sh:8080", true},
32+
}
33+
34+
var npOptions Options
35+
npOptions.DenyList = append(npOptions.DenyList,
36+
"projectdiscovery.io",
37+
"projectdiscovery.io:80",
38+
"http://scanm.\\.sh",
39+
"honey\\.scanme\\.sh",
40+
)
41+
42+
np, err := New(npOptions)
43+
if err != nil {
44+
log.Fatal(err)
45+
}
46+
47+
for _, tc := range testCases {
48+
ok := np.Validate(tc.address)
49+
require.Equal(t, tc.expectedValid, ok, "Unexpected result for address: "+tc.address)
50+
}
51+
}

0 commit comments

Comments
 (0)