/
ignore.rb
304 lines (238 loc) · 8.12 KB
/
ignore.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
require_relative '../test'
require 'brakeman/report/ignore/config'
require 'tempfile'
class IgnoreConfigTests < Minitest::Test
attr_reader :config
def setup
@config_json = JSON.parse(IGNORE_JSON, symbolize_names: true)
@config_file = Tempfile.new("brakeman.ignore")
@config_file.write IGNORE_JSON
@config_file.close
@config = make_config
end
def make_config file = @config_file.path
c = Brakeman::IgnoreConfig.new file, report.warnings
c.read_from_file
c.filter_ignored
c
end
def teardown
@config_file.unlink
end
def report
@@report ||= Brakeman.run(File.join(TEST_PATH, "apps", "rails5.2"))
end
def test_sanity
assert_equal @config_json[:ignored_warnings].length, config.ignored_warnings.length
end
def test_ignored_warnings
assert_equal 3, config.ignored_warnings.length
end
def test_shown_warnings
expected = report.warnings.length - config.ignored_warnings.length
assert_equal expected, config.shown_warnings.length
end
def test_unignore_warning
original_ignored = config.ignored_warnings.dup
original_shown = config.shown_warnings.dup
first_ignored = config.ignored_warnings.first
config.unignore first_ignored
config.filter_ignored
refute config.ignored? first_ignored
assert config.ignored_warnings.length < original_ignored.length
assert config.shown_warnings.length > original_shown.length
refute_includes config.ignored_warnings, first_ignored
assert_includes config.shown_warnings, first_ignored
end
def test_ignore_warning
original_ignored = config.ignored_warnings.dup
original_shown = config.shown_warnings.dup
first_warning = config.shown_warnings.first
config.ignore first_warning
config.filter_ignored
assert config.ignored? first_warning
assert config.ignored_warnings.length > original_ignored.length
assert config.shown_warnings.length < original_shown.length
assert_includes config.ignored_warnings, first_warning
refute_includes config.shown_warnings, first_warning
end
def test_add_note
warning = config.ignored_warnings.first
note = "Here is an updated note for an ignored warning"
config.add_note warning, note
config.save_with_old
new_config = make_config
assert note, new_config.note_for(warning)
end
def test_note_for_warning
warning = config.ignored_warnings.find { |w| w.fingerprint == "97ecaa5677c8eadaed09217a704e59092921fab24cc751e05dfb7b167beda2cf" }
note = config.note_for warning
refute note.empty?
end
def test_note_for_hash
warning = { fingerprint: "97ecaa5677c8eadaed09217a704e59092921fab24cc751e05dfb7b167beda2cf" }
note = config.note_for warning
refute note.empty?
end
def test_empty_note
warning = { fingerprint: "3bc375c9cb79d8bcd9e7f1c09a574fa3deeab17f924cf20455cbd4c15e9c66eb" }
note = config.note_for warning
assert_equal "", note
end
def test_note_missing_for_warning
warning = config.shown_warnings.first
note = config.note_for warning
assert_nil note
end
def test_note_missing_for_hash
warning = { fingerprint: "not_real" }
note = config.note_for warning
assert_nil note
end
def test_obsolete
first_ignored = config.ignored_warnings.first
known_warnings = config.instance_variable_get(:@new_warnings)
known_warnings.delete first_ignored
config.filter_ignored
assert_equal 1, config.obsolete_fingerprints.length
end
def test_prune_obsolete
first_ignored = config.ignored_warnings.first
known_warnings = config.instance_variable_get(:@new_warnings)
known_warnings.delete first_ignored
config.filter_ignored
assert_includes config.obsolete_fingerprints, first_ignored.fingerprint
config.prune_obsolete
refute_includes config.ignored_warnings, first_ignored
config.save_with_old
new_config = make_config
refute_includes new_config.ignored_warnings, first_ignored
end
def test_read_from_nonexistent_file
make_config("/tmp/not_a_real_file_brakeman.ignore")
end
def test_save_new_ignored
first_ignored = config.ignored_warnings.first
known_warnings = config.instance_variable_get(:@new_warnings)
known_warnings.delete first_ignored
config.filter_ignored
config.save_with_old
new_config = make_config
assert new_config.ignored? first_ignored
end
def test_bad_ignore_json_error_message
file = Tempfile.new("brakeman.ignore2")
file.write "{[ This is bad json cuz I don't have a closing square bracket, bwahahaha...}"
file.close
begin
c = Brakeman::IgnoreConfig.new file.path, report.warnings
c.read_from_file
rescue => e
# The message should clearly show that there was a problem parsing the json
assert e.message.index("JSON::ParserError") > 0
# The message should clearly reference the file containing the bad json
assert e.message.index(file.path) > 0
end
end
def test_relative_paths_everywhere
require 'pathname'
config.shown_warnings.each do |w|
config.ignore w
end
config.filter_ignored
config.save_with_old
JSON.parse(File.read(config.file), symbolize_names: true)[:ignored_warnings].each do |w|
assert_relative w[:file]
if w[:render_path]
w[:render_path].each do |loc|
assert_relative loc[:file]
if loc[:rendered]
assert_relative loc[:rendered][:file]
end
end
end
end
end
def test_already_ignored_entries_with_empty_notes
require 'set'
assert_equal(
config.already_ignored_entries_with_empty_notes.map { |i| i[:fingerprint] }.to_set,
[
'3bc375c9cb79d8bcd9e7f1c09a574fa3deeab17f924cf20455cbd4c15e9c66eb',
'006ac5fe3834bf2e73ee51b67eb111066f618be46e391d493c541ea2a906a82f',
].to_set
)
end
private
def assert_relative path
assert Pathname.new(path).relative?, "#{path} is not relative"
end
end
IGNORE_JSON = <<JSON
{
"ignored_warnings": [
{
"warning_type": "Remote Code Execution",
"warning_code": 25,
"fingerprint": "006ac5fe3834bf2e73ee51b67eb111066f618be46e391d493c541ea2a906a82f",
"check_name": "Deserialize",
"message": "`Oj.load` called with parameter value",
"file": "app/controllers/users_controller.rb",
"line": 52,
"link": "https://brakemanscanner.org/docs/warning_types/unsafe_deserialization",
"code": "Oj.load(params[:json], :mode => :object)",
"render_path": null,
"location": {
"type": "method",
"class": "UsersController",
"method": "some_api"
},
"user_input": "params[:json]",
"confidence": "High",
"note": ""
},
{
"warning_type": "Remote Code Execution",
"warning_code": 25,
"fingerprint": "3bc375c9cb79d8bcd9e7f1c09a574fa3deeab17f924cf20455cbd4c15e9c66eb",
"check_name": "Deserialize",
"message": "`Oj.object_load` called with parameter value",
"file": "app/controllers/users_controller.rb",
"line": 53,
"link": "https://brakemanscanner.org/docs/warning_types/unsafe_deserialization",
"code": "Oj.object_load(params[:json], :mode => :strict)",
"render_path": null,
"location": {
"type": "method",
"class": "UsersController",
"method": "some_api"
},
"user_input": "params[:json]",
"confidence": "High",
"note": ""
},
{
"warning_type": "Remote Code Execution",
"warning_code": 25,
"fingerprint": "97ecaa5677c8eadaed09217a704e59092921fab24cc751e05dfb7b167beda2cf",
"check_name": "Deserialize",
"message": "`Oj.load` called with parameter value",
"file": "app/controllers/users_controller.rb",
"line": 51,
"link": "https://brakemanscanner.org/docs/warning_types/unsafe_deserialization",
"code": "Oj.load(params[:json])",
"render_path": null,
"location": {
"type": "method",
"class": "UsersController",
"method": "some_api"
},
"user_input": "params[:json]",
"confidence": "High",
"note": "Here's a note!"
}
],
"updated": "2019-04-02 12:15:05 -0700",
"brakeman_version": "4.5.0"
}
JSON