app.ts

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