gpt4 book ai didi

ruby-on-rails - Brakeman 中的这个 "Unscoped call to"警告是什么?

转载 作者:太空宇宙 更新时间:2023-11-03 17:14:23 25 4
gpt4 key购买 nike

当我使用 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

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

最佳答案

Brakeman 只是警告您,您正在查询整个 Applicant 表,而不是将其范围限定在另一个模型下,例如 current_tenant.applicants.find...。来自 Brakeman's docs :

Unscoped find (and related methods) are a form of Direct Object Reference. Models which belong to another model should typically be accessed via a scoped query.

For example, if an Account belongs to a User, then this may be an unsafe unscoped find:

Account.find(params[:id])

Depending on the action, this could allow an attacker to access any account they wish.

Instead, it should be scoped to the currently logged-in user:

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

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

简而言之:

$ 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 时,不应出现此警告。

关于ruby-on-rails - Brakeman 中的这个 "Unscoped call to"警告是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40778760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com