98 lines
3.0 KiB
TypeScript
98 lines
3.0 KiB
TypeScript
import { Router } from "@oak/oak";
|
|
import {
|
|
fetchWithTimeout,
|
|
isValidHttpUrl,
|
|
tryFetchRichContent,
|
|
} from "../services/rich-content-service.ts";
|
|
import { APIErrorCode } from "../model/interfaces.ts";
|
|
import { authMiddleware } from "../middleware/auth.ts";
|
|
|
|
const previewRouter = new Router();
|
|
|
|
const PROXYABLE_IMAGE_TYPES = new Set([
|
|
"image/jpeg",
|
|
"image/png",
|
|
"image/gif",
|
|
"image/webp",
|
|
"image/avif",
|
|
"image/bmp",
|
|
"image/x-icon",
|
|
"image/vnd.microsoft.icon",
|
|
]);
|
|
|
|
// Signed-in only: it's the create form's preview, and an open one lets anyone
|
|
// use the server to fetch pages for them.
|
|
previewRouter.get("/api/preview", authMiddleware, async (ctx) => {
|
|
const url = ctx.request.url.searchParams.get("url") ?? "";
|
|
if (!isValidHttpUrl(url)) {
|
|
ctx.response.status = 400;
|
|
ctx.response.body = {
|
|
success: false,
|
|
error: { code: APIErrorCode.VALIDATION_ERROR, message: "Invalid URL" },
|
|
};
|
|
return;
|
|
}
|
|
// `reached` is reported separately because a failed fetch still yields a
|
|
// usable stub (hostname only). Without the flag the create form cannot tell
|
|
// "this page has no preview" from "this link is dead", and shows the same
|
|
// bare card for both.
|
|
const { ok, content } = await tryFetchRichContent(url);
|
|
ctx.response.body = {
|
|
success: true,
|
|
data: { reached: ok, richContent: content ?? null },
|
|
};
|
|
});
|
|
|
|
/**
|
|
* Proxy an external image through the server so HTTP thumbnail URLs don't
|
|
* trigger mixed-content blocks when the frontend is served over HTTPS.
|
|
*/
|
|
previewRouter.get("/api/proxy-image", async (ctx) => {
|
|
const url = ctx.request.url.searchParams.get("url") ?? "";
|
|
if (!isValidHttpUrl(url)) {
|
|
ctx.response.status = 400;
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const res = await fetchWithTimeout(url, 8000);
|
|
// Raster formats only. The bytes go out under this site's origin, so an
|
|
// SVG (which can carry script) or anything else a browser might execute
|
|
// would run with the app's origin if opened directly.
|
|
const contentType = (res.headers.get("content-type") ?? "")
|
|
.split(";")[0].trim().toLowerCase();
|
|
if (!PROXYABLE_IMAGE_TYPES.has(contentType)) {
|
|
await res.body?.cancel();
|
|
ctx.response.status = 400;
|
|
return;
|
|
}
|
|
|
|
const MAX_SIZE = 5 * 1024 * 1024; // 5 MB
|
|
const contentLength = Number(res.headers.get("content-length") ?? "0");
|
|
if (contentLength > MAX_SIZE) {
|
|
await res.body?.cancel();
|
|
ctx.response.status = 400;
|
|
return;
|
|
}
|
|
|
|
const bytes = new Uint8Array(await res.arrayBuffer());
|
|
if (bytes.length > MAX_SIZE) {
|
|
ctx.response.status = 400;
|
|
return;
|
|
}
|
|
|
|
ctx.response.headers.set("Content-Type", contentType);
|
|
ctx.response.headers.set("X-Content-Type-Options", "nosniff");
|
|
ctx.response.headers.set(
|
|
"Content-Security-Policy",
|
|
"sandbox; default-src 'none'",
|
|
);
|
|
ctx.response.headers.set("Cache-Control", "public, max-age=86400");
|
|
ctx.response.body = bytes;
|
|
} catch {
|
|
ctx.response.status = 502;
|
|
}
|
|
});
|
|
|
|
export default previewRouter;
|