-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsemgrep_output_v1.atd
2175 lines (1935 loc) · 73.4 KB
/
semgrep_output_v1.atd
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(* Specification of the Semgrep CLI JSON output formats using ATD
* (see https://atd.readthedocs.io/en/latest/ for information on ATD).
*
* This file specifies mainly the JSON formats of:
* - the output of the 'semgrep scan --json' command
* - the output of the 'semgrep test --json' command
* - the messages exchanged with the Semgrep backend by the
* 'semgrep ci' command
*
* It's also (ab)used to specify the JSON input and output of semgrep-core,
* some RPC between pysemgrep and semgrep-core, and a few more internal
* things. We should use separate .atd for those different purposes but
* ATD does not have a proper module system yet and many types are shared
* so it is simpler for now to have everything in one file.
*
* There are other important form of outputs which are not specified here:
* - The semgrep metrics sent to https://metrics.semgrep.dev in
* semgrep_metrics.atd
* - The parsing stats of semgrep-core -parsing_stats -json have its own
* Parsing_stats.atd
* - The schema for the generic AST dump is in AST_generic_v1.atd
* For the definition of the Semgrep input (the rules), see rule_schema_v2.atd
*
* This file has the _v1 suffix to explicitely represent the
* version of this JSON format. If you need to extend this file, please
* be careful because you may break consumers of this format (e.g., the
* Semgrep playground or Semgrep backend or external users of this JSON).
* See https://atd.readthedocs.io/en/latest/atdgen-tutorial.html#smooth-protocol-upgrades
* for more information on how to smoothly extend the types in this file.
*
* Any backward incompatible changes should require to upgrade the major
* version of Semgrep as this JSON output is part of the "API" of Semgrep
* (any incompatible changes to the rule format should also require a major
* version upgrade). Hopefully, we will always be backward compatible.
* However, a few fields are tagged with [EXPERIMENTAL] meaning external users
* should not rely on them as those fields may be changed or removed.
* They are not part of the "API" of Semgrep.
*
* Again, keep in mind that this file is used both by the CLI to *produce* a
* JSON output, and by our backends to *consume* the JSON, including to
* consume the JSON produced by old versions of the CLI. As of Nov 2024,
* our backend is still supporting as far as Semgrep 1.50.0 released Nov 2023.
* (see server/semgrep_app/util/cli_version_support.py in the semgrep-app repo)
*
*
* This file is translated in OCaml modules by atdgen. Look for the
* corresponding Semgrep_output_v1_[tj].ml[i] generated files
* under dune's _build/ folder. A few types below have the 'deriving show'
* decorator because those types are reused in semgrep core data structures
* and we make heavy use of 'deriving show' in OCaml to help debug things.
*
* This file is also translated in Python modules by atdpy.
* For Python, a few types have the 'dataclass(frozen=True)' decorator
* so that the class can be hashed and put in set. Indeed, with 'Frozen=True'
* the class is immutable and dataclass can autogenerate a hash function for it.
*
* Finally this file is translated in jsonschema/openapi spec by atdcat, and
* in Typescript modules by atdts.
*
* history:
* - the types in this file were originally inferred from JSON_report.ml for
* use by spacegrep when it was separate from semgrep-core. It's now also
* useds in JSON_report.ml (now called Core_json_output.ml)
* - it was extended to not only support semgrep-core JSON output but also
* (py)semgrep CLI output!
* - it was then simplified with the osemgrep migration effort by
* removing gradually the semgrep-core JSON output.
* - it was extended to support 'semgrep ci' output to type most messages
* sent between the Semgrep CLI and the Semgrep backend
* - we use this file to specify RPCs between pysemgrep and semgrep-core
* for the gradual migration effort of osemgrep
* - merged what was in Input_to_core.atd here
*)
(* escape hatch *)
type raw_json <ocaml module="Yojson.Basic" t="t"> = abstract
(*****************************************************************************)
(* String aliases *)
(*****************************************************************************)
(* File path.
* less: could convert directly to Path class of pathlib library for Python
* See libs/commons/ATD_string_wrap.ml for more info on those ATD_string_wrap.
*)
type fpath
<ocaml attr="deriving show, eq">
<python decorator="dataclass(frozen=True)"> =
string wrap <ocaml module="ATD_string_wrap.Fpath">
type uri = string wrap <ocaml module="ATD_string_wrap.Uri">
type sha1 = string wrap <ocaml module="ATD_string_wrap.Sha1">
type uuid = string wrap <ocaml module="ATD_string_wrap.Uuidm">
(* RFC 3339 format *)
type datetime = string wrap <ocaml module="ATD_string_wrap.Datetime">
type glob = string
(*****************************************************************************)
(* Versioning *)
(*****************************************************************************)
type version <ocaml attr="deriving show"> = string (* e.g., "1.1.0" *)
(*****************************************************************************)
(* Location *)
(*****************************************************************************)
(* Note that there is no filename here like in 'location' below *)
type position
<ocaml attr="deriving show">
<python decorator="dataclass(frozen=True, order=True)"> =
{
line: int; (* starts from 1 *)
col: int; (* starts from 1 *)
(* Byte position from the beginning of the file, starts at 0.
* OCaml code sets it correctly. Python code sets it to a dummy value (-1).
* This uses '~' because pysemgrep < 1.30? was *producing* positions without
* offset sometimes, and we want the backend to still *consume* such positions.
* Note that pysemgrep 1.97 was still producing dummy positions without
* an offset so we might need this ~offset longer than expected?
*)
~offset: int;
}
(* a.k.a range *)
type location
<ocaml attr="deriving show">
<python decorator="dataclass(frozen=True)"> =
{
path: fpath;
start: position;
end <ocaml name="end_">: position;
}
(*****************************************************************************)
(* Simple semgrep types *)
(*****************************************************************************)
(* e.g., "javascript.security.do-not-use-eval" *)
type rule_id
<ocaml attr="deriving show">
<python decorator="dataclass(frozen=True)"> =
string wrap <ocaml module="Rule_ID">
(*
This is used in rules to specify the severity of matches/findings.
alt: could be called rule_severity, or finding_severity.
Error = something wrong that must be fixed
Warning = something wrong that should be fixed
Info = some special condition worth knowing about
Experiment = deprecated: guess what
Inventory = deprecated: was used for the Code Asset Inventory (CAI) project
coupling: with 'severity' in 'rule_schema_v1.yaml'
coupling: with 'severity' in 'rule_schema_v2.atd'
*)
type match_severity
<ocaml attr="deriving show, eq">
<python decorator="dataclass(frozen=True)"> =
[
| Error <json name="ERROR">
| Warning <json name="WARNING">
| Experiment <json name="EXPERIMENT">
| Inventory <json name="INVENTORY">
(* since 1.72.0, meant to replace the cases above where
* Error -> High, Warning -> Medium. Critical/Low are the only really
* new category here without equivalent before.
* Experiment and Inventory above should be removed. Info can be kept.
*)
| Critical <json name="CRITICAL">
| High <json name="HIGH">
| Medium <json name="MEDIUM">
| Low <json name="LOW">
(* generic placeholder for non-risky things (including experiments) *)
| Info <json name="INFO">
]
(*
This is used to specify the severity of errors which
happened during Semgrep execution (e.g., a parse error).
Error = Always an error
Warning = Only an error if "strict" is set
Info = Nothing may be wrong
alt: could reuse match_severity but seems cleaner to define its own type
*)
type error_severity
<ocaml attr="deriving show, eq">
<python decorator="dataclass(frozen=True)"> =
[
| Error <json name="error">
| Warning <json name="warn">
| Info <json name="info">
]
(* Used for a best-effort report to users about what findings they get with
the pro engine that they couldn't with the oss engine.
Interproc_taint = requires interprocedural taint
Interfile_taint = requires interfile taint
Other_pro_feature = requires some non-taint pro feature *)
type pro_feature
<ocaml attr="deriving show">
<python decorator="dataclass(frozen=True)"> =
{
interproc_taint: bool;
interfile_taint: bool;
proprietary_language: bool;
}
(* Report the engine used to detect each finding. Additionally, if we are able
to infer that the finding could only be detected using the pro engine,
report that the pro engine is required and include basic information about
which feature is required.
OSS = ran with OSS
PRO = ran with PRO, but we didn't infer that OSS couldn't have found this
finding
PRO_REQUIRED = ran with PRO and requires a PRO feature (see pro_feature_used)
Note: OSS and PRO could have clearer names, but for backwards compatibility
we're leaving them as is
*)
type engine_of_finding
<ocaml attr="deriving show">
<python decorator="dataclass(frozen=True)"> =
[
| OSS
| PRO
(* Semgrep 1.64.0 or later *)
| PRO_REQUIRED of pro_feature
]
type engine_kind
<ocaml attr="deriving show">
<python decorator="dataclass(frozen=True)"> =
[
| OSS
| PRO
]
type rule_id_and_engine_kind <python decorator="dataclass(frozen=True)"> =
(rule_id * engine_kind)
type product
<ocaml attr="deriving show, eq">
<python decorator="dataclass(frozen=True)"> =
[
| SAST (* a.k.a. Code *) <json name="sast">
| SCA <json name="sca">
| Secrets <json name="secrets">
]
type match_based_id <ocaml attr="deriving show, eq"> = string (* ex:"ab023_1"*)
(*****************************************************************************)
(* Matches *)
(*****************************************************************************)
type cli_match = {
check_id: rule_id;
inherit location;
extra: cli_match_extra;
}
type cli_match_extra = {
(* Since 1.98.0, you need to be logged in to get this field.
* note: we also need ?metavars because dependency_aware code *)
?metavars: metavars option;
(* Those fields are derived from the rule but the metavariables
* they contain have been expanded to their concrete value. *)
message: string;
(* If present, semgrep was able to compute a string that should be
* inserted in place of the text in the matched range in order to fix the
* finding. Note that this is the result of applying both the fix: or
* fix_regex: in a rule. *)
?fix: string option;
(* TODO: done with monkey patching right now in the Python code,
* and seems to be used only when sending findings to the backend. *)
?fixed_lines: string list option;
(* fields coming from the rule *)
metadata: raw_json;
severity: match_severity;
(* Since 1.98.0, you need to be logged in to get those fields *)
fingerprint: string;
lines: string;
(* for nosemgrep *)
?is_ignored: bool option;
(* EXPERIMENTAL: added by dependency_aware code *)
?sca_info: sca_match option;
(* EXPERIMENTAL: If present indicates the status of postprocessor validation.
* This field not being present should be equivalent to No_validator.
* Added in semgrep 1.37.0 *)
?validation_state: validation_state option;
(* EXPERIMENTAL: added by secrets post-processing & historical scanning code
* Since 1.60.0. *)
?historical_info: historical_info option;
(* EXPERIMENTAL: For now, present only for taint findings. May be extended to
* otherslater on. *)
?dataflow_trace: match_dataflow_trace option;
?engine_kind: engine_of_finding option;
(* EXPERIMENTAL: see core_match_extra.extra_extra *)
?extra_extra: raw_json option;
}
(*****************************************************************************)
(* Metavariables *)
(*****************************************************************************)
(* Name/value map of the matched metavariables.
* The leading '$' must be included in the metavariable name.
*)
type metavars = (string * metavar_value) list
<json repr="object"> <python repr="dict"> <ts repr="map">
(* TODO: should just inherit location. Maybe it was optimized to not contain
* the filename, which might be redundant with the information in core_match,
* but with deep-semgrep a metavar could also refer to code in another file,
* so simpler to generalize and 'inherit location'.
*)
type metavar_value <python decorator="dataclass(frozen=True)"> = {
(* for certain metavariable like $...ARGS, 'end' may be equal to 'start'
* to represent an empty metavariable value. The rest of the Python
* code (message metavariable substitution and autofix) works
* without change for empty ranges (when end = start).
*)
start: position;
end <ocaml name="end_">: position;
abstract_content: string; (* value? *)
?propagated_value: svalue_value option;
}
type svalue_value <python decorator="dataclass(frozen=True)"> = {
?svalue_start: position option;
?svalue_end: position option;
svalue_abstract_content: string; (* value? *)
}
(*****************************************************************************)
(* Matching explanations *)
(*****************************************************************************)
(* coupling: semgrep-core/src/core/Matching_explanation.ml
* LATER: merge with Matching_explanation.t at some point
* EXPERIMENTAL *)
type matching_explanation = {
op: matching_operation;
children: matching_explanation list;
(* result matches at this node (can be empty when we reach a nomatch) *)
matches: core_match list;
(* location in the rule file! not target file.
* This tries to delimit the part of the rule relevant to the current
* operation (e.g., the position of the 'patterns:' token in the rule
* for the And operation).
*)
loc: location;
(* NEW: since v1.79 *)
?extra: matching_explanation_extra option;
}
(* For any "extra" information that we cannot fit at the node itself.
This is useful for kind-specific information, which we cannot put
in the operation itself without giving up our ability to derive `show`
(needed for `matching_operation` below).
*)
type matching_explanation_extra = {
(* only present in And kind *)
(* this information is useful for determining the input matches
to the first Negation node
*)
before_negation_matches: core_match list option;
(* only present in nodes which have children Filter nodes *)
(* this information is useful for determining the input matches
to the first Filter node, as there is otherwise no way of
obtaining the post-intersection matches in an And node, for instance
*)
before_filter_matches: core_match list option;
}
(* TODO:
* - Negation
* - Where filters (metavar-comparison, etc)
* - tainting source/sink/sanitizer
* - subpattern EllipsisAndStmt, ClassHeaderAndElems
* Note that this type is used in Matching_explanation.ml hence the need
* for deriving show below.
*)
type matching_operation
<ocaml attr="deriving show { with_path = false}"> =
[
| And
| Or
| Inside
| Anywhere
(* XPat for eXtended pattern. Can be a spacegrep pattern, a
* regexp pattern, or a proper semgrep pattern.
* see semgrep-core/src/core/XPattern.ml
*)
| XPat of string
(* TODO *)
| Negation
(* TODO "metavar-regex:xxx" | "metavar-comparison:xxx" | "metavar-pattern" *)
| Filter of string
(* TODO tainting "operations" *)
| Taint
| TaintSource
| TaintSink
| TaintSanitizer
(* TODO subpatterns *)
| EllipsisAndStmts
| ClassHeaderAndElems
] <ocaml repr="classic">
(*****************************************************************************)
(* Match dataflow trace *)
(*****************************************************************************)
(* EXPERIMENTAL *)
(* It's easier to understand the dataflow trace data structures on a simple
* example. Here is one simple Python target file:
*
* 1: def foo():
* 2: return source()
* 3:
* 4: def bar(v):
* 5: sink(v)
* 6:
* 7: x = foo()
* 8: y = x
* 9: bar(y)
*
* and here is roughly the generated match_dataflow_trace assuming
* a Semgrep rule where source() is a taint source and sink() the taint sink:
*
* taint_source = CliCall("foo() @l7", [], CliLoc "source() @l2")
* intermediate_vars = ["x", "y"]
* taint_sink = CliCall("bar()" @l9, ["v"], CliLoc "sink(v) @l5")
*)
type match_dataflow_trace <python decorator="dataclass(frozen=True)"> = {
?taint_source: match_call_trace option;
(* Intermediate variables which are involved in the dataflow. This
* explains how the taint flows from the source to the sink. *)
?intermediate_vars: match_intermediate_var list option;
?taint_sink: match_call_trace option;
}
(* The string attached to the location is the actual code from the file.
* This can contain sensitive information so be careful!
*
* TODO: the type seems redundant since location already specifies a range.
* maybe this saves some effort to the user of this type which do not
* need to read the file to get the content.
*)
type loc_and_content = (location * string)
type match_call_trace <python decorator="dataclass(frozen=True, order=True)"> =
[
| CliLoc of loc_and_content
| CliCall of (loc_and_content * match_intermediate_var list * match_call_trace)
] <ocaml repr="classic">
(* This type happens to be mostly the same as a loc_and_content for now, but
* it's split out because Iago has plans to extend this with more information
*)
type match_intermediate_var <python decorator="dataclass(frozen=True)"> = {
location: location;
(* Unlike abstract_content, this is the actual text read from the
* corresponding source file *)
content: string;
}
(*****************************************************************************)
(* Software Composition Analysis (SCA) match info (SCA part1) *)
(*****************************************************************************)
(* This is also known as Semgrep Supply Chain (SSC) *)
(* EXPERIMENTAL *)
(* both ecosystem and transitivity below have frozen=True so the generated
* classes can be hashed and put in sets (see calls to reachable_deps.add()
* in semgrep SCA code)
* TODO: use <ocaml repr="classic">, and do the same for manifest
*)
type ecosystem
<python decorator="dataclass(frozen=True)">
<ocaml attr="deriving show,eq"> =
[
| Npm <json name="npm">
| Pypi <json name="pypi">
| Gem <json name="gem">
| Gomod <json name="gomod">
| Cargo <json name="cargo">
| Maven <json name="maven">
| Composer <json name="composer">
| Nuget <json name="nuget">
| Pub <json name="pub">
| SwiftPM <json name="swiftpm">
| Cocoapods <json name="cocoapods">
(* Deprecated: Mix is a build system, should use Hex, which is the ecosystem *)
| Mix <json name="mix">
| Hex <json name="hex">
]
type transitivity
<python decorator="dataclass(frozen=True)">
<ocaml attr="deriving show,eq"> =
[
| Direct <json name="direct">
| Transitive <json name="transitive">
| Unknown <json name="unknown">
]
(* part of cli_match_extra *)
type sca_match = {
reachable: bool;
reachability_rule: bool;
sca_finding_schema: int;
dependency_match: dependency_match;
}
type dependency_match = {
dependency_pattern: sca_pattern;
found_dependency: found_dependency;
lockfile: fpath;
}
type sca_pattern = {
ecosystem: ecosystem;
package: string;
semver_range: string;
}
(* alt: sca_dependency? *)
type found_dependency = {
package: string;
version: string;
ecosystem: ecosystem;
(* ??? *)
allowed_hashes: (string * string list) list
<json repr="object"> <python repr="dict"> <ts repr="map">;
?resolved_url: string option;
transitivity: transitivity;
(* Path to the manifest file that defines the project containing this
* dependency. Examples: package.json, nested/folder/pom.xml
*)
?manifest_path: fpath option;
(* Path to the lockfile that contains this dependency.
* Examples: package-lock.json, nested/folder/requirements.txt, go.mod
* Since 1.87.0
*)
?lockfile_path: fpath option;
(* The line number of the dependency in the lockfile. When combined with the
* lockfile_path, this can identify the location of the dependency in the
* lockfile.
*)
?line_number: int option;
(* If we have dependency relationship information for this dependency, this
* field will include the name and version of other found_dependency items
* that this dependency requires.
* These fields must match values in `package` and `version` of another
* `found_dependency` in the same set
*)
?children: dependency_child list option;
(* Git ref of the dependency if the dependency comes directly from a git repo.
* Examples: refs/heads/main, refs/tags/v1.0.0, e5c704df4d308690fed696faf4c86453b4d88a95
* since 1.66.0 *)
?git_ref: string option;
}
type dependency_child <python decorator="dataclass(frozen=True)"> = {
package: string;
version: string;
}
(*****************************************************************************)
(* Semgrep Secrets match info *)
(*****************************************************************************)
(* EXPERIMENTAL *)
(* This type is used by postprocessors for secrets to report back
* the validity of a finding. No_validator is currently also used when no
* validation has yet occurred, which if that becomes confusing we
* could adjust that, by adding another state.
* TODO: use <ocaml repr="classic">
*)
type validation_state
<ocaml attr="deriving show, eq">
<python decorator="dataclass(frozen=True)"> =
[
| Confirmed_valid <json name="CONFIRMED_VALID">
| Confirmed_invalid <json name="CONFIRMED_INVALID">
| Validation_error <json name="VALIDATION_ERROR">
| No_validator <json name="NO_VALIDATOR">
]
(* part of cli_match_extra *)
type historical_info = {
(* Git commit at which the finding is present. Used by "historical" scans,
* which scan non-HEAD commits in the git history. Relevant for finding, e.g.,
* secrets which are buried in the git history which we wouldn't find at HEAD
*)
git_commit: sha1;
(* Git blob at which the finding is present. Sent in addition to the commit
* since some SCMs have permalinks which use the blob sha, so this information
* is useful when generating links back to the SCM. *)
?git_blob: sha1 option;
git_commit_timestamp: datetime;
}
(*****************************************************************************)
(* Errors *)
(*****************************************************************************)
(* coupling: if you add a constructor here with arguments, you probably need
* to adjust _error_type_string() in error.py for pysemgrep and
* Error.string_of_error_type() for osemgrep.
*)
type error_type
<ocaml attr="deriving show">
<python decorator="dataclass(frozen=True, order=True)"> =
[
(* File parsing related errors;
coupling: if you add a target parse error then metrics for
cli need to be updated. See cli/src/semgrep/parsing_data.py.
*)
| LexicalError <json name="Lexical error">
| ParseError (* a.k.a SyntaxError *) <json name="Syntax error">
| OtherParseError <json name="Other syntax error">
| AstBuilderError <json name="AST builder error">
(* Pattern parsing related errors.
* There are more precise info about the error in
* Rule.invalid_rule_error_kind in Rule.ml.
* TODO? should we move invalid_rule_error_kind here?
*)
| RuleParseError <json name="Rule parse error">
(* generated in pysemgrep only. TODO: some should take error_span in param *)
| SemgrepWarning <json name="SemgrepWarning">
| SemgrepError <json name="SemgrepError">
| InvalidRuleSchemaError <json name="InvalidRuleSchemaError">
| UnknownLanguageError <json name="UnknownLanguageError">
| InvalidYaml <json name="Invalid YAML">
(* matching (semgrep) related *)
| MatchingError (* internal error, e.g., NoTokenLocation *)
<json name="Internal matching error">
| SemgrepMatchFound (* TODO of string (* check_id *) *)
<json name="Semgrep match found">
| TooManyMatches <json name="Too many matches">
(* other *)
| FatalError (* missing file, OCaml errors, etc. *) <json name="Fatal error">
| Timeout <json name="Timeout">
| OutOfMemory <json name="Out of memory">
(* since semgrep 1.86.0 *)
| StackOverflow <json name="Stack overflow">
(* pro-engine specific *)
| TimeoutDuringInterfile <json name="Timeout during interfile analysis">
| OutOfMemoryDuringInterfile <json name="OOM during interfile analysis">
(* since semgrep 1.40.0 *)
| MissingPlugin <json name="Missing plugin">
(* !constructors with arguments! *)
(* the string list is the "YAML path" of the pattern, e.g. ["rules"; "1"; ...] *)
| PatternParseError of string list
(* since semgrep 0.97 *)
| PartialParsing of location list (* list of skipped tokens *)
(* since semgrep 1.38.0 *)
| IncompatibleRule of incompatible_rule
(* Those Xxx0 variants were introduced in semgrep 1.45.0, but actually they
* are here so that our backend can read the cli_error.type_ from old semgrep
* versions that were translating the PatternParseError _ and IncompatibleRule _
* above as a single string (instead of a list ["PatternParseError", ...] now).
* There is no PartialParsing0 because this was encoded as a ParseError
* instead.
*)
| PatternParseError0 <json name="Pattern parse error">
| IncompatibleRule0 <json name="Incompatible rule">
(* since semgrep 1.94.0 *)
| DependencyResolutionError of resolution_error
] <ocaml repr="classic">
type incompatible_rule
<ocaml attr="deriving show">
<python decorator="dataclass(frozen=True)"> =
{
rule_id: rule_id;
this_version: version;
?min_version: version option;
?max_version: version option;
}
(* TODO: type exit_code = ... *)
(* (called SemgrepError in error.py) *)
type cli_error = {
(* exit code for the type_ of error *)
code: int;
level: error_severity;
(* before 1.45.0 the type below was 'string', but was the result
* of converting error_type into a string, so using directly
* 'error_type' below should be mostly backward compatible
* thx to the <json name> annotations in error_type.
* To be fully backward compatible, we actually introduced the
* PatternParseError0 and IncompatibleRule0 cases in error_type.
*)
type_ <json name="type">: error_type;
(* LATER: use a variant instead of all those ?xxx types *)
?rule_id: rule_id option;
(* for most parsing errors those are set *)
?message: string option; (* contains error location *)
?path: fpath option;
(* for invalid rules, for ErrorWithSpan *)
?long_msg: string option;
?short_msg: string option;
?spans: error_span list option;
?help: string option;
}
type error_span = {
(* for InvalidRuleSchemaError *)
(* LATER: could inherit location; but file: vs path: *)
(* TODO: source hash should probably also be mandatory? *)
(* TODO: sometimes set to "<No file>" in rule_lang.py *)
file: fpath;
start: position;
end <ocaml name="end_">: position;
?source_hash: string option;
(* The path to the pattern in the yaml rule
* and an adjusted start/end within just the pattern
* Used to report playground parse errors in the simple editor
* TODO: add an example because our source code doesn't make much sense.
*
* TODO: remove this or add back simple editor error highlighting
*)
?config_start: position nullable option;
?config_end: position nullable option;
?config_path: string list nullable option;
(* LATER: what is this for? *)
?context_start: position nullable option;
?context_end: position nullable option;
}
(*****************************************************************************)
(* Skipping information *)
(*****************************************************************************)
(* A reason for skipping a target file or a pair (target, rule).
Note that this type is also used in Report.ml hence the need
for deriving show here.
For consistency, please make sure all the JSON constructors use the
same case rules (lowercase, underscores). This is hard to fix later!
Please review your code carefully before committing to interface changes.
*)
type skip_reason <ocaml attr="deriving show"> = [
(* Originally returned by the Python CLI *)
| Always_skipped <json name="always_skipped">
| Semgrepignore_patterns_match <json name="semgrepignore_patterns_match">
| Cli_include_flags_do_not_match <json name="cli_include_flags_do_not_match">
| Cli_exclude_flags_match <json name="cli_exclude_flags_match">
| Exceeded_size_limit <json name="exceeded_size_limit">
| Analysis_failed_parser_or_internal_error
<json name="analysis_failed_parser_or_internal_error">
(* Originally returned by semgrep-core *)
| Excluded_by_config <json name="excluded_by_config">
| Wrong_language <json name="wrong_language">
| Too_big <json name="too_big">
| Minified <json name="minified">
| Binary <json name="binary">
| Irrelevant_rule <json name="irrelevant_rule">
| Too_many_matches <json name="too_many_matches">
(* New in osemgrep *)
| Gitignore_patterns_match (* TODO: use JSON lowercase for consistency *)
(* since 1.40.0. There were always ignored, but not shown in the skip report *)
| Dotfile (* TODO: use JSON lowercase for consistency *)
(* since 1.44.0 *)
| Nonexistent_file (* TODO: use JSON lowercase for consistency *)
(* since 1.94.0 *)
| Insufficient_permissions <json name="insufficient_permissions">
] <ocaml repr="classic">
(* coupling: ugly: with yield_json_objects() in target_manager.py *)
type skipped_target <ocaml attr="deriving show"> = {
path: fpath;
reason: skip_reason;
(* since semgrep 1.39.0 (used to be return only by semgrep-core) *)
?details: string option;
(* If the 'rule_id' field is missing, the target is assumed to have been
* skipped for all the rules *)
?rule_id: rule_id option;
}
type scanned_and_skipped = {
scanned: fpath list;
(* Note that you get this field only if you use semgrep --verbose.
* TODO: needs fix in atdpy; see note tagged [X584759]
* ~skipped: skipped_target list;
*)
?skipped: skipped_target list option;
}
type skipped_rule = {
rule_id: rule_id;
details: string;
(* position of the error in the rule file *)
position: position;
}
(*****************************************************************************)
(* Profiling information *)
(*****************************************************************************)
(* coupling: with semgrep_metrics.atd performance section *)
(* coupling: if you change the JSON schema below, you probably need to
* also modify perf/run-benchmarks.
* Run locally $ ./run-benchmarks --dummy --upload
*)
type profile = {
(* List of rules, including the one read but not run on any target.
* TODO? is this still true now that we just pass around the profile
* computed in semgrep-core?
* This list is actually more an array which allows other
* fields to reference rule by number instead of rule_id
* (e.g., match_times further below) saving space in the JSON.
*
* Upgrade note: this used to be defined as a rule_id_dict where
* each rule_id was inside a {id: rule_id; ...} record so
* we could give parsing time info about each rule, but
* parsing one rule was never the slow part, so now we just juse the
* aggregated rules_parse_time below and do not need a
* complex rule_id_dict record anymore.
*)
rules: rule_id list;
(* LESS? could be part of profiling_times below instead *)
rules_parse_time: float;
(* coupling: semgrep_metrics.atd profilingTimes field?
* Those fields are not produced by semgrep-core; they
* are added by pysemgrep (and later osemgrep).
*
* LATER? define a cli_profiling_times with more precise keys?
* type cli_profiling_times <ocaml attr="deriving show"> = {
* config_time: float;
* core_time: float;
* ignores_time: float;
* total_time: float;
* }
* LATER: get rid of profiler.dump_stats
*)
profiling_times: (string * float) list
<json repr="object">
<python repr="dict">
<ts repr="map">;
targets: target_times list;
total_bytes: int;
(* maximum amount of memory used by Semgrep(-core) during its execution *)
?max_memory_bytes : int option;
}
type target_times = {
path: fpath;
num_bytes: int;
(* each elt in the list refers to a rule in profile.rules *)
match_times: float list;
(* emma: "when we were first diagnosing performance, I recorded every time
* the file was read (including the later times that were just reloading
* the parsed file) to make sure reading the file wasn't taking a significant
* amount of time. Now that we know it isn't, we don't need to record this
* anymore.
* TODO: just use a single float instead."
*)
parse_times: float list;
(* run time for all rules on target *)
run_time: float;
}
(*****************************************************************************)
(* Final 'semgrep scan' output *)
(*****************************************************************************)
(* TODO: rename to scan_output at some point *)
type cli_output = {
(* since: 0.92 *)
?version: version option;
results: cli_match list;
errors: cli_error list;
inherit cli_output_extra;
}
(* TODO? used only in TEXT format:
* ?color_output, per_finding_max_lines_limit, per_line_max_chars_limit
*)
type cli_output_extra = {
(* targeting information *)
paths: scanned_and_skipped;
(* profiling information *)
?time: profile option;
(* debugging (rule writing) information.
* Note that as opposed to the dataflow trace, the explanations are not
* embedded inside a match because we give also explanations when things are
* not matching.
* EXPERIMENTAL: since semgrep 0.109
*)
?explanations: matching_explanation list option;
(* These rules, classified by engine used, will let us be transparent in
* the CLI output over what rules were run with what.
* EXPERIMENTAL: since: 1.11.0
*)
?rules_by_engine: rule_id_and_engine_kind list option;
?engine_requested: engine_kind option;
(* Reporting just the requested engine isn't granular enough. We want to
* know what languages had rules that invoked interfile. This is
* particularly important for tracking the performance impact of new
* interfile languages
* EXPERIMENTAL: since 1.49.0
*)
?interfile_languages_used: string list option;
(* EXPERIMENTAL: since: 1.37.0 *)
~skipped_rules: skipped_rule list;
}
(*****************************************************************************)
(* 'semgrep test' output *)
(*****************************************************************************)
type config_error_reason = [
| UnparsableRule <json name="unparsable_rule">
] <ocaml repr="classic">
type config_error = {
file: fpath;
reason: config_error_reason
}
type tests_result = {
(* would like to use rule_id here but then can't use json repr *)
results: (string (* rule file *) * checks) list <json repr="object">;
fixtest_results: (string (* target file *) * fixtest_result) list
<json repr="object">;
config_missing_tests: fpath list;
config_missing_fixtests: fpath list;
config_with_errors: config_error list;
}
type checks = {
(* would like to use fpath *)
checks: (string (* rule_id *) * rule_result) list <json repr="object">;
}
type rule_result = {
passed: bool;
(* would like to use fpath *)
matches: (string (* target filename *) * expected_reported) list
<json repr="object">;
errors: todo list;
(* NEW: since 1.79 *)
?diagnosis: matching_diagnosis option;
}
type expected_reported = {
expected_lines: int list;
reported_lines: int list;
}
type fixtest_result = {
passed: bool;
}
type todo = int
(* ----------------------------- *)
(* Matching diagnosis (Brandon's stuff) *)
(* ----------------------------- *)