Menu

Post image 1
Post image 2
Post image 3
Post image 4
Post image 5
Post image 6
Post image 7
Post image 8
Post image 9
Post image 10
Post image 11
1 / 11
0

How Zod's .refine() Can Cause a Denial of Service — And How to Fix It

DEV Community·Hrushikesh Shinde·about 1 month ago
#jPIwM0hh
Reading 0:00
15s threshold

TL;DR Zod's .refine() executes on every input — even when earlier validators like .min() and .max() have already failed. If you place an expensive operation such as a database query inside .refine() , an attacker can trigger that query with every request, including requests containing completely invalid inputs that would never pass validation. Flood enough of those requests concurrently and the server goes down. The fix is one line away — validate first, query after — but only if you know the behavior exists. Introduction Zod is one of the most widely adopted TypeScript validation libraries. If you are building a Next.js API route, a tRPC endpoint, or a server action, Zod is likely involved in your validation layer. It is trusted precisely because it makes validation straightforward — define a schema once, validate everywhere. That trust makes this behavioral edge case more dangerous than it would be in a less-used library. Developers assume that if .min() or .max() rejects an input, Zod stops there.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More