Home

Awesome

ReDoS Comprehension from LLM and Developer Forums

This repository contains the source code of the paper "Understanding Regular Expression Denial of Service (ReDoS): Insights from LLM-Generated Regexes and Developer Forums", accepted at the 32nd International Conference on Program Comprehension (ICPC 2024).

Abstract

Regular expression Denial of Service (ReDoS) represents an algo- rithmic complexity attack that exploits the processing of regular expressions (regexes) to produce a denial-of-service attack. This attack occurs when a regex’s evaluation time scales polynomially or exponentially with input length, posing significant challenges for software developers. The advent of Large Language Models (LLMs) has revolutionized the generation of regexes from natural language prompts, but not without its risks. Prior works showed that LLMs can generate code with vulnerabilities and security smells. In this paper, we examined the correctness and security of regexes generated by LLMs as well as the characteristics of LLM-generated vulner- able regexes. Our study also examined ReDoS patterns in actual software projects, aligning them with corresponding regex equiva- lence classes and algorithmic complexity. Moreover, we analyzed developer discussions on GitHub and StackOverflow, constructing a taxonomy to investigate their experiences and perspectives on ReDoS. In this study, we found that GPT-3.5 was the best LLM to generate regexes that are both correct and secure. We also observed that LLM-generated regexes mainly have polynomial ReDoS vulner- ability patterns and it is consistent with vulnerable regexes found in open-source projects. We also found that developers’ main discussions around insecure regexes are related to mitigation strategies to remove vulnerable regexes.

Project Structure

Usage

Installation

Run the following command to install the required packages:

conda create -n "redos_study"  python=3.9.4
conda activate redos_study
pip install -r requirements.txt
pip install torch torchvision torchaudio
pip install accelerate

Generation

There are three Python files for three models: gpt35.py, Phi_RegexGen, and T5_Regexgen.py. You can run them to generate the regexes.

You will need an OpenAI API key for gpt35.py and have to create a config.json file as the example.json file. Update your API key in the config.json file.

fix_gpt35.ipynb will try to regenerate the regexes generated by GPT-3.5 if there is an error.

gpt35_filter.ipynb will filter additional text generated by GPT-3.5.

phi_filter.ipynb will filter additional text generated by Phi model.

ReDoSHunter_Input_Creation.ipynb will create the input for ReDoSHunter. Check the corresponding link about running it.

Evaluation

Compilation.ipynb will compile the Regexes and tests with the corresponding tests. It will generate {Model_name}_{Prompt_type}_Output_Compiled_Result.json. Then, you can run Pass_at_k_Evaluation.ipynb to get the pass@k score.

DFA_Equ_Evaluation.ipynb will use regex_dfa_equals.jar to find out the DFA match and calculate the DFA-EQ@k score.

EM_Evaluation.ipynb will calculate the Exact match ratio.

RQ4 Analysis

Citation

If you use this code, please cite the following paper:

@inproceedings{siddiq2024understanding,
  author={Siddiq, Mohammed Latif and Zhang, Jiahao and Santos, Joanna C. S.},
  booktitle={32nd IEEE/ACM International Conference on Program Comprehension (ICPC 2024)}, 
  title={Understanding Regular Expression Denial of Service (ReDoS): Insights from LLM-Generated Regexes and Developer Forums}, 
  year={2024},
  doi={10.1145/3643916.3644424}
}