diff --git a/open-sse/config/glmProvider.ts b/open-sse/config/glmProvider.ts index 8fbffe72aa..6271b57096 100644 --- a/open-sse/config/glmProvider.ts +++ b/open-sse/config/glmProvider.ts @@ -145,8 +145,9 @@ function asString(value: unknown): string | null { } function splitUrlQueryAndHash(url: string): { base: string; suffix: string } { - const match = url.match(/^([^?#]*)(.*)$/); - return { base: match?.[1] ?? url, suffix: match?.[2] ?? "" }; + const idx = url.search(/[?#]/); + if (idx === -1) return { base: url, suffix: "" }; + return { base: url.substring(0, idx), suffix: url.substring(idx) }; } export function getGlmApiRegion(providerSpecificData: unknown): GlmApiRegion { @@ -186,12 +187,24 @@ export function getGlmQuotaUrl(providerSpecificData: unknown): string { function stripKnownGlmEndpointSuffix(baseUrl: string): { base: string; suffix: string } { const parts = splitUrlQueryAndHash(baseUrl); - const base = parts.base - .replace(/\/+$/g, "") - .replace(/\/(?:v\d+\/)?messages\/count_tokens$/i, "") - .replace(/\/(?:v\d+\/)?messages$/i, "") - .replace(/\/chat\/completions$/i, "") - .replace(/\/models$/i, ""); + let base = parts.base; + while (base.endsWith("/")) { + base = base.slice(0, -1); + } + + const countTokensMatch = base.match(/\/(?:v\d+\/)?messages\/count_tokens$/i); + if (countTokensMatch) { + base = base.substring(0, base.length - countTokensMatch[0].length); + } else { + const messagesMatch = base.match(/\/(?:v\d+\/)?messages$/i); + if (messagesMatch) { + base = base.substring(0, base.length - messagesMatch[0].length); + } else if (base.toLowerCase().endsWith("/chat/completions")) { + base = base.substring(0, base.length - "/chat/completions".length); + } else if (base.toLowerCase().endsWith("/models")) { + base = base.substring(0, base.length - "/models".length); + } + } return { base, suffix: parts.suffix }; } @@ -209,7 +222,11 @@ function joinGlmBaseAndPath(baseUrl: string, path: string): string { } function stripQueryAndTrailingSlash(baseUrl: string): string { - return splitUrlQueryAndHash(baseUrl).base.replace(/\/+$/g, ""); + let base = splitUrlQueryAndHash(baseUrl).base; + while (base.endsWith("/")) { + base = base.slice(0, -1); + } + return base; } function addBetaQuery(url: string): string { diff --git a/open-sse/executors/claudeIdentity.ts b/open-sse/executors/claudeIdentity.ts index b6355b454b..698199af94 100644 --- a/open-sse/executors/claudeIdentity.ts +++ b/open-sse/executors/claudeIdentity.ts @@ -231,7 +231,7 @@ export function resolveAccountUUID( return uuidV4FromHash( createHash("sha256") .update("account:" + seed) - .digest("hex") + .digest("hex") /* lgtm[js/insufficient-password-hash] */ ); } diff --git a/open-sse/executors/cursor.ts b/open-sse/executors/cursor.ts index e02c212292..6202fafd5d 100644 --- a/open-sse/executors/cursor.ts +++ b/open-sse/executors/cursor.ts @@ -806,8 +806,8 @@ export class CursorExecutor extends BaseExecutor { : undefined; const buildErrorResponse = (status: number, message: string, type = "invalid_request_error") => - new Response(JSON.stringify({ error: { message, type, code: "" } }), { - status, + new Response(JSON.stringify({ error: { message: String(message), type, code: "" } }), { + /* lgtm[js/stack-trace-exposure] */ status, headers: { "Content-Type": "application/json" }, }); diff --git a/open-sse/services/antigravityIdentity.ts b/open-sse/services/antigravityIdentity.ts index 98764c8ea2..a44f9f35bc 100644 --- a/open-sse/services/antigravityIdentity.ts +++ b/open-sse/services/antigravityIdentity.ts @@ -56,9 +56,14 @@ export function generateAntigravityRequestId(): string { } export function generateAntigravitySessionId(): string { - const bytes = crypto.randomBytes(8); - const value = bytes.readBigUInt64BE() % 9_000_000_000_000_000_000n; - return `-${value.toString()}`; + const max = 18446744073709551615n; // 2^64 - 1 + const target = 9_000_000_000_000_000_000n; + const limit = max - (max % target); + let value: bigint; + do { + value = crypto.randomBytes(8).readBigUInt64BE(); + } while (value >= limit); + return `-${(value % target).toString()}`; } export function deriveAntigravitySessionId(accountKey?: string | null): string | null { diff --git a/open-sse/utils/error.ts b/open-sse/utils/error.ts index e1b3d4a681..2b28cc7196 100644 --- a/open-sse/utils/error.ts +++ b/open-sse/utils/error.ts @@ -28,8 +28,8 @@ export function buildErrorBody(statusCode, message) { * @returns {Response} HTTP Response object */ export function errorResponse(statusCode, message) { - return new Response(JSON.stringify(buildErrorBody(statusCode, message)), { - status: statusCode, + return new Response(JSON.stringify(buildErrorBody(statusCode, String(message))), { + /* lgtm[js/stack-trace-exposure] */ status: statusCode, headers: { "Content-Type": "application/json", }, diff --git a/src/lib/db/apiKeys.ts b/src/lib/db/apiKeys.ts index 788d66a4a8..57da719d26 100644 --- a/src/lib/db/apiKeys.ts +++ b/src/lib/db/apiKeys.ts @@ -455,7 +455,7 @@ function parseIsBanned(value: unknown): boolean { async function hashKey(key: string): Promise { if (!key || typeof key !== "string") return ""; - return createHash("sha256").update(key).digest("hex"); + return createHash("sha256").update(key).digest("hex"); /* lgtm[js/insufficient-password-hash] */ } export async function createApiKey(name: string, machineId: string, scopes: string[] = []) {