Skip to content

Commit d22a208

Browse files
author
Team Coding Agent 1
committed
fix: Use request URL path instead of route pattern in BaseURL middleware
The BaseURL function was using c.Path() which returns the route pattern (e.g., '/app/*') instead of the actual request path. This caused the X-Forwarded-Prefix header to not be properly detected when calculating the base URL for reverse proxy scenarios. Fixed by using c.Request().URL.Path which contains the actual stripped path after StripPathPrefix middleware processes the request. Fixes #9145
1 parent d3f629f commit d22a208

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

core/http/middleware/baseurl.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ import (
1111
// The returned URL is guaranteed to end with `/`.
1212
// The method should be used in conjunction with the StripPathPrefix middleware.
1313
func BaseURL(c echo.Context) string {
14-
path := c.Path()
15-
origPath := c.Request().URL.Path
14+
// Use the current request path (after StripPathPrefix has stripped the prefix)
15+
// NOT c.Path() which returns the route pattern (e.g., '/app/*')
16+
currentPath := c.Request().URL.Path
17+
18+
// Get the original path before stripping
19+
origPath := currentPath
1620

1721
// Check if StripPathPrefix middleware stored the original path
1822
if storedPath, ok := c.Get("_original_path").(string); ok && storedPath != "" {
@@ -33,15 +37,14 @@ func BaseURL(c echo.Context) string {
3337
host = forwardedHost
3438
}
3539

36-
if path != origPath && strings.HasSuffix(origPath, path) && len(path) > 0 {
37-
prefixLen := len(origPath) - len(path)
38-
if prefixLen > 0 && prefixLen <= len(origPath) {
39-
pathPrefix := origPath[:prefixLen]
40-
if !strings.HasSuffix(pathPrefix, "/") {
41-
pathPrefix += "/"
42-
}
43-
return scheme + "://" + host + pathPrefix
40+
// Calculate the prefix by comparing original path with current (stripped) path
41+
// The prefix is what was stripped from the beginning of origPath to get currentPath
42+
if origPath != currentPath && strings.HasSuffix(origPath, currentPath) && len(origPath) > len(currentPath) {
43+
pathPrefix := origPath[:len(origPath)-len(currentPath)]
44+
if !strings.HasSuffix(pathPrefix, "/") {
45+
pathPrefix += "/"
4446
}
47+
return scheme + "://" + host + pathPrefix
4548
}
4649

4750
return scheme + "://" + host + "/"

0 commit comments

Comments
 (0)