使用 base64 图像清理 Html(并将其转换为图像)

Sanitize Html with base64 image (and convert it to an image)

我有一个带有 TinyMCE Html 编辑器的 WebApp,允许用户从网页输入一些 html。可以粘贴图像并将其编码为 base64。 在将用户输入保存到数据库之前,我使用 OWASP java-html-sanitizer 丢弃潜在的危险代码 (javascript,...)。

图像的 base64 字符串中的一些字符被转义,当我尝试取回图像时(使用 apache commons Base64)我无法获得有效图像。

这是我解码图像的代码:

byte[] b;
String s = html;
b = s.getBytes(Utility.UTF8);
b = org.apache.commons.codec.binary.Base64.decodeBase64(b);

对于Html消毒剂我没有做任何特别的事情,只是遵循Ebay Policy Example allowing base64 images as suggested here

嗯,建议here我需要"to HTML decode before base64 decoding"。

我已经尝试使用 apache common StringEscapeUtils:

org.apache.commons.lang3.StringEscapeUtils.unescapeHtml4(html);

它正在运行。太好了。

allowedSchemes: [ 'data']allowedSchemesByTag: { img: [ 'data' ]} 可用于允许 img 标记为 accept/allow base64。