fix(web): inline the site manifest (#40198)

Inline the web app manifest as a `data:` URL instead of serving it from
`/assets/site-manifest.json`. That endpoint has been linked through
`STATIC_URL_PREFIX` since https://github.com/go-gitea/gitea/pull/37405
and fails to load when:

1. a reverse proxy checks auth on every request, because browsers fetch
manifests without cookies
2. `STATIC_URL_PREFIX` serves the `public/` directory from disk, which
has no such file
3. `STATIC_URL_PREFIX` is cross-origin, because the endpoint sends no
CORS headers

The manifest is built per request so `start_url` follows
`PUBLIC_URL_DETECTION`. A reverse proxy that adds a stricter CSP must
allow `manifest-src data:`.

Fixes https://github.com/go-gitea/gitea/issues/40197

---------

Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
Lazizbek Ergashevandsilverwind authored and GitHub committed 2026-11-04 18:11:25 +00:00
1 parent 1152bc0cf2
commit d1c980cf8b
6 files changed
+36 -34

No files matched your search

-26
View File
@@ -7,11 +7,8 @@ import (
"net/http"
"path"
"strconv"
"strings"
"gitea.dev/modules/httpcache"
"gitea.dev/modules/httplib"
"gitea.dev/modules/json"
"gitea.dev/modules/log"
"gitea.dev/modules/setting"
"gitea.dev/modules/util"
@@ -20,29 +17,6 @@ import (
"gitea.dev/services/context"
)
func SiteManifest(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/manifest+json")
if httpcache.HandleGenericETagPublicCache(req, w, "", &setting.AppStartTime) {
return
}
if req.Method == http.MethodHead {
return
}
ctx := req.Context()
absoluteAssetURL := strings.TrimSuffix(httplib.MakeAbsoluteURL(ctx, setting.StaticURLPrefix), "/")
manifest := map[string]any{
"name": setting.AppName,
"short_name": setting.AppName,
"start_url": httplib.GuessCurrentAppURL(ctx),
"icons": []map[string]string{
{"src": absoluteAssetURL + "/assets/img/logo.png", "type": "image/png", "sizes": "512x512"},
{"src": absoluteAssetURL + "/assets/img/logo.svg", "type": "image/svg+xml", "sizes": "512x512"},
},
}
_ = json.NewEncoder(w).Encode(manifest)
}
func SSHInfo(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("content-type", "text/json;charset=UTF-8")
_, err := rw.Write([]byte(agit.SshInfoJson))
-1
View File
@@ -261,7 +261,6 @@ func Routes() *web.Router {
routes.BeforeRouting(user.UsernamePreRoute)
routes.Head("/", misc.DummyOK) // for health check - doesn't need to be passed through gzip handler
routes.Methods("GET, HEAD", "/assets/site-manifest.json", misc.SiteManifest)
routes.Methods("GET, HEAD, OPTIONS", "/assets/*", routing.MarkLogLevelTrace, public.AssetsCors(), public.FileHandlerFunc())
routes.Methods("GET, HEAD", "/avatars/*", avatarStorageHandler(setting.Avatar.Storage, "avatars", storage.Avatars))
routes.Methods("GET, HEAD", "/repo-avatars/*", avatarStorageHandler(setting.RepoAvatar.Storage, "repo-avatars", storage.RepoAvatars))
+17
View File
@@ -5,6 +5,7 @@ package context
import (
"context"
"encoding/base64"
"html"
"html/template"
"net/http"
@@ -14,6 +15,7 @@ import (
user_model "gitea.dev/models/user"
"gitea.dev/modules/httplib"
"gitea.dev/modules/json"
"gitea.dev/modules/public"
"gitea.dev/modules/reqctx"
"gitea.dev/modules/setting"
@@ -99,6 +101,21 @@ func (c TemplateContext) AppFullLink(link ...string) template.URL {
return template.URL(s + "/" + strings.TrimPrefix(link[0], "/"))
}
func (c TemplateContext) SiteManifestURL() template.URL {
ctx := c.parentContext()
absoluteAssetURL := strings.TrimSuffix(httplib.MakeAbsoluteURL(ctx, setting.StaticURLPrefix), "/")
manifest, _ := json.Marshal(map[string]any{
"name": setting.AppName,
"short_name": setting.AppName,
"start_url": httplib.GuessCurrentAppURL(ctx),
"icons": []map[string]string{
{"src": absoluteAssetURL + "/assets/img/logo.png", "type": "image/png", "sizes": "512x512"},
{"src": absoluteAssetURL + "/assets/img/logo.svg", "type": "image/svg+xml", "sizes": "512x512"},
},
})
return template.URL("data:application/manifest+json;base64," + base64.StdEncoding.EncodeToString(manifest))
}
func (c TemplateContext) ScriptImport(path string, typ ...string) template.HTML {
if len(typ) > 0 {
if typ[0] == "module" {
+1 -1
View File
@@ -4,7 +4,7 @@
{{ctx.HeadMetaContentSecurityPolicy}}
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{if .Title}}{{.Title}} - {{end}}{{.PageTitleCommon}}</title>
<link rel="manifest" href="{{AssetUrlPrefix}}/site-manifest.json">
<link rel="manifest" href="{{ctx.SiteManifestURL}}">
<meta name="author" content="{{if .Repository}}{{.Owner.Name}}{{else}}{{MetaAuthor}}{{end}}">
<meta name="description" content="{{if .Repository}}{{.Repository.Name}}{{if .Repository.Description}} - {{.Repository.Description}}{{end}}{{else}}{{MetaDescription}}{{end}}">
<meta name="keywords" content="{{MetaKeywords}}">
+10
View File
@@ -0,0 +1,10 @@
import {test, expect} from '@playwright/test';
test('browser parses the site manifest', async ({page, browserName}) => {
test.skip(browserName !== 'chromium', 'only CDP exposes the parsed manifest'); // eslint-disable-line playwright/no-skipped-test -- conditional skip, the reason is in the message
await page.goto('/');
const cdp = await page.context().newCDPSession(page);
const {errors, manifest} = await cdp.send('Page.getAppManifest');
expect(errors).toEqual([]);
expect(manifest.icons).not.toHaveLength(0);
});
+8 -6
View File
@@ -4,6 +4,7 @@
package integration
import (
"encoding/base64"
"fmt"
"net/http"
"strings"
@@ -17,6 +18,7 @@ import (
"gitea.dev/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestVersion(t *testing.T) {
@@ -104,11 +106,11 @@ func testSecurityHeadersDefaults(t *testing.T) {
func testSiteManifest(t *testing.T) {
req := NewRequest(t, "GET", "/")
resp := MakeRequest(t, req, http.StatusOK)
assert.Contains(t, resp.Body.String(), `<link rel="manifest" href="/assets/site-manifest.json">`)
req = NewRequest(t, "GET", "/assets/site-manifest.json")
resp = MakeRequest(t, req, http.StatusOK)
assert.Equal(t, "application/manifest+json", resp.Header().Get("Content-Type"))
manifestURL, _ := NewHTMLParser(t, resp.Body).Find(`link[rel="manifest"]`).Attr("href")
manifestBase64, ok := strings.CutPrefix(manifestURL, "data:application/manifest+json;base64,")
require.True(t, ok)
manifestJSON, err := base64.StdEncoding.DecodeString(manifestBase64)
require.NoError(t, err)
assetBase := strings.TrimSuffix(setting.AppURL, "/")
expectedJSON := fmt.Sprintf(`{
@@ -126,7 +128,7 @@ func testSiteManifest(t *testing.T) {
assetBase+"/assets/img/logo.png",
assetBase+"/assets/img/logo.svg",
)
assert.JSONEq(t, expectedJSON, resp.Body.String())
assert.JSONEq(t, expectedJSON, string(manifestJSON))
}
func testViewPageCurrentURL(t *testing.T) {