Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

fix: #1283 better hostname setting/detection and fallback

Matthew Kilgore3c1432244546
diff --git a/backend/app/api/handlers/v1/helpers.go b/backend/app/api/handlers/v1/helpers.go
index 9bc318b0..605b6b1d 100644
--- a/backend/app/api/handlers/v1/helpers.go
+++ b/backend/app/api/handlers/v1/helpers.go
@@ -1,18 +1,67 @@
package v1
import (
+ "net/http"
"net/url"
+ "strings"
"github.com/rs/zerolog/log"
+ "github.com/sysadminsmedia/homebox/backend/internal/sys/config"
)
-func GetHBURL(refererHeader, fallback string) (hbURL string) {
- hbURL = refererHeader
- if hbURL == "" {
- hbURL = fallback
+// GetHBURL determines the base URL of the Homebox instance using the following priority:
+// 1. Configured hostname from Options.Hostname
+// 2. X-Forwarded headers (if TrustProxy is enabled)
+// 3. Referer header
+// 4. Fallback URL (ctrl.url)
+func GetHBURL(r *http.Request, options *config.Options, fallback string) string {
+ // 1. Use configured hostname if set
+ if options.Hostname != "" {
+ return ensureScheme(options.Hostname, r, options.TrustProxy)
}
- return stripPathFromURL(hbURL)
+ // 2. Use X-Forwarded headers if TrustProxy is enabled
+ if options.TrustProxy {
+ if xfHost := r.Header.Get("X-Forwarded-Host"); xfHost != "" {
+ scheme := getScheme(r, options.TrustProxy)
+ return scheme + "://" + xfHost
+ }
+ }
+
+ // 3. Fall back to Referer header
+ if referer := r.Header.Get("Referer"); referer != "" {
+ return stripPathFromURL(referer)
+ }
+
+ // 4. Fall back to the controller's URL
+ if fallback != "" {
+ return stripPathFromURL(fallback)
+ }
+
+ return ""
+}
+
+// ensureScheme ensures the hostname has a proper URL scheme
+func ensureScheme(hostname string, r *http.Request, trustProxy bool) string {
+ // If hostname already has a scheme, use it as-is
+ if strings.HasPrefix(hostname, "http://") || strings.HasPrefix(hostname, "https://") {
+ return strings.TrimSuffix(hostname, "/")
+ }
+
+ // Otherwise, determine scheme from request
+ scheme := getScheme(r, trustProxy)
+ return scheme + "://" + hostname
+}
+
+// getScheme determines the appropriate URL scheme based on request and proxy settings
+func getScheme(r *http.Request, trustProxy bool) string {
+ if r.TLS != nil {
+ return "https"
+ }
+ if trustProxy && r.Header.Get("X-Forwarded-Proto") == "https" {
+ return "https"
+ }
+ return "http"
}
// stripPathFromURL removes the path from a URL.
diff --git a/backend/app/api/handlers/v1/v1_ctrl_items.go b/backend/app/api/handlers/v1/v1_ctrl_items.go
index f71fcfad..7de13820 100644
--- a/backend/app/api/handlers/v1/v1_ctrl_items.go
+++ b/backend/app/api/handlers/v1/v1_ctrl_items.go
@@ -360,7 +360,7 @@ func (ctrl *V1Controller) HandleItemsExport() errchain.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error {
ctx := services.NewContext(r.Context())
- csvData, err := ctrl.svc.Items.ExportCSV(r.Context(), ctx.GID, GetHBURL(r.Header.Get("Referer"), ctrl.url))
+ csvData, err := ctrl.svc.Items.ExportCSV(r.Context(), ctx.GID, GetHBURL(r, &ctrl.config.Options, ctrl.url))
if err != nil {
log.Err(err).Msg("failed to export items")
return validate.NewRequestError(err, http.StatusInternalServerError)
diff --git a/backend/app/api/handlers/v1/v1_ctrl_labelmaker.go b/backend/app/api/handlers/v1/v1_ctrl_labelmaker.go
index 91f47492..37c7961d 100644
--- a/backend/app/api/handlers/v1/v1_ctrl_labelmaker.go
+++ b/backend/app/api/handlers/v1/v1_ctrl_labelmaker.go
@@ -56,7 +56,7 @@ func (ctrl *V1Controller) HandleGetLocationLabel() errchain.HandlerFunc {
return err
}
- hbURL := GetHBURL(r.Header.Get("Referer"), ctrl.url)
+ hbURL := GetHBURL(r, &ctrl.config.Options, ctrl.url)
return generateOrPrint(ctrl, w, r, location.Name, "Homebox Location", fmt.Sprintf("%s/location/%s", hbURL, location.ID))
}
}
@@ -90,7 +90,7 @@ func (ctrl *V1Controller) HandleGetItemLabel() errchain.HandlerFunc {
description += fmt.Sprintf("\nLocation: %s", item.Location.Name)
}
- hbURL := GetHBURL(r.Header.Get("Referer"), ctrl.url)
+ hbURL := GetHBURL(r, &ctrl.config.Options, ctrl.url)
return generateOrPrint(ctrl, w, r, item.Name, description, fmt.Sprintf("%s/item/%s", hbURL, item.ID))
}
}
@@ -130,7 +130,7 @@ func (ctrl *V1Controller) HandleGetAssetLabel() errchain.HandlerFunc {
description += fmt.Sprintf("\nLocation: %s", item.Items[0].Location.Name)
}
- hbURL := GetHBURL(r.Header.Get("Referer"), ctrl.url)
+ hbURL := GetHBURL(r, &ctrl.config.Options, ctrl.url)
return generateOrPrint(ctrl, w, r, item.Items[0].AssetID.String(), description, fmt.Sprintf("%s/a/%s", hbURL, item.Items[0].AssetID.String()))
}
}