Skip to content

Commit 1b400bd

Browse files
leggetterclaude
andcommitted
fix(listen): start the event path with a slash when the forwarding URL has no path
With no path on the forwarding URL, the event path is appended directly after the host. An event path that does not begin with "/", "?" or "#" now gets a "/" in front, so the forwarded URL always keeps the host the user asked to forward to, and the printed URL matches the requested one. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 7cd909f commit 1b400bd

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

‎pkg/listen/forwardurl/forwardurl.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ import (
1313
// the event path starts with one, and the event path itself is left as is: a
1414
// receiver may route on its own trailing slash, which is why path.Join is not
1515
// 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.
1620
func JoinPath(base, eventPath string) string {
21+
if base == "" && eventPath != "" && !strings.ContainsRune("/?#", rune(eventPath[0])) {
22+
eventPath = "/" + eventPath
23+
}
1724
if strings.HasPrefix(eventPath, "/") {
1825
base = strings.TrimSuffix(base, "/")
1926
}

‎pkg/listen/forwardurl/forwardurl_test.go‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ func TestBuild(t *testing.T) {
2121
{"forwarding URL has no path", "http://localhost:3000", "/webhook", "http://localhost:3000/webhook"},
2222
{"event path keeps its own trailing slash", "http://localhost:3000/base/", "/webhook/", "http://localhost:3000/base/webhook/"},
2323
{"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"},
2427
}
2528

2629
for _, tt := range tests {
@@ -31,3 +34,34 @@ func TestBuild(t *testing.T) {
3134
})
3235
}
3336
}
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/proxy/forward_path_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func TestProcessAttemptJoinsForwardingURLPathAndEventPath(t *testing.T) {
2525
{"forwarding URL without a trailing slash", "/base", "/webhook", "/base/webhook"},
2626
{"forwarding URL is the root", "/", "/webhook", "/webhook"},
2727
{"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"},
2829
{"event path keeps its own trailing slash", "/base/", "/webhook/", "/base/webhook/"},
2930
{"event path is empty", "/base/", "", "/base/"},
3031
}

0 commit comments

Comments
 (0)