2021-11-01
2092
Godson Obielum
75053
Nov 1, 2021 ⋅ 7 min read

How to protect against regex denial-of-service (ReDoS) attacks

Godson Obielum I'm a software developer with a life goal of using technology as a tool for solving problems across major industries.

Recent posts:

Master JavaScript Date And Time: From Moment.js To Temporal

Master JavaScript date and time: From Moment.js to Temporal

JavaScript’s Date API has many limitations. Explore alternative libraries like Moment.js, date-fns, and the new Temporal API.

Yan Sun
Dec 4, 2024 ⋅ 9 min read
Npm Vs. Npx: What’s The Difference?

npm vs. npx: What’s the difference?

Explore use cases for using npm vs. npx such as long-term dependency management or temporary tasks and running packages on the fly.

Fimber Elemuwa
Dec 3, 2024 ⋅ 5 min read
How To Audit And Validate AI-Generated Code Output

How to audit and validate AI-generated code output

Validating and auditing AI-generated code reduces code errors and ensures that code is compliant.

Boemo Mmopelwa
Dec 2, 2024 ⋅ 5 min read
Building A Background Remover With Vue And Transformers.js

Building a background remover with Vue and Transformers.js

Build a real-time image background remover in Vue using Transformers.js and WebGPU for client-side processing with privacy and efficiency.

Emmanuel John
Nov 29, 2024 ⋅ 9 min read
View all posts

One Reply to "How to protect against regex denial-of-service (ReDoS) attacks"

  1. Interesting article.

    Your explanation is wrong though. \w+\s* does not return “A long sentence with invalid characters that takes so much time to be matched that it potentially causes our CPU usage to increase”. it matches “A “, because \w is only a single char, so \w+ matches as many word char are available (in this case just the letter A), then \s* matches as many spaces as possible (just one in this case), the result is “A “. then (\w+\s*)* matches the whole string. It matches as many “at least one word char followed by 0 or more space”. The rest of your explanation is therefore erroneous.

    Too bad also your solution is not a real solution. It rejects rapidly the sequence with invalid chars, but it also reject any sequence with valid char ! In fact, this formula will never match anything but the empty string. This is due to the fact that you reference the 1st group from within the first group (the \1 is within the first pair of ()). If you define the first group as “The first group is the first group plus the repetition of itself”, the only solution is the empty group.

    A solution that works to you problem is “an optional blank separated list of words plus one word” and it’s spelled like this :
    /^(\w+\s+)*\w+$/
    which can be decoded as :
    ^: start
    (…)* repeat 0 or more time
    \w+: at least one word char
    \s+: at least one space char :
    \w+: followed by at least one word char
    $: then end

    It instantly matches “correct”
    it instantly matches “this is a list of word”
    it instantly does not match “this is an invalid list!”
    it instantly does not match “A long sentence with invalid characters that takes soo much time to be matched that it potentially causes our CPU usage to increase drastically!!!”

Leave a Reply