Skip to content

Union policy skipIfEmpty value issue #197

@yangbongsoo

Description

@yangbongsoo

Hello First of all, thank you for making a good open source project.
I found union policy skipIfEmpty value issue.

the code is below.

@Test
public void testSkipIfEmptyUnions1() {
	PolicyFactory beforePolicy = new HtmlPolicyBuilder()
			.allowElements("span")
			.allowWithoutAttributes("span")
			.toFactory();

	String spanTagString = "<span>Hi</span>";
	String resultString = beforePolicy.sanitize(spanTagString);
	assertEquals("<span>Hi</span>", resultString);

	PolicyFactory afterPolicy = beforePolicy.and(new HtmlPolicyBuilder()
			.allowElements("span")
			.disallowWithoutAttributes("span")
			.toFactory());

	resultString = afterPolicy.sanitize(spanTagString);
	// todo I think this result has problem
	assertEquals("<span>Hi</span>", resultString);
}

I think resultString value made by afterPolicy is Hi(only string). but <span>Hi</span>.
Because in afterPolicy I add disallowWithoutAttributes span tag.

opposite situation

@Test
public void testSkipIfEmptyUnions2() {
	PolicyFactory beforePolicy = new HtmlPolicyBuilder()
			.allowElements("span")
			.toFactory();

	String spanTagString = "<span>Hi</span>";
	String resultString = beforePolicy.sanitize(spanTagString);
	assertEquals("Hi", resultString);

	PolicyFactory afterPolicy = beforePolicy.and(new HtmlPolicyBuilder()
			.allowElements("span")
			.allowWithoutAttributes("span")
			.toFactory());

	resultString = afterPolicy.sanitize(spanTagString);
	assertEquals("<span>Hi</span>", resultString);
}

this result has no problem.

in ElementAndAttributePolicies.java(Line 88),

before

boolean combinedSkipIfEmpty;
if (HtmlPolicyBuilder.DEFAULT_SKIP_IF_EMPTY.contains(elementName)) {
  // Either policy explicitly opted out of skip if empty.
  combinedSkipIfEmpty = skipIfEmpty && p.skipIfEmpty;
} else {
  // Either policy explicitly specified skip if empty.
  combinedSkipIfEmpty = skipIfEmpty || p.skipIfEmpty;
}

How about below code?

after

if (HtmlPolicyBuilder.DEFAULT_SKIP_IF_EMPTY.contains(elementName)) {
  if (true == skipIfEmpty && false == p.skipIfEmpty) {
      combinedSkipIfEmpty = false;
  } else if (false == skipIfEmpty && true == p.skipIfEmpty) {
      combinedSkipIfEmpty = true;
  } else {
      combinedSkipIfEmpty = skipIfEmpty && p.skipIfEmpty;
  }
} else {
  // Either policy explicitly specified skip if empty.
  combinedSkipIfEmpty = skipIfEmpty || p.skipIfEmpty;
}

or

if (HtmlPolicyBuilder.DEFAULT_SKIP_IF_EMPTY.contains(elementName)) {
  combinedSkipIfEmpty = p.skipIfEmpty;
} else {
  // Either policy explicitly specified skip if empty.
  combinedSkipIfEmpty = skipIfEmpty || p.skipIfEmpty;
}

If It has any problem, I'd appreciate it if you could let me know. thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions