Skip to content

Commit 1118e4f

Browse files
waleedlatif1claude
andcommitted
fix(cloudwatch): reject non-numeric metricValue instead of silently publishing 0
Add NaN guard in block config and .finite() refinement in Zod schema so "abc" → NaN is caught at both layers instead of coercing to 0. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 66ffcd9 commit 1118e4f

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

apps/sim/app/api/tools/cloudwatch/put-metric-data/route.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ const PutMetricDataSchema = z.object({
4646
secretAccessKey: z.string().min(1, 'AWS secret access key is required'),
4747
namespace: z.string().min(1, 'Namespace is required'),
4848
metricName: z.string().min(1, 'Metric name is required'),
49-
value: z.number({ coerce: true }),
49+
value: z.number({ coerce: true }).refine((v) => Number.isFinite(v), {
50+
message: 'Metric value must be a finite number',
51+
}),
5052
unit: z.enum(VALID_UNITS).optional(),
5153
dimensions: z
5254
.string()

apps/sim/blocks/blocks/cloudwatch.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,14 +570,18 @@ Return ONLY the numeric timestamp - no explanations, no quotes, no extra text.`,
570570
if (rest.metricValue === undefined || rest.metricValue === '') {
571571
throw new Error('Metric value is required')
572572
}
573+
const numericValue = Number(rest.metricValue)
574+
if (Number.isNaN(numericValue)) {
575+
throw new Error('Metric value must be a valid number')
576+
}
573577

574578
return {
575579
awsRegion,
576580
awsAccessKeyId,
577581
awsSecretAccessKey,
578582
namespace: rest.metricNamespace,
579583
metricName: rest.metricName,
580-
value: Number(rest.metricValue),
584+
value: numericValue,
581585
...(rest.metricUnit && rest.metricUnit !== 'None' && { unit: rest.metricUnit }),
582586
...(rest.publishDimensions && {
583587
dimensions: (() => {

0 commit comments

Comments
 (0)