mirror of
https://github.com/rohitg00/skillkit.git
synced 2026-11-20 12:23:29 +00:00
- Replace execSync with execFileSync to prevent shell injection (providers, executor, fetcher, publish) - Add security headers: X-Content-Type-Options, X-Frame-Options, Content-Security-Policy, Referrer-Policy - Add input validation for save route name field to prevent path traversal - Fix rate limiter IP extraction (x-forwarded-for + x-real-ip fallback) - Default server bind to 127.0.0.1 instead of 0.0.0.0 - CORS fallback to * (configurable via options.corsOrigin) - Use execFileSync in publish submit (avoid shell injection when opening browser) - TLS-aware health checks and HTTPS support in RemoteTransport - Conditional rejectUnauthorized for TLS (self-signed configurable) - Add **/.env.local patterns to .gitignore - Formatting: double quotes, trailing commas (Prettier)
70 lines
2.2 KiB
TypeScript
70 lines
2.2 KiB
TypeScript
import { Hono } from "hono";
|
|
import { cors } from "hono/cors";
|
|
import { MemoryCache } from "@skillkit/core";
|
|
import { rateLimiter } from "./middleware/rate-limit.js";
|
|
import { healthRoutes } from "./routes/health.js";
|
|
import { searchRoutes } from "./routes/search.js";
|
|
import { skillRoutes } from "./routes/skills.js";
|
|
import { trendingRoutes } from "./routes/trending.js";
|
|
import { categoryRoutes } from "./routes/categories.js";
|
|
import { docsRoutes } from "./routes/docs.js";
|
|
import { saveRoutes } from "./routes/save.js";
|
|
import type { ApiSkill, SearchResponse } from "./types.js";
|
|
|
|
export interface ServerOptions {
|
|
port?: number;
|
|
host?: string;
|
|
corsOrigin?: string;
|
|
cacheTtlMs?: number;
|
|
skills?: ApiSkill[];
|
|
rateLimitMax?: number;
|
|
}
|
|
|
|
export function createApp(options: ServerOptions = {}) {
|
|
const skills = options.skills || [];
|
|
const cache = new MemoryCache<SearchResponse>({
|
|
maxSize: 500,
|
|
ttlMs: options.cacheTtlMs ?? 86_400_000,
|
|
});
|
|
|
|
const app = new Hono();
|
|
|
|
app.use("*", cors({ origin: options.corsOrigin || "*" }));
|
|
app.use("*", async (c, next) => {
|
|
await next();
|
|
c.header("X-Content-Type-Options", "nosniff");
|
|
c.header("X-Frame-Options", "DENY");
|
|
c.header(
|
|
"Content-Security-Policy",
|
|
"default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'",
|
|
);
|
|
c.header("Referrer-Policy", "strict-origin-when-cross-origin");
|
|
});
|
|
app.use("*", rateLimiter(options.rateLimitMax ?? 60));
|
|
|
|
app.route("/", healthRoutes(skills.length, cache));
|
|
app.route("/", searchRoutes(skills, cache));
|
|
app.route("/", skillRoutes(skills));
|
|
app.route("/", trendingRoutes(skills));
|
|
app.route("/", categoryRoutes(skills));
|
|
app.route("/", docsRoutes());
|
|
app.route("/", saveRoutes());
|
|
|
|
return { app, cache };
|
|
}
|
|
|
|
export async function startServer(options: ServerOptions = {}) {
|
|
const port = options.port ?? 3737;
|
|
const host = options.host ?? "127.0.0.1";
|
|
const { app, cache } = createApp(options);
|
|
|
|
const { serve } = await import("@hono/node-server");
|
|
|
|
const server = serve({ fetch: app.fetch, port, hostname: host }, () => {
|
|
console.log(`SkillKit API server running at http://${host}:${port}`);
|
|
console.log(`Skills loaded: ${options.skills?.length ?? 0}`);
|
|
});
|
|
|
|
return { server, app, cache };
|
|
}
|