Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
8628be1
Investigate Convex Upstash warnings
leoisadev1 Feb 11, 2026
a626c43
Investigate Convex Upstash warnings
leoisadev1 Feb 11, 2026
88366b2
Merge remote-tracking branch 'origin/add-upstash-rate-limits-and' int…
leoisadev1 Feb 11, 2026
9162dc0
Apply Cubic AI review suggestions
leoisadev1 Feb 11, 2026
9393e14
Address Cubic follow-up findings
leoisadev1 Feb 11, 2026
7cb6edd
fix(security): harden workflow endpoints and rate-limit checks
leoisadev1 Feb 11, 2026
3b59d07
fix(security): address remaining review findings
leoisadev1 Feb 11, 2026
1ba1454
fix(security): address all remaining Cubic + Tembo review findings
leoisadev1 Feb 11, 2026
6b80816
fix(security): use HMAC+timingSafeEqual for cleanup token comparison
leoisadev1 Feb 11, 2026
ee54eec
fix(security): extract cleanup action to node runtime, fix auth bypas…
leoisadev1 Feb 11, 2026
8302416
fix(security): stop forwarding cookies to QStash, add rate limiting a…
leoisadev1 Feb 11, 2026
4f97319
fix(security): fix parser passthrough, remove hardcoded HMAC keys, ha…
leoisadev1 Feb 11, 2026
04de610
fix(security): add NaN guard to bounds validation, log fetch errors
leoisadev1 Feb 11, 2026
76c4e04
fix(security): remove workflow token persistence and harden cleanup/m…
leoisadev1 Feb 11, 2026
7c103dc
fix(security): consume workflow auth refs once and harden error/rate …
leoisadev1 Feb 11, 2026
8e696e5
fix(security): harden fail-closed limits and workflow retries
leoisadev1 Feb 11, 2026
537b548
Fix: Broken access control in chat stream init (#621)
tembo[bot] Feb 12, 2026
8034851
fix(security): close auth fallback and token replay gaps
leoisadev1 Feb 12, 2026
0f4e92f
fix(security): reduce workflow secret exposure and endpoint hazards
leoisadev1 Feb 12, 2026
ec3519e
fix(security): stabilize workflow auth and endpoint safeguards
leoisadev1 Feb 12, 2026
664efc2
fix(security): enforce signed workflow callbacks and safer parsing
leoisadev1 Feb 12, 2026
e535f6b
fix(security): harden stream limits and workflow error surfaces
leoisadev1 Feb 12, 2026
770d822
fix(web): restore generic proxy IP extraction path
leoisadev1 Feb 12, 2026
795001a
fix(security): fail closed when usage reserve is unavailable
leoisadev1 Feb 12, 2026
ebd3049
fix(security): reject empty workflow auth tokens
leoisadev1 Feb 12, 2026
059948e
fix(security): harden workflow mode selection and model headers
leoisadev1 Feb 12, 2026
d200d7c
fix(security): restrict cookie fallback and keep key off qstash
leoisadev1 Feb 12, 2026
48ca3c4
fix(security): tighten cleanup auth and usage safeguards
leoisadev1 Feb 12, 2026
7b5d722
fix(security): tighten workflow and error handling responses
leoisadev1 Feb 12, 2026
020fefa
fix(security): tighten workflow token and usage edge cases
leoisadev1 Feb 12, 2026
826a74b
fix(web): execute chat exports inline for reliable delivery
leoisadev1 Feb 12, 2026
76f7360
fix(web): tighten workflow validation and retry behavior
leoisadev1 Feb 12, 2026
37c88fb
fix(web): harden export workflow payload guards
leoisadev1 Feb 12, 2026
4575997
fix(security): harden workflow callback and auth safeguards
leoisadev1 Feb 12, 2026
ec1e22a
fix(web): clean up workflow validation and rate-limit UX
leoisadev1 Feb 12, 2026
c048027
fix(web): enforce models fallback limit and workflow callback host
leoisadev1 Feb 12, 2026
3500ca5
fix(web): harden model IP gating and workflow title sanitization
leoisadev1 Feb 12, 2026
bb0e999
fix(security): tighten workflow auth fallback and usage counters
leoisadev1 Feb 12, 2026
0a44f88
fix: resolve merge conflict — keep chat.ts deleted (moved to workflow)
leoisadev1 Feb 12, 2026
3871cbc
fix: resolve all Cubic review findings
leoisadev1 Feb 12, 2026
181af3f
Merge pull request #622 from opencoredev/add-upstash-rate-limits-and
leoisadev1 Feb 12, 2026
6722ceb
fix(server): retain deprecated jonMode/dynamicPrompt in schema for ex…
leoisadev1 Feb 12, 2026
abe7aa9
fix(api): validate IP addresses and prefer platform-specific proxy he…
tembo[bot] Feb 13, 2026
782c70c
Merge main
leoisadev1 Feb 14, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions apps/server/convex/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ export const getByExternalId = query({
profile?.encryptedOpenRouterKey ?? user.encryptedOpenRouterKey,
=======
<<<<<<< HEAD
||||||| 54e09ce
encryptedOpenRouterKey:
profile?.encryptedOpenRouterKey ?? user.encryptedOpenRouterKey,
=======
<<<<<<< HEAD
||||||| 54e09ce
encryptedOpenRouterKey:
profile?.encryptedOpenRouterKey ?? user.encryptedOpenRouterKey,
Expand All @@ -262,6 +267,7 @@ export const getByExternalId = query({
>>>>>>> main
>>>>>>> main
>>>>>>> main
>>>>>>> main
>>>>>>> main
fileUploadCount: profile?.fileUploadCount ?? user.fileUploadCount ?? 0,
aiUsageCents: user.aiUsageCents,
Expand Down Expand Up @@ -356,6 +362,11 @@ export const getByExternalIdInternal = internalQuery({
profile?.encryptedOpenRouterKey ?? user.encryptedOpenRouterKey,
=======
<<<<<<< HEAD
||||||| 54e09ce
encryptedOpenRouterKey:
profile?.encryptedOpenRouterKey ?? user.encryptedOpenRouterKey,
=======
<<<<<<< HEAD
||||||| 54e09ce
encryptedOpenRouterKey:
profile?.encryptedOpenRouterKey ?? user.encryptedOpenRouterKey,
Expand All @@ -364,6 +375,7 @@ export const getByExternalIdInternal = internalQuery({
>>>>>>> main
>>>>>>> main
>>>>>>> main
>>>>>>> main
>>>>>>> main
fileUploadCount: profile?.fileUploadCount ?? user.fileUploadCount ?? 0,
aiUsageCents: user.aiUsageCents,
Expand Down
121 changes: 121 additions & 0 deletions apps/web/src/routes/api/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,126 @@ const OPENROUTER_MODELS_URL = "https://openrouter.ai/api/v1/models";
const OPENROUTER_FETCH_TIMEOUT_MS = 10_000;
const TRUST_PROXY_MODE = process.env.TRUST_PROXY?.trim().toLowerCase();

<<<<<<< HEAD
// Basic IPv4 and IPv6 validation to reject obviously spoofed or malformed values.
const IPV4_REGEX = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/;
const IPV6_REGEX = /^[\da-fA-F:]+$/;

@cubic-dev-ai cubic-dev-ai Bot Feb 14, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: IPv4-mapped IPv6 addresses (e.g., ::ffff:192.168.1.1) are rejected because the regex doesn't include . in the character class. These are common in practice — Node.js and many proxies report IPv4 addresses in this format. Affected users would get 400 errors.

Include . in the character class to support IPv4-mapped/embedded addresses.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/web/src/routes/api/models.ts, line 14:

<comment>IPv4-mapped IPv6 addresses (e.g., `::ffff:192.168.1.1`) are rejected because the regex doesn't include `.` in the character class. These are common in practice — Node.js and many proxies report IPv4 addresses in this format. Affected users would get 400 errors.

Include `.` in the character class to support IPv4-mapped/embedded addresses.</comment>

<file context>
@@ -0,0 +1,194 @@
+
+// Basic IPv4 and IPv6 validation to reject obviously spoofed or malformed values.
+const IPV4_REGEX = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/;
+const IPV6_REGEX = /^[\da-fA-F:]+$/;
+
+function isValidIp(value: string): boolean {
</file context>
Fix with Cubic


function isValidIp(value: string): boolean {
if (IPV4_REGEX.test(value)) return true;
// Rough IPv6 check: only hex digits and colons, reasonable length
if (IPV6_REGEX.test(value) && value.includes(":") && value.length <= 45) return true;
return false;
}

if (TRUST_PROXY_MODE === "true") {
console.warn(
"[Models API] TRUST_PROXY=true blindly trusts X-Forwarded-For and is vulnerable to " +
"IP spoofing if not behind a trusted proxy. Prefer TRUST_PROXY=cloudflare or " +
"TRUST_PROXY=vercel for platform-specific secure headers.",
);
}

if (!TRUST_PROXY_MODE) {
console.warn("[Models API] TRUST_PROXY is unset; models endpoint will reject requests when IP is unavailable");
}

if (
TRUST_PROXY_MODE &&
TRUST_PROXY_MODE !== "cloudflare" &&
TRUST_PROXY_MODE !== "vercel" &&
TRUST_PROXY_MODE !== "true"
) {
console.warn("[Models API] Unrecognized TRUST_PROXY value; models endpoint will reject requests when IP is unavailable");
}

const modelsIpRatelimit = upstashRedis
? new Ratelimit({
redis: upstashRedis,
limiter: Ratelimit.slidingWindow(30, "60 s"),
prefix: "ratelimit:models:ip",
})
: null;

async function fetchModelsFromOpenRouter(): Promise<Response> {
try {
const response = await fetch(OPENROUTER_MODELS_URL, {
headers: {
Accept: "application/json",
},
signal: AbortSignal.timeout(OPENROUTER_FETCH_TIMEOUT_MS),
});

if (!response.ok) {
return json(
{ error: "Upstream service error" },
{ status: 502 },
);
}

const payload = await response.text();

if (upstashRedis) {
try {
await upstashRedis.set(MODELS_CACHE_KEY, payload, {
ex: MODELS_CACHE_TTL_SECONDS,
});
} catch (error) {
console.warn("[Models API] Failed to write cache:", error);
}
}

return new Response(payload, {
status: 200,
headers: {
"Content-Type": "application/json",
"Cache-Control": "no-store",
},
});
} catch (error) {
console.warn("[Models API] OpenRouter fetch failed:", error);
return json({ error: "Upstream service unavailable" }, { status: 502 });
}
}

function getClientIp(request: Request): string | null {
if (!TRUST_PROXY_MODE) {
return null;
}

if (TRUST_PROXY_MODE === "cloudflare") {
const cfConnectingIp = request.headers.get("cf-connecting-ip")?.trim();
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
if (cfConnectingIp && isValidIp(cfConnectingIp)) return cfConnectingIp;
return null;
}

if (TRUST_PROXY_MODE === "vercel") {
const vercelForwardedFor = request.headers.get("x-vercel-forwarded-for")?.trim();
if (vercelForwardedFor) {
const first = vercelForwardedFor.split(",")[0]?.trim();
if (first && isValidIp(first)) return first;
}
return null;
}

if (TRUST_PROXY_MODE === "true") {
// Prefer platform-specific headers that are harder to spoof, then fall
// back to the generic X-Forwarded-For only if none are present.
const cfConnectingIp = request.headers.get("cf-connecting-ip")?.trim();
if (cfConnectingIp && isValidIp(cfConnectingIp)) return cfConnectingIp;

const vercelForwardedFor = request.headers.get("x-vercel-forwarded-for")?.trim();
if (vercelForwardedFor) {
const first = vercelForwardedFor.split(",")[0]?.trim();
if (first && isValidIp(first)) return first;
}

const forwardedFor = request.headers.get("x-forwarded-for")?.trim();
if (forwardedFor) {
const first = forwardedFor.split(",")[0]?.trim();
if (first && isValidIp(first)) return first;
||||||| 54e09ce
=======
<<<<<<< HEAD
if (TRUST_PROXY_MODE === "true") {
console.warn("[Models API] TRUST_PROXY=true requires x-forwarded-for for rate limiting");
Expand Down Expand Up @@ -403,6 +523,7 @@ function getClientIp(request: Request): string | null {
if (forwardedFor) {
const first = forwardedFor.split(",")[0]?.trim();
if (first) return first;
>>>>>>> main
}
return null;
}
Expand Down
Loading