From 7b645f4eaf9a2fdefc386768b212e134c1301161 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Sun, 20 Sep 2026 13:18:02 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=B7=20fix:=20Isolate=20Jina=20Credenti?= =?UTF-8?q?al=20Provenance=20(#16134)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/api/src/web/web.spec.ts | 113 ++++++++++++++++++++++++++++++- packages/api/src/web/web.ts | 15 ++++ 2 files changed, 126 insertions(+), 2 deletions(-) diff --git a/packages/api/src/web/web.spec.ts b/packages/api/src/web/web.spec.ts index be0748b779..6dc6af2104 100644 --- a/packages/api/src/web/web.spec.ts +++ b/packages/api/src/web/web.spec.ts @@ -671,6 +671,115 @@ describe('web.ts', () => { } }); + it('should not send a system Jina key to a user-provided API URL', async () => { + const originalApiKey = process.env.JINA_API_KEY; + const originalApiUrl = process.env.JINA_API_URL; + process.env.JINA_API_KEY = 'system-jina-key'; + process.env.JINA_API_URL = AuthType.USER_PROVIDED; + mockIsSSRFTarget.mockReturnValue(false); + mockResolveHostnameSSRF.mockResolvedValue(false); + mockLoadAuthValues.mockImplementation(({ authFields }) => { + const result: Record = {}; + authFields.forEach((field: string) => { + if (field === 'JINA_API_KEY') { + result[field] = 'system-jina-key'; + } else if (field === 'JINA_API_URL') { + result[field] = 'https://user-jina.example.com/v1/rerank'; + } else { + result[field] = + field === 'FIRECRAWL_API_URL' ? 'https://api.firecrawl.dev' : 'test-api-key'; + } + }); + return Promise.resolve(result); + }); + + try { + const result = await loadWebSearchAuth({ + userId, + webSearchConfig: { + ...webSearchConfig, + rerankerType: RerankerTypes.JINA, + }, + loadAuthValues: mockLoadAuthValues, + }); + + expect(result.authResult.jinaApiKey).toBe('system-jina-key'); + expect(result.authResult.jinaApiUrl).toBe('https://api.jina.ai/v1/rerank'); + expect(result.authTypes).toContainEqual([ + SearchCategories.RERANKERS, + AuthType.SYSTEM_DEFINED, + ]); + expect(mockResolveHostnameSSRF).toHaveBeenCalledWith( + 'user-jina.example.com', + undefined, + '443', + ); + } finally { + if (originalApiKey == null) { + delete process.env.JINA_API_KEY; + } else { + process.env.JINA_API_KEY = originalApiKey; + } + if (originalApiUrl == null) { + delete process.env.JINA_API_URL; + } else { + process.env.JINA_API_URL = originalApiUrl; + } + } + }); + + it("should keep a user-provided Jina URL with the same user's key", async () => { + const originalApiKey = process.env.JINA_API_KEY; + const originalApiUrl = process.env.JINA_API_URL; + delete process.env.JINA_API_KEY; + delete process.env.JINA_API_URL; + mockIsSSRFTarget.mockReturnValue(false); + mockResolveHostnameSSRF.mockResolvedValue(false); + mockLoadAuthValues.mockImplementation(({ authFields }) => { + const result: Record = {}; + authFields.forEach((field: string) => { + if (field === 'JINA_API_KEY') { + result[field] = 'user-jina-key'; + } else if (field === 'JINA_API_URL') { + result[field] = 'https://user-jina.example.com/v1/rerank'; + } else { + result[field] = + field === 'FIRECRAWL_API_URL' ? 'https://api.firecrawl.dev' : 'test-api-key'; + } + }); + return Promise.resolve(result); + }); + + try { + const result = await loadWebSearchAuth({ + userId, + webSearchConfig: { + ...webSearchConfig, + rerankerType: RerankerTypes.JINA, + }, + loadAuthValues: mockLoadAuthValues, + }); + + expect(result.authResult.jinaApiKey).toBe('user-jina-key'); + expect(result.authResult.jinaApiUrl).toBe('https://user-jina.example.com/v1/rerank'); + expect(result.authTypes).toContainEqual([ + SearchCategories.RERANKERS, + AuthType.USER_PROVIDED, + ]); + } finally { + if (originalApiKey == null) { + delete process.env.JINA_API_KEY; + } else { + process.env.JINA_API_KEY = originalApiKey; + } + if (originalApiUrl == null) { + delete process.env.JINA_API_URL; + } else { + process.env.JINA_API_URL = originalApiUrl; + } + } + }); + it('should treat a whitespace-only Keenable URL env value as user-provided', async () => { const originalApiKey = process.env.KEENABLE_API_KEY; const originalApiUrl = process.env.KEENABLE_API_URL; @@ -2486,7 +2595,7 @@ describe('web.ts', () => { loadAuthValues: mockLoadAuthValues, }); - expect(result.authResult.jinaApiUrl).toBeUndefined(); + expect(result.authResult.jinaApiUrl).toBe('https://api.jina.ai/v1/rerank'); expect(mockIsSSRFTarget).toHaveBeenCalledWith('localhost', undefined, '8080'); }); @@ -2961,7 +3070,7 @@ describe('web.ts', () => { loadAuthValues: mockLoadAuthValues, }); - expect(result.authResult.jinaApiUrl).toBeUndefined(); + expect(result.authResult.jinaApiUrl).toBe('https://api.jina.ai/v1/rerank'); expect(result.authenticated).toBe(true); const rerankersAuth = result.authTypes.find(([c]) => c === 'rerankers')?.[1]; expect(rerankersAuth).toBe(AuthType.SYSTEM_DEFINED); diff --git a/packages/api/src/web/web.ts b/packages/api/src/web/web.ts index b4ba2949ad..bb56a7c2b8 100644 --- a/packages/api/src/web/web.ts +++ b/packages/api/src/web/web.ts @@ -32,6 +32,7 @@ const USER_PROVIDED_OPT_IN_URL_KEYS = new Set([ const SEARCH_PROVIDER_VALUES = new Set(Object.values(SearchProviders)); const SCRAPER_PROVIDER_VALUES = new Set(Object.values(ScraperProviders)); const RERANKER_VALUES = new Set(Object.values(RerankerTypes)); +const DEFAULT_JINA_API_URL = 'https://api.jina.ai/v1/rerank'; function isUserProvidedEnabled(field: string): boolean { return process.env[field] === AuthType.USER_PROVIDED; @@ -480,6 +481,12 @@ export async function loadWebSearchAuth({ throwError, }); + const jinaApiKeyIndex = allKeys.indexOf('jinaApiKey'); + const jinaApiKeyField = allAuthFields[jinaApiKeyIndex]; + const jinaApiKeyValue = jinaApiKeyField ? authValues[jinaApiKeyField] : undefined; + const isJinaApiKeyUserProvided = + jinaApiKeyValue != null && process.env[jinaApiKeyField] !== jinaApiKeyValue; + let allFieldsAuthenticated = true; for (let j = 0; j < allAuthFields.length; j++) { const field = allAuthFields[j]; @@ -519,6 +526,10 @@ export async function loadWebSearchAuth({ } continue; } + + if (originalKey === 'jinaApiUrl' && isFieldUserProvided && !isJinaApiKeyUserProvided) { + continue; + } if (originalKey) { authResult[originalKey] = value; contributed = true; @@ -529,6 +540,10 @@ export async function loadWebSearchAuth({ } } + if (jinaApiKeyValue && authResult.jinaApiUrl == null) { + authResult.jinaApiUrl = DEFAULT_JINA_API_URL; + } + if (!allFieldsAuthenticated) { continue; }