app.ts

4.2 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		if (c.req.path.endsWith(".svg")) c.header("Content-Security-Policy", "sandbox");43	});44	app.use(45		secureHeaders({46			contentSecurityPolicy: {47				defaultSrc: ["'self'"],48				scriptSrc: ["'self'"],49				styleSrc: ["'self'", FONTS],50				fontSrc: [FONTS],51				imgSrc,52				objectSrc: ["'none'"],53				baseUri: ["'none'"],54				formAction: ["'self'"],55				frameAncestors: ["'none'"],56			},57			xFrameOptions: "DENY",58		}),59	);60	app.use(61		bodyLimit({ maxSize: MAX_BODY_BYTES, onError: (c) => c.text("Request body too large", 413) }),62	);63	// `/static/assets/` is the operator's uploads in `data/assets/` (§19), such as the signature.64	const assetsDir = join(config.dataDir, "assets");65	app.get("/static/:path{.+}", async (c) => {66		const path = c.req.param("path");67		if (!isSafePath(path)) return c.notFound();68		const file = Bun.file(69			path.startsWith("assets/") ? join(assetsDir, path.slice(7)) : join(PUBLIC_DIR, path),70		);71		if (!(await file.exists())) return c.notFound();72		c.header("Cache-Control", "public, max-age=3600");73		if (file.type === "image/svg+xml") c.header("Content-Security-Policy", "sandbox");74		return c.body(await file.bytes(), 200, { "Content-Type": file.type });75	});76	const ctx = { domain: config.domain, kepr, mirrorDir: join(config.dataDir, "mirror") };77	const bucket = openBucket(config.bucket);78	// Before every other route, so the admin guard runs ahead of each `/admin` handler.79	registerLogin(app, ctx, config.adminPasswordHash);80	const admin = {81		...ctx,82		dataDir: config.dataDir,83		sync: { bucket, kepr, mirrorDir: ctx.mirrorDir, tarballDir: join(config.dataDir, "tarballs") },84	};85	registerStatus(app, admin);86	registerRepos(app, admin);87	registerIssues(app, admin);88	registerReleases(app, admin);89	registerSite(app, admin);90	registerHousekeeping(app, ctx);91	registerFrontPages(app, ctx);92	registerDocsPages(app, ctx);93	registerProfilePage(app, ctx);94	registerKepr2Routes(app, ctx);95	registerRepoPage(app, ctx);96	registerFileRoutes(app, ctx);97	registerSavePage(app, ctx);98	registerArchivesPage(app, ctx);99	registerReleasesPage(app, ctx);100	registerIssuePages(app, ctx);101	registerTarballRoutes(app, ctx, join(config.dataDir, "tarballs"));102	registerReleaseRoutes(app, ctx, bucket);103	return app;104}