All Downloads are FREE. Search and download functionalities are using the official Maven repository.

preprocessor.WildCardExpansionRule Maven / Gradle / Ivy

Go to download

A tool to perform static analysis on regexes to determine whether they are vulnerable to ReDoS.

There is a newer version: 1.0.8
Show newest version
package preprocessor;

import java.util.List;

import preprocessor.ParsingPreprocessor.GroupFactor;
import preprocessor.ParsingPreprocessor.GroupFactor.GroupType;
import preprocessor.ParsingPreprocessor.RegexFactor;
import preprocessor.ParsingPreprocessor.RegexFactor.FactorType;
import preprocessor.ParsingPreprocessor.RegexToken;
import preprocessor.ParsingPreprocessor.RegexToken.TokenType;

import nfa.transitionlabel.TransitionLabel;

public class WildCardExpansionRule implements PreprocessorRule {
	
	private final boolean MATCH_NEWLINE = true;

	@Override
	public String process(List tokenStream) {
		StringBuilder regexBuilder = new StringBuilder();
		RegexToken tokens[] = new RegexToken[tokenStream.size()];
		tokens = tokenStream.toArray(tokens);
		int numTokens = tokens.length;
		int i = 0;
		while (i < numTokens) {
			
			if (tokens[i].getTokenType() == TokenType.REGEX_FACTOR) {
				
				RegexFactor factorToken = (RegexFactor) tokens[i];
				if (factorToken.getFactorType() == FactorType.WILD_CARD) {
					String expandTo;
					int low = TransitionLabel.MIN_16UNICODE;
					int high = TransitionLabel.MAX_16UNICODE - 1;
					int nl = (int) '\n';
					if (MATCH_NEWLINE) {
						expandTo = (String.format("\\x%02x", low)) + "-" + (String.format("\\x{%02x}", high));
					} else {
						expandTo = (String.format("\\x%02x", low)) + "-" + (String.format("\\x%02x", nl - 1)) + (String.format("\\x%02x", nl + 1)) + "-" + (String.format("\\x{%02x}", high));
					}
					regexBuilder.append("[" + expandTo + "]");
					
					
				} else if (factorToken.getFactorType() == FactorType.GROUP) {
					GroupFactor groupFactorToken = (GroupFactor) factorToken;
					GroupType type = groupFactorToken.getGroupType();
					StringBuilder groupBuilder = new StringBuilder();
					groupBuilder.append(process(groupFactorToken.factorContent));
					switch (type) {
					case NORMAL:
						regexBuilder.append("(" + groupBuilder.toString() + ")");
						break;
					case NEGLOOKAHEAD:
						regexBuilder.append("(?!" + groupBuilder.toString() + ")");
						break;
					case NEGLOOKBEHIND:
						regexBuilder.append("(?




© 2015 - 2025 Weber Informatics LLC | Privacy Policy