From 8d921e6f7e5352cb3036c69d11e455d71abe050b Mon Sep 17 00:00:00 2001 From: "lia-by-librechat[bot]" <328778573+lia-by-librechat[bot]@users.noreply.github.com> Date: Fri, 23 Oct 2026 15:57:05 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=B8=20test:=20Cover=20Overlapping=20We?= =?UTF-8?q?b=20Search=20Secret=20Patches=20(#17390)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Lia --- .../api/src/admin/secrets.integration.spec.ts | 117 +++++++++++++++++- 1 file changed, 116 insertions(+), 1 deletion(-) diff --git a/packages/api/src/admin/secrets.integration.spec.ts b/packages/api/src/admin/secrets.integration.spec.ts index 38bab92e5e..d4b5ef0c83 100644 --- a/packages/api/src/admin/secrets.integration.spec.ts +++ b/packages/api/src/admin/secrets.integration.spec.ts @@ -803,7 +803,7 @@ describe('config secret registry — real handlers against a real Config collect secretPath: 'webSearch.searxngApiKey', }; - type Entry = { fieldPath: string; value: unknown }; + type Entry = { value: unknown } & ({ fieldPath: string } | { segments: string[] }); async function patch(principalId: string, entries: Entry[]): Promise { const res = mockRes(); @@ -870,6 +870,121 @@ describe('config secret registry — real handlers against a real Config collect expect(await readRawConfig(principalId)).toEqual(before); }); + describe.each([ + { name: 'dotted paths', segmentedKey: false, segmentedSection: false }, + { name: 'segment paths', segmentedKey: true, segmentedSection: true }, + { name: 'segmented key and dotted section', segmentedKey: true, segmentedSection: false }, + { name: 'dotted key and segmented section', segmentedKey: false, segmentedSection: true }, + ])('overlapping writes with $name', ({ segmentedKey, segmentedSection }) => { + const REENTERED_KEY = 'sx-reentered-0002'; + const keyTarget = segmentedKey + ? { segments: ['webSearch', 'searxngApiKey'] } + : { fieldPath: 'webSearch.searxngApiKey' }; + const sectionTarget = segmentedSection + ? { segments: ['webSearch'] } + : { fieldPath: 'webSearch' }; + const replacement = (url: string): Entry => ({ + ...sectionTarget, + value: { + searxngInstanceUrl: url, + searchProvider: 'searxng', + scraperProvider: 'keenable', + rerankerType: 'none', + }, + }); + const key: Entry = { ...keyTarget, value: REENTERED_KEY }; + + it('rejects a key entry overwritten by a section that restores the stored key', async () => { + const principalId = nextPrincipalId(); + const before = await storeSearxng(principalId); + + const res = await patch(principalId, [key, replacement(NEW_URL)]); + + expect(response(res)).toEqual(reentryRequired([SEARXNG_REENTRY])); + expect(await readRawConfig(principalId)).toEqual(before); + }); + + it('accepts a key entered after the section and stores only that key', async () => { + const principalId = nextPrincipalId(); + const before = await storeSearxng(principalId); + + const res = await patch(principalId, [replacement(NEW_URL), key]); + + expect(res.statusCode).toBe(200); + const after = await readRawOverrides(principalId); + const ciphertext = getAtPath(after, 'webSearch.searxngApiKey'); + expect(getAtPath(after, 'webSearch.searxngInstanceUrl')).toBe(NEW_URL); + expect(ciphertext).not.toBe(getAtPath(before.overrides, 'webSearch.searxngApiKey')); + expect(decryptV3(ciphertext as string)).toBe(REENTERED_KEY); + expect(getAtPath(after, 'webSearch.searxngApiKeyPreview')).toBe( + getSecretPreview(REENTERED_KEY), + ); + expect(JSON.stringify(res.body)).not.toContain(REENTERED_KEY); + }); + + it('preserves the stored ciphertext and preview when the destination stays', async () => { + const principalId = nextPrincipalId(); + const before = await storeSearxng(principalId); + + const res = await patch(principalId, [key, replacement(STORED_URL)]); + + expect(res.statusCode).toBe(200); + const after = await readRawOverrides(principalId); + for (const field of ['searxngInstanceUrl', 'searxngApiKey', 'searxngApiKeyPreview']) { + expect(getAtPath(after, `webSearch.${field}`)).toBe( + getAtPath(before.overrides, `webSearch.${field}`), + ); + } + }); + + it('rechecks a restored key after a revision conflict', async () => { + const principalId = nextPrincipalId(); + const before = await storeSearxng(principalId); + const concurrentFields = encryptConfigSecretFields({ + 'webSearch.searxngInstanceUrl': 'https://searx.example.com/concurrent', + 'webSearch.searxngApiKey': 'sx-concurrent-0003', + }); + const write = jest.fn< + ReturnType, + Parameters + >(async (...args) => { + await patchConfigFields( + PrincipalType.ROLE, + principalId, + PrincipalModel.ROLE, + concurrentFields, + ); + return configMethods.writeConfigDocument(...args); + }); + const racing = createRealHandlers({ writeConfigDocument: write }); + const res = mockRes(); + + await racing.patchConfigField( + mockReq({ + params: { principalType: 'role', principalId }, + body: { entries: [key, replacement(STORED_URL)] }, + }), + res, + ); + + expect(write).toHaveBeenCalledTimes(1); + expect(response(res)).toEqual(reentryRequired([SEARXNG_REENTRY])); + const after = await readRawOverrides(principalId); + expect(getAtPath(after, 'webSearch.searxngInstanceUrl')).toBe( + concurrentFields['webSearch.searxngInstanceUrl'], + ); + expect(getAtPath(after, 'webSearch.searxngApiKey')).toBe( + concurrentFields['webSearch.searxngApiKey'], + ); + expect(decryptV3(getAtPath(after, 'webSearch.searxngApiKey') as string)).toBe( + 'sx-concurrent-0003', + ); + expect(getAtPath(after, 'webSearch.searxngApiKey')).not.toBe( + getAtPath(before.overrides, 'webSearch.searxngApiKey'), + ); + }); + }); + it('rejects a section patch that changes the URL and omits the key', async () => { const principalId = nextPrincipalId(); const before = await storeSearxng(principalId);