Skip to content

Commit 415e8c3

Browse files
committed
fix(protocol): coerce malformed bugReports before feature schema parse
1 parent b639f66 commit 415e8c3

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

packages/protocol/src/features.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,21 @@ export function coerceBugReportsFeatureFromFeaturesPayload(payload: unknown): Bu
102102
return parsed.success ? parsed.data : DEFAULT_BUG_REPORTS_FEATURE;
103103
}
104104

105-
export const FeaturesResponseSchema = z.object({
105+
function coerceFeaturesResponsePayload(raw: unknown): unknown {
106+
if (!isRecord(raw) || !isRecord(raw.features)) return raw;
107+
108+
// Robustness: a malformed bugReports payload should not invalidate unrelated features.
109+
// Coerce it to a safe default while preserving the rest of the payload.
110+
return {
111+
...raw,
112+
features: {
113+
...raw.features,
114+
bugReports: coerceBugReportsFeatureFromFeaturesPayload(raw),
115+
},
116+
};
117+
}
118+
119+
export const FeaturesResponseSchema = z.preprocess(coerceFeaturesResponsePayload, z.object({
106120
features: z.object({
107121
bugReports: BugReportsFeatureSchema.optional().default(DEFAULT_BUG_REPORTS_FEATURE),
108122
automations: AutomationsFeatureSchema.optional().default(DEFAULT_AUTOMATIONS_FEATURE),
@@ -186,6 +200,6 @@ export const FeaturesResponseSchema = z.object({
186200
),
187201
}),
188202
}),
189-
});
203+
}));
190204

191205
export type FeaturesResponse = z.infer<typeof FeaturesResponseSchema>;

0 commit comments

Comments
 (0)