This is a lab exercise on developing secure software. For more information, see the introduction to the labs.
Learn how to identify and fix a code susceptible to ReDoS attack
Regular expressions are a way to do input validation, checking whether data matches a specific pattern. Poorly designed regular expressions may result in a vulnerability to regular expression denial-of-service (ReDoS) attacks. This attack exploits the fact that most Regular Expression implementations may reach extreme situations, so an attacker can force situations where the regular expression implementation will run for an extremely long time, often exponentially increasing time based on the input size.
In this exercise we are going to fix an input validation that uses regular expressions but is susceptible to a ReDoS attack.
The code below sets up handlers for a get request on path /parts. This code could be triggered, for example, by requesting http://localhost:3000/part?id=AB123 (if it was running at localhost and responding to port 3000). If there are no validation errors, the code is supposed to show the part id. If there is a validation error, it responds with HTTP error code 422 ("Unprocessable Content"), a status code suggesting that the request was invalid for some reason, along with an error message.
For this lab we want to fix a regex that is susceptible to a ReDoS attack, applying these countermeasures:
Use the “hint” and “give up” buttons if necessary.
Change the code below, adding the mitigation steps to avoid ReDoS: