Skip to content

Commit f6a4ed1

Browse files
ShangeethR29aalmiray
authored andcommitted
Fix invalid nested <select> when sanitizing <optgroup>
1 parent 21dcaa0 commit f6a4ed1

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

owasp-java-html-sanitizer/src/main/java/org/owasp/html/HtmlElementTables.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public HtmlElementTables(
109109
LI_TAG = indexForName("li");
110110
SELECT_TAG = indexForName("select");
111111
OPTION_TAG = indexForName("option");
112-
OPTGROUP_TAG = indexForName("opgroup");
112+
OPTGROUP_TAG = indexForName("optgroup");
113113
SCRIPT_TAG = indexForName("script");
114114
STYLE_TAG = indexForName("style");
115115
TABLE_TAG = indexForName("table");
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.owasp.html;
2+
3+
import org.junit.Test;
4+
import static org.junit.Assert.assertEquals;
5+
6+
public class OptgroupBugTest {
7+
8+
/**
9+
* Test that optgroup elements inside select are not corrupted with extra select tags.
10+
*
11+
* Before fix: <select><optgroup><select><option></option></select></optgroup></select>
12+
* After fix: <select><optgroup><option></option></optgroup></select>
13+
*/
14+
@Test
15+
public void testOptgroupInsideSelectDoesNotAddExtraSelectTags() {
16+
PolicyFactory factory = new HtmlPolicyBuilder()
17+
.allowElements("select", "optgroup", "option")
18+
.allowAttributes("label").globally()
19+
.toFactory();
20+
21+
String input = "<select><optgroup label=\"mygroup\"><option>My option</option></optgroup></select>";
22+
String result = factory.sanitize(input);
23+
24+
// The key assertion: no extra select tags should be inserted
25+
assertEquals(input, result);
26+
}
27+
}

0 commit comments

Comments
 (0)