Skip to content

Commit 715cf46

Browse files
Normalize AppURL according to RFC 3986 (#21950)
Fixes #21865. Scheme-based normalization ([RFC 3986, section 6.2.3](https://www.rfc-editor.org/rfc/rfc3986#section-6.2.3)) was already implemented, but only for `defaultAppURL`. This PR implements the same for `AppURL`. Signed-off-by: Saswat Padhi <[email protected]> Co-authored-by: John Olheiser <[email protected]>
1 parent f047ee0 commit 715cf46

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

modules/setting/setting.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -748,19 +748,22 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
748748
PerWriteTimeout = sec.Key("PER_WRITE_TIMEOUT").MustDuration(PerWriteTimeout)
749749
PerWritePerKbTimeout = sec.Key("PER_WRITE_PER_KB_TIMEOUT").MustDuration(PerWritePerKbTimeout)
750750

751-
defaultAppURL := string(Protocol) + "://" + Domain
752-
if (Protocol == HTTP && HTTPPort != "80") || (Protocol == HTTPS && HTTPPort != "443") {
753-
defaultAppURL += ":" + HTTPPort
754-
}
755-
AppURL = sec.Key("ROOT_URL").MustString(defaultAppURL + "/")
756-
// This should be TrimRight to ensure that there is only a single '/' at the end of AppURL.
757-
AppURL = strings.TrimRight(AppURL, "/") + "/"
751+
defaultAppURL := string(Protocol) + "://" + Domain + ":" + HTTPPort
752+
AppURL = sec.Key("ROOT_URL").MustString(defaultAppURL)
758753

759-
// Check if has app suburl.
754+
// Check validity of AppURL
760755
appURL, err := url.Parse(AppURL)
761756
if err != nil {
762757
log.Fatal("Invalid ROOT_URL '%s': %s", AppURL, err)
763758
}
759+
// Remove default ports from AppURL.
760+
// (scheme-based URL normalization, RFC 3986 section 6.2.3)
761+
if (appURL.Scheme == string(HTTP) && appURL.Port() == "80") || (appURL.Scheme == string(HTTPS) && appURL.Port() == "443") {
762+
appURL.Host = appURL.Hostname()
763+
}
764+
// This should be TrimRight to ensure that there is only a single '/' at the end of AppURL.
765+
AppURL = strings.TrimRight(appURL.String(), "/") + "/"
766+
764767
// Suburl should start with '/' and end without '/', such as '/{subpath}'.
765768
// This value is empty if site does not have sub-url.
766769
AppSubURL = strings.TrimSuffix(appURL.Path, "/")

0 commit comments

Comments
 (0)