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

<span> elements get removed even when allowed by the policy #283

Open
kocakosm opened this issue Jun 6, 2023 · 2 comments
Open

<span> elements get removed even when allowed by the policy #283

kocakosm opened this issue Jun 6, 2023 · 2 comments

Comments

@kocakosm
Copy link

kocakosm commented Jun 6, 2023

Hi,

<span> elements get removed by the sanitizer even when they are allowed by the policy.

For instance I'd expect the following code :

Sanitizers.FORMATTING.sanitize("<span>Hi!</span>")

to return <span>Hi!</span> instead of Hi!.

The exact same behaviour can be observed with a custom policy :

new HtmlPolicyBuilder().allowElements("span").toFactory().sanitize("<span>Hi!</span>")

returns Hi! instead of <span>Hi!</span>.

Also, note that other HTML5 inline formatting elements (such as b, i, s, u, sup, sub, ins, del, strong, code, small and em) are not affected by this "bug".

Thanks for your help.

@kocakosm
Copy link
Author

kocakosm commented Jun 6, 2023

You can see this behaviour in this sample project.

@csware
Copy link
Contributor

csware commented Jan 29, 2024

Empty span is dropped, because it is part on DEFAULT_SKIP_IF_EMPTY.

You need to allow it using allowWithoutAttributes. cf.

public static final void testSpanTagFilter() {
PolicyFactory policy = new HtmlPolicyBuilder()
.allowElements("span")
.allowWithoutAttributes("span")
.toFactory();
String unsafeHtml = policy.sanitize(
"<span>test1</span>");
String safeHtml = policy.sanitize(unsafeHtml);
String expected =
"<span>test1</span>";
assertEquals(expected, safeHtml);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants