Skip to content

Commit 4a867a2

Browse files
authored
Merge pull request #478 from pcbeingused333/fix/listen-trailing-slash-forward-url
fix(listen): join the forwarding URL path and the event path with a single slash
2 parents 8c728c6 + 8e1a3a2 commit 4a867a2

12 files changed

Lines changed: 378 additions & 6 deletions
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Package forwardurl builds the URL that `hookdeck listen` forwards an event
2+
// to. The request and every place that prints the URL share it, so what the
3+
// CLI shows is what the CLI sent.
4+
package forwardurl
5+
6+
import (
7+
"net/url"
8+
"strings"
9+
)
10+
11+
// JoinPath appends the event's path to the forwarding URL's path with a single
12+
// "/" between them. A trailing "/" on the forwarding URL is dropped only when
13+
// the event path starts with one, and the event path itself is left as is: a
14+
// receiver may route on its own trailing slash, which is why path.Join is not
15+
// used.
16+
//
17+
// When the forwarding URL has no path, the event path is appended straight
18+
// after the host, so an event path that does not start with "/", "?" or "#"
19+
// would be read as part of the host. It gets a "/" in front instead.
20+
func JoinPath(base, eventPath string) string {
21+
if base == "" && eventPath != "" && !strings.ContainsRune("/?#", rune(eventPath[0])) {
22+
eventPath = "/" + eventPath
23+
}
24+
if strings.HasPrefix(eventPath, "/") {
25+
base = strings.TrimSuffix(base, "/")
26+
}
27+
return base + eventPath
28+
}
29+
30+
// Build returns the full URL for an event: the forwarding URL's scheme, host
31+
// and path, then the event's path.
32+
func Build(target *url.URL, eventPath string) string {
33+
return target.Scheme + "://" + target.Host + JoinPath(target.Path, eventPath)
34+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package forwardurl
2+
3+
import (
4+
"net/url"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestBuild(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
target string
15+
eventPath string
16+
want string
17+
}{
18+
{"forwarding URL with a trailing slash", "http://localhost:3000/base/", "/webhook", "http://localhost:3000/base/webhook"},
19+
{"forwarding URL without a trailing slash", "http://localhost:3000/base", "/webhook", "http://localhost:3000/base/webhook"},
20+
{"forwarding URL is the root", "http://localhost:3000/", "/webhook", "http://localhost:3000/webhook"},
21+
{"forwarding URL has no path", "http://localhost:3000", "/webhook", "http://localhost:3000/webhook"},
22+
{"event path keeps its own trailing slash", "http://localhost:3000/base/", "/webhook/", "http://localhost:3000/base/webhook/"},
23+
{"event path is empty", "http://localhost:3000/base/", "", "http://localhost:3000/base/"},
24+
{"no forwarding path and an event path without a leading slash", "http://localhost:3000", "webhook", "http://localhost:3000/webhook"},
25+
{"no forwarding path and an event path that is only a query", "http://localhost:3000", "?a=b", "http://localhost:3000?a=b"},
26+
{"no forwarding path and an event path that is only a fragment", "http://localhost:3000", "#frag", "http://localhost:3000#frag"},
27+
}
28+
29+
for _, tt := range tests {
30+
t.Run(tt.name, func(t *testing.T) {
31+
target, err := url.Parse(tt.target)
32+
require.NoError(t, err)
33+
assert.Equal(t, tt.want, Build(target, tt.eventPath))
34+
})
35+
}
36+
}
37+
38+
// With no path on the forwarding URL the event path follows the host directly,
39+
// so an event path that does not begin with "/" must not be able to turn into
40+
// part of the host or into user information. The URL has to parse back to the
41+
// host the user asked to forward to.
42+
func TestBuildKeepsTheForwardingHost(t *testing.T) {
43+
for _, eventPath := range []string{
44+
"@other.example",
45+
"@other.example/x",
46+
":8080/x",
47+
".other.example",
48+
"other.example",
49+
"/@other.example",
50+
"//other.example/x",
51+
"/../x",
52+
"%2f%2fother.example",
53+
} {
54+
for _, forwardTo := range []string{"http://localhost:3000", "http://localhost:3000/", "http://localhost:3000/base"} {
55+
t.Run(forwardTo+" "+eventPath, func(t *testing.T) {
56+
target, err := url.Parse(forwardTo)
57+
require.NoError(t, err)
58+
59+
built, err := url.Parse(Build(target, eventPath))
60+
require.NoError(t, err)
61+
62+
assert.Equal(t, "localhost:3000", built.Host)
63+
assert.Nil(t, built.User)
64+
})
65+
}
66+
}
67+
}

‎pkg/listen/printer.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hookdeck/hookdeck-cli/pkg/ansi"
1111
"github.com/hookdeck/hookdeck-cli/pkg/config"
1212
"github.com/hookdeck/hookdeck-cli/pkg/hookdeck"
13+
"github.com/hookdeck/hookdeck-cli/pkg/listen/forwardurl"
1314
"github.com/hookdeck/hookdeck-cli/pkg/listen/links"
1415
"github.com/hookdeck/hookdeck-cli/pkg/listen/summary"
1516
)
@@ -61,7 +62,7 @@ func printSourcesWithConnections(config *config.Config, projectID string, source
6162
if cliPath != nil {
6263
path = *cliPath
6364
}
64-
fullPath := targetURL.Scheme + "://" + targetURL.Host + path
65+
fullPath := forwardurl.Build(targetURL, path)
6566

6667
// Get connection name from FullName (format: "source -> destination")
6768
// Split on "->" and take the second part (destination)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package listen
2+
3+
import (
4+
"net/url"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/hookdeck/hookdeck-cli/pkg/hookdeck"
11+
)
12+
13+
// The startup banner lists "Forwards to → <URL>" for each connection. It has to
14+
// show the URL the events will be forwarded to, including the forwarding URL's
15+
// path, joined to the connection's path with a single "/".
16+
func TestCompactBannerForwardsToJoinsForwardingURLAndConnectionPath(t *testing.T) {
17+
for _, tc := range []struct {
18+
name string
19+
forwardTo string
20+
cliPath string
21+
want string
22+
}{
23+
{"trailing slash on the forwarding URL", "http://localhost:3030/base/", "/webhook", "Forwards to → http://localhost:3030/base/webhook"},
24+
{"no trailing slash on the forwarding URL", "http://localhost:3030/base", "/webhook", "Forwards to → http://localhost:3030/base/webhook"},
25+
{"forwarding URL has no path", "http://localhost:3030", "/webhook", "Forwards to → http://localhost:3030/webhook"},
26+
{"connection path is the default root", "http://localhost:3030/base/", "/", "Forwards to → http://localhost:3030/base/"},
27+
} {
28+
t.Run(tc.name, func(t *testing.T) {
29+
resetColorState(t)
30+
cfg, sources, connections, _ := printerFixture(t, 1)
31+
targetURL, err := url.Parse(tc.forwardTo)
32+
require.NoError(t, err)
33+
connections[0].Destination = &hookdeck.Destination{
34+
ID: "des_1", Type: "CLI", Config: map[string]interface{}{"path": tc.cliPath},
35+
}
36+
37+
out := capturePrinterOutput(t, func() {
38+
printSourcesWithConnections(cfg, "tm_1", sources, connections, targetURL, "")
39+
})
40+
41+
assert.Contains(t, out, tc.want)
42+
})
43+
}
44+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package proxy
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"net/url"
8+
"testing"
9+
"time"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
14+
"github.com/hookdeck/hookdeck-cli/pkg/websocket"
15+
)
16+
17+
func TestProcessAttemptJoinsForwardingURLPathAndEventPath(t *testing.T) {
18+
tests := []struct {
19+
name string
20+
forwardTo string
21+
eventPath string
22+
expectPath string
23+
}{
24+
{"forwarding URL with a trailing slash", "/base/", "/webhook", "/base/webhook"},
25+
{"forwarding URL without a trailing slash", "/base", "/webhook", "/base/webhook"},
26+
{"forwarding URL is the root", "/", "/webhook", "/webhook"},
27+
{"forwarding URL has no path", "", "/webhook", "/webhook"},
28+
{"forwarding URL has no path and the event path has no leading slash", "", "@other.example/x", "/@other.example/x"},
29+
{"event path keeps its own trailing slash", "/base/", "/webhook/", "/base/webhook/"},
30+
{"event path is empty", "/base/", "", "/base/"},
31+
}
32+
33+
for _, tt := range tests {
34+
t.Run(tt.name, func(t *testing.T) {
35+
received := make(chan string, 1)
36+
local := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
37+
received <- r.URL.Path
38+
w.WriteHeader(http.StatusOK)
39+
}))
40+
t.Cleanup(local.Close)
41+
42+
target, err := url.Parse(local.URL + tt.forwardTo)
43+
require.NoError(t, err)
44+
p := New(&Config{URL: target, NoHealthcheck: true}, nil, newRecordingRenderer())
45+
46+
p.processAttempt(websocket.IncomingMessage{Attempt: &websocket.Attempt{
47+
Body: websocket.AttemptBody{
48+
Path: tt.eventPath,
49+
EventID: "evt_test",
50+
AttemptId: "evt_test",
51+
Request: websocket.AttemptRequest{Method: http.MethodPost, DataString: "{}", Headers: json.RawMessage(`{}`)},
52+
},
53+
}})
54+
55+
select {
56+
case got := <-received:
57+
assert.Equal(t, tt.expectPath, got)
58+
case <-time.After(5 * time.Second):
59+
t.Fatal("local server never received the request")
60+
}
61+
})
62+
}
63+
}

