mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-07-31 20:32:20 +03:00
* feat(cursor): vision (image_url) input + tool-commit/output-constraint enhancements Add image/vision input to the Cursor provider's agent.v1 endpoint, plus the supporting prompt-engineering and resilience work developed alongside it. Vision input - Decode OpenAI `image_url` parts (base64 `data:` URIs and remote `http(s)` URLs) and inline them as `SelectedContext.selected_images[]` — field numbers pinned from the cursor-agent agent.v1 protobuf descriptor (SelectedImage.data oneof, uuid, optional Dimension, mime_type). Cross-checked against composer-api's shape. - New `resolveCursorImages` helper: SSRF-guarded remote fetches via the repo's canonical `parseAndValidatePublicUrl` (always public-only for client URLs), <=1 MiB per image (pre-decode + streaming cap), `image/*` enforced, max 12 images, sanitized `CursorImageError` (no stack/path leakage). - `openai-to-cursor` translator now preserves `image_url` parts instead of dropping them; executor `buildRequest` resolves images and attaches them to the user turn. The no-image path is byte-identical to before (test-asserted). Supporting cursor enhancements - Tool-commit directive (raises composer-2.5 tool-call rate ~53% -> ~88%), `tool_choice` none/required/specific handling, and output constraints (`response_format` / `max_tokens` / `stop` surfaced as prompt instructions). - `cursorSessionManager`: clear pending tool-call mappings on session close. - `cursorVersionDetector`: export `FALLBACK_VERSION` as a single source of truth. Tests & docs - New unit suite for the image encoder + resolver (field layout, byte-identical no-image path, SSRF / oversize / bad-base64 / too-many rejections, sanitized error body), translator image-preservation tests, and live e2e tests (base64 + remote URL, gated on `CURSOR_E2E_TOKEN`). - Documented `CURSOR_TOOL_DIRECTIVE` and `CURSOR_IMAGE_FETCH_TIMEOUT_MS` in `.env.example` and `docs/reference/ENVIRONMENT.md`. * fix(cursor): address review — redirect SSRF, large-payload guard, stream OOM, case/NaN nits Resolves the gemini-code-assist review on #3104: - SSRF via redirect (critical): fetchImageBytes now uses redirect:"manual" and re-validates every hop through parseAndValidatePublicUrl, so a public URL can't 30x-redirect to a private/link-local address. Bounded to 3 redirects. - Large data URL (high): reject on raw payload length before the whitespace-strip regex, so an oversized data URL can't burn CPU. - Stream read (high): readCapped consumes the body as an async iterable (Node Readable + Web Streams) or via getReader, capping mid-read; uncapped arrayBuffer() is only a last resort. - data: scheme (medium): match case-insensitively (RFC 2397) while preserving the original payload. - NaN timeouts (medium): CURSOR_IMAGE_FETCH_TIMEOUT_MS and CURSOR_STREAM_TIMEOUT_MS fall back to defaults when the env value isn't a positive integer. Adds tests: redirect-to-private blocked, redirect-to-public followed, too-many- redirects rejected, uppercase DATA: accepted. * fix(cursor): defend image fetch against DNS-rebinding SSRF Address the @codex review on #3104: parseAndValidatePublicUrl only checks the hostname string, so a public-looking host that (re)resolves to a private / link-local / metadata IP would still be fetched. Each hop now resolves the host via dns.lookup({all:true}) and rejects if ANY answer is private (isPrivateHost), before connecting. IP literals are skipped (already validated by the URL guard). This narrows but doesn't fully close the TOCTOU window vs fetch's own resolution; a connection-time IP filter on the shared outbound guard would close it for every caller. Adds unit tests for the IP gate and a mocked DNS-rebinding case (public host -> 127.0.0.1, fetch never reached).
75 lines
2.4 KiB
TypeScript
75 lines
2.4 KiB
TypeScript
/**
|
|
* Auto-detect the installed Cursor IDE version from its local SQLite database.
|
|
* Falls back to the hardcoded default when the DB is unavailable.
|
|
* The detected version is cached in-memory for 1 hour to avoid repeated DB reads.
|
|
*
|
|
* Override the DB path with the CURSOR_STATE_DB_PATH env var for non-standard installs.
|
|
*/
|
|
|
|
import { homedir } from "os";
|
|
import { join } from "path";
|
|
import { createRequire } from "module";
|
|
|
|
const CACHE_TTL_MS = 60 * 60 * 1000;
|
|
const DB_KEY = "cursorupdate.lastUpdatedAndShown.version";
|
|
/**
|
|
* Version reported when the Cursor IDE state DB is unavailable (the common
|
|
* case for a headless OmniRoute deployment). Kept in sync with
|
|
* `CURSOR_REGISTRY_VERSION` in providerHeaderProfiles.ts. Exported so tests
|
|
* assert against the single source of truth instead of a drifting literal.
|
|
*/
|
|
export const FALLBACK_VERSION = "3.3";
|
|
|
|
let cachedVersion: string | null = null;
|
|
let cachedAt = 0;
|
|
|
|
export function getCursorDbPath(): string {
|
|
if (process.env.CURSOR_STATE_DB_PATH) {
|
|
return process.env.CURSOR_STATE_DB_PATH;
|
|
}
|
|
const home = process.env.HOME || process.env.USERPROFILE || homedir();
|
|
const platform = process.platform;
|
|
if (platform === "darwin") {
|
|
return join(home, "Library/Application Support/Cursor/User/globalStorage/state.vscdb");
|
|
}
|
|
if (platform === "win32") {
|
|
return join(process.env.APPDATA || home, "Cursor/User/globalStorage/state.vscdb");
|
|
}
|
|
return join(home, ".config/Cursor/User/globalStorage/state.vscdb");
|
|
}
|
|
|
|
export function getCursorVersion(): string {
|
|
const now = Date.now();
|
|
if (cachedVersion && now - cachedAt < CACHE_TTL_MS) {
|
|
return cachedVersion;
|
|
}
|
|
|
|
try {
|
|
const esmRequire = createRequire(import.meta.url);
|
|
const Database = esmRequire("better-sqlite3");
|
|
const db = new Database(getCursorDbPath(), { readonly: true, fileMustExist: true });
|
|
try {
|
|
const row = db.prepare("SELECT value FROM itemTable WHERE key = ?").get(DB_KEY) as
|
|
| { value: string }
|
|
| undefined;
|
|
if (row?.value) {
|
|
cachedVersion = row.value;
|
|
cachedAt = now;
|
|
return cachedVersion;
|
|
}
|
|
} finally {
|
|
db.close();
|
|
}
|
|
} catch {
|
|
// DB missing or unreadable — fall through to default
|
|
}
|
|
|
|
return FALLBACK_VERSION;
|
|
}
|
|
|
|
/** Exposed for testing: reset the in-memory cache so the next call re-reads the DB. */
|
|
export function resetCursorVersionCache(): void {
|
|
cachedVersion = null;
|
|
cachedAt = 0;
|
|
}
|