From 408572dd35a283afd4632ef16043bb260fff63c4 Mon Sep 17 00:00:00 2001 From: AverageHelper Date: Mon, 8 Jun 2026 17:35:15 +0200 Subject: [PATCH] feat: in-browser validation of website URLs for user, repository, and organization profiles (#12991) This PR pertains to the client-side validation of the Website input on user, repo, and org profiles. #12962 extends `[service].VALID_SITE_URL_SCHEMES` to cover Website fields on repo and org profiles, where before that config key only applied to the one on user profiles. If that change merges, it will then be possible to construct an HTML [`pattern`](https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/input#pattern) attribute for general use on any Website form input that the server validates this way, thus enabling browsers to catch errors early relating to URL scheme confusion. This PR (1) introduces such a `pattern` attribute, and (2) adds a new UI note to make clear to users which URL schemes are permitted. This change helps explain the browser's otherwise cryptic error messages regarding pattern mismatch, while also letting users know what URI schemes the Forgejo instance supports as Website links (e.g. gemini:// URLs). ![A text field labeled "Website", with a note below which reads, "Allowed URL schemes include: http, https"](/attachments/304e17ee-b5aa-414e-a4fa-d48639336c6c) This MUST NOT merge before #12962. To do so would introduce a regression wherein the UI may suggest and validate a different set of allowed URL schemes than the server actually permits. See also #5519 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12991 Reviewed-by: 0ko <0ko@noreply.codeberg.org> --- modules/setting/service.go | 24 +++++++++ options/locale_next/locale_en-US.json | 1 + services/context/context.go | 2 + templates/admin/user/edit.tmpl | 3 +- templates/org/settings/options.tmpl | 3 +- templates/repo/settings/options.tmpl | 3 +- templates/user/settings/profile.tmpl | 3 +- tests/integration/form_validation_test.go | 63 +++++++++++++++++++++++ 8 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 tests/integration/form_validation_test.go diff --git a/modules/setting/service.go b/modules/setting/service.go index 5717225578..7e43ebc5d4 100644 --- a/modules/setting/service.go +++ b/modules/setting/service.go @@ -288,6 +288,10 @@ func loadServiceFrom(rootCfg ConfigProvider) { } } Service.ValidSiteURLSchemes = schemes + + // A pattern from ValidSiteURLSchemes must be valid for use in HTML validation + _ = regexp.MustCompile(`^(?:` + ValidSiteURLPattern() + `)$`) + Service.UsernameCooldownPeriod = sec.Key("USERNAME_COOLDOWN_PERIOD").MustInt64(0) // Only set a default if USERNAME_COOLDOWN_PERIOD's feature is active. @@ -302,6 +306,26 @@ func loadServiceFrom(rootCfg ConfigProvider) { loadOpenIDSetting(rootCfg) } +// Returns a regex pattern string based on the current value of +// `Service.ValidSiteURLSchemes`. +// +// This pattern string is meant to be used as the value of an element's +// `pattern` attribute. As such, this function assumes that the pattern will be +// implicitly wrapped with `^(?:` and `)$`, such that the match is required +// against the entire input value, i.e., `^(?:)$`. +// +// See [MDN] for more details. +// +// [MDN]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/input#pattern +func ValidSiteURLPattern() string { + // While technically possible, returning a compiled Regexp from this seems heavy-handed. + // We could store a compiled Regexp instead of generating on the fly, but that complicates testing! + // Much easier to simply edit `Service.ValidSiteURLSchemes for a given test and proceed. + // We run the compiler against the initial config value anyway, so this string always works in production. + schemes := strings.Join(Service.ValidSiteURLSchemes, "|") + return `(` + schemes + `)://.+` +} + func loadOpenIDSetting(rootCfg ConfigProvider) { sec := rootCfg.Section("openid") Service.EnableOpenIDSignIn = sec.Key("ENABLE_OPENID_SIGNIN").MustBool(!InstallLock) diff --git a/options/locale_next/locale_en-US.json b/options/locale_next/locale_en-US.json index 39f98ef7a9..03a7ab28a4 100644 --- a/options/locale_next/locale_en-US.json +++ b/options/locale_next/locale_en-US.json @@ -937,6 +937,7 @@ "markup.filepreview.lines": "Lines %[1]d to %[2]d in %[3]s", "markup.filepreview.truncated": "Preview has been truncated", "form.RunnerName": "Name", + "form.website.valid_url_schemes": "Allowed URL schemes include: %s", "graphs.recent_commits.title": "Number of commits in the past year", "graphs.code_frequency.title": "Code frequency over the history of {0}", "meta.last_line": "Thank you for translating Forgejo! This line isn't seen by the users but it serves other purposes in the translation management. You can place a fun fact in the translation instead of translating it." diff --git a/services/context/context.go b/services/context/context.go index d5c4f6fd2a..650b8e33e8 100644 --- a/services/context/context.go +++ b/services/context/context.go @@ -146,6 +146,8 @@ func Contexter() func(next http.Handler) http.Handler { ctx.Data["Context"] = ctx // TODO: use "ctx" in template and remove this ctx.Data["CurrentURL"] = setting.AppSubURL + req.URL.RequestURI() ctx.Data["Link"] = ctx.Link + ctx.Data["ValidSiteURLSchemes"] = setting.Service.ValidSiteURLSchemes + ctx.Data["ValidSiteURLPattern"] = setting.ValidSiteURLPattern() // PageData is passed by reference, and it will be rendered to `window.config.pageData` in `head.tmpl` for JavaScript modules ctx.PageData = map[string]any{} diff --git a/templates/admin/user/edit.tmpl b/templates/admin/user/edit.tmpl index f18317e694..fe4c0bcaa7 100644 --- a/templates/admin/user/edit.tmpl +++ b/templates/admin/user/edit.tmpl @@ -97,7 +97,8 @@
- + + {{ctx.Locale.Tr "form.website.valid_url_schemes" (StringUtils.Join .ValidSiteURLSchemes ", ")}}
diff --git a/templates/org/settings/options.tmpl b/templates/org/settings/options.tmpl index 371de3b4b0..ebf6db3b60 100644 --- a/templates/org/settings/options.tmpl +++ b/templates/org/settings/options.tmpl @@ -31,7 +31,8 @@
- + + {{ctx.Locale.Tr "form.website.valid_url_schemes" (StringUtils.Join .ValidSiteURLSchemes ", ")}}
diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index 6c8e59622f..d5d8371db3 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -42,7 +42,8 @@
- + + {{ctx.Locale.Tr "form.website.valid_url_schemes" (StringUtils.Join .ValidSiteURLSchemes ", ")}}
diff --git a/templates/user/settings/profile.tmpl b/templates/user/settings/profile.tmpl index 49a349a4f1..bc9115693d 100644 --- a/templates/user/settings/profile.tmpl +++ b/templates/user/settings/profile.tmpl @@ -46,7 +46,8 @@