Skip to content

Commit

Permalink
fix(nahsragh-546): bug which resulted in an IllegalStateException for…
Browse files Browse the repository at this point in the history
… percentage rgb values
  • Loading branch information
jonah1und1 committed Jan 20, 2025
1 parent 786b18c commit 90581de
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/main/java/org/owasp/validator/css/CssValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,15 @@ public String lexicalValueToString(LexicalUnit lu) {
// this is a rgb encoded color
StringBuffer sb = new StringBuffer("rgb(");
LexicalUnit param = lu.getParameters();
sb.append(param.getIntegerValue()); // R value
sb.append(getColorIntValue(param)); // R value
sb.append(',');
param = param.getNextLexicalUnit(); // comma
param = param.getNextLexicalUnit(); // G value
sb.append(param.getIntegerValue());
sb.append(getColorIntValue(param));
sb.append(',');
param = param.getNextLexicalUnit(); // comma
param = param.getNextLexicalUnit(); // B value
sb.append(param.getIntegerValue());
sb.append(getColorIntValue(param));
sb.append(')');

return sb.toString();
Expand Down Expand Up @@ -404,4 +404,20 @@ public String lexicalValueToString(LexicalUnit lu) {
return null;
}
}

/**
* Returns color value as int.
* Maps percentages to values between 0 and 255.
* Negative percentages are mapped to 0, values bigger than 100% to 255.
*
* @param param LexicalUnit
* @return color value as int
*/
private int getColorIntValue(LexicalUnit param) {
if (param.getLexicalUnitType() == LexicalUnit.SAC_PERCENTAGE) {
return Math.min(0xff, Math.max(0, (int) (0xff * (param.getFloatValue() / 100.0))));
} else {
return param.getIntegerValue();
}
}
}
17 changes: 17 additions & 0 deletions src/test/java/org/owasp/validator/html/test/AntiSamyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2725,4 +2725,21 @@ public void testGithubIssue484() throws ScanException, PolicyException {
assertEquals("<p>this is para data</p>\n" + "<br/>\n" + "<p>this is para data 2</p>", domValue);
assertEquals("<p>this is para data</p>\n" + "<br/>\n" + "<p>this is para data 2</p>", saxValue);
}

@Test
public void testGithubIssue546() throws ScanException, PolicyException {
//Given
String taintedHtml = "<style>.cl { color: rgb(50%, -20%, 150%); }</style>";

//When
CleanResults crDom = as.scan(taintedHtml, policy, AntiSamy.DOM);
CleanResults crSax = as.scan(taintedHtml, policy, AntiSamy.SAX);

//Then
String expectedCleanHtml = "<style>*.cl {\n\tcolor: rgb(127,0,255);\n}\n</style>";
assertEquals(0, crDom.getNumberOfErrors());
assertEquals(0, crSax.getNumberOfErrors());
assertEquals(expectedCleanHtml, crDom.getCleanHTML());
assertEquals(expectedCleanHtml, crSax.getCleanHTML());
}
}

0 comments on commit 90581de

Please sign in to comment.