‎pkg/listen/proxy/proxy.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
log "github.com/sirupsen/logrus"
2323

2424
"github.com/hookdeck/hookdeck-cli/pkg/hookdeck"
25+
"github.com/hookdeck/hookdeck-cli/pkg/listen/forwardurl"
2526
"github.com/hookdeck/hookdeck-cli/pkg/listen/healthcheck"
2627
"github.com/hookdeck/hookdeck-cli/pkg/websocket"
2728
)
@@ -400,7 +401,7 @@ func (p *Proxy) processAttempt(msg websocket.IncomingMessage) {
400401
"prefix": "proxy.Proxy.processAttempt",
401402
}).Debugf("Processing webhook event")
402403

403-
url := p.cfg.URL.Scheme + "://" + p.cfg.URL.Host + p.cfg.URL.Path + webhookEvent.Body.Path
404+
url := forwardurl.Build(p.cfg.URL, webhookEvent.Body.Path)
404405

405406
// Create request with context for timeout control
406407
timeout := webhookEvent.Body.Request.Timeout
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package proxy
2+
3+
import (
4+
"net/url"
5+
"testing"
6+
"time"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
11+
"github.com/hookdeck/hookdeck-cli/pkg/listen/tui"
12+
"github.com/hookdeck/hookdeck-cli/pkg/websocket"
13+
)
14+
15+
// The request is sent to a URL built in processAttempt, and each renderer
16+
// prints the URL it believes was requested. These tests pin both renderers to
17+
// the single-slash join, so a forwarding URL ending in "/" does not print
18+
// "/base//webhook" for a request that went to "/base/webhook".
19+
20+
func forwardPathRendererConfig(t *testing.T, forwardTo string) *RendererConfig {
21+
t.Helper()
22+
cfg := linksRendererConfig(t, "gateway")
23+
target, err := url.Parse(forwardTo)
24+
require.NoError(t, err)
25+
cfg.TargetURL = target
26+
return cfg
27+
}
28+
29+
func forwardPathAttempt(eventPath string) *websocket.Attempt {
30+
return &websocket.Attempt{Body: websocket.AttemptBody{
31+
Path: eventPath,
32+
EventID: "evt_1",
33+
AttemptId: "atm_1",
34+
ConnectionId: "web_1",
35+
Request: websocket.AttemptRequest{Method: "POST"},
36+
}}
37+
}
38+
39+
var forwardPathRendererCases = []struct {
40+
name string
41+
forwardTo string
42+
eventPath string
43+
want string
44+
}{
45+
{"trailing slash on the forwarding URL", "http://localhost:3000/base/", "/webhook", "POST http://localhost:3000/base/webhook "},
46+
{"no trailing slash on the forwarding URL", "http://localhost:3000/base", "/webhook", "POST http://localhost:3000/base/webhook "},
47+
{"forwarding URL is the root", "http://localhost:3000/", "/webhook", "POST http://localhost:3000/webhook "},
48+
{"event path keeps its own trailing slash", "http://localhost:3000/base/", "/webhook/", "POST http://localhost:3000/base/webhook/ "},
49+
}
50+
51+
func TestSimpleRendererJoinsForwardingURLAndEventPath(t *testing.T) {
52+
response := &EventResponse{StatusCode: 200, Duration: 5 * time.Millisecond}
53+
54+
for _, tc := range forwardPathRendererCases {
55+
t.Run(tc.name, func(t *testing.T) {
56+
r := NewSimpleRenderer(forwardPathRendererConfig(t, tc.forwardTo), false)
57+
out := captureStdout(t, func() {
58+
r.OnEventComplete("evt_1", forwardPathAttempt(tc.eventPath), response, time.Now())
59+
})
60+
assert.Contains(t, out, tc.want)
61+
})
62+
}
63+
}
64+
65+
func TestInteractiveRendererJoinsForwardingURLAndEventPath(t *testing.T) {
66+
response := &EventResponse{StatusCode: 200, Duration: 5 * time.Millisecond}
67+
68+
for _, tc := range forwardPathRendererCases {
69+
t.Run(tc.name, func(t *testing.T) {
70+
r, sent := recordingInteractiveRenderer()
71+
r.cfg = forwardPathRendererConfig(t, tc.forwardTo)
72+
73+
r.OnEventComplete("evt_1", forwardPathAttempt(tc.eventPath), response, time.Now())
74+
75+
require.Len(t, *sent, 1)
76+
update, ok := (*sent)[0].(tui.UpdateEventMsg)
77+
require.True(t, ok, "unexpected message %T", (*sent)[0])
78+
assert.Contains(t, update.LogLine, tc.want)
79+
})
80+
}
81+
}

