All checks were successful
Deploy to Production / Deploy to Server (push) Successful in 2m52s
- CRITICAL: DNS rebinding SSRF - pin DNS resolution via request interception - CRITICAL: XSS in billing success - use data-attribute instead of JS string - HIGH: Webhook signature bypass - refuse unverified webhooks (500) - HIGH: Filename header injection - sanitize Content-Disposition filename - HIGH: Verification code timing attack - use crypto.timingSafeEqual() - HIGH: Remove duplicate unreachable 404 handler - HIGH: Add IPv6 unique local (fc00::/7) to SSRF private IP check - HIGH: Replace console.warn with structured logger
340 lines
12 KiB
TypeScript
340 lines
12 KiB
TypeScript
import express from "express";
|
|
import { randomUUID } from "crypto";
|
|
import compression from "compression";
|
|
import logger from "./services/logger.js";
|
|
import helmet from "helmet";
|
|
import path from "path";
|
|
import { fileURLToPath } from "url";
|
|
import rateLimit from "express-rate-limit";
|
|
import { convertRouter } from "./routes/convert.js";
|
|
import { templatesRouter } from "./routes/templates.js";
|
|
import { healthRouter } from "./routes/health.js";
|
|
import { signupRouter } from "./routes/signup.js";
|
|
import { recoverRouter } from "./routes/recover.js";
|
|
import { billingRouter } from "./routes/billing.js";
|
|
import { emailChangeRouter } from "./routes/email-change.js";
|
|
import { authMiddleware } from "./middleware/auth.js";
|
|
import { usageMiddleware, loadUsageData } from "./middleware/usage.js";
|
|
import { getUsageStats } from "./middleware/usage.js";
|
|
import { pdfRateLimitMiddleware, getConcurrencyStats } from "./middleware/pdfRateLimit.js";
|
|
import { initBrowser, closeBrowser } from "./services/browser.js";
|
|
import { loadKeys, getAllKeys } from "./services/keys.js";
|
|
import { verifyToken, loadVerifications } from "./services/verification.js";
|
|
import { initDatabase, pool } from "./services/db.js";
|
|
|
|
const app = express();
|
|
const PORT = parseInt(process.env.PORT || "3100", 10);
|
|
|
|
app.use(helmet({ crossOriginResourcePolicy: { policy: "cross-origin" } }));
|
|
|
|
// Request ID + request logging middleware
|
|
app.use((req, res, next) => {
|
|
const requestId = (req.headers["x-request-id"] as string) || randomUUID();
|
|
(req as any).requestId = requestId;
|
|
res.setHeader("X-Request-Id", requestId);
|
|
const start = Date.now();
|
|
res.on("finish", () => {
|
|
const ms = Date.now() - start;
|
|
if (req.path !== "/health") {
|
|
logger.info({ method: req.method, path: req.path, status: res.statusCode, ms, requestId }, "request");
|
|
}
|
|
});
|
|
next();
|
|
});
|
|
|
|
// Permissions-Policy header
|
|
app.use((_req, res, next) => {
|
|
res.setHeader("Permissions-Policy", "camera=(), microphone=(), geolocation=(), payment=(self)");
|
|
next();
|
|
});
|
|
|
|
// Compression
|
|
app.use(compression());
|
|
|
|
// Differentiated CORS middleware
|
|
app.use((req, res, next) => {
|
|
const isAuthBillingRoute = req.path.startsWith('/v1/signup') ||
|
|
req.path.startsWith('/v1/recover') ||
|
|
req.path.startsWith('/v1/billing') ||
|
|
req.path.startsWith('/v1/email-change');
|
|
|
|
if (isAuthBillingRoute) {
|
|
res.setHeader("Access-Control-Allow-Origin", "https://docfast.dev");
|
|
} else {
|
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
}
|
|
|
|
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
|
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, X-API-Key");
|
|
res.setHeader("Access-Control-Max-Age", "86400");
|
|
|
|
if (req.method === "OPTIONS") {
|
|
res.status(204).end();
|
|
return;
|
|
}
|
|
next();
|
|
});
|
|
|
|
// Raw body for Stripe webhook signature verification
|
|
app.use("/v1/billing/webhook", express.raw({ type: "application/json" }));
|
|
app.use(express.json({ limit: "2mb" }));
|
|
app.use(express.text({ limit: "2mb", type: "text/*" }));
|
|
|
|
// Trust nginx proxy
|
|
app.set("trust proxy", 1);
|
|
|
|
// Global rate limiting - reduced from 10,000 to reasonable limit
|
|
const limiter = rateLimit({
|
|
windowMs: 60_000,
|
|
max: 100,
|
|
standardHeaders: true,
|
|
legacyHeaders: false,
|
|
});
|
|
app.use(limiter);
|
|
|
|
// Public routes
|
|
app.use("/health", healthRouter);
|
|
app.use("/v1/signup", signupRouter);
|
|
app.use("/v1/recover", recoverRouter);
|
|
app.use("/v1/billing", billingRouter);
|
|
app.use("/v1/email-change", emailChangeRouter);
|
|
|
|
// Authenticated routes
|
|
app.use("/v1/convert", authMiddleware, usageMiddleware, pdfRateLimitMiddleware, convertRouter);
|
|
app.use("/v1/templates", authMiddleware, usageMiddleware, templatesRouter);
|
|
|
|
// Admin: usage stats
|
|
app.get("/v1/usage", authMiddleware, (req: any, res) => {
|
|
res.json(getUsageStats(req.apiKeyInfo?.key));
|
|
});
|
|
|
|
// Admin: concurrency stats
|
|
app.get("/v1/concurrency", authMiddleware, (_req, res) => {
|
|
res.json(getConcurrencyStats());
|
|
});
|
|
|
|
// Email verification endpoint
|
|
app.get("/verify", (req, res) => {
|
|
const token = req.query.token as string;
|
|
if (!token) {
|
|
res.status(400).send(verifyPage("Invalid Link", "No verification token provided.", null));
|
|
return;
|
|
}
|
|
|
|
const result = verifyToken(token);
|
|
|
|
switch (result.status) {
|
|
case "ok":
|
|
res.send(verifyPage("Email Verified! 🚀", "Your DocFast API key is ready:", result.verification!.apiKey));
|
|
break;
|
|
case "already_verified":
|
|
res.send(verifyPage("Already Verified", "This email was already verified. Here's your API key:", result.verification!.apiKey));
|
|
break;
|
|
case "expired":
|
|
res.status(410).send(verifyPage("Link Expired", "This verification link has expired (24h). Please sign up again.", null));
|
|
break;
|
|
case "invalid":
|
|
res.status(404).send(verifyPage("Invalid Link", "This verification link is not valid.", null));
|
|
break;
|
|
}
|
|
});
|
|
|
|
function verifyPage(title: string, message: string, apiKey: string | null): string {
|
|
return `<!DOCTYPE html>
|
|
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<title>${title} — DocFast</title>
|
|
<link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>⚡</text></svg>">
|
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700;800&display=swap" rel="stylesheet">
|
|
<style>
|
|
*{box-sizing:border-box;margin:0;padding:0}
|
|
body{font-family:'Inter',sans-serif;background:#0b0d11;color:#e4e7ed;min-height:100vh;display:flex;align-items:center;justify-content:center;padding:24px}
|
|
.card{background:#151922;border:1px solid #1e2433;border-radius:16px;padding:48px;max-width:520px;width:100%;text-align:center}
|
|
h1{font-size:1.8rem;margin-bottom:12px;font-weight:800}
|
|
p{color:#7a8194;margin-bottom:24px;line-height:1.6}
|
|
.key-box{background:#0b0d11;border:1px solid #34d399;border-radius:8px;padding:16px;font-family:monospace;font-size:0.82rem;word-break:break-all;margin:16px 0;cursor:pointer;transition:background 0.2s;position:relative}
|
|
.key-box:hover{background:#12151c}
|
|
.key-box::after{content:'Click to copy';position:absolute;top:-24px;right:0;font-size:0.7rem;color:#7a8194;font-family:'Inter',sans-serif}
|
|
.warning{background:rgba(251,191,36,0.06);border:1px solid rgba(251,191,36,0.15);border-radius:8px;padding:12px 16px;font-size:0.85rem;color:#fbbf24;margin-bottom:16px;text-align:left}
|
|
.links{margin-top:24px;color:#7a8194;font-size:0.9rem}
|
|
.links a{color:#34d399;text-decoration:none}
|
|
.links a:hover{color:#5eead4}
|
|
</style></head><body>
|
|
<div class="card">
|
|
<h1>${title}</h1>
|
|
<p>${message}</p>
|
|
${apiKey ? `
|
|
<div class="warning">⚠️ Save your API key securely. You can recover it via email if needed.</div>
|
|
<div class="key-box" onclick="navigator.clipboard.writeText('${apiKey}');this.style.borderColor='#5eead4';setTimeout(()=>this.style.borderColor='#34d399',1500)">${apiKey}</div>
|
|
<div class="links">100 free PDFs/month · <a href="/docs">Read the docs →</a></div>
|
|
` : `<div class="links"><a href="/">← Back to DocFast</a></div>`}
|
|
</div></body></html>`;
|
|
}
|
|
|
|
// Landing page
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
// Favicon route
|
|
app.get("/favicon.ico", (_req, res) => {
|
|
res.setHeader('Content-Type', 'image/svg+xml');
|
|
res.setHeader('Cache-Control', 'public, max-age=604800');
|
|
res.sendFile(path.join(__dirname, "../public/favicon.svg"));
|
|
});
|
|
|
|
app.use(express.static(path.join(__dirname, "../public"), {
|
|
maxAge: "1d",
|
|
etag: true,
|
|
setHeaders: (res) => {
|
|
res.setHeader('Cache-Control', 'public, max-age=86400');
|
|
}
|
|
}));
|
|
|
|
// Docs page (clean URL)
|
|
app.get("/docs", (_req, res) => {
|
|
res.setHeader('Cache-Control', 'public, max-age=86400');
|
|
res.sendFile(path.join(__dirname, "../public/docs.html"));
|
|
});
|
|
|
|
// Legal pages (clean URLs)
|
|
app.get("/impressum", (_req, res) => {
|
|
res.setHeader('Cache-Control', 'public, max-age=86400');
|
|
res.sendFile(path.join(__dirname, "../public/impressum.html"));
|
|
});
|
|
|
|
app.get("/privacy", (_req, res) => {
|
|
res.setHeader('Cache-Control', 'public, max-age=86400');
|
|
res.sendFile(path.join(__dirname, "../public/privacy.html"));
|
|
});
|
|
|
|
app.get("/terms", (_req, res) => {
|
|
res.setHeader('Cache-Control', 'public, max-age=86400');
|
|
res.sendFile(path.join(__dirname, "../public/terms.html"));
|
|
});
|
|
|
|
// API root
|
|
app.get("/api", (_req, res) => {
|
|
res.json({
|
|
name: "DocFast API",
|
|
version: "0.2.1",
|
|
endpoints: [
|
|
"POST /v1/signup/free — Get a free API key",
|
|
"POST /v1/convert/html",
|
|
"POST /v1/convert/markdown",
|
|
"POST /v1/convert/url",
|
|
"POST /v1/templates/:id/render",
|
|
"GET /v1/templates",
|
|
"POST /v1/billing/checkout — Start Pro subscription",
|
|
],
|
|
});
|
|
});
|
|
|
|
// 404 handler - must be after all routes
|
|
app.use((req, res) => {
|
|
// Check if it's an API request
|
|
const isApiRequest = req.path.startsWith('/v1/') || req.path.startsWith('/api') || req.path.startsWith('/health');
|
|
|
|
if (isApiRequest) {
|
|
// JSON 404 for API paths
|
|
res.status(404).json({
|
|
error: "Not Found",
|
|
message: `The requested endpoint ${req.method} ${req.path} does not exist`,
|
|
statusCode: 404,
|
|
timestamp: new Date().toISOString()
|
|
});
|
|
} else {
|
|
// HTML 404 for browser paths
|
|
res.status(404).send(`<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>404 - Page Not Found | DocFast</title>
|
|
<link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>⚡</text></svg>">
|
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap" rel="stylesheet">
|
|
<style>
|
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
body { font-family: 'Inter', sans-serif; background: #0b0d11; color: #e4e7ed; min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; }
|
|
.container { background: #151922; border: 1px solid #1e2433; border-radius: 16px; padding: 48px; max-width: 520px; width: 100%; text-align: center; }
|
|
h1 { font-size: 3rem; margin-bottom: 12px; font-weight: 700; color: #34d399; }
|
|
h2 { font-size: 1.5rem; margin-bottom: 16px; font-weight: 600; }
|
|
p { color: #7a8194; margin-bottom: 24px; line-height: 1.6; }
|
|
a { color: #34d399; text-decoration: none; font-weight: 600; }
|
|
a:hover { color: #5eead4; }
|
|
.emoji { font-size: 4rem; margin-bottom: 24px; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="emoji">⚡</div>
|
|
<h1>404</h1>
|
|
<h2>Page Not Found</h2>
|
|
<p>The page you're looking for doesn't exist or has been moved.</p>
|
|
<p><a href="/">← Back to DocFast</a> | <a href="/docs">Read the docs</a></p>
|
|
</div>
|
|
</body>
|
|
</html>`);
|
|
}
|
|
});
|
|
|
|
|
|
|
|
async function start() {
|
|
// Initialize PostgreSQL
|
|
await initDatabase();
|
|
|
|
// Load data from PostgreSQL
|
|
await loadKeys();
|
|
await loadVerifications();
|
|
await loadUsageData();
|
|
|
|
await initBrowser();
|
|
logger.info(`Loaded ${getAllKeys().length} API keys`);
|
|
const server = app.listen(PORT, () => logger.info(`DocFast API running on :${PORT}`));
|
|
|
|
let shuttingDown = false;
|
|
const shutdown = async (signal: string) => {
|
|
if (shuttingDown) return;
|
|
shuttingDown = true;
|
|
logger.info(`Received ${signal}, starting graceful shutdown...`);
|
|
|
|
// 1. Stop accepting new connections, wait for in-flight requests (max 10s)
|
|
await new Promise<void>((resolve) => {
|
|
const forceTimeout = setTimeout(() => {
|
|
logger.warn("Forcing server close after 10s timeout");
|
|
resolve();
|
|
}, 10_000);
|
|
server.close(() => {
|
|
clearTimeout(forceTimeout);
|
|
logger.info("HTTP server closed (all in-flight requests completed)");
|
|
resolve();
|
|
});
|
|
});
|
|
|
|
// 2. Close Puppeteer browser pool
|
|
try {
|
|
await closeBrowser();
|
|
logger.info("Browser pool closed");
|
|
} catch (err) {
|
|
logger.error({ err }, "Error closing browser pool");
|
|
}
|
|
|
|
// 3. Close PostgreSQL connection pool
|
|
try {
|
|
await pool.end();
|
|
logger.info("PostgreSQL pool closed");
|
|
} catch (err) {
|
|
logger.error({ err }, "Error closing PostgreSQL pool");
|
|
}
|
|
|
|
logger.info("Graceful shutdown complete");
|
|
process.exit(0);
|
|
};
|
|
process.on("SIGTERM", () => shutdown("SIGTERM"));
|
|
process.on("SIGINT", () => shutdown("SIGINT"));
|
|
}
|
|
|
|
start().catch((err) => {
|
|
logger.error({ err }, "Failed to start");
|
|
process.exit(1);
|
|
});
|
|
|
|
export { app };
|