📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

What is ReDoS, and how do you stop catastrophic regex backtracking in .NET?

Mid-Level Debugging Intermediate

🎯 Key points

  • Backtracking engines try exponentially many splits on nested quantifiers
  • A few crafted requests can pin all cores: availability impact, so it's a security issue
  • Length limit before the regex runs
  • Avoid nested quantifiers and overlapping alternations
  • RegexOptions.NonBacktracking (.NET 7+) gives linear time but no backreferences or lookarounds
  • matchTimeout throws RegexMatchTimeoutException
  • GeneratedRegex plus static analysis to review and flag risky patterns

⏱ 30-second answer

ReDoS is a denial of service where a crafted input makes a backtracking regex take exponential time, so a handful of requests can pin every core and take an API down. In .NET you defend in layers: limit input length before matching; avoid nested quantifiers like (a+)+ and overlapping alternations; use RegexOptions.NonBacktracking (added in .NET 7, linear time) when you don't need backreferences or lookarounds; always set a match timeout on untrusted input; and prefer [GeneratedRegex] so the pattern is compiled and reviewable. Treat it as a security finding in the threat model, not just a performance bug.

🔍 What the interviewer is checking

Checks whether the candidate treats input-driven CPU burn as a security issue

↩ Usually asked after

More in Debugging Production Issues

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me