让用户能够提交代码,在 php 中替换什么?
嗨,我计划让用户能够提交一些代码(php、java、javascript c++ 等......无论他们想要什么,我的意思是)。
那么有人可以建议我最佳实践以确保我的网站安全吗? :))
我的意思是一旦提交代码字符串,在 php 中转义哪些标签/字符/字符串?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的目的是在屏幕上显示代码,则在将其存储到数据库之前不需要转义或替换任何内容(如果您打算存储它)。当然,这不适用于通过诸如 mysql_real_escape_string() 之类的方法(或 RDBMS 的等效清理例程)转义数据库插入。这一步仍然是绝对必要的。
显示代码时,请确保:
您不要通过
eval()
或系统调用评估任何提交的代码。将代码显示回浏览器时,使用
htmlspecialchars()
。切勿在未转义的情况下显示它,否则您将引入跨站点脚本漏洞。If your intent is to display the code on screen, you do not need to escape or replace anything before storing it in your database (if you intend to store it) . This doesn't apply, of course, to escaping for database insertion via something like
mysql_real_escape_string()
, for example (or your RDBMS' equivalent sanitization routine). That step is still absolutely necessary.When displaying the code, just be sure that:
You DO NOT evaluate any submitted code via an
eval()
or system call.When displaying code back to the browser, escape it with
htmlspecialchars()
. Never display it unescaped, or you will introduce cross site scripting vulnerabilities.在查询中使用占位符,您甚至不必转义输入。
占位符、绑定和准备好的语句绝对是首选方法。
对于超过 1 个查询的任何内容都会更快,因为您可以重复使用句柄并只需更改输入。
这样更安全。该字符串不会被查询解释...永远。你存储什么,你就得到什么。
我需要更多地了解您的目标 sql 才能提供相关示例,但这里有一些链接:
PDO 样式绑定: http://docs.php.net/pdo.prepared-statements
MySqli 样式绑定:http://docs.php.net/manual/en/mysqli-stmt .bind-param.php
当您读回它时,使用
ENT_QUOTES 选项显示可确保单引号和双引号都被转义。
Use placeholders in your queries and you don't even have to escape the input.
Placeholders, binding, and prepared statements are definitely the preferred method.
It's faster for anything over 1 query as you can reuse the handles and just change the input.
It's safer. The string is not interpreted with the query... ever. What you store is what you get.
I'd need to know a bit more about your target sql to give pertinent examples, but here's some links:
PDO style binding: http://docs.php.net/pdo.prepared-statements
MySqli style binding: http://docs.php.net/manual/en/mysqli-stmt.bind-param.php
When you read it back, display with
ENT_QUOTES option ensures that both single and double quotes get escaped.
如果你不打算自动运行它,你不需要逃避任何东西(除了通常的 mysql 卫生)。
You don't need to escape anything (other then the usual mysql sanitation), if you don't intend to automatically run it.
我不是专家(昨天才得知这一点),但至少对于 HTML,您可以尝试使用 htmlentities(查看 这个 )。
一旦使用 htmlentities 转换某些内容,它就会变成纯文本,因此如果在浏览器中打开,您将看到标签和所有内容(例如,它将写入
),如果将其写入日志或其他内容,然后在基于文本的编辑器中打开,您将看到一些代表 html 实体的符号和shnaz。
如果您需要转换回来,我认为您可以使用 html_entity_decode 函数,但我将进行猜测并假设您不需要转换回来。
对于其他语言,我不知道你应该做什么。
I am no expert ( I only got told about this yesterday), but at least for HTML, you could try and use htmlentities (look at this ).
Once something has been converted using htmlentities, it becomes plain text, so if opened in a browser, you will see the tag and everything, (e.g. it will write
<a href="blah blah">
), if it's written to a log or something else, and then opened in a text based editor, you will some symbols and shnaz that represent the html entities.If you need to convert back, you can use the html_entity_decode function, I think, but I am going to wager a guess and presume that you don't need to convert back.
For other languages, I have no idea what you should do.