[Performance Bug Report] Specific input cause infinite loop in CssCompressor.java
- Dominant language
- Java
- Stars
- 3.6k
- Forks
- 519
- PR merge metrics
- No merged PRs in 30d
Description
We are working on the [Algorithmic Complexity Denial-of-Service](https://www.usenix.org/legacy/events/sec03/tech/full_papers/crosby/crosby.pdf) problem and detected a performance bug from your code.
We didn’t create a pull request because we're not sure whether this bug can be triggered from the user interface. We also do not understand the functionality of this code snippet as you do. Thanks for your understanding.
### Outcome
In [CssCompressor.java](https://github.com/dropbox/hackpad/blob/master/infrastructure/yuicompressor/src/com/yahoo/platform/yui/compressor/CssCompressor.java) the method `extractDataUrls` would take forever to parse a simple string `"url(data::"`
### Reasons
The `extractDataUrls` function does not correctly handle some specific inputs without a terminator.
### Repeatability
A simplified test case is provided here.
```
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.ArrayList;
public class CssCompressor {
protected static String extractDataUrls(String css, ArrayList preservedTokens) {
int maxIndex = css.length() - 1;
int appendIndex = 0;
StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile("(?i)url\\(\\s*([\"']?)data\\:");
Matcher m = p.matcher(css);
while (m.find()) {
int startIndex = m.start() + 4;
String terminator = m.group(1);
if (terminator.length() == 0) {
terminator = ")";
}
boolean foundTerminator = false;
int endIndex = m.end() - 1;
while(foundTerminator == false && endIndex+1 <= maxIndex) {
endIndex = css.indexOf(terminator, endIndex+1);
if ((endIndex > 0) && (css.charAt(endIndex-1) != '\\')) {
foundTerminator = true;
if (!")".equals(terminator)) {
endIndex = css.indexOf(")", endIndex);
}
}
}
sb.append(css.substring(appendIndex, m.start()));
if (foundTerminator) {
String token = css.substring(startIndex, endIndex);
token = token.replaceAll("\\s+", "");
preservedTokens.add(token);
String preserver = "url(___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.size() - 1) + "___)";
sb.append(preserver);
appendIndex = endIndex + 1;
} else {
sb.append(css.substring(m.start(), m.end()));
appendIndex = m.end();
}
}
sb.append(css.substring(appendIndex));
return sb.toString();
}
public static void main(String[] args) {
extractDataUrls("url(data::", null);
}
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.