From ff87aac4e7853c9112b7b312673b4f7c6c2ea12f Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Wed, 14 Jan 2026 18:47:44 -0500 Subject: [PATCH] fix(web): include base prefix in generated URLs (#1403) * fix(web): include base prefix in generated URLs Forgot to add the base prefix to these URLs. Committed a fix for this and added a test to ensure this does not repeat. Oops! Closes: #1402 Signed-off-by: Xe Iaso * docs: update CHANGELOG Signed-off-by: Xe Iaso --------- Signed-off-by: Xe Iaso --- .github/actions/spelling/allow.txt | 1 + docs/docs/CHANGELOG.md | 1 + web/index.templ | 4 +- web/index_templ.go | 6 +-- web/index_test.go | 81 ++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 web/index_test.go diff --git a/.github/actions/spelling/allow.txt b/.github/actions/spelling/allow.txt index 9d7a56c9..6ec3321d 100644 --- a/.github/actions/spelling/allow.txt +++ b/.github/actions/spelling/allow.txt @@ -23,3 +23,4 @@ fout iplist NArg blocklists +rififi diff --git a/docs/docs/CHANGELOG.md b/docs/docs/CHANGELOG.md index 2fba492a..175e001b 100644 --- a/docs/docs/CHANGELOG.md +++ b/docs/docs/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add iplist2rule tool that lets admins turn an IP address blocklist into an Anubis ruleset. - Add Polish locale ([#1292](https://github.com/TecharoHQ/anubis/pull/1309)) +- Fix honeypot and imprint links missing `BASE_PREFIX` when deployed behind a path prefix ([#1402](https://github.com/TecharoHQ/anubis/issues/1402)) diff --git a/web/index.templ b/web/index.templ index 181262ae..9ad02b3f 100644 --- a/web/index.templ +++ b/web/index.templ @@ -64,7 +64,7 @@ templ base(title string, body templ.Component, impressum *config.Impressum, chal @templ.JSONScript("anubis_public_url", anubis.PublicUrl) - @honeypotLink(fmt.Sprintf("%shoneypot/%s/init", anubis.APIPrefix, uuid.NewString())) + @honeypotLink(anubis.BasePrefix + fmt.Sprintf("%shoneypot/%s/init", anubis.APIPrefix, uuid.NewString()))

{ title }

@body @@ -79,7 +79,7 @@ templ base(title string, body templ.Component, impressum *config.Impressum, chal if impressum != nil {

@templ.Raw(impressum.Footer) - -- Imprint + -- Imprint

}

{ localizer.T("version_info") } { anubis.Version }.

diff --git a/web/index_templ.go b/web/index_templ.go index 94d3702c..de0af4fc 100644 --- a/web/index_templ.go +++ b/web/index_templ.go @@ -137,7 +137,7 @@ func base(title string, body templ.Component, impressum *config.Impressum, chall if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = honeypotLink(fmt.Sprintf("%shoneypot/%s/init", anubis.APIPrefix, uuid.NewString())).Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = honeypotLink(anubis.BasePrefix+fmt.Sprintf("%shoneypot/%s/init", anubis.APIPrefix, uuid.NewString())).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -245,9 +245,9 @@ func base(title string, body templ.Component, impressum *config.Impressum, chall return templ_7745c5c3_Err } var templ_7745c5c3_Var13 templ.SafeURL - templ_7745c5c3_Var13, templ_7745c5c3_Err = templ.JoinURLErrs(templ.SafeURL(fmt.Sprintf("%simprint", anubis.APIPrefix))) + templ_7745c5c3_Var13, templ_7745c5c3_Err = templ.JoinURLErrs(templ.SafeURL(anubis.BasePrefix + fmt.Sprintf("%simprint", anubis.APIPrefix))) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `index.templ`, Line: 82, Col: 78} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `index.templ`, Line: 82, Col: 98} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var13)) if templ_7745c5c3_Err != nil { diff --git a/web/index_test.go b/web/index_test.go new file mode 100644 index 00000000..b48b787c --- /dev/null +++ b/web/index_test.go @@ -0,0 +1,81 @@ +package web + +import ( + "context" + "net/http/httptest" + "strings" + "testing" + + "github.com/TecharoHQ/anubis" + "github.com/TecharoHQ/anubis/lib/config" + "github.com/TecharoHQ/anubis/lib/localization" + "github.com/a-h/templ" +) + +func TestBasePrefixInLinks(t *testing.T) { + tests := []struct { + name string + basePrefix string + wantInLink string + }{ + { + name: "no prefix", + basePrefix: "", + wantInLink: "/.within.website/x/cmd/anubis/api/", + }, + { + name: "with rififi prefix", + basePrefix: "/rififi", + wantInLink: "/rififi/.within.website/x/cmd/anubis/api/", + }, + { + name: "with myapp prefix", + basePrefix: "/myapp", + wantInLink: "/myapp/.within.website/x/cmd/anubis/api/", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Save original BasePrefix and restore after test + origPrefix := anubis.BasePrefix + defer func() { anubis.BasePrefix = origPrefix }() + + anubis.BasePrefix = tt.basePrefix + + // Create test impressum + impressum := &config.Impressum{ + Footer: "

Test footer

", + Page: config.ImpressumPage{ + Title: "Test Imprint", + Body: "

Test imprint body

", + }, + } + + // Create localizer using a dummy request + req := httptest.NewRequest("GET", "/", nil) + localizer := &localization.SimpleLocalizer{} + localizer.Localizer = localization.NewLocalizationService().GetLocalizerFromRequest(req) + + // Render the base template to a buffer + var buf strings.Builder + component := base(tt.name, templ.NopComponent, impressum, nil, nil, localizer) + err := component.Render(context.Background(), &buf) + if err != nil { + t.Fatalf("failed to render template: %v", err) + } + + output := buf.String() + + // Check that honeypot link includes the base prefix + if !strings.Contains(output, `href="`+tt.wantInLink+`honeypot/`) { + t.Errorf("honeypot link does not contain base prefix %q\noutput: %s", tt.wantInLink, output) + } + + // Check that imprint link includes the base prefix + if !strings.Contains(output, `href="`+tt.wantInLink+`imprint`) { + t.Errorf("imprint link does not contain base prefix %q\noutput: %s", tt.wantInLink, output) + } + }) + } +}