app.ts

4.6 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 { connectKeys, noCode, registerConnect } from "./connect.ts";28import { registerLogin } from "./login.ts";2930const MAX_BODY_BYTES = 64 * 1024;31const PUBLIC_DIR = join(import.meta.dir, "../../public");32// §18 allows Bunny Fonts: its stylesheet and the font files it points at.33const FONTS = "https://fonts.bunny.net";3435/** The Hono app with §25's headers and body cap, and every route built so far. */36export function createApp(config: Config, kepr: Database): Hono {37	const app = new Hono();38	// §25 allows images from the presigned host, which is the bucket endpoint's origin.39	const imgSrc =40		config.bucket.kind === "s3" ? ["'self'", new URL(config.bucket.endpoint).origin] : ["'self'"];41	app.use("*", async (c, next) => {42		await next();43		const path = c.req.path;44		if (/^\/[^/]+\/raw\/.+/.test(path) || path.startsWith("/static/assets/")) {45			const policy = c.res.headers.get("Content-Security-Policy");46			c.header("Content-Security-Policy", policy ? `${policy}; sandbox` : "sandbox");47		}48	});49	app.use(50		secureHeaders({51			contentSecurityPolicy: {52				defaultSrc: ["'self'"],53				scriptSrc: ["'self'"],54				styleSrc: ["'self'", FONTS],55				fontSrc: [FONTS],56				imgSrc,57				objectSrc: ["'none'"],58				baseUri: ["'none'"],59				formAction: ["'self'"],60				frameAncestors: ["'none'"],61			},62			xFrameOptions: "DENY",63		}),64	);65	app.use(66		bodyLimit({67			maxSize: MAX_BODY_BYTES,68			onError: (c) =>69				// An oversized redemption is one more failed redemption — but only where the route70				// exists: without write keys it is an unknown path and answers as one.71				c.req.path === "/api/connect" && connectKeys(config)72					? noCode(c)73					: c.text("Request body too large", 413),74		}),75	);76	// `/static/assets/` is the operator's uploads in `data/assets/` (§19), such as the signature.77	const assetsDir = join(config.dataDir, "assets");78	app.get("/static/:path{.+}", async (c) => {79		const path = c.req.param("path");80		if (!isSafePath(path)) return c.notFound();81		const file = Bun.file(82			path.startsWith("assets/") ? join(assetsDir, path.slice(7)) : join(PUBLIC_DIR, path),83		);84		if (!(await file.exists())) return c.notFound();85		c.header("Cache-Control", "public, max-age=3600");86		return c.body(await file.bytes(), 200, { "Content-Type": file.type });87	});88	const ctx = { domain: config.domain, kepr, mirrorDir: join(config.dataDir, "mirror") };89	const bucket = openBucket(config.bucket);90	registerLogin(app, ctx, config.adminPasswordHash);91	registerConnect(app, config, ctx);92	const admin = {93		...ctx,94		canConnect: connectKeys(config) !== null,95		dataDir: config.dataDir,96		sync: { bucket, kepr, mirrorDir: ctx.mirrorDir, tarballDir: join(config.dataDir, "tarballs") },97	};98	registerStatus(app, admin);99	registerRepos(app, admin);100	registerIssues(app, admin);101	registerReleases(app, admin);102	registerSite(app, admin);103	registerHousekeeping(app, ctx);104	registerFrontPages(app, ctx);105	registerDocsPages(app, ctx);106	registerProfilePage(app, ctx);107	registerKepr2Routes(app, ctx);108	registerRepoPage(app, ctx);109	registerFileRoutes(app, ctx);110	registerSavePage(app, ctx);111	registerArchivesPage(app, ctx);112	registerReleasesPage(app, ctx);113	registerIssuePages(app, ctx);114	registerTarballRoutes(app, ctx, join(config.dataDir, "tarballs"));115	registerReleaseRoutes(app, ctx, bucket);116	return app;117}