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 <me@xeiaso.net>

* docs: update CHANGELOG

Signed-off-by: Xe Iaso <me@xeiaso.net>

---------

Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
Xe Iaso
2026-01-14 18:47:44 -05:00
committed by GitHub
parent 3c76724aeb
commit ff87aac4e7
5 changed files with 88 additions and 5 deletions

View File

@@ -23,3 +23,4 @@ fout
iplist
NArg
blocklists
rififi

View File

@@ -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))
<!-- This changes the project to: -->

View File

@@ -64,7 +64,7 @@ templ base(title string, body templ.Component, impressum *config.Impressum, chal
@templ.JSONScript("anubis_public_url", anubis.PublicUrl)
</head>
<body id="top">
@honeypotLink(fmt.Sprintf("%shoneypot/%s/init", anubis.APIPrefix, uuid.NewString()))
@honeypotLink(anubis.BasePrefix + fmt.Sprintf("%shoneypot/%s/init", anubis.APIPrefix, uuid.NewString()))
<main>
<h1 id="title" class="centered-div">{ title }</h1>
@body
@@ -79,7 +79,7 @@ templ base(title string, body templ.Component, impressum *config.Impressum, chal
if impressum != nil {
<p>
@templ.Raw(impressum.Footer)
-- <a href={ templ.SafeURL(fmt.Sprintf("%simprint", anubis.APIPrefix)) }>Imprint</a>
-- <a href={ templ.SafeURL(anubis.BasePrefix + fmt.Sprintf("%simprint", anubis.APIPrefix)) }>Imprint</a>
</p>
}
<p>{ localizer.T("version_info") } <code>{ anubis.Version }</code>.</p>

6
web/index_templ.go generated
View File

@@ -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 {

81
web/index_test.go Normal file
View File

@@ -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: "<p>Test footer</p>",
Page: config.ImpressumPage{
Title: "Test Imprint",
Body: "<p>Test imprint body</p>",
},
}
// 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)
}
})
}
}