Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(otel): prevent prototype pollution in OTel attribute key parsing (#13201)

Steffen Schmitz2839f659bb36
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
index 4f8e552217..15382435b0 100644
--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts
+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
@@ -1315,18 +1315,25 @@ export class OtelIngestionProcessor {
const keys = Object.keys(input).map((key) => key.replace(`${prefix}.`, ""));
const useArray = keys.some((key) => key.match(/^\d+\./));
+ // Blocklist to prevent prototype pollution via crafted OTel attribute keys
+ const DANGEROUS_KEYS = new Set(["__proto__", "constructor", "prototype"]);
+
// Helper function to set a value at a nested path
const setNestedValue = (obj: any, path: string[], value: unknown): void => {
let current = obj;
for (let i = 0; i < path.length - 1; i++) {
const key = path[i];
+ if (DANGEROUS_KEYS.has(key)) return;
if (!(key in current)) {
// Check if next key is a number to decide if we need an array or object
current[key] = /^\d+$/.test(path[i + 1]) ? [] : {};
}
current = current[key];
}
- current[path[path.length - 1]] = value;
+ const finalKey = path[path.length - 1];
+ if (!DANGEROUS_KEYS.has(finalKey)) {
+ current[finalKey] = value;
+ }
};
if (useArray) {
@@ -1335,7 +1342,7 @@ export class OtelIngestionProcessor {
const pathParts = key.split(".");
const index = parseInt(pathParts[0], 10);
if (!result[index]) {
- result[index] = {};
+ result[index] = Object.create(null);
}
if (pathParts.length === 2) {
// Simple case: 0.content -> result[0].content
@@ -1351,7 +1358,7 @@ export class OtelIngestionProcessor {
}
return result;
} else {
- const result: Record<string, unknown> = {};
+ const result: Record<string, unknown> = Object.create(null);
for (const key of keys) {
const pathParts = key.split(".");
if (pathParts.length === 1) {
diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
index 9b2f098943..ad9006f90d 100644
--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
@@ -7465,4 +7465,73 @@ describe("OTel Resource Span Mapping", () => {
);
});
});
+
+ describe("Prototype pollution protection", () => {
+ const publicKey = "pk-lf-1234567890";
+
+ it("should not pollute Object.prototype via __proto__ in gen_ai.prompt attributes", async () => {
+ const traceId = "abcdef1234567890abcdef1234567890";
+ const spanId = "abcdef1234567890";
+
+ const 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").toJSON(),
+ spanId: Buffer.from(spanId, "hex").toJSON(),
+ name: "pollution-test",
+ kind: 1,
+ startTimeUnixNano: { low: 1000000000, high: 0, unsigned: true },
+ endTimeUnixNano: { low: 2000000000, high: 0, unsigned: true },
+ attributes: [
+ {
+ key: "gen_ai.prompt.role",
+ value: { stringValue: "user" },
+ },
+ {
+ key: "gen_ai.prompt.content",
+ value: { stringValue: "hello" },
+ },
+ {
+ key: "gen_ai.prompt.__proto__.POLLUTED",
+ value: { stringValue: "SUCCESS" },
+ },
+ ],
+ status: {},
+ },
+ ],
+ },
+ ],
+ };
+
+ await convertOtelSpanToIngestionEvent(
+ resourceSpan,
+ new Set([traceId]),
+ publicKey,
+ );
+
+ // Verify Object.prototype was NOT polluted
+ expect(({} as any).POLLUTED).toBeUndefined();
+ expect(Object.prototype.hasOwnProperty("POLLUTED" as any)).toBe(false);
+ });
+ });
});