Class: LintFu::Plugins::Rails::UnsafeFind

Inherits:
Issue
  • Object
show all
Defined in:
lib/lint_fu/plugins/rails/unsafe_find_checker.rb

Instance Attribute Summary

Attributes inherited from Issue

#confidence, #file, #sexp

Instance Method Summary collapse

Methods inherited from Issue

#brief, #file_basename, #issue_hash, #line, #relative_file

Constructor Details

#initialize(scan, file, sexp, subject) ⇒ UnsafeFind

Returns a new instance of UnsafeFind.



4
5
6
7
# File 'lib/lint_fu/plugins/rails/unsafe_find_checker.rb', line 4

def initialize(scan, file, sexp, subject)
  super(scan, file, sexp)
  @subject = subject
end

Instance Method Details

#detailObject



10
11
12
# File 'lib/lint_fu/plugins/rails/unsafe_find_checker.rb', line 10

def detail
  return "Could a bad guy manipulate <code>#{@subject}</code> and get/change stuff he shouldn't?"
end

#reference_infoObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lint_fu/plugins/rails/unsafe_find_checker.rb', line 14

def reference_info
  return <<EOF
h4. What is it?

An unsafe find is an ActiveRecord query that is performed without checking whether the logged-in user is authorized to view or manipulate the resulting models.

h4. When does it happen?

Some trivial examples:

bc. BankAccount.first(params[:id]).destroy
Account.all(:conditions=>{:nickname=>params[:nickname]})

In reality, it is often hard to determine whether a find is safe. Authorization can happen in many ways and the "right" way to do it depends on the application requirements.

Here are some things to consider when evaluating whether a find is safe:

* Is authorization checked beforehand or afterward, e.g. by checking ownership of the model?
* Do the query's conditions scope it in some way to the current user or account?
* How will the results be used? What information is displayed in the view?
* Are the results scoped afterward, e.g. by calling @select@ on the result set?

h4. How do I fix it?

Use named scopes to scope your queries instead of calling the class-level finders:

bc. current_user.bank_accounts.first(params[:id])

If a named scope is not convenient, include conditions that scope the query:

bc. BankAccount.find(:conditions=>{:owner_id=>current_user})

If your authorization rules are so complex that neither of those approaches work, always make sure to perform authorization yourself:

bc. #My bank allows customers to access ANY account on their birthday
@bank_account = BankAccount.first(params[:id])
raise ActiveRecord::RecordNotFound unless current_user.born_on = Date.today
EOF
end