[GO] I tried to find out if ReDoS is possible with Python

What is ReDoS

https://blog.ohgaki.net/redos-must-review-mail-address-validation

What is this scary

Verification

Try with ipython

import re
from datetime import datetime


n = 5
while n < 12:
    s = "username@host{}.".format(".abcde" * n)
    start = datetime.now()
    print(re.match(r"\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z", s, flags=re.IGNORECASE))
    print('{}: {}'.format(len(s), (datetime.now() - start).total_seconds()))
    n += 1

Execution result

None
44: 0.085964
None
50: 0.41799
None
56: 2.106463
None
62: 10.319476
None
68: 57.825968
None
74: 269.451777
None
80: 1400.865988

It is increasing exponentially. Koe

Try it with Django's EmailValidator

from datetime import datetime
from django.core.exceptions import ValidationError
from django.core.validators import EmailValidator


validator = EmailValidator()
n = 5
while n < 12:
    s = "username@host{}.".format(".abcde" * n)
    start = datetime.now()
    try:
        validator(s)
        print(True)
    except ValidationError:
        print(False)

    print('{}: {}'.format(len(s), (datetime.now() - start).total_seconds()))
    n += 1

Execution result

False
44: 0.015296
False
50: 0.000116
False
56: 9.3e-05
False
62: 0.000127
False
68: 0.00016
False
74: 0.000122
False
80: 0.000125

Oh, it looks okay.

I will also try my own Validator

import re
from datetime import datetime
from django.core.exceptions import ValidationError
from django.core.validators import EmailValidator


class JapaneseEmailValidator(EmailValidator):
    user_regex = re.compile(
        r"(^[-.!#$%&'*+/=?^_`{}|~0-9A-Z]+$"  # dot-atom
        r"|^\"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*\"$)",  # quoted-string
        re.IGNORECASE
    )


validator = JapaneseEmailValidator()
n = 5
while n < 12:
    s = "username@host{}.".format(".abcde" * n)
    start = datetime.now()
    try:
        validator(s)
        print(True)
    except ValidationError:
        print(False)

    print('{}: {}'.format(len(s), (datetime.now() - start).total_seconds()))
    n += 1

Execution result

False
44: 0.00613
False
50: 9.8e-05
False
56: 8.6e-05
False
62: 9.9e-05
False
68: 0.000127
False
74: 0.000145
False
80: 0.000105

Summary

You have to be careful about secure programming If you're not sure, you can use Django's official validator. It's only 1400sec (23min20sec) with 80 characters.

Recommended Posts

I tried to find out if ReDoS is possible with Python
python beginners tried to find out
I tried to find the entropy of the image with python
I tried to find out how to streamline the work flow with Excel x Python ②
I tried to find out how to streamline the work flow with Excel x Python ④
I tried to find out how to streamline the work flow with Excel x Python ⑤
I tried to find out how to streamline the work flow with Excel x Python ①
I tried to find out how to streamline the work flow with Excel x Python ③
I tried to find out as much as possible about the GIL that you should know if you are doing parallel processing with Python
I tried to output LLVM IR with Python
I tried to automate sushi making with python
Mayungo's Python Learning Episode 2: I tried to put out characters with variables
I tried to find out what I can do because slicing is convenient
I tried to find out how to streamline the work flow with Excel × Python, my article summary ★
I tried to implement merge sort in Python with as few lines as possible
I tried fp-growth with python
I tried scraping with Python
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I tried to explain what a Python generator is for as easily as possible.
I tried to get started with blender python script_Part 02
I tried to implement an artificial perceptron with python
I tried to automatically generate a password with Python3
I tried to solve the problem with Python Vol.1
I tried to analyze J League data with Python
I tried gRPC with Python
I tried scraping with python
I tried to find an alternating series with tensorflow
I tried to solve AOJ's number theory with Python
I tried to put out the frequent word ranking of LINE talk with Python
I tried to find out what would happen if I converted NaN or INF to int
I want to initialize if the value is empty (python)
I tried to simulate how the infection spreads with Python
I tried to make various "dummy data" with Python faker
I tried to find the average of the sequence with TensorFlow
I tried various methods to send Japanese mail with Python
[Python] I tried to visualize tweets about Corona with WordCloud
Mayungo's Python Learning Episode 3: I tried to print numbers with print
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried to divide the file into folders with Python
I tried to touch Python (installation)
I tried web scraping with python.
I want to debug with Python
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python
[1 hour challenge] I tried to make a fortune-telling site that is too suitable with Python
[5th] I tried to make a certain authenticator-like tool with python
[2nd] I tried to make a certain authenticator-like tool with python
[3rd] I tried to make a certain authenticator-like tool with python
[Python] A memo that I tried to get started with asyncio
I tried to create a list of prime numbers with python
[Pandas] I tried to analyze sales data with Python [For beginners]
I tried to fix "I tried stochastic simulation of bingo game with Python"
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I tried to summarize everyone's remarks on slack with wordcloud (Python)
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
I tried to easily detect facial landmarks with python and dlib