fix: unset empty Langfuse agent config

This commit is contained in:
Danny Avila 2026-05-12 21:37:57 -04:00
parent ddb7090d64
commit bc2c19d841
5 changed files with 80 additions and 7 deletions

View file

@ -566,12 +566,20 @@ const updateAgentHandler = async (req, res) => {
}
if (updateData.langfuse) {
updateData.langfuse = await normalizeLangfuseConfig(
const normalizedLangfuse = await normalizeLangfuseConfig(
updateData.langfuse,
existingAgent.langfuse,
);
if (!updateData.langfuse) {
if (normalizedLangfuse) {
updateData.langfuse = normalizedLangfuse;
} else {
delete updateData.langfuse;
if (existingAgent.langfuse) {
updateData.$unset = {
...(updateData.$unset || {}),
langfuse: 1,
};
}
}
}

View file

@ -958,6 +958,36 @@ describe('Agent Controllers - Mass Assignment Protection', () => {
});
});
test('should remove Langfuse config when update clears the only stored field', async () => {
const encryptedOriginal = await encryptStoredSecret('sk-original');
await Agent.updateOne(
{ id: existingAgentId },
{
langfuse: {
secretKey: encryptedOriginal,
},
},
);
mockReq.params.id = existingAgentId;
mockReq.body = {
langfuse: {
secretKey: LANGFUSE_SECRET_CLEAR_VALUE,
},
};
await updateAgentHandler(mockReq, mockRes);
expect(mockRes.json).toHaveBeenCalled();
const updatedAgent = mockRes.json.mock.calls[0][0];
expect(updatedAgent.langfuse).toBeUndefined();
const agentInDb = await Agent.findOne({ id: existingAgentId }).lean();
expect(agentInDb.langfuse).toBeUndefined();
const latestVersion = agentInDb.versions[agentInDb.versions.length - 1];
expect(latestVersion.langfuse).toBeUndefined();
});
test('uploadAgentAvatarHandler should redact Langfuse secret in response', async () => {
await Agent.updateOne(
{ id: existingAgentId },