forked from semgrep/semgrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semgrep.yml
421 lines (391 loc) · 12.5 KB
/
semgrep.yml
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# This file contains Semgrep rules. See https://semgrep.dev for more info.
#
# You can use this file locally either with:
# - docker run --rm -v "${PWD}:/home/repo" returntocorp/semgrep:develop --config semgrep.yml
# or if you have already installed semgrep:
# - semgrep --config semgrep.yml .
#
# This file is also used in CI, see .circleci/config.yml
#
# Put semgrep-specific rules here.
# Put general OCaml or Python rules in the semgrep-rules repository
# under ocaml/ or python/.
rules:
- id: no-print-in-semgrep
match:
all:
- any:
- UCommon.pr ...
- pr ...
- print_string ...
- not:
inside: |
if !Flag.debug
then ...
- not:
inside: |
let $F ... =
...
[@@action]
message: you should not print anything on stdout as it may interfere with
the JSON output we must return to pysemgrep from semgrep-core, or it may
interfere with the output of osemgrep.
Use Logs.debug() for debugging, UConsole.print() for actual valid output
in semgrep-core, or Logs.app() and CapConsole.print() in osemgrep.
languages: [ocaml]
severity: ERROR
paths:
exclude:
- Test.ml
- Check_*.ml
- Unit_*.ml
- Test_*.ml
- libs/*
- languages/*
- scripts/*
- tools/*
- id: use-state-for-global-settings
pattern: global $VAR
message: |
Instead of setting global variables,
keep your variables around on the semgrep.state.SemgrepState class.
You'll then be able to access this anywhere with:
from semgrep.state import get_state
$VAR = get_state().$VAR
languages: [python]
severity: ERROR
- id: not-using-our-pcre-wrappers
patterns:
- pattern-either:
- pattern: $PCRE.regexp
- pattern: $PCRE.pmatch
- pattern: $PCRE.exec
- pattern: $PCRE.exec_all
- pattern: $PCRE.split
- metavariable-pattern:
metavariable: $PCRE
pattern-either:
- pattern: Pcre
- pattern: Pcre2
message: >-
You should use one of the equivalent functions in Pcre2_ (or
Pcre_), which automatically sets some flags and handles
exceptions.
languages: [ocaml]
severity: ERROR
paths:
exclude:
- Pcre_.ml
- Pcre2_.ml
- id: no-list-map2
pattern: List.map2
message: >-
`List.map2` creates O(N) stack depth, and can lead to a
stack overflow. Use `List_.map2` instead.
fix: List_.map2
languages: [ocaml]
severity: ERROR
paths:
include:
- src/*
exclude:
- profiling/*
- ppx_profiling/*
- commons/*
- graph_code/*
- lib_parsing/*
- id: no-list-map
pattern: List.map
message: >-
`List.map` creates O(N) stack depth, and can lead to a
stack overflow. Use `List_.map` instead.
fix: List_.map
languages: [ocaml]
severity: ERROR
paths:
include:
- src/*
exclude:
- profiling/*
- ppx_profiling/*
- commons/*
- graph_code/*
- lib_parsing/*
- id: no-list-filter-map
pattern: List.filter_map
message: >-
`List.filter_map` creates O(N) stack depth, and can lead to a
stack overflow. Use `List_.map_filter` instead.
fix: List_.map_filter
languages: [ocaml]
severity: ERROR
paths:
include:
- src/*
exclude:
- profiling/*
- ppx_profiling/*
- commons/*
- graph_code/*
- lib_parsing/*
- id: no-list-mapi
pattern: List.mapi
message: >-
`List.mapi` creates O(N) stack depth, and can lead to a
stack overflow. Use `List_.mapi` instead.
fix: List_.mapi
languages: [ocaml]
severity: ERROR
paths:
include:
- src/*
exclude:
- profiling/*
- ppx_profiling/*
- commons/*
- graph_code/*
- lib_parsing/*
- id: no-list-fold-right
pattern: List.fold_right
fix: List_.fold_right
message: >-
`List.fold_right` creates O(N) stack depth, and can lead to a
stack overflow. Use `List_.fold_right` instead.
languages: [ocaml]
severity: ERROR
- id: use-concat-map
pattern-either:
- pattern: List.map ... |> List.flatten
- pattern: List_.map ... |> List.flatten
- pattern: List.map ... |> List.concat
- pattern: List_.map ... |> List.concat
- pattern: ... |> List.map ... |> List.flatten
- pattern: ... |> List_.map ... |> List.flatten
- pattern: ... |> List.map ... |> List.concat
- pattern: ... |> List_.map ... |> List.concat
- pattern: List.flatten ( List.map ... )
- pattern: List.flatten ( List_.map ... )
- pattern: List.concat ( List.map ... )
- pattern: List.concat ( List_.map ... )
message: >-
`List.concat_map` is more efficient and more readable than a `map` followed
by `concat`.
languages: [ocaml]
severity: ERROR
paths:
include:
- src/*
- id: safe-ast-equality
patterns:
- pattern: AST_generic.$X
- metavariable-regex:
metavariable: $X
regex: equal.*
- pattern-not-inside: AST_generic_equals.with_structural_equal ...
- pattern-not-inside: AST_generic_equals.with_syntactic_equal ...
# These are safe because those nodes can't contain statements within them
- pattern-not: AST_generic.equal_literal
- pattern-not: AST_generic.equal_resolved_name
message: >-
The autogenerated AST_generic equality functions must be wrapped in either
`AST_generic_equals.with_structural_equal` or `AST_generic_equals.with_syntactic_equal`.
Otherwise, they will raise if any statements are compared. See
`AST_generic_equals.ml` for more information.
languages: [ocaml]
severity: ERROR
paths:
include:
- src/*
- id: no-exit-code-1-in-semgrep
pattern: sys.exit(1)
fix: sys.exit(2)
message: >-
Exit code 1 is reserved for notifying users that blocking findings were found.
Please use a different exit code, or better yet, a SemgrepError exception.
For generic fatal errors, we use exit code 2.
languages: [python]
severity: ERROR
paths:
include:
- cli/*
- id: no-env-vars-on-top-level
patterns:
- pattern-either:
- pattern: os.getenv
- pattern: os.environ
- pattern-not-inside: "def $F(...): ..."
message: >-
If you access environment variables on the top level of a module,
it'll be near impossible to mock the value of that variable in tests.
Please make sure to only access environment variables in functions,
preferably in semgrep.env.Env
languages: [python]
severity: ERROR
paths:
include:
- cli/src/*
- id: typehint-run-semgrep
patterns:
- focus-metavariable: $FIXTURE
- pattern-inside: |
def $FUNC(..., $FIXTURE, ...): ...
- pattern-not-inside: |
def $FUNC(..., $FIXTURE: tests.fixtures.RunSemgrep, ...): ...
- metavariable-regex:
metavariable: $FIXTURE
regex: ^run_semgrep.*$
message: >-
Please add a type hint for your use of $FIXTURE.
It should look like this: `$FIXTURE: RunSemgrep`,
and you should import it like this: `from tests.fixtures import RunSemgrep`.
languages: [python]
severity: ERROR
paths:
include:
- cli/tests/*
- id: use-git-check-output-helper
pattern-either:
- pattern: subprocess.$METHOD(["git", ...], ...)
- pattern: semgrep.util.sub_check_output(["git", ...], ...)
message: >-
We have a helper function git_check_output in meta.py that
handles printing nice error+debug messages on failure. Use
that instead of using subprocess
languages: [python]
severity: ERROR
paths:
include:
- cli/src/*
# Rules that used to be in pfff.yml and could be put in p/ocaml at some point
- id: bad-pervasives
pattern: Pervasives.$X
message: Pervasives is deprecated and not available after 4.10. Use Stdlib.
languages: [ocaml]
severity: ERROR
- id: physical-inequality
pattern: $X != $Y
message: You probably want structural inequality with <>
languages: [ocaml]
severity: WARNING
- id: stupid-equal
pattern: $X = $X
message: this will always be true
languages: [ocaml]
severity: ERROR
- id: length-vs-equal
pattern: List.length $X = 0
message: you probably want $X = [], which is faster
languages: [ocaml]
severity: WARNING
- id: not-portable-tmp
pattern: |
"=~/\/tmp/"
message: you should probably use Filename.get_temp_dirname().
languages: [ocaml]
severity: WARNING
#TODO: fix at some point
paths:
exclude:
- libs/commons2/common2.ml
- id: bad-changelog-extension
pattern-regex: .*
message: |
This file has an invalid extension. Please choose from one of the allowed
extensions: .added, .changed, .fixed, or .infra
languages: [generic]
severity: WARNING
paths:
include:
- changelog.d/*
exclude:
- changelog.d/.gitignore
- changelog.d/README
- changelog.d/gh-1234.example
- changelog.d/*.added
- changelog.d/*.changed
- changelog.d/*.fixed
- changelog.d/*.infra
- id: pass-console-to-rich-progress
patterns:
- pattern: rich.progress.track(...)
- pattern-not: rich.progress.track(..., console=semgrep.console.console)
message: >-
You need to pass our custom console to rich progress bars
so that it progress to the correct output stream (usually stderr).
languages: [python]
severity: ERROR
- id: list-hd
pattern: List.hd
message: >-
Don't use 'List.hd' because it raises an unhelpful exception if its
argument is an empty list. Prefer a match-with, possibly combined with
'assert false' e.g. 'match xs with [] -> assert false | x :: _ -> ...'.
fix: List_.hd_exn "unexpected empty list"
languages: [ocaml]
severity: WARNING
- id: list-tl
pattern: List.tl
message: >-
Don't use 'List.tl' because it raises an unhelpful exception if its
argument is an empty list. Prefer a match-with, possibly combined with
'assert false' e.g. 'match xs with [] -> assert false | _ :: xs -> ...'.
fix: List_.tl_exn "unexpected empty list"
languages: [ocaml]
severity: WARNING
- id: no-metavariable-equal-xyz
patterns:
- pattern: Metavariable.$F
- pattern-not: Metavariable.equal_mvar
- metavariable-regex:
metavariable: $F
regex: equal_.*
message: >-
Don't use 'Metavariable.$F' directly, see 'AST_generic_equals'. Instead use
either 'Metavariable.Structural.$F' or 'Metavariable.Referential.$F'.
You can also consider replacing `equal_mvalue` with
'Matching_generic.equal_ast_bound_code'.
fix: Metavariable.Structural.$F
languages: [ocaml]
severity: WARNING
- id: no-direct-assign-to-id_svalue
patterns:
- pattern-not-inside: |
if no_cycles_in_svalue ... then
...
# FIXME: This doesn't work: $X.id_svalue := Some $E
- pattern-regex: \s([a-z0-5_]+)\.id_svalue\s+:=\s+Some\s+([a-z0-5_]+)
message: >-
Dot not set '$1.id_svalue' directly, instead use
'Dataflow_svalue.set_svalue_ref' which performs a cycle check.
fix: Dataflow_svalue.set_svalue_ref $1 $2
languages: [ocaml]
severity: WARNING
- id: no-logger-flash
pattern-either:
- pattern-regex: "logger#flash"
- pattern-regex: "_logger#flash"
message: >-
logger#flash is just for debuggging purposes.
fix: ""
languages: [ocaml]
severity: WARNING
- id: no-match-x-x-with
pattern: match $X, $X with ... -> ...
message: >-
Pattern matching on ($X, $X) is pointless, you are comparing $X with itself.
You probably meant ($X, something-else) and this is due to a typo.
languages: [ocaml]
severity: WARNING
- id: no-fun-protect
pattern: Fun.protect
message: >-
`Fun.protect` does not block SIGALRM, which we use to implement timeouts (see
module 'Time_limit'). The alarm could trigger while we are executing a 'finally'
ths causing Semgrep to crash. Use `Common.protect` instead.
fix: Common.protect
languages: [ocaml]
severity: ERROR
paths:
include:
- src/*
- libs/*