Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support standard attributes in TABLES Policy #326

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/main/java/org/owasp/html/Sanitizers.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
// POSSIBILITY OF SUCH DAMAGE.

package org.owasp.html;
import java.util.Arrays;
import java.util.List;


/**
* Pre-packaged HTML sanitizer policies.
Expand All @@ -52,6 +55,25 @@
*/
public final class Sanitizers {


/**
* An AttributePolicy to allow only string literals "row", "col", "rowgroup" and "colgroup" as attribute values for "scope" in element th
* Reference : https://developer.mozilla.org/en-US/docs/Web/HTML/Element/th
*/
private static final AttributePolicy TABLE_SCOPE_POLICY = new AttributePolicy() {

private List<String> thScopeWhitelistValues = Arrays.asList("row","col","rowgroup","colgroup");

@Override
public String apply(String elementName, String attributeName, String value) {
if("scope".equals(attributeName)) {
if(thScopeWhitelistValues.contains(value.toLowerCase()))
return value;
}
return null;
}
};

/**
* Allows common formatting elements including {@code <b>}, {@code <i>}, etc.
*/
Expand Down Expand Up @@ -93,6 +115,10 @@ public final class Sanitizers {
.onElements("table", "tr", "td", "th",
"colgroup", "col",
"thead", "tbody", "tfoot")
.allowAttributes("colspan","rowspan","headers")
.onElements("td","th")
.allowAttributes("scope").matching(TABLE_SCOPE_POLICY)
.onElements("th")
.allowTextIn("table") // WIDGY
.toFactory();

Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/owasp/html/SanitizersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ public static final void testFormatting() {
"<p>Hello, <b onclick=alert(1337)>World</b>!</p>"));
}

@Test
public static final void testTableAttributes() {
String input = "<table><tbody><tr align=\"center\"><th headers=\"test\">Month</th><th scope=\"rowgroup\">Test</th></tr>"
+ "<tr><td headers=\"name\">Test</td><td>A</td></tr><tr><td rowspan=\"2\">Test</td><td>B</td></tr><tr>"
+ "<td colspan=\"2\">Test</td></tr></tbody></table>";
assertEquals(input, Sanitizers.TABLES.sanitize(input));

//Negative test to ensure 'scope' doesn't allow random values
assertEquals("<table><tbody><tr><th></th></tr></tbody></table>\n"
,Sanitizers.TABLES.sanitize("<table><tbody><tr><th scope=\"random\"></th></tr></tbody></table>\n"
+ ""));
}
@Test
public static final void testBlockElements() {
assertEquals("", Sanitizers.BLOCKS.sanitize(null));
Expand Down
Loading