Skip to content

Commit 13c6624

Browse files
committed
fix gateway crashes
1 parent e39d75f commit 13c6624

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

lib/gateway/gateway.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ func New(deniedDomains, allowedDomains, blockedFileExts, allowedFileExts []strin
6363
func (g *Gateway) setup() {
6464
// we need the `validateUrl` middleware no matter what, so prepend it to the list of enabled middleware.
6565
// this middleware is the core functionality of the http gateway's allow/block lists.
66-
temp := []string{"validateUrl"}
67-
g.EnabledMiddleware = append(temp, g.EnabledMiddleware...)
66+
// temp := []string{"validateUrl"}
67+
g.EnabledMiddleware = append(g.EnabledMiddleware, "validateUrl")
68+
GatewayMiddleware.AddPreMiddleware(&ValidateUrlMiddleware)
69+
70+
// fmt.Printf("enabled middleware: %v\n", g.EnabledMiddleware)
6871

6972
for _, name := range g.EnabledMiddleware {
7073
if !GatewayMiddleware.HasMiddleware(name) {
@@ -224,6 +227,7 @@ func (g *Gateway) runUrlRequestPipeline(link string) error {
224227

225228
for _, mw := range g.Middleware {
226229
if err := (*mw).Handler(g, link); err != nil {
230+
// fmt.Printf("error on mw: %v; %v\n", mw.Name, err)
227231
return err
228232
}
229233
}

lib/gateway/gateway_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/stackup-app/stackup/lib/gateway"
7+
"github.com/stackup-app/stackup/lib/settings"
78
"github.com/stretchr/testify/assert"
89
)
910

@@ -23,6 +24,49 @@ func TestGatewayDisable(t *testing.T) {
2324

2425
func TestGatewayAllowed(t *testing.T) {
2526
g := gateway.New([]string{}, []string{"*.example.com", "*.one.example.net", "api.**.com"}, []string{}, []string{})
27+
verifyChecksums := true
28+
enableStats := false
29+
gatewayAllow := "allow"
30+
s := &settings.Settings{
31+
AnonymousStatistics: &enableStats,
32+
DotEnvFiles: []string{".env"},
33+
Cache: &settings.WorkflowSettingsCache{TtlMinutes: 15},
34+
ChecksumVerification: &verifyChecksums,
35+
Domains: &settings.WorkflowSettingsDomains{
36+
Allowed: []string{"*.example.com", "*.one.example.net", "api.**.com"},
37+
Hosts: []settings.WorkflowSettingsDomainsHosts{
38+
{Hostname: "raw.githubusercontent.com", Gateway: &gatewayAllow, Headers: nil},
39+
{Hostname: "api.github.com", Gateway: &gatewayAllow, Headers: nil},
40+
},
41+
},
42+
Defaults: &settings.WorkflowSettingsDefaults{
43+
Tasks: &settings.WorkflowSettingsDefaultsTasks{
44+
Silent: false,
45+
Path: "./",
46+
Platforms: []string{"windows", "linux", "darwin"},
47+
},
48+
},
49+
Gateway: &settings.WorkflowSettingsGateway{
50+
ContentTypes: &settings.GatewayContentTypes{
51+
Blocked: []string{},
52+
Allowed: []string{"*"},
53+
},
54+
FileExtensions: &settings.WorkflowSettingsGatewayFileExtensions{
55+
Allow: []string{"*"},
56+
Block: []string{},
57+
},
58+
Middleware: []string{},
59+
},
60+
Notifications: &settings.WorkflowSettingsNotifications{
61+
Telegram: &settings.WorkflowSettingsNotificationsTelegram{
62+
APIKey: "",
63+
ChatIds: []string{},
64+
},
65+
},
66+
}
67+
gateway.GatewayMiddleware.AddPreMiddleware(&gateway.ValidateUrlMiddleware)
68+
69+
g.Initialize(s, nil, nil)
2670
assert.True(t, g.Allowed("https://www.example.com"), "www.example.com should be allowed")
2771
assert.True(t, g.Allowed("https://example.com"), "example.com should be allowed")
2872
assert.False(t, g.Allowed("https://www.example.net"), "www.example.net should not be allowed")

lib/gateway/middleware.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package gateway
22

3-
import "strings"
3+
import (
4+
"strings"
5+
)
46

57
type GatewayMiddlewareStore struct {
68
PreMiddleware []*GatewayUrlRequestMiddleware

0 commit comments

Comments
 (0)