Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[subscriptions] Fix for Validating URLs #239

Merged
merged 3 commits into from
Nov 9, 2023
Merged
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
3 changes: 2 additions & 1 deletion internal/events/eventstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,8 @@ func (a *eventStream) performActionWithRetry(batchNumber uint64, events []*event
func (a *eventStream) isAddressUnsafe(ip *net.IPAddr) bool {
ip4 := ip.IP.To4()
return !a.allowPrivateIPs &&
(ip4[0] == 0 ||
(len(ip4) < 1 ||
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Safeguard to prevent panics and instead treat an empty addr as unsafe

ip4[0] == 0 ||
ip4[0] >= 224 ||
ip4[0] == 127 ||
ip4[0] == 10 ||
Expand Down
25 changes: 18 additions & 7 deletions internal/events/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,10 @@ func newWebhookAction(es *eventStream, spec *webhookActionInfo) (*webhookAction,
func (w *webhookAction) attemptBatch(batchNumber, attempt uint64, events []*eventData) error {
// We perform DNS resolution before each attempt, to exclude private IP address ranges from the target
esID := w.es.spec.ID
u, _ := url.Parse(w.spec.URL)
addr, err := net.ResolveIPAddr("ip4", u.Hostname())
u, addr, err := w.validateURL()
if err != nil {
return err
}
if w.es.isAddressUnsafe(addr) {
err := errors.Errorf(errors.EventStreamsWebhookProhibitedAddress, u.Hostname())
log.Errorf(err.Error())
return err
}
// Set the timeout
var transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
Expand Down Expand Up @@ -114,3 +108,20 @@ func (w *webhookAction) attemptBatch(batchNumber, attempt uint64, events []*even
}
return err
}

func (w *webhookAction) validateURL() (*url.URL, *net.IPAddr, error) {
u, err := url.Parse(w.spec.URL)
Copy link
Contributor Author

@onelapahead onelapahead Nov 9, 2023

Choose a reason for hiding this comment

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

We now handle this error to late validate if what was provided on the subscription is in fact a URL and not something like a hostname or arbitrary string

if err != nil {
return nil, nil, err
}
addr, err := net.ResolveIPAddr("ip4", u.Hostname())
if err != nil {
return nil, nil, err
}
if w.es.isAddressUnsafe(addr) {
err := errors.Errorf(errors.EventStreamsWebhookProhibitedAddress, u.Hostname())
log.Errorf(err.Error())
return nil, nil, err
}
return u, addr, nil
}
24 changes: 24 additions & 0 deletions internal/events/webhooks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package events

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestValidateURL(t *testing.T) {
w := &webhookAction{
es: &eventStream{
allowPrivateIPs: false,
},
spec: &webhookActionInfo{
URL: "badurl",
},
}

_, _, err := w.validateURL()
assert.Error(t, err)

w.spec.URL = "https://google.com"
_, _, err = w.validateURL()
assert.NoError(t, err)
}
Loading