Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: scope api.papermark.com host to /v1 only

Marc Seitz55a4d222bbbf
diff --git a/middleware.ts b/middleware.ts
index 8193aa70..bde88c5a 100644
--- a/middleware.ts
+++ b/middleware.ts
@@ -51,6 +51,24 @@ export default async function middleware(req: NextRequest, ev: NextFetchEvent) {
const path = req.nextUrl.pathname;
const host = req.headers.get("host");
+ // api.papermark.com is restricted to the v1 surface. Filesystem pages
+ // (/dashboard, /settings, /login, …) would otherwise render here too,
+ // because Next.js routes pages on every host bound to the project.
+ // The Host header carries the port for non-default ports (e.g.
+ // `localhost:3000` in dev), so strip it before comparing to the env var
+ // — which is set to a bare hostname.
+ const apiHost = process.env.NEXT_PUBLIC_API_BASE_HOST?.toLowerCase().trim();
+ const requestHostname = host?.split(":")[0]?.toLowerCase().trim();
+ if (apiHost && requestHostname === apiHost) {
+ if (path === "/v1" || path.startsWith("/v1/") || path === "/openapi.json") {
+ return NextResponse.next();
+ }
+ if (path === "/") {
+ return NextResponse.redirect("https://www.papermark.com/docs/api", 302);
+ }
+ return new NextResponse(null, { status: 404 });
+ }
+
if (isAnalyticsPath(path)) {
return PostHogMiddleware(req);
}
diff --git a/next.config.mjs b/next.config.mjs
index df5a6a6b..025f31d5 100644
--- a/next.config.mjs
+++ b/next.config.mjs
@@ -38,11 +38,52 @@ const nextConfig = {
const beforeFiles = [];
const apiHost = process.env.NEXT_PUBLIC_API_BASE_HOST;
if (apiHost) {
- beforeFiles.push({
- source: "/:path*",
- destination: "/api/:path*",
- has: [{ type: "host", value: apiHost }],
- });
+ beforeFiles.push(
+ // The only allowed surface on the api subdomain is the public v1 API.
+ {
+ source: "/v1/:path*",
+ destination: "/api/v1/:path*",
+ has: [{ type: "host", value: apiHost }],
+ },
+ // Block direct hits to the internal /api/*, /oauth/*, and
+ // /.well-known/* routes on the api host. These paths bypass
+ // middleware (excluded in the matcher) so we rewrite them to /404
+ // here. Order matters — the /v1 rule above wins for /v1/* requests.
+ {
+ source: "/api/:path*",
+ destination: "/404",
+ has: [{ type: "host", value: apiHost }],
+ },
+ {
+ source: "/oauth/:path*",
+ destination: "/404",
+ has: [{ type: "host", value: apiHost }],
+ },
+ {
+ source: "/.well-known/:path*",
+ destination: "/404",
+ has: [{ type: "host", value: apiHost }],
+ },
+ // Public static files served from /public also bypass middleware
+ // (excluded in the matcher). Block them on the api host so the
+ // surface is genuinely just /v1/* — no marketing-site favicon /
+ // sitemap leaking through.
+ {
+ source: "/favicon.ico",
+ destination: "/404",
+ has: [{ type: "host", value: apiHost }],
+ },
+ {
+ source: "/sitemap.xml",
+ destination: "/404",
+ has: [{ type: "host", value: apiHost }],
+ },
+ {
+ source: "/robots.txt",
+ destination: "/404",
+ has: [{ type: "host", value: apiHost }],
+ },
+ );
}
// mcp.papermark.com — Linear-style short MCP entry point. Clients paste
// `https://mcp.papermark.com/mcp` in their configs; this host rewrites