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



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 "h4. What is it?\n\nAn 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.\n\nh4. When does it happen?\n\nSome trivial examples:\n\nbc. BankAccount.first(params[:id]).destroy\nAccount.all(:conditions=>{:nickname=>params[:nickname]})\n\nIn 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.\n\nHere are some things to consider when evaluating whether a find is safe:\n\n* Is authorization checked beforehand or afterward, e.g. by checking ownership of the model?\n* Do the query's conditions scope it in some way to the current user or account?\n* How will the results be used? What information is displayed in the view?\n* Are the results scoped afterward, e.g. by calling @select@ on the result set?\n\nh4. How do I fix it?\n\nUse named scopes to scope your queries instead of calling the class-level finders:\n\nbc. current_user.bank_accounts.first(params[:id])\n\nIf a named scope is not convenient, include conditions that scope the query:\n\nbc. BankAccount.find(:conditions=>{:owner_id=>current_user})\n\nIf your authorization rules are so complex that neither of those approaches work, always make sure to perform authorization yourself:\n\nbc. #My bank allows customers to access ANY account on their birthday\n@bank_account = BankAccount.first(params[:id])\nraise ActiveRecord::RecordNotFound unless current_user.born_on = Date.today\n"
end