Improve forbidden hostname checks

pull/84/head
Aloïs Micard 3 years ago
parent 1640f424d1
commit 72458ea9c4
No known key found for this signature in database
GPG Key ID: 1A0EB82F071F5EFE

@ -145,7 +145,7 @@ func (state *state) handleURLFoundEvent(subscriber event.Subscriber, body io.Rea
// Make sure hostname is not forbidden
for _, hostname := range state.forbiddenHostnames {
if u.Hostname() == hostname {
if strings.Contains(u.Hostname(), hostname) {
return fmt.Errorf("%s %w", u, errHostnameNotAllowed)
}
}

@ -128,30 +128,34 @@ func TestHandleMessageHostnameForbidden(t *testing.T) {
subscriberMock := event_mock.NewMockSubscriber(mockCtrl)
type test struct {
URL string
url string
forbiddenHostnames []string
}
tests := []test{
{
URL: "https://facebookcorewwwi.onion/image.png?id=12&test=2",
url: "https://facebookcorewwwi.onion/image.png?id=12&test=2",
forbiddenHostnames: []string{"facebookcorewwwi.onion"},
},
{
URL: "https://google.onion:9099",
url: "https://google.onion:9099",
forbiddenHostnames: []string{"google.onion"},
},
{
URL: "http://facebook.onion:443/news/test.php?id=12&username=test",
url: "http://facebook.onion:443/news/test.php?id=12&username=test",
forbiddenHostnames: []string{"facebook.onion"},
},
{
url: "https://www.facebookcorewwwi.onion/recover/initiate?ars=facebook_login",
forbiddenHostnames: []string{"facebookcorewwwi.onion"},
},
}
for _, test := range tests {
msg := bytes.NewReader(nil)
subscriberMock.EXPECT().
Read(msg, &event.FoundURLEvent{}).
SetArg(1, event.FoundURLEvent{URL: test.URL}).
SetArg(1, event.FoundURLEvent{URL: test.url}).
Return(nil)
s := state{
@ -162,7 +166,7 @@ func TestHandleMessageHostnameForbidden(t *testing.T) {
}
if err := s.handleURLFoundEvent(subscriberMock, msg); !errors.Is(err, errHostnameNotAllowed) {
t.FailNow()
t.Errorf("%s has not returned errHostnameNotAllowed", test.url)
}
}
}

Loading…
Cancel
Save