Class: Brakeman::IgnoreConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/brakeman/report/ignore/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, new_warnings) ⇒ IgnoreConfig

Returns a new instance of IgnoreConfig.



9
10
11
12
13
14
15
16
17
18
# File 'lib/brakeman/report/ignore/config.rb', line 9

def initialize file, new_warnings
  @file = file
  @new_warnings = new_warnings
  @already_ignored = []
  @ignored_fingerprints = Set.new
  @used_fingerprints = Set.new
  @notes = {}
  @shown_warnings = @ignored_warnings = nil
  @changed = false
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



7
8
9
# File 'lib/brakeman/report/ignore/config.rb', line 7

def file
  @file
end

#ignored_warningsObject (readonly)

Returns the value of attribute ignored_warnings.



6
7
8
# File 'lib/brakeman/report/ignore/config.rb', line 6

def ignored_warnings
  @ignored_warnings
end

#shown_warningsObject (readonly)

Returns the value of attribute shown_warnings.



6
7
8
# File 'lib/brakeman/report/ignore/config.rb', line 6

def shown_warnings
  @shown_warnings
end

Instance Method Details

#add_note(warning, note) ⇒ Object

Add note for warning



57
58
59
60
# File 'lib/brakeman/report/ignore/config.rb', line 57

def add_note warning, note
  @changed = true
  @notes[warning.fingerprint] = note
end

#filter_ignoredObject

Populate ignored_warnings and shown_warnings based on ignore configuration



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/brakeman/report/ignore/config.rb', line 22

def filter_ignored
  @shown_warnings = []
  @ignored_warnings = []

  @new_warnings.each do |w|
    if ignored? w
      @ignored_warnings << w
    else
      @shown_warnings << w
    end
  end

  @shown_warnings
end

#ignore(warning) ⇒ Object



51
52
53
54
# File 'lib/brakeman/report/ignore/config.rb', line 51

def ignore warning
  @changed = true unless ignored? warning
  @ignored_fingerprints << warning.fingerprint
end

#ignored?(warning) ⇒ Boolean

Determine if warning should be ignored

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/brakeman/report/ignore/config.rb', line 46

def ignored? warning
  @used_fingerprints << warning.fingerprint
  @ignored_fingerprints.include? warning.fingerprint
end

#note_for(warning) ⇒ Object

Retrieve note for warning if it exists. Returns nil if no note is found



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/brakeman/report/ignore/config.rb', line 64

def note_for warning
  if warning.is_a? Warning
    fingerprint = warning.fingerprint
  else
    fingerprint = warning[:fingerprint]
  end

  @already_ignored.each do |w|
    if fingerprint == w[:fingerprint]
      return w[:note]
    end
  end

  nil
end

#obsolete_fingerprintsObject

The set of unused ignore entries



81
82
83
# File 'lib/brakeman/report/ignore/config.rb', line 81

def obsolete_fingerprints
  (@ignored_fingerprints - @used_fingerprints).to_a
end

#prune_obsoleteObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/brakeman/report/ignore/config.rb', line 85

def prune_obsolete
  obsolete = obsolete_fingerprints.to_set
  @ignored_fingerprints -= obsolete

  @already_ignored.reject! do |w|
    if obsolete.include? w[:fingerprint]
      @changed = true
    end
  end
end

#read_from_file(file = @file) ⇒ Object

Read configuration to file



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/brakeman/report/ignore/config.rb', line 97

def read_from_file file = @file
  if File.exist? file
    @already_ignored = JSON.parse(File.read(file), :symbolize_names => true)[:ignored_warnings]
  else
    Brakeman.notify "[Notice] Could not find ignore configuration in #{file}"
    @already_ignored = []
  end

  @already_ignored.each do |w|
    @ignored_fingerprints << w[:fingerprint]
    @notes[w[:fingerprint]] = w[:note]
  end
end

#save_to_file(warnings, file = @file) ⇒ Object

Save configuration to file



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/brakeman/report/ignore/config.rb', line 112

def save_to_file warnings, file = @file
  warnings = warnings.map do |w|
    if w.is_a? Warning
      w_hash = w.to_hash
      w_hash[:file] = w.relative_path
      w = w_hash
    end

    w[:note] = @notes[w[:fingerprint]] || ""
    w
  end.sort_by { |w| w[:fingerprint] }

  output = {
    :ignored_warnings => warnings,
    :updated => Time.now.to_s,
    :brakeman_version => Brakeman::Version
  }

  File.open file, "w" do |f|
    f.puts JSON.pretty_generate(output)
  end
end

#save_with_oldObject

Save old ignored warnings and newly ignored ones



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/brakeman/report/ignore/config.rb', line 136

def save_with_old
  warnings = @ignored_warnings.dup

  # Only add ignored warnings not already ignored
  @already_ignored.each do |w|
    fingerprint = w[:fingerprint]

    unless @ignored_warnings.find { |ignored_warning| ignored_warning.fingerprint == fingerprint }
      warnings << w
    end
  end

  if @changed
    save_to_file warnings
  end
end

#unignore(warning) ⇒ Object

Remove warning from ignored list



38
39
40
41
42
43
# File 'lib/brakeman/report/ignore/config.rb', line 38

def unignore warning
  @ignored_fingerprints.delete warning.fingerprint
  if @already_ignored.reject! { |w|w[:fingerprint] == warning.fingerprint }
    @changed = true
  end
end