-
-
Notifications
You must be signed in to change notification settings - Fork 24.2k
Fix: prevent crash when parsing invalid JSON in analytics handler #5906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
5e8ba84
ff2f287
1896071
f63949b
c399973
75403ed
ebdfeda
b974b42
3a1458f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,7 @@ import { ChainValues } from '@langchain/core/utils/types' | |
| import { AgentAction } from '@langchain/core/agents' | ||
| import { LunaryHandler } from '@langchain/community/callbacks/handlers/lunary' | ||
|
|
||
| import { getCredentialData, getCredentialParam, getEnvironmentVariable } from './utils' | ||
| import { getCredentialData, getCredentialParam, getEnvironmentVariable, parseJsonBody } from './utils' | ||
| import { EvaluationRunTracer } from '../evaluation/EvaluationRunTracer' | ||
| import { EvaluationRunTracerLlama } from '../evaluation/EvaluationRunTracerLlama' | ||
| import { ICommonObject, IDatabaseEntity, INodeData, IServerSideEventStreamer } from './Interface' | ||
|
|
@@ -517,7 +517,14 @@ export const additionalCallbacks = async (nodeData: INodeData, options: ICommonO | |
| try { | ||
| if (!options.analytic) return [] | ||
|
|
||
| const analytic = JSON.parse(options.analytic) | ||
| let analytic: unknown | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how and why would this fail originally? It should be stored as json string straight into database
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question! In practice, this can fail if the value is empty, malformed, or already an object instead of a JSON string (which can happen with legacy data or inconsistent inputs). The defensive parsing was introduced to prevent runtime crashes in those cases and keep the system stable. That said, I agree that ideally this should always be stored as a valid JSON string at the database level, and enforcing that upstream would be the best long-term approach. |
||
| try { | ||
| analytic = parseJsonBody(options.analytic ?? '') | ||
| } catch (e) { | ||
| return [] | ||
| } | ||
|
|
||
| if (!analytic || typeof analytic !== 'object' || Array.isArray(analytic)) return [] | ||
| const callbacks: any = [] | ||
|
|
||
| for (const provider in analytic) { | ||
|
|
@@ -776,7 +783,14 @@ export class AnalyticHandler { | |
| try { | ||
| if (!this.options.analytic) return | ||
|
|
||
| const analytic = JSON.parse(this.options.analytic) | ||
| let analytic: unknown | ||
| try { | ||
| analytic = parseJsonBody(this.options.analytic ?? '') | ||
| } catch (e) { | ||
| return | ||
| } | ||
|
|
||
| if (!analytic || typeof analytic !== 'object' || Array.isArray(analytic)) return | ||
| for (const provider in analytic) { | ||
| const providerStatus = analytic[provider].status as boolean | ||
| if (providerStatus) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation
parsedCredential ?? googleApplicationCredentialcan lead to runtime errors. IfsafeJsonParsefails and returnsnull, the original (and likely malformed)googleApplicationCredentialstring is assigned toauthOptions.credentials. Thecredentialsproperty expects an object, not a string. This would replace an immediate crash with a deferred one. To fix this, we should only assign the parsed value if it's a valid object, similar to the validation added inhandler.ts.