使用 Markdown 和 Markdown 时的正确操作顺序MySQL?
我希望我的用户能够用 Markdown 编写一篇文章,将其存储在 MySQL 数据库中(可以选择将来进行编辑),并向其他用户显示。
在实践中,这是我对其工作原理的理解:
输入
- 使用 Markdown 语法通过 HTML 表单
- 用户输入
$queryInput = mysql_real_escape_string($userInput);
- 将清理后的字符串插入数据库
OUTPUT
- 数据库中的查询字段
$output = Markdown($queryResult);
- 显示
$output
是吗?
PHP Markdown 是否不需要 htmlspecialchars
或 Pure HTML
?
谢谢!
I want my users to be able to write an article in Markdown, have it stored in the MySQL database (with the option to edit it in the future), and displayed for other users.
In practice, this is my understanding of how it works:
INPUT
- user input via HTML form using Markdown syntax
$queryInput = mysql_real_escape_string($userInput);
- insert sanitized string into database
OUTPUT
- query field from database
$output = Markdown($queryResult);
- display
$output
Is that it?
Does PHP Markdown preclude the need for htmlspecialchars
or Pure HTML
?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几周前我评估了 PHP 中 Markdown 的使用(顺便说一句,我决定不使用它)。我的想法:
每次渲染输出时运行 markdown 解析器可能不是一个好主意 - 解析评论非常昂贵,并且通常的博客评论(作为示例)的阅读次数远多于写入次数。您应该在将用户输入保存到数据库之前运行 markdown 解析器!
现在真正棘手的问题是:Markdown 本身不进行任何安全检查。所有xss攻击都顺利通过。如果您现在认为“没问题,我会在获取用户输入后立即 strip_tags”,请再想一想:markdown 在处理用户输入时很可能会创建包含 XSS 的标签。因此,您必须检查 Markdown 创建的 HTML 代码是否存在安全问题 - 这是一项非常艰巨的任务,而且很容易出错。 (这就是我没有使用它的原因 - 其好处与潜在成本没有很好的比例)
I evaluated the use of markdown in PHP some weeks ago (and decided not to use it, by the way). My thoughts:
It might not be a good idea to run the markdown parser each time the output is rendered - parsing the comment is quite expensive and the usual blog comment (as an example) is far more often read than written. You should run the markdown parser BEFORE saving the user input into the database!
Now the really tough problem: Markdown does not do any security checks by itself. All xss attacks are happily passed through. If you now think "no problem, I'll just strip_tags right after getting the user input", think again: it is quite possible that markdown creates the tags containing the XSS while processing the user input. So, you have to check the HTML code created by markdown for security problems - a very hard task which is very error prone. (That was the reason for not using it in my case - the benefit had no good ratio to the potential costs)