|
| 1 | +package delivery |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/go-resty/resty/v2" |
| 6 | + . "github.com/onsi/ginkgo/v2" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/webhookx-io/webhookx/app" |
| 9 | + "github.com/webhookx-io/webhookx/constants" |
| 10 | + "github.com/webhookx-io/webhookx/db" |
| 11 | + "github.com/webhookx-io/webhookx/db/entities" |
| 12 | + "github.com/webhookx-io/webhookx/db/query" |
| 13 | + "github.com/webhookx-io/webhookx/test/helper" |
| 14 | + "github.com/webhookx-io/webhookx/test/helper/factory" |
| 15 | + "github.com/webhookx-io/webhookx/utils" |
| 16 | + "github.com/webhookx-io/webhookx/worker/deliverer" |
| 17 | + "net/netip" |
| 18 | + "time" |
| 19 | +) |
| 20 | + |
| 21 | +type ResolverFunc func(ctx context.Context, network, host string) ([]netip.Addr, error) |
| 22 | + |
| 23 | +func (fn ResolverFunc) LookupNetIP(ctx context.Context, network, host string) ([]netip.Addr, error) { |
| 24 | + return fn(ctx, network, host) |
| 25 | +} |
| 26 | + |
| 27 | +var _ = Describe("network acl", Ordered, func() { |
| 28 | + Context("acl", func() { |
| 29 | + var proxyClient *resty.Client |
| 30 | + |
| 31 | + var app *app.Application |
| 32 | + var db *db.DB |
| 33 | + |
| 34 | + entitiesConfig := helper.EntitiesConfig{ |
| 35 | + Endpoints: []*entities.Endpoint{ |
| 36 | + factory.EndpointP(func(o *entities.Endpoint) { |
| 37 | + o.Events = []string{"test1"} |
| 38 | + }), |
| 39 | + factory.EndpointP(func(o *entities.Endpoint) { |
| 40 | + o.Events = []string{"test2"} |
| 41 | + o.Request.URL = "http://www.example.com" |
| 42 | + }), |
| 43 | + factory.EndpointP(func(o *entities.Endpoint) { |
| 44 | + o.Events = []string{"test3"} |
| 45 | + o.Request.URL = "http://suspicious.webhookx.io" |
| 46 | + }), |
| 47 | + factory.EndpointP(func(o *entities.Endpoint) { |
| 48 | + o.Events = []string{"unicode-test"} |
| 49 | + o.Request.URL = "http://тест.foo.com" |
| 50 | + }), |
| 51 | + }, |
| 52 | + Sources: []*entities.Source{factory.SourceP()}, |
| 53 | + } |
| 54 | + |
| 55 | + var resolver = deliverer.DefaultResolver |
| 56 | + |
| 57 | + BeforeAll(func() { |
| 58 | + deliverer.DefaultResolver = ResolverFunc(func(ctx context.Context, network, host string) ([]netip.Addr, error) { |
| 59 | + if host == "suspicious.webhookx.io" { |
| 60 | + return []netip.Addr{netip.MustParseAddr("127.0.0.1")}, nil |
| 61 | + } |
| 62 | + return resolver.LookupNetIP(ctx, network, host) |
| 63 | + }) |
| 64 | + |
| 65 | + db = helper.InitDB(true, &entitiesConfig) |
| 66 | + proxyClient = helper.ProxyClient() |
| 67 | + |
| 68 | + app = utils.Must(helper.Start(map[string]string{ |
| 69 | + "WEBHOOKX_PROXY_LISTEN": "0.0.0.0:8081", |
| 70 | + "WEBHOOKX_WORKER_ENABLED": "true", |
| 71 | + "WEBHOOKX_WORKER_DELIVERER_ACL_DENY": "@default,*.example.com,xn--e1aybc.foo.com", |
| 72 | + })) |
| 73 | + |
| 74 | + }) |
| 75 | + |
| 76 | + AfterAll(func() { |
| 77 | + deliverer.DefaultResolver = resolver |
| 78 | + app.Stop() |
| 79 | + }) |
| 80 | + |
| 81 | + It("request denied", func() { |
| 82 | + err := waitForServer("0.0.0.0:8081", time.Second) |
| 83 | + assert.NoError(GinkgoT(), err) |
| 84 | + |
| 85 | + resp, err := proxyClient.R(). |
| 86 | + SetBody(`{"event_type": "test1","data": {"key": "value"}}`). |
| 87 | + Post("/") |
| 88 | + assert.NoError(GinkgoT(), err) |
| 89 | + assert.Equal(GinkgoT(), 200, resp.StatusCode()) |
| 90 | + eventId := resp.Header().Get(constants.HeaderEventId) |
| 91 | + |
| 92 | + var attempt *entities.Attempt |
| 93 | + assert.Eventually(GinkgoT(), func() bool { |
| 94 | + q := query.AttemptQuery{} |
| 95 | + q.EventId = &eventId |
| 96 | + list, err := db.Attempts.List(context.TODO(), &q) |
| 97 | + if err != nil || len(list) == 0 { |
| 98 | + return false |
| 99 | + } |
| 100 | + attempt = list[0] |
| 101 | + return attempt.Status == entities.AttemptStatusFailure |
| 102 | + }, time.Second*5, time.Second) |
| 103 | + |
| 104 | + // attempt.request |
| 105 | + assert.Equal(GinkgoT(), entities.AttemptErrorCodeDenied, *attempt.ErrorCode) |
| 106 | + assert.Equal(GinkgoT(), true, attempt.Exhausted) |
| 107 | + assert.Nil(GinkgoT(), attempt.Response) |
| 108 | + |
| 109 | + detail, err := db.AttemptDetails.Get(context.TODO(), attempt.ID) |
| 110 | + assert.NoError(GinkgoT(), err) |
| 111 | + assert.NotNil(GinkgoT(), detail.RequestHeaders) |
| 112 | + assert.NotNil(GinkgoT(), detail.RequestBody) |
| 113 | + assert.Nil(GinkgoT(), detail.ResponseHeaders) |
| 114 | + assert.Nil(GinkgoT(), detail.ResponseBody) |
| 115 | + }) |
| 116 | + |
| 117 | + It("request denied by hostname", func() { |
| 118 | + err := waitForServer("0.0.0.0:8081", time.Second) |
| 119 | + assert.NoError(GinkgoT(), err) |
| 120 | + |
| 121 | + resp, err := proxyClient.R(). |
| 122 | + SetBody(`{"event_type": "test2","data": {"key": "value"}}`). |
| 123 | + Post("/") |
| 124 | + assert.NoError(GinkgoT(), err) |
| 125 | + assert.Equal(GinkgoT(), 200, resp.StatusCode()) |
| 126 | + eventId := resp.Header().Get(constants.HeaderEventId) |
| 127 | + |
| 128 | + var attempt *entities.Attempt |
| 129 | + assert.Eventually(GinkgoT(), func() bool { |
| 130 | + q := query.AttemptQuery{} |
| 131 | + q.EventId = &eventId |
| 132 | + list, err := db.Attempts.List(context.TODO(), &q) |
| 133 | + if err != nil || len(list) == 0 { |
| 134 | + return false |
| 135 | + } |
| 136 | + attempt = list[0] |
| 137 | + return attempt.Status == entities.AttemptStatusFailure |
| 138 | + }, time.Second*5, time.Second) |
| 139 | + |
| 140 | + // attempt.request |
| 141 | + assert.Equal(GinkgoT(), entities.AttemptErrorCodeDenied, *attempt.ErrorCode) |
| 142 | + assert.Equal(GinkgoT(), true, attempt.Exhausted) |
| 143 | + assert.Nil(GinkgoT(), attempt.Response) |
| 144 | + }) |
| 145 | + |
| 146 | + It("request denied by unicode hostname", func() { |
| 147 | + err := waitForServer("0.0.0.0:8081", time.Second) |
| 148 | + assert.NoError(GinkgoT(), err) |
| 149 | + |
| 150 | + resp, err := proxyClient.R(). |
| 151 | + SetBody(`{"event_type": "unicode-test","data": {"key": "value"}}`). |
| 152 | + Post("/") |
| 153 | + assert.NoError(GinkgoT(), err) |
| 154 | + assert.Equal(GinkgoT(), 200, resp.StatusCode()) |
| 155 | + eventId := resp.Header().Get(constants.HeaderEventId) |
| 156 | + |
| 157 | + var attempt *entities.Attempt |
| 158 | + assert.Eventually(GinkgoT(), func() bool { |
| 159 | + q := query.AttemptQuery{} |
| 160 | + q.EventId = &eventId |
| 161 | + list, err := db.Attempts.List(context.TODO(), &q) |
| 162 | + if err != nil || len(list) == 0 { |
| 163 | + return false |
| 164 | + } |
| 165 | + attempt = list[0] |
| 166 | + return attempt.Status == entities.AttemptStatusFailure |
| 167 | + }, time.Second*5, time.Second) |
| 168 | + |
| 169 | + // attempt.request |
| 170 | + assert.Equal(GinkgoT(), entities.AttemptErrorCodeDenied, *attempt.ErrorCode) |
| 171 | + assert.Equal(GinkgoT(), true, attempt.Exhausted) |
| 172 | + assert.Nil(GinkgoT(), attempt.Response) |
| 173 | + }) |
| 174 | + |
| 175 | + It("request denied by ip resolved by dns", func() { |
| 176 | + err := waitForServer("0.0.0.0:8081", time.Second) |
| 177 | + assert.NoError(GinkgoT(), err) |
| 178 | + |
| 179 | + resp, err := proxyClient.R(). |
| 180 | + SetBody(`{"event_type": "test3","data": {"key": "value"}}`). |
| 181 | + Post("/") |
| 182 | + assert.NoError(GinkgoT(), err) |
| 183 | + assert.Equal(GinkgoT(), 200, resp.StatusCode()) |
| 184 | + eventId := resp.Header().Get(constants.HeaderEventId) |
| 185 | + |
| 186 | + var attempt *entities.Attempt |
| 187 | + assert.Eventually(GinkgoT(), func() bool { |
| 188 | + q := query.AttemptQuery{} |
| 189 | + q.EventId = &eventId |
| 190 | + list, err := db.Attempts.List(context.TODO(), &q) |
| 191 | + if err != nil || len(list) == 0 { |
| 192 | + return false |
| 193 | + } |
| 194 | + attempt = list[0] |
| 195 | + return attempt.Status == entities.AttemptStatusFailure |
| 196 | + }, time.Second*5, time.Second) |
| 197 | + |
| 198 | + // attempt.request |
| 199 | + assert.Equal(GinkgoT(), entities.AttemptErrorCodeDenied, *attempt.ErrorCode) |
| 200 | + assert.Equal(GinkgoT(), true, attempt.Exhausted) |
| 201 | + assert.Nil(GinkgoT(), attempt.Response) |
| 202 | + }) |
| 203 | + }) |
| 204 | +}) |
0 commit comments