Skip to content

about PolicyFactory.and(PolicyFactory f) job #204

@yangbongsoo

Description

@yangbongsoo

I organized the results made through PolicyFactory.and(PolicyFactory f)

What I want to know exactly : with afterPolicy = beforePolicy.and(newPolicy) how has the afterPolicy changed from beforePolicy?

1. allowWithoutAttributes disallowWithoutAttributes

we've investigated in #197

2. allowElements disallowElements

beforePolicy afterPolicy result
X X nothing allowed no problem
X allow beforePolicy tag all remove, afterPolicy tag alive(only added tags) no problem
X disallow beforePolicy tag all remove, afterPolicy tag all remove no problem
allow X beforePolicy tag alive(only added tags), afterPolicy tag alive(only added tags) no problem
allow allow only allowed elements are allowed. no problem
allow disallow If I allow p tag in beforePolicy and disallow p tag in newPolicy, result afterPolicy allow p tag I think that disallowing p tag in afterPolicy is right
  // beforePolicy : allow
  // afterPolicy : disallow
  @Test
  public void testAllowElements6() {
    PolicyFactory beforePolicy = new HtmlPolicyBuilder()
            .allowElements("p")
            .toFactory();

    String input = "<p>pText</p>";
    assertEquals("<p>pText</p>", beforePolicy.sanitize(input));

    PolicyFactory newPolicy = new HtmlPolicyBuilder()
            .disallowElements("p")
            .toFactory();

    PolicyFactory afterPolicy = beforePolicy.and(newPolicy);
    assertEquals("<p>pText</p>", afterPolicy.sanitize(input));
  }
beforePolicy afterPolicy result
disallow X beforePolicy tag remove, afterPolicy tag remove no problem
disallow allow beforePolicy tag remove, afterPolicy tag alive(only added tags) no problem
disallow disallow beforePolicy tag remove, afterPolicy tag remove no problem

3. allowUrlProtocols disallowUrlProtocols

beforePolicy afterPolicy result
X X nothing allowed protocols no problem
X allow nothing allowed protocols I think that afterPolicy allowing http protocol is right
  // before : X
  // after : allow
  @Test
  public void testAllowUrlProtocol2() {
    PolicyFactory beforePolicy = new HtmlPolicyBuilder()
            .allowElements("a")
            .allowAttributes("href")
            .onElements("a")
            .toFactory();

    String input = "<a href='http://example.com'>Hi</a>";
    assertEquals("Hi", beforePolicy.sanitize(input));

    PolicyFactory newPolicy = new HtmlPolicyBuilder()
            .allowElements("a")
            .allowAttributes("href")
            .onElements("a")
            .allowUrlProtocols("http")
            .toFactory();

    PolicyFactory afterPolicy = beforePolicy.and(newPolicy);

    assertEquals("Hi", afterPolicy.sanitize(input));
  }
beforePolicy afterPolicy result
X disallow beforePolicy tag all remove, afterPolicy tag all remove no problem
allow X If I allow http protocol in beforePolicy and do nothing in newPolicy, afterPolicy result disallow http protocol In order to be consistent with the previous strategy, I think that afterPolicy allow http protocol is right
  // before : allow
  // after : X
  @Test
  public void testAllowUrlProtocol4() {
    PolicyFactory beforePolicy = new HtmlPolicyBuilder()
            .allowElements("a")
            .allowAttributes("href")
            .onElements("a")
            .allowUrlProtocols("http")
            .toFactory();

    String input = "<a href='http://example.com'>Hi</a>";
    assertEquals("<a href=\"http://example.com\">Hi</a>", beforePolicy.sanitize(input));

    PolicyFactory newPolicy = new HtmlPolicyBuilder()
            .allowElements("a")
            .allowAttributes("href")
            .onElements("a")
            .toFactory();

    PolicyFactory afterPolicy = beforePolicy.and(newPolicy);

    assertEquals("Hi", afterPolicy.sanitize(input));
}
beforePolicy afterPolicy result
allow allow If I allow http protocol in beforePolicy and allow https protocol in newPolicy, result afterPolicy is not allow http In order to be consistent with the previous strategy, I think that afterPolicy allow http protocol is right
  // before : allow
  // after : allow
  @Test
  public void testAllowUrlProtocol5_1() {
    PolicyFactory beforePolicy = new HtmlPolicyBuilder()
            .allowElements("a")
            .allowAttributes("href")
            .onElements("a")
            .allowUrlProtocols("http")
            .toFactory();

    String input = "<a href='http://example.com'>Hi</a>";
    assertEquals("<a href=\"http://example.com\">Hi</a>", beforePolicy.sanitize(input));

    PolicyFactory newPolicy = new HtmlPolicyBuilder()
            .allowElements("a")
            .allowAttributes("href")
            .onElements("a")
            .allowUrlProtocols("https")
            .toFactory();

    PolicyFactory afterPolicy = beforePolicy.and(newPolicy);

    assertEquals("Hi", afterPolicy.sanitize(input));
}
beforePolicy afterPolicy result
allow disallow beforePolicy(allow) and afterPolicy(disallow) no problem
disallow X beforePolicy(disallow) and afterPolicy(disallow) no problem
disallow allow If i disallow http protocol in beforePolicy and allow http protocol in newPolicy, result afterPolicy is not allow http I think that afterPolicy allow http protocol is right
  // before : disallow
  // after : allow
  @Test
  public void testAllowUrlProtocol8() {
    PolicyFactory beforePolicy = new HtmlPolicyBuilder()
            .allowElements("a")
            .allowAttributes("href")
            .onElements("a")
            .disallowUrlProtocols("http")
            .toFactory();

    String input = "<a href='https://example.com'>Hi</a>";
    assertEquals("Hi", beforePolicy.sanitize(input));

    PolicyFactory newPolicy = new HtmlPolicyBuilder()
            .allowElements("a")
            .allowAttributes("href")
            .onElements("a")
            .allowUrlProtocols("http")
            .toFactory();

    PolicyFactory afterPolicy = beforePolicy.and(newPolicy);

    assertEquals("Hi", afterPolicy.sanitize(input));
}
beforePolicy afterPolicy result
disallow disallow all disallow no problem

4. allowTextIn disallowTextIn

at first, I tried to organize it all at once, but it got complicated.
I'll organize it separately in the next issue.

5. allowAttributes disallowAttributes

at first, I tried to organize it all at once, but it got complicated.
I'll organize it separately in the next issue.

Etc

and I have an opinion(it's tiny). I want your feedback.

ElementAndAttributePolicies p = e.getValue();
ElementAndAttributePolicies q = f.policies.get(elName);
if (q != null) {
    p = p.and(q);
}

above code, we separate each policy using p, q variables.
then we send q variable in ElementAndAttributePolicies and method.
but in ElementAndAttributePolicies and(ElementAndAttributePolicies p)
parameter name is p. It keeps p and q confused.

ElementAndAttributePolicies and(ElementAndAttributePolicies p) {
    assert elementName.equals(p.elementName):
    elementName + " != " + p.elementName;
    ...
}

How about we change the naming ElementAndAttributePolicies p -> ElementAndAttributePolicies q?

this method only use in PolicyFactory.and()
if you have better idea, I'd love to hear.

Metadata

Metadata

Assignees

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