Skip to content

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

fix(security): sanitize score config names to prevent CSS injection (#13206)

Steffen Schmitz53a0e9d074e4
diff --git a/fern/apis/server/definition/score-configs.yml b/fern/apis/server/definition/score-configs.yml
index 25a3975585..dbb91e1350 100644
--- a/fern/apis/server/definition/score-configs.yml
+++ b/fern/apis/server/definition/score-configs.yml
@@ -54,7 +54,9 @@ types:
meta: pagination.MetaResponse
CreateScoreConfigRequest:
properties:
- name: string
+ name:
+ type: string
+ docs: "Name of the score config. Max 35 characters. Only letters, numbers, underscores, spaces, periods, parentheses, and hyphens are allowed."
dataType: commons.ScoreConfigDataType
categories:
type: optional<list<commons.ConfigCategory>>
@@ -75,7 +77,7 @@ types:
docs: The status of the score config showing if it is archived or not
name:
type: optional<string>
- docs: The name of the score config
+ docs: "Name of the score config. Max 35 characters. Only letters, numbers, underscores, spaces, periods, parentheses, and hyphens are allowed."
categories:
type: optional<list<commons.ConfigCategory>>
docs: Configure custom categories for categorical scores. Pass a list of objects with `label` and `value` properties. Categories are autogenerated for boolean configs and cannot be passed
diff --git a/packages/shared/src/domain/score-configs.ts b/packages/shared/src/domain/score-configs.ts
index 58871eef5c..801b54f17f 100644
--- a/packages/shared/src/domain/score-configs.ts
+++ b/packages/shared/src/domain/score-configs.ts
@@ -7,6 +7,13 @@ export const ScoreConfigCategory = z.object({
value: z.number(),
});
+/** Input-only schema for score config names. Use at API/tRPC boundaries, not for DB reads. */
+export const ScoreConfigNameSchema = z
+ .string()
+ .min(1)
+ .max(35)
+ .regex(/^[\p{L}\p{N}_ .()-]+$/u, "Name contains invalid characters");
+
// Numeric config fields
export const NumericConfigFields = z.object({
maxValue: z.number().nullish(),
diff --git a/web/public/generated/api/openapi.yml b/web/public/generated/api/openapi.yml
index a437ac1b97..246ab14942 100644
--- a/web/public/generated/api/openapi.yml
+++ b/web/public/generated/api/openapi.yml
@@ -10760,6 +10760,9 @@ components:
properties:
name:
type: string
+ description: >-
+ Name of the score config. Max 35 characters. Only letters, numbers,
+ underscores, spaces, periods, parentheses, and hyphens are allowed.
dataType:
$ref: '#/components/schemas/ScoreConfigDataType'
categories:
@@ -10806,7 +10809,9 @@ components:
name:
type: string
nullable: true
- description: The name of the score config
+ description: >-
+ Name of the score config. Max 35 characters. Only letters, numbers,
+ underscores, spaces, periods, parentheses, and hyphens are allowed.
categories:
type: array
items:
diff --git a/web/src/components/ui/chart.tsx b/web/src/components/ui/chart.tsx
index 07356a5be4..fd2ac7ab40 100644
--- a/web/src/components/ui/chart.tsx
+++ b/web/src/components/ui/chart.tsx
@@ -99,7 +99,8 @@ ${colorConfig
const color =
itemConfig.theme?.[theme as keyof typeof itemConfig.theme] ||
itemConfig.color;
- return color ? ` --color-${key}: ${color};` : null;
+ const safeKey = key.replace(/[^\p{L}\p{N}_ .()-]/gu, "_");
+ return color ? ` --color-${safeKey}: ${color};` : null;
})
.join("\n")}
}
diff --git a/web/src/features/public-api/types/score-configs.ts b/web/src/features/public-api/types/score-configs.ts
index d74f73daad..6c0bfa6aa5 100644
--- a/web/src/features/public-api/types/score-configs.ts
+++ b/web/src/features/public-api/types/score-configs.ts
@@ -7,6 +7,7 @@ import {
paginationMetaResponseZod,
publicApiPaginationZod,
ScoreConfigCategory,
+ ScoreConfigNameSchema,
validateCategories,
validateNumericRangeFields,
} from "@langfuse/shared";
@@ -70,7 +71,7 @@ export const GetScoreConfigResponse = APIScoreConfig;
// POST /score-configs
const PostScoreConfigBase = z.object({
- name: z.string(),
+ name: ScoreConfigNameSchema,
description: z.string().nullish(),
});
@@ -119,7 +120,7 @@ export const PutScoreConfigQuery = z.object({
export const PutScoreConfigBody = z
.object({
isArchived: z.boolean().optional(),
- name: z.string().min(1).max(35).optional(),
+ name: ScoreConfigNameSchema.optional(),
minValue: z.number().optional(),
maxValue: z.number().optional(),
categories: CategoriesWithCustomError.optional(),
diff --git a/web/src/server/api/routers/scoreConfigs.ts b/web/src/server/api/routers/scoreConfigs.ts
index d52d7588ff..af05f6c7b9 100644
--- a/web/src/server/api/routers/scoreConfigs.ts
+++ b/web/src/server/api/routers/scoreConfigs.ts
@@ -12,6 +12,7 @@ import {
optionalPaginationZod,
ScoreConfigCategory,
ScoreConfigDataType,
+ ScoreConfigNameSchema,
validateDbScoreConfig,
validateDbScoreConfigSafe,
} from "@langfuse/shared";
@@ -28,7 +29,7 @@ const ScoreConfigAllInputPaginated = ScoreConfigAllInput.extend({
const ScoreConfigCreateInput = z.object({
projectId: z.string(),
- name: z.string().min(1).max(35),
+ name: ScoreConfigNameSchema,
dataType: z.enum(ScoreConfigDataType),
minValue: z.number().optional(),
maxValue: z.number().optional(),
@@ -41,7 +42,7 @@ const ScoreConfigUpdateInput = z.object({
id: z.string(),
// Optional fields that may be updated
isArchived: z.boolean().optional(),
- name: z.string().min(1).max(35).optional(),
+ name: ScoreConfigNameSchema.optional(),
description: z.string().nullish(),
minValue: z.number().optional(),
maxValue: z.number().optional(),