app.ts

4.3 KB · raw

1import type { Database } from "bun:sqlite";2import { join } from "node:path";3import { Hono } from "hono";4import { bodyLimit } from "hono/body-limit";5import { secureHeaders } from "hono/secure-headers";6import { registerStatus } from "../admin/console.ts";7import { registerIssues } from "../admin/issues.ts";8import { registerReleases } from "../admin/releases.ts";9import { registerRepos } from "../admin/repos.ts";10import { registerSite } from "../admin/site.ts";11import { openBucket } from "../bucket/reader.ts";12import { isSafePath } from "../bucket/repo.ts";13import { registerIssuePages } from "../issues/pages.ts";14import { registerArchivesPage } from "../pages/archives.ts";15import { registerDocsPages } from "../pages/docs.ts";16import { registerFileRoutes } from "../pages/files.ts";17import { registerFrontPages } from "../pages/front.ts";18import { registerHousekeeping } from "../pages/housekeeping.ts";19import { registerKepr2Routes } from "../pages/kepr2.ts";20import { registerProfilePage } from "../pages/profile.ts";21import { registerReleasesPage } from "../pages/releases.ts";22import { registerRepoPage } from "../pages/repo.ts";23import { registerSavePage } from "../pages/save.ts";24import { registerReleaseRoutes } from "../releases/routes.ts";25import { registerTarballRoutes } from "../tarball/archive.ts";26import type { Config } from "./config.ts";27import { registerLogin } from "./login.ts";2829const MAX_BODY_BYTES = 64 * 1024;30const PUBLIC_DIR = join(import.meta.dir, "../../public");31// §18 allows Bunny Fonts: its stylesheet and the font files it points at.32const FONTS = "https://fonts.bunny.net";3334/** The Hono app with §25's headers and body cap, and every route built so far. */35export function createApp(config: Config, kepr: Database): Hono {36	const app = new Hono();37	// §25 allows images from the presigned host, which is the bucket endpoint's origin.38	const imgSrc =39		config.bucket.kind === "s3" ? ["'self'", new URL(config.bucket.endpoint).origin] : ["'self'"];40	app.use("*", async (c, next) => {41		await next();42		const path = c.req.path;43		if (/^\/[^/]+\/raw\/.+/.test(path) || path.startsWith("/static/assets/")) {44			const policy = c.res.headers.get("Content-Security-Policy");45			c.header("Content-Security-Policy", policy ? `${policy}; sandbox` : "sandbox");46		}47	});48	app.use(49		secureHeaders({50			contentSecurityPolicy: {51				defaultSrc: ["'self'"],52				scriptSrc: ["'self'"],53				styleSrc: ["'self'", FONTS],54				fontSrc: [FONTS],55				imgSrc,56				objectSrc: ["'none'"],57				baseUri: ["'none'"],58				formAction: ["'self'"],59				frameAncestors: ["'none'"],60			},61			xFrameOptions: "DENY",62		}),63	);64	app.use(65		bodyLimit({ maxSize: MAX_BODY_BYTES, onError: (c) => c.text("Request body too large", 413) }),66	);67	// `/static/assets/` is the operator's uploads in `data/assets/` (§19), such as the signature.68	const assetsDir = join(config.dataDir, "assets");69	app.get("/static/:path{.+}", async (c) => {70		const path = c.req.param("path");71		if (!isSafePath(path)) return c.notFound();72		const file = Bun.file(73			path.startsWith("assets/") ? join(assetsDir, path.slice(7)) : join(PUBLIC_DIR, path),74		);75		if (!(await file.exists())) return c.notFound();76		c.header("Cache-Control", "public, max-age=3600");77		return c.body(await file.bytes(), 200, { "Content-Type": file.type });78	});79	const ctx = { domain: config.domain, kepr, mirrorDir: join(config.dataDir, "mirror") };80	const bucket = openBucket(config.bucket);81	// Before every other route, so the admin guard runs ahead of each `/admin` handler.82	registerLogin(app, ctx, config.adminPasswordHash);83	const admin = {84		...ctx,85		dataDir: config.dataDir,86		sync: { bucket, kepr, mirrorDir: ctx.mirrorDir, tarballDir: join(config.dataDir, "tarballs") },87	};88	registerStatus(app, admin);89	registerRepos(app, admin);90	registerIssues(app, admin);91	registerReleases(app, admin);92	registerSite(app, admin);93	registerHousekeeping(app, ctx);94	registerFrontPages(app, ctx);95	registerDocsPages(app, ctx);96	registerProfilePage(app, ctx);97	registerKepr2Routes(app, ctx);98	registerRepoPage(app, ctx);99	registerFileRoutes(app, ctx);100	registerSavePage(app, ctx);101	registerArchivesPage(app, ctx);102	registerReleasesPage(app, ctx);103	registerIssuePages(app, ctx);104	registerTarballRoutes(app, ctx, join(config.dataDir, "tarballs"));105	registerReleaseRoutes(app, ctx, bucket);106	return app;107}