fix(mobile): trust Gitea release download assets
Fork Android APK / release (push) Has been cancelled

This commit is contained in:
bonzi committed 2026-09-19 03:07:38 +00:00
1 parent 0d0f592a54
commit 92724f1509
2 files changed
+70 -11

No files matched your search

@@ -159,9 +159,26 @@ describe("Android APK release selection", () => {
).toBeNull();
});
it("rejects same-origin assets outside the exact Gitea attachment path", () => {
it("accepts the exact Gitea repository release download path", () => {
const browser_download_url =
"https://git.example.com/alex/t3code/releases/download/apk-5/t3code-apk-5.apk";
expect(
selectLatestAndroidApkRelease(
[
release(5, {
assets: [{ id: 5, name: "t3code-apk-5.apk", size: 4, browser_download_url }],
}),
],
API_URL,
),
).toEqual({ versionCode: 5, downloadUrl: browser_download_url, expectedSize: 4 });
});
it("rejects same-origin assets outside trusted Gitea asset paths", () => {
for (const browser_download_url of [
`https://git.example.com/alex/t3code/releases/download/apk-5/t3code-apk-5.apk`,
`https://git.example.com/other/t3code/releases/download/apk-5/t3code-apk-5.apk`,
`https://git.example.com/alex/t3code/releases/download/apk-6/t3code-apk-5.apk`,
`https://git.example.com/alex/t3code/releases/download/apk-5/other.apk`,
`https://git.example.com/api/v1/repos/alex/t3code/releases/assets/5`,
`https://git.example.com/other/attachments/${attachmentUuid(5)}`,
`https://git.example.com/attachments/not-a-uuid`,
@@ -201,6 +218,22 @@ describe("Android APK release selection", () => {
expect(selectLatestAndroidApkRelease([valid], API_URL)).toBeNull();
});
it("uses the exact configured Gitea subpath for release downloads", () => {
const valid = release(5, {
assets: [
{
id: 5,
name: "t3code-apk-5.apk",
size: 4,
browser_download_url:
"https://git.example.com/git/alex/t3code/releases/download/apk-5/t3code-apk-5.apk",
},
],
});
expect(selectLatestAndroidApkRelease([valid], SUBPATH_API_URL)?.versionCode).toBe(5);
expect(selectLatestAndroidApkRelease([valid], API_URL)).toBeNull();
});
it("rejects assets with invalid or excessive declared sizes", () => {
for (const size of [0, -1, 1.5, 500 * 1024 * 1024 + 1]) {
expect(
@@ -136,23 +136,44 @@ function trustedHttpsUrl(rawUrl: string, kind: "API" | "asset"): URL {
return url;
}
function giteaServerBase(apiUrl: string): { readonly origin: string; readonly path: string } {
function giteaServerBase(apiUrl: string): {
readonly origin: string;
readonly path: string;
readonly owner: string;
readonly repository: string;
} {
const url = trustedHttpsUrl(apiUrl, "API");
const match = /^(.*)\/api\/v1\/repos\/[^/]+\/[^/]+\/releases\/?$/.exec(url.pathname);
const match = /^(.*)\/api\/v1\/repos\/([^/]+)\/([^/]+)\/releases\/?$/.exec(url.pathname);
if (!match) throw new Error("The update API URL is not a Gitea repository releases URL.");
return { origin: url.origin, path: match[1] ?? "" };
return {
origin: url.origin,
path: match[1] ?? "",
owner: match[2] ?? "",
repository: match[3] ?? "",
};
}
function trustedAttachmentUrl(rawUrl: unknown, apiUrl: string): string | null {
function trustedAssetUrl(
rawUrl: unknown,
apiUrl: string,
releaseTag: string,
assetName: string,
): string | null {
if (typeof rawUrl !== "string") return null;
try {
const url = trustedHttpsUrl(rawUrl, "asset");
const server = giteaServerBase(apiUrl);
if (url.origin !== server.origin) return null;
const prefix = `${server.path}/attachments/`;
if (!url.pathname.startsWith(prefix)) return null;
const uuid = url.pathname.slice(prefix.length);
return GITEA_ATTACHMENT_UUID.test(uuid) ? url.href : null;
const attachmentPrefix = `${server.path}/attachments/`;
if (url.pathname.startsWith(attachmentPrefix)) {
const uuid = url.pathname.slice(attachmentPrefix.length);
return GITEA_ATTACHMENT_UUID.test(uuid) ? url.href : null;
}
const releaseUrl = new URL(
`${server.path}/${server.owner}/${server.repository}/releases/download/${releaseTag}/${assetName}`,
server.origin,
);
return url.href === releaseUrl.href ? url.href : null;
} catch {
return null;
}
@@ -411,7 +432,12 @@ export function selectLatestAndroidApkRelease(
asset.name !== expectedAssetName
)
continue;
const downloadUrl = trustedAttachmentUrl(asset.browser_download_url, apiUrl);
const downloadUrl = trustedAssetUrl(
asset.browser_download_url,
apiUrl,
release.tag_name,
expectedAssetName,
);
if (!downloadUrl) continue;
candidate = { versionCode, downloadUrl, expectedSize: asset.size };
break;