Skip to content
Prev Previous commit
Next Next commit
fix(cloudwatch): reject non-numeric metricValue instead of silently p…
…ublishing 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>
  • Loading branch information
waleedlatif1 and claude committed Apr 9, 2026
commit 1118e4fa8e681cc20ed4b5ec265e94a8103ce6c5
4 changes: 3 additions & 1 deletion apps/sim/app/api/tools/cloudwatch/put-metric-data/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ const PutMetricDataSchema = z.object({
secretAccessKey: z.string().min(1, 'AWS secret access key is required'),
namespace: z.string().min(1, 'Namespace is required'),
metricName: z.string().min(1, 'Metric name is required'),
value: z.number({ coerce: true }),
value: z.number({ coerce: true }).refine((v) => Number.isFinite(v), {
message: 'Metric value must be a finite number',
}),
unit: z.enum(VALID_UNITS).optional(),
dimensions: z
.string()
Expand Down
6 changes: 5 additions & 1 deletion apps/sim/blocks/blocks/cloudwatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,14 +570,18 @@ Return ONLY the numeric timestamp - no explanations, no quotes, no extra text.`,
if (rest.metricValue === undefined || rest.metricValue === '') {
throw new Error('Metric value is required')
}
Comment thread
waleedlatif1 marked this conversation as resolved.
const numericValue = Number(rest.metricValue)
if (Number.isNaN(numericValue)) {
throw new Error('Metric value must be a valid number')
Comment thread
waleedlatif1 marked this conversation as resolved.
Outdated
}

return {
awsRegion,
awsAccessKeyId,
awsSecretAccessKey,
namespace: rest.metricNamespace,
metricName: rest.metricName,
value: Number(rest.metricValue),
value: numericValue,
...(rest.metricUnit && rest.metricUnit !== 'None' && { unit: rest.metricUnit }),
...(rest.publishDimensions && {
dimensions: (() => {
Expand Down
Loading