rewrite Gitea's URLs so the frame stays inside the proxy
Gitea believes it lives at /. Its pages are full of root-absolute links, and inside the frame those resolve against OFFICER'S origin — the page renders perfectly and falls apart on the first click. Four places a URL hides, each failing differently: href/src/action attributes, window.config.appSubUrl, Location headers, and absolute URLs built from ROOT_URL. appSubUrl is the highest-value line. Gitea already supports being hosted under a subpath and its client JS prefixes every URL it builds with that value, so filling it in makes Gitea correct ITSELF rather than by interception. The ROOT_URL case needs Gitea's public origin, which the sidecar does not have — it fetches from loopback. Rather than add a second setting that can disagree with Gitea's own, appUrlOrigin reads it out of the page: Gitea emits "appUrl" in the same config blob as appSubUrl. Not rewritten, deliberately: JavaScript bodies, because a blanket rewrite of absolute paths inside a script corrupts strings that are not URLs; and clone URLs, which must keep working for whoever copies them. /_web/* now forwards by hand rather than through createSidecarProxy. That forwarder passes three headers and nothing else, on purpose — it is shared by every sidecar and its restraint is what makes it safe to reuse. It strips cookies, and this path is nothing but cookies. Loosening it for gitea would loosen it for the wallet. Eighteen tests, the only part of this plugin verifiable without a running Gitea. One caught a real bug: rewriteAbsolute turns a full URL into <base>/path and the attribute pass then prefixed it again.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import type { SidecarCommand, SidecarEvent } from '@@/sidecar/protocol';
|
||||
import { createSidecarConnector } from '@@/sidecar/connect';
|
||||
import { rewriteHtml, rewriteLocation, rewriteSetCookie } from './rewrite';
|
||||
import {
|
||||
getServiceConnection,
|
||||
saveServiceConnection,
|
||||
@@ -265,6 +266,34 @@ const server = Bun.serve({
|
||||
out.delete('x-frame-options');
|
||||
out.delete('content-security-policy');
|
||||
for (const h of ['connection', 'keep-alive', 'transfer-encoding']) out.delete(h);
|
||||
|
||||
// Where the browser thinks this is. Passed by the plugin's router rather than hardcoded — the
|
||||
// sidecar has no business knowing how the platform routes to it, and `mountPrefix` is the only
|
||||
// thing entitled to decide.
|
||||
const base = req.headers.get('X-Officer-Frame-Base');
|
||||
const origins = [config.base];
|
||||
|
||||
if (base) {
|
||||
const location = out.get('location');
|
||||
if (location) out.set('location', rewriteLocation(location, base, origins));
|
||||
|
||||
// Every Set-Cookie, not just the first: Gitea sets a session and a CSRF cookie together, and
|
||||
// `Headers.get` would concatenate them into one broken value.
|
||||
const cookies = (out as unknown as { getSetCookie?: () => string[] }).getSetCookie?.() ?? [];
|
||||
if (cookies.length) {
|
||||
out.delete('set-cookie');
|
||||
for (const c of cookies) out.append('set-cookie', rewriteSetCookie(c, base));
|
||||
}
|
||||
|
||||
// HTML only. Rewriting JavaScript hits strings that are not URLs and corrupts the file —
|
||||
// `appSubUrl`, patched below by the same pass, is what makes Gitea's own scripts correct.
|
||||
if ((out.get('content-type') ?? '').includes('text/html')) {
|
||||
const body = rewriteHtml(await upstream.text(), base, origins);
|
||||
out.delete('content-length'); // the body just changed length
|
||||
return new Response(body, { status: upstream.status, headers: out });
|
||||
}
|
||||
}
|
||||
|
||||
return new Response(upstream.body, { status: upstream.status, headers: out });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user