-
-
Notifications
You must be signed in to change notification settings - Fork 0
Add Redis cache, origin validation, SSRF hardening, and code quality refactor #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
8d20959
f1dd47f
2f50934
4be3469
d73a5d8
4856045
be45f8a
d86c6cd
64b2d57
65e6fd4
0e2a2cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,7 +4,11 @@ import { config } from "./config"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { healthRoutes } from "./routes/health"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { imageRoutes } from "./routes/image"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { ogRoutes } from "./routes/og"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { startCacheCleanup } from "./services/cache"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| disconnectRedisCache, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| initRedisCache, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| startCacheCleanup, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } from "./services/cache"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Detect if running as a cluster worker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isWorker = process.env.PIXELSERVE_WORKER_ID !== undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -15,12 +19,55 @@ if (config.cacheMode === "disk" || config.cacheMode === "hybrid") { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await Bun.$`mkdir -p ${config.cacheDir}`.quiet(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Initialize Redis cache if configured | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (config.cacheMode === "redis") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await initRedisCache(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const app = new Elysia() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .use( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cors({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| origin: config.allowedOrigins.length > 0 ? config.allowedOrigins : true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .onRequest(({ request, set }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (config.allowedOrigins.length === 0) return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const url = new URL(request.url); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (url.pathname === "/health") return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const origin = request.headers.get("Origin"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const referer = request.headers.get("Referer"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!origin && !referer) return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Requests without Origin/Referer headers bypass validation entirely. Returning early when both headers are missing allows direct API calls (curl, server-side requests, tools like Postman) to bypass origin restrictions completely. If the intent is to restrict access to specific origins, this creates a bypass path. If this is intentional (e.g., allowing server-to-server calls), consider documenting it. Otherwise, consider denying requests without a recognizable origin when 🛡️ Proposed fix to deny requests without origin headers- if (!origin && !referer) return;
+ if (!origin && !referer) {
+ set.status = 403;
+ return { error: "FORBIDDEN", message: "Origin not allowed" };
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const sourceOrigin = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| origin || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new URL(referer as string).origin; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return referer as string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| })(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isAllowed = config.allowedOrigins.some((allowed) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (sourceOrigin === allowed) return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const parsed = new URL(sourceOrigin); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parsed.hostname === allowed || parsed.hostname.endsWith(`.${allowed}`) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return sourceOrigin === allowed; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Origin matching logic is inconsistent with URL vs hostname comparison. The matching logic mixes URL and hostname comparisons:
If Consider normalizing both sides to hostnames for consistent comparison: ♻️ Proposed fix for consistent hostname extraction const isAllowed = config.allowedOrigins.some((allowed) => {
if (sourceOrigin === allowed) return true;
try {
const parsed = new URL(sourceOrigin);
+ // Normalize allowed to hostname (handle both "example.com" and "https://example.com")
+ let allowedHostname: string;
+ try {
+ allowedHostname = new URL(allowed).hostname;
+ } catch {
+ allowedHostname = allowed; // Treat as bare hostname
+ }
return (
- parsed.hostname === allowed || parsed.hostname.endsWith(`.${allowed}`)
+ parsed.hostname === allowedHostname || parsed.hostname.endsWith(`.${allowedHostname}`)
);
} catch {
return sourceOrigin === allowed;
}
});📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!isAllowed) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| set.status = 403; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { error: "FORBIDDEN", message: "Origin not allowed" }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .onError(({ error, code, set }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Don't log Elysia's built-in NOT_FOUND errors (these are normal 404s) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (code === "NOT_FOUND") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -72,6 +119,10 @@ function getCacheInfo(): string { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return `memory (max ${config.maxMemoryCacheItems} items)`; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case "hybrid": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return `hybrid (memory + ${config.cacheDir})`; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case "redis": { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const masked = config.redisUrl.replace(/:\/\/[^@]*@/, "://***@"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return `redis (${masked})`; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case "none": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "disabled"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -121,4 +172,12 @@ ${c.magenta} ____ _ _ ____ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ${c.dim}Discord:${c.reset} ${c.blue}https://go.climactic.co/discord${c.reset} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| `); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Graceful shutdown | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const shutdown = async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await disconnectRedisCache(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| process.exit(0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| process.on("SIGTERM", shutdown); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| process.on("SIGINT", shutdown); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export type App = typeof app; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.