4

当我使用 Brakeman 的工具扫描我的代码时收到一条警告消息。它指出有对以下查询的Unscoped 调用:

@applicant = Applicant.find(params[:id])

这是实际的错误消息:

+------------+----------------------+---------+---------------+-----------------------------------------------------------------------------------------------------------------------------------------+
| Confidence | Class                | Method  | Warning Type  | Message                                                                                                                                 |
+------------+----------------------+---------+---------------+-----------------------------------------------------------------------------------------------------------------------------------------+
| Weak       | ApplicantsController | show    | Unscoped Find | Unscoped call to Applicant#find near line 25: Applicant.find(+params[:id]+)                                                             |                                                       |
+------------+----------------------+---------+---------------+-----------------------------------------------------------------------------------------------------------------------------------------+

但是,当我用以下查询替换上述查询时,就可以了:

@applicant = Applicant.where("id = ?", params[:id]).first

我不明白第一个查询有什么问题。

4

1 回答 1

12

Brakeman 只是警告您,您正在查询整个申请人表,而不是在另一个模型下确定它的范围,例如current_tenant.applicants.find.... 来自Brakeman 的文档

无范围查找(和相关方法)是直接对象引用的一种形式。属于另一个模型的模型通常应该通过范围查询来访问。

例如,如果一个 Account 属于一个 User,那么这可能是一个不安全的 unscoped find:

Account.find(params[:id])

根据操作,这可能允许攻击者访问他们希望访问的任何帐户。

相反,它的作用域应该是当前登录的用户:

current_user = User.find(session[:user_id])
current_user.accounts.find(params[:id])

如果这是您想要的行为,您可以将 Brakeman 配置为忽略此警告作为误报。为此,请brakeman使用-I标志(或--interactive-ignore)运行。按照忽略误报的说明逐步完成所有警告,并将这个特定的警告添加到您的忽略文件中。

简而言之:

$ brakeman -I
Input file: |config/brakeman.ignore| 
# press Enter to accept the default ignore file
No such file. Continue with empty config? 
# press Enter to create the file
> 
1. Inspect all warnings
2. Hide previously ignored warnings
3. Skip - use current ignore configuration
# press 2 to step through all warnings, skipping previously ignored 
# Brakeman will now step through each warning, prompting you to for each one. 
# Press i to add this warning to the ignore list. 
# When finished, Brakeman will ask you what to do. 
# Press 1 to save changes to the ignore file. 

下次运行 Brakeman 时,不应出现此警告。

于 2016-11-24T05:56:02.687 回答