Skip to content

feat(framework): Expose HTML sanitization utilities #3525

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

Merged
merged 3 commits into from
Jul 26, 2021
Merged

Conversation

vladitasev
Copy link
Contributor

@vladitasev vladitasev commented Jul 21, 2021

Changes:

  • The base package now integrates files from OpenUI5, similarly to the localization package
  • The 3 lib scripts that support this process have been moved from localization to tools so that they can be reused in base
  • The new HTMLSanitizer.js module exposes both sanitizeHTML and URLListValidator for public usage
  • The third-party caja-html-sanitizer.js file contains an octal escape sequence \240 which is not allowed in strict mode. This breaks all build tools. Therefore it needs to be replaced with the hexadecimal escape sequence \xA0 beforehand.

Sample usage:

import { sanitizeHTML, URLListValidator } from "@ui5/webcomponents-base/dist/util/HTMLSanitizer.js";
URLListValidator.add("http", "www.google.com"); // Add all allowed URLs
URLListValidator.add("https", "www.google.com"); // format is: protocol, host, port, path

sanitizeHTML(`
<div style="color: blue;">This is OK</div>
<script>alert(1);</script>
<a onlick="alert(2)" href="https://www.google.com">This link is OK</a>
<a href="http://my.site.com">This link is not</a>
`);

The result will be:

<div style="color: blue">This is OK</div>
<a href="https://www.google.com">This link is OK</a>
<a>This link is not</a>

Note: As long as you have set at least one URL via URLListValidator.add, all other URLs will be stripped. If you don't want any URLs to be stripped, not not call URLListValidator.add.

For the API Reference of URLListValidator click here

closes: #3427

@vladitasev
Copy link
Contributor Author

closes: #3427

@vladitasev vladitasev merged commit 3c2e659 into master Jul 26, 2021
@vladitasev vladitasev deleted the sanitize-html branch July 26, 2021 07:35
ilhan007 added a commit that referenced this pull request Jul 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HTML Control needed (to embed and sanitize third party HTML properly)
2 participants