ruby-on-rails - 允许用户输入代码示例的最不危险的方法是什么?

标签 ruby-on-rails ruby security sql-injection

我正在实现一个 Rails 应用程序,用户可以在其中存储代码片段以供日后引用。我打算使用 Markdown 进行文本输入,并且可能会使用 wmd markdown editor . (正是 Stackoverflow 使用的那个。)

我有点担心人们在编辑框中输入代码的想法。据我所知,输入 SQL 可能会搞砸我的数据库,或者输入 JavaScript 可能会稍后运行并造成恶作剧。

通常情况下,Rails 具有防止这种情况发生的功能,但我是否处于特殊情况,因为我的用户会被鼓励输入代码片段?

我应该注意哪些额外的预防措施?

最佳答案

只需清理您的数据库条目即可。 Rails 现在默认这样做。您只需要正确使用框架即可。查看此以获取更多信息:http://wiki.rubyonrails.org/howtos/security/sql_injection

这样做:

Project.find(:all, :conditions => ["name = ?", params[:name]])
# or
Project.find(:all, :conditions => {:name => params[:name]})

不是这个:

Project.find(:all, :conditions => "name = '#{params[:name]}'")

你还必须通过以下方式防止 XSS 攻击

<%=h possible_harmful_text %>

供引用:

Cross-site scripting (XSS) is a type of computer security vulnerability typically found in web applications which allow code injection by malicious web users into the web pages viewed by other users. Examples of such code include client-side scripts. An exploited cross-site scripting vulnerability can be used by attackers to bypass access controls such as the same origin policy. Vulnerabilities of this kind have been exploited to craft powerful phishing attacks and browser exploits. Cross-site scripting carried out on websites were roughly 80% of all documented security vulnerabilities as of 2007. Often during an attack "everything looks fine" to the end-user who may be subject to unauthorized access, theft of sensitive data, and financial loss. (via wikipedia)

当然

SQL injection is a code injection technique that exploits a security vulnerability occurring in the database layer of an application. The vulnerability is present when user input is either incorrectly filtered for string literal escape characters embedded in SQL statements or user input is not strongly typed and thereby unexpectedly executed. It is an instance of a more general class of vulnerabilities that can occur whenever one programming or scripting language is embedded inside another. SQL injection attacks are also known as SQL insertion attacks.1 (via wikipedia)

关于ruby-on-rails - 允许用户输入代码示例的最不危险的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1081025/

相关文章:

asp.net-mvc - ASP.NET MVC ValidateAntiForgeryToken — 可以用授权检查和引用检查代替吗?

javascript - 将 JSON 数据存储在数据库中有多危险?

ruby-on-rails - SCSS 文件名

python - 当您还需要缩进换行时,如何使用正则表达式在文本中换行?

ruby-on-rails - 大多数现代 Smalltalks 的集成 IDE 最让您害怕的是什么?

ruby-on-rails - 返回记录后如何在 ActiveRecord 中加入?

ruby-on-rails - 执行 rhc 设置时出错 - 发生意外错误 : invalid character at "&lt;!doctype "

php - PHP 中的 CSRF 保护

ruby-on-rails - 确定您不是在寻找/faye?

ruby-on-rails - 如何为 Rails 中的集成测试编写帮助程序?