🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix: prevent prototype-chain clobbering in OTel attribute mapping (#16616)
Valery Meleshkin1c5df63be9c5
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.tsindex 5ab3cb1d47..f5e1e6ea0c 100644--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts@@ -1568,11 +1568,16 @@ export class OtelIngestionProcessor {for (let i = 0; i < path.length - 1; i++) {const key = path[i];if (DANGEROUS_KEYS.has(key)) return;- if (!(key in current)) {+ if (!Object.hasOwn(current, key)) {// Check if next key is a number to decide if we need an array or object- current[key] = /^\d+$/.test(path[i + 1]) ? [] : {};+ current[key] = /^\d+$/.test(path[i + 1]) ? [] : Object.create(null);}- current = current[key];+ const next = current[key];+ if (typeof next !== "object" || next === null) {+ // Preserve an earlier leaf value when a later attribute conflicts.+ return;+ }+ current = next;}const finalKey = path[path.length - 1];if (!DANGEROUS_KEYS.has(finalKey)) {diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.tsindex ce77e36b2d..2e46d6bdc8 100644--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts@@ -9611,6 +9611,159 @@ describe("OTel Resource Span Mapping", () => {describe("Prototype pollution protection", () => {const publicKey = "pk-lf-1234567890";+ const traceId = "abcdef1234567890abcdef1234567890";+ const spanId = "abcdef1234567890";++ const createResourceSpan = (+ attributes: Array<{ key: string; value: Record<string, unknown> }>,+ ): ResourceSpan => ({+ resource: {+ attributes: [+ {+ key: "service.name",+ value: { stringValue: "test-service" },+ },+ ],+ },+ scopeSpans: [+ {+ scope: {+ name: "langfuse-sdk",+ version: "2.0.0",+ attributes: [+ {+ key: "public_key",+ value: { stringValue: publicKey },+ },+ ],+ },+ spans: [+ {+ traceId: Buffer.from(traceId, "hex"),+ spanId: Buffer.from(spanId, "hex"),+ name: "prototype-protection-test",+ kind: 1,+ startTimeUnixNano: {+ low: 1000000000,+ high: 0,+ },+ endTimeUnixNano: {+ low: 2000000000,+ high: 0,+ },+ attributes,+ status: {},+ },+ ],+ },+ ],+ });++ const convertAndObserveHasOwnPropertyCall = async (+ attributes: Array<{ key: string; value: Record<string, unknown> }>,+ ) => {+ const hasOwnPropertyFunction = Object.prototype.hasOwnProperty;+ const originalCall = hasOwnPropertyFunction.call;+ const originalCallDescriptor = Object.getOwnPropertyDescriptor(+ hasOwnPropertyFunction,+ "call",+ );++ try {+ const events = await convertOtelSpanToIngestionEvent(+ createResourceSpan(attributes),+ new Set([traceId]),+ publicKey,+ );+ return {+ events,+ observedCall: hasOwnPropertyFunction.call,+ originalCall,+ };+ } finally {+ if (originalCallDescriptor) {+ Object.defineProperty(+ hasOwnPropertyFunction,+ "call",+ originalCallDescriptor,+ );+ } else {+ delete (hasOwnPropertyFunction as { call?: unknown }).call;+ }+ }+ };++ it("should not traverse inherited properties in nested gen_ai.prompt attributes", async () => {+ const { events, observedCall, originalCall } =+ await convertAndObserveHasOwnPropertyCall([+ {+ key: "gen_ai.prompt.a.safe",+ value: { stringValue: "still-here" },+ },+ {+ key: "gen_ai.prompt.a.hasOwnProperty.call",+ value: { stringValue: "pwned" },+ },+ ]);++ expect(observedCall).toBe(originalCall);+ expect(+ events.find((event) => event.type === "span-create")?.body.input,+ ).toMatchObject({+ a: {+ safe: "still-here",+ hasOwnProperty: { call: "pwned" },+ },+ });+ });++ it("should not traverse inherited properties through an array branch", async () => {+ const { events, observedCall, originalCall } =+ await convertAndObserveHasOwnPropertyCall([+ {+ key: "gen_ai.prompt.a.0",+ value: { stringValue: "seed" },+ },+ {+ key: "gen_ai.prompt.a.hasOwnProperty.call",+ value: { stringValue: "pwned" },+ },+ ]);++ expect(observedCall).toBe(originalCall);+ const input = events.find((event) => event.type === "span-create")?.body+ .input as { a?: unknown[] } | undefined;+ expect(input?.a?.[0]).toBe("seed");+ expect(Object.hasOwn(input?.a ?? [], "hasOwnProperty")).toBe(true);+ expect(Reflect.get(input?.a ?? [], "hasOwnProperty")).toMatchObject({+ call: "pwned",+ });+ });++ it("should preserve a scalar leaf and ignore a conflicting nested attribute", () => {+ const events = createTestOtelProcessor({ publicKey }).processToEvent([+ createResourceSpan([+ {+ key: "gen_ai.prompt.a",+ value: { stringValue: "leaf" },+ },+ {+ key: "gen_ai.prompt.a.b",+ value: { stringValue: "nested" },+ },+ {+ key: "gen_ai.prompt.safe",+ value: { stringValue: "still-here" },+ },+ ]),+ ]);++ expect(events).toHaveLength(1);+ expect(events[0].input).toMatchObject({+ a: "leaf",+ safe: "still-here",+ });+ });it("should not pollute Object.prototype via __proto__ in gen_ai.prompt attributes", async () => {const traceId = "abcdef1234567890abcdef1234567890";