Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(alerts): enforce max_length on alert and threshold name fields (#70348)

posthog[bot]afc64e66fefe
diff --git a/products/alerts/backend/api/alert.py b/products/alerts/backend/api/alert.py
index e13f54dbc27c..f228d4d2ee1e 100644
--- a/products/alerts/backend/api/alert.py
+++ b/products/alerts/backend/api/alert.py
@@ -117,6 +117,7 @@ class ThresholdSerializer(serializers.ModelSerializer):
name = serializers.CharField(
required=False,
allow_blank=True,
+ max_length=255,
help_text="Optional name for the threshold.",
)
@@ -250,6 +251,7 @@ class AlertSerializer(SearchMatchTypeSerializerMixin, serializers.ModelSerialize
name = serializers.CharField(
required=False,
allow_blank=True,
+ max_length=255,
help_text="Human-readable name for the alert.",
)
# nosemgrep: unscoped-primary-key-related-field — User model is not team-scoped; validate_subscribed_users() checks team membership
diff --git a/products/alerts/backend/api/test/test_alert.py b/products/alerts/backend/api/test/test_alert.py
index 0152cc00efa3..8ec2017520fe 100644
--- a/products/alerts/backend/api/test/test_alert.py
+++ b/products/alerts/backend/api/test/test_alert.py
@@ -207,6 +207,45 @@ class TestAlert(APIBaseTest, QueryMatchingTest):
assert response.status_code == status.HTTP_400_BAD_REQUEST, response.content
assert "At least one threshold bound" in str(response.json())
+ @parameterized.expand(
+ [
+ ("alert_name", "a" * 256, ""),
+ ("threshold_name", "alert name", "a" * 256),
+ ]
+ )
+ def test_create_alert_rejects_over_long_name(self, _name: str, alert_name: str, threshold_name: str) -> None:
+ # Over-long names used to sail past validation and 500 on the Postgres write
+ # (value too long for varchar(255)); the serializer must reject them with a 400.
+ creation_request: dict[str, Any] = {
+ "insight": self.insight["id"],
+ "subscribed_users": [self.user.id],
+ "condition": {"type": AlertConditionType.ABSOLUTE_VALUE},
+ "config": {"type": "TrendsAlertConfig", "series_index": 0},
+ "name": alert_name,
+ "threshold": {
+ "name": threshold_name,
+ "configuration": {"type": InsightThresholdType.ABSOLUTE, "bounds": {"upper": 100}},
+ },
+ "calculation_interval": "daily",
+ }
+
+ response = self.client.post(f"/api/projects/{self.team.id}/alerts", creation_request)
+ assert response.status_code == status.HTTP_400_BAD_REQUEST, response.content
+
+ def test_patch_alert_rejects_over_long_name(self) -> None:
+ creation_request = {
+ "insight": self.insight["id"],
+ "subscribed_users": [self.user.id],
+ "condition": {"type": AlertConditionType.ABSOLUTE_VALUE},
+ "config": {"type": "TrendsAlertConfig", "series_index": 0},
+ "name": "alert name",
+ "threshold": {"configuration": {"type": InsightThresholdType.ABSOLUTE, "bounds": {"upper": 100}}},
+ "calculation_interval": "daily",
+ }
+ alert_id = self.client.post(f"/api/projects/{self.team.id}/alerts", creation_request).json()["id"]
+ response = self.client.patch(f"/api/projects/{self.team.id}/alerts/{alert_id}", {"name": "a" * 256})
+ assert response.status_code == status.HTTP_400_BAD_REQUEST, response.content
+
def test_patch_legacy_empty_bounds_alert_without_touching_threshold(self) -> None:
threshold = Threshold.objects.create(
team=self.team,
diff --git a/products/alerts/frontend/generated/api.schemas.ts b/products/alerts/frontend/generated/api.schemas.ts
index ec76a152965b..8c4b7d14b041 100644
--- a/products/alerts/frontend/generated/api.schemas.ts
+++ b/products/alerts/frontend/generated/api.schemas.ts
@@ -85,7 +85,10 @@ export interface InsightThresholdApi {
export interface ThresholdApi {
readonly id: string
readonly created_at: string
- /** Optional name for the threshold. */
+ /**
+ * Optional name for the threshold.
+ * @maxLength 255
+ */
name?: string
/** Threshold bounds and type. Includes bounds (lower/upper floats) and type (absolute or percentage). For threshold-based alerts (no detector_config), at least one of lower or upper must be set. */
configuration: InsightThresholdApi
@@ -494,7 +497,10 @@ export interface AlertApi {
readonly created_at: string
/** Insight ID monitored by this alert. Note: Response returns full InsightBasicSerializer object. */
insight: number
- /** Human-readable name for the alert. */
+ /**
+ * Human-readable name for the alert.
+ * @maxLength 255
+ */
name?: string
/** User IDs to subscribe to this alert. Note: Response returns full UserBasicSerializer object. */
subscribed_users: number[]
@@ -576,7 +582,10 @@ export interface PatchedAlertApi {
readonly created_at?: string
/** Insight ID monitored by this alert. Note: Response returns full InsightBasicSerializer object. */
insight?: number
- /** Human-readable name for the alert. */
+ /**
+ * Human-readable name for the alert.
+ * @maxLength 255
+ */
name?: string
/** User IDs to subscribe to this alert. Note: Response returns full UserBasicSerializer object. */
subscribed_users?: number[]
@@ -713,7 +722,10 @@ export interface AlertSimulateResponseApi {
export interface ThresholdWithAlertApi {
readonly id: string
readonly created_at: string
- /** Optional name for the threshold. */
+ /**
+ * Optional name for the threshold.
+ * @maxLength 255
+ */
name?: string
/** Threshold bounds and type. Includes bounds (lower/upper floats) and type (absolute or percentage). For threshold-based alerts (no detector_config), at least one of lower or upper must be set. */
configuration: InsightThresholdApi
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index ba4aa022c4d4..40a60525c84f 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -8934,7 +8934,10 @@ export namespace Schemas {
export interface Threshold {
readonly id: string;
readonly created_at: string;
- /** Optional name for the threshold. */
+ /**
+ * Optional name for the threshold.
+ * @maxLength 255
+ */
name?: string;
/** Threshold bounds and type. Includes bounds (lower/upper floats) and type (absolute or percentage). For threshold-based alerts (no detector_config), at least one of lower or upper must be set. */
configuration: InsightThreshold;
@@ -9322,7 +9325,10 @@ export namespace Schemas {
readonly created_at: string;
/** Insight ID monitored by this alert. Note: Response returns full InsightBasicSerializer object. */
insight: number;
- /** Human-readable name for the alert. */
+ /**
+ * Human-readable name for the alert.
+ * @maxLength 255
+ */
name?: string;
/** User IDs to subscribe to this alert. Note: Response returns full UserBasicSerializer object. */
subscribed_users: number[];
@@ -37319,7 +37325,10 @@ export namespace Schemas {
export interface ThresholdWithAlert {
readonly id: string;
readonly created_at: string;
- /** Optional name for the threshold. */
+ /**
+ * Optional name for the threshold.
+ * @maxLength 255
+ */
name?: string;
/** Threshold bounds and type. Includes bounds (lower/upper floats) and type (absolute or percentage). For threshold-based alerts (no detector_config), at least one of lower or upper must be set. */
configuration: InsightThreshold;
@@ -38717,7 +38726,10 @@ export namespace Schemas {
readonly created_at?: string;
/** Insight ID monitored by this alert. Note: Response returns full InsightBasicSerializer object. */
insight?: number;
- /** Human-readable name for the alert. */
+ /**
+ * Human-readable name for the alert.
+ * @maxLength 255
+ */
name?: string;
/** User IDs to subscribe to this alert. Note: Response returns full UserBasicSerializer object. */
subscribed_users?: number[];
diff --git a/services/mcp/src/generated/alerts/api.ts b/services/mcp/src/generated/alerts/api.ts
index 982b4bb8955f..6900310b48e7 100644
--- a/services/mcp/src/generated/alerts/api.ts
+++ b/services/mcp/src/generated/alerts/api.ts
@@ -40,6 +40,10 @@ export const AlertsCreateParams = /* @__PURE__ */ zod.object({
),
})
+export const alertsCreateBodyNameMax = 255
+
+export const alertsCreateBodyThresholdOneNameMax = 255
+
export const alertsCreateBodyConfigOneOneTypeDefault = `TrendsAlertConfig`
export const alertsCreateBodyConfigOneTwoTypeDefault = `HogQLAlertConfig`
export const alertsCreateBodyConfigOneThreeTypeDefault = `FunnelsAlertConfig`
@@ -73,7 +77,7 @@ export const AlertsCreateBody = /* @__PURE__ */ zod.object({
insight: zod
.number()
.describe('Insight ID monitored by this alert. Note: Response returns full InsightBasicSerializer object.'),
- name: zod.string().optional().describe('Human-readable name for the alert.'),
+ name: zod.string().max(alertsCreateBodyNameMax).optional().describe('Human-readable name for the alert.'),
subscribed_users: zod
.array(zod.number())
.describe('User IDs to subscribe to this alert. Note: Response returns full UserBasicSerializer object.'),
@@ -81,7 +85,11 @@ export const AlertsCreateBody = /* @__PURE__ */ zod.object({
.object({
id: zod.string().optional(),
created_at: zod.iso.datetime({ offset: true }).optional(),
- name: zod.string().optional().describe('Optional name for the threshold.'),
+ name: zod
+ .string()
+ .max(alertsCreateBodyThresholdOneNameMax)
+ .optional()
+ .describe('Optional name for the threshold.'),
configuration: zod
.object({
bounds: zod
@@ -1328,6 +1336,10 @@ export const AlertsPartialUpdateParams = /* @__PURE__ */ zod.object({
),
})
+export const alertsPartialUpdateBodyNameMax = 255
+
+export const alertsPartialUpdateBodyThresholdOneNameMax = 255
+
export const alertsPartialUpdateBodyConfigOneOneTypeDefault = `TrendsAlertConfig`
export const alertsPartialUpdateBodyConfigOneTwoTypeDefault = `HogQLAlertConfig`
export const alertsPartialUpdateBodyConfigOneThreeTypeDefault = `FunnelsAlertConfig`
@@ -1362,7 +1374,7 @@ export const AlertsPartialUpdateBody = /* @__PURE__ */ zod.object({
.number()
.optional()
.describe('Insight ID monitored by this alert. Note: Response returns full InsightBasicSerializer object.'),
- name: zod.string().optional().describe('Human-readable name for the alert.'),
+ name: zod.string().max(alertsPartialUpdateBodyNameMax).optional().describe('Human-readable name for the alert.'),
subscribed_users: zod
.array(zod.number())
.optional()
@@ -1371,7 +1383,11 @@ export const AlertsPartialUpdateBody = /* @__PURE__ */ zod.object({
.object({
id: zod.string().optional(),
created_at: zod.iso.datetime({ offset: true }).optional(),
- name: zod.string().optional().describe('Optional name for the threshold.'),
+ name: zod
+ .string()
+ .max(alertsPartialUpdateBodyThresholdOneNameMax)
+ .optional()
+ .describe('Optional name for the threshold.'),
configuration: zod
.object({
bounds: zod
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-create.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-create.json
index 1ece7bfa7097..d02d42ee554f 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-create.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-create.json
@@ -2225,6 +2225,7 @@
},
"name": {
"description": "Human-readable name for the alert.",
+ "maxLength": 255,
"type": "string"
},
"schedule_restriction": {
@@ -2347,6 +2348,7 @@
},
"name": {
"description": "Optional name for the threshold.",
+ "maxLength": 255,
"type": "string"
}
},
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-update.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-update.json
index 570c28768471..2f496873be04 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-update.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/alert-update.json
@@ -2229,6 +2229,7 @@
},
"name": {
"description": "Human-readable name for the alert.",
+ "maxLength": 255,
"type": "string"
},
"schedule_restriction": {
@@ -2351,6 +2352,7 @@
},
"name": {
"description": "Optional name for the threshold.",
+ "maxLength": 255,
"type": "string"
}
},