‎pkg/listen/proxy/renderer_interactive.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
log "github.com/sirupsen/logrus"
1111

1212
"github.com/hookdeck/hookdeck-cli/pkg/ansi"
13+
"github.com/hookdeck/hookdeck-cli/pkg/listen/forwardurl"
1314
"github.com/hookdeck/hookdeck-cli/pkg/listen/tui"
1415
"github.com/hookdeck/hookdeck-cli/pkg/websocket"
1516
)
@@ -152,7 +153,7 @@ func (r *InteractiveRenderer) OnEventComplete(eventID string, attempt *websocket
152153
color.Faint(localTime),
153154
ansi.ColorizeStatus(response.StatusCode),
154155
attempt.Body.Request.Method,
155-
r.cfg.TargetURL.Scheme+"://"+r.cfg.TargetURL.Host+r.cfg.TargetURL.Path+attempt.Body.Path,
156+
forwardurl.Build(r.cfg.TargetURL, attempt.Body.Path),
156157
color.Faint(fmt.Sprintf("(%dms)", durationMs)),
157158
color.Faint("→"),
158159
color.Faint(displayURL),

‎pkg/listen/proxy/renderer_simple.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
log "github.com/sirupsen/logrus"
1010

1111
"github.com/hookdeck/hookdeck-cli/pkg/ansi"
12+
"github.com/hookdeck/hookdeck-cli/pkg/listen/forwardurl"
1213
"github.com/hookdeck/hookdeck-cli/pkg/websocket"
1314
)
1415

@@ -149,7 +150,7 @@ func (r *SimpleRenderer) OnEventComplete(eventID string, attempt *websocket.Atte
149150
color.Faint(localTime),
150151
ansi.ColorizeStatus(response.StatusCode),
151152
attempt.Body.Request.Method,
152-
r.cfg.TargetURL.Scheme+"://"+r.cfg.TargetURL.Host+r.cfg.TargetURL.Path+attempt.Body.Path,
153+
forwardurl.Build(r.cfg.TargetURL, attempt.Body.Path),
153154
color.Faint(fmt.Sprintf("(%dms)", durationMs)),
154155
color.Faint("→"),
155156
color.Faint(displayURL),

0 commit comments

Comments
 (0)