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

fix(kernel xml): sanitize invalid chars before xml parse #1419

Merged
merged 1 commit into from
Dec 8, 2018

Conversation

scplay
Copy link
Contributor

@scplay scplay commented Dec 8, 2018

问题描述

之前有人提到 issue #989,偶尔在线上也发现无法解析 XML 的报错如下

production.ERROR: simplexml_load_string(): Entity: line 5: parser error : CData section not finished
ä {"exception":"[object] (ErrorException(code: 0): simplexml_load_string(): Entity: line 5: parser error : CData section not finished
ä at /project/vendor/overtrue/wechat/src/Kernel/Support/XML.php:32)

原因分析

经测试把 XML 内容解密发现有一个 \x10 字符,查 XML1.0 文档 表示这是一个非法字符,不能在 XML 中的任何位置出现(虽然在 XML1.1 中是合法字符,但由于 XML1.1 授受度较低,并且 libxml 没有支持 XML1.1

解决方案

  1. 可以在 simplexml_load_string 调用前,使用 libxml_use_internal_errors(true) 关闭抛错,这样遇到非法字符 simplexml_load_string 只会返回 false,而不是直接抛错
  2. 在 \Kernel\Support\XML 中新增 sanitize 方法,在 simplexml_load_string 调用前对非法字符进行过滤

@overtrue overtrue merged commit 777107e into w7corp:master Dec 8, 2018
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

Successfully merging this pull request may close these issues.

2 participants