Skip to content

Commit 4ec13c0

Browse files
authored
Specify SSC in rule_schema_v2.atd (#190)
test plan: see related semgrep PR - [x] I ran `make setup && make` to update the generated code after editing a `.atd` file (TODO: have a CI check) - [x] I made sure we're still backward compatible with old versions of the CLI. For example, the Semgrep backend need to still be able to *consume* data generated by Semgrep 1.17.0. See https://atd.readthedocs.io/en/latest/atdgen-tutorial.html#smooth-protocol-upgrades
1 parent dd89d3c commit 4ec13c0

File tree

2 files changed

+117
-12
lines changed

2 files changed

+117
-12
lines changed

rule_schema_v2.atd

Lines changed: 100 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
*
3333
* TODO:
3434
* - extract
35-
* - r2c-internal-project-depends-on-content
3635
* - secrets
3736
* - steps (but not join)
3837
* - new metavariable types
@@ -74,15 +73,20 @@ type rule = {
7473
?match_ <json name="match">: formula option;
7574
?taint: taint option;
7675
?extract: extract option;
77-
(* TODO: steps, secrets, sca *)
76+
(* TODO: steps, secrets *)
7877
(* TODO? product: product; *)
7978

79+
(* can work with match/taint/..., maybe not extract *)
80+
?project_depends_on <json name="r2c-internal-project-depends-on">:
81+
project_depends_on option;
82+
8083
(* alt: later: could be replaced by a pattern-filename: *)
8184
?paths: paths option;
8285

8386
?fix: string option;
8487
?fix_regex: fix_regex option;
85-
88+
89+
(* TODO? impose more constraints on metadata? standard fields? *)
8690
?metadata: raw_json option;
8791
?options: rule_options option;
8892

@@ -112,7 +116,7 @@ type severity = [
112116
| Info <json name="INFO">
113117
]
114118

115-
(* coupling: language.ml *)
119+
(* coupling: Language.ml *)
116120
type language = [
117121
(* programming (and configuration) languages *)
118122
| Apex <json name="apex">
@@ -183,7 +187,63 @@ type fix_regex = {
183187
?count: int option;
184188
}
185189

186-
type rule_options <ocaml from="Rule_options" t="t"> = abstract
190+
(* coupling: Rule_options.atd
191+
* alt: <ocaml from="Rule_options" t="t"> but I prefer to repeat
192+
* its content here so one can fully see the syntax for a rule in one file.
193+
*)
194+
type rule_options = {
195+
?constant_propagation: bool option;
196+
?symbolic_propagation: bool option;
197+
?taint_unify_mvars: bool option;
198+
?taint_assume_safe_functions: bool option;
199+
?taint_assume_safe_indexes: bool option;
200+
?taint_assume_safe_comparisons: bool option;
201+
?taint_assume_safe_booleans: bool option;
202+
?taint_assume_safe_numbers: bool option;
203+
?taint_only_propagate_through_assignments: bool option;
204+
?ac_matching: bool option;
205+
?commutative_boolop: bool option;
206+
?commutative_compop: bool option;
207+
?vardef_assign: bool option;
208+
?flddef_assign: bool option;
209+
?attr_expr: bool option;
210+
?arrow_is_function: bool option;
211+
?let_is_var: bool option;
212+
?go_deeper_expr: bool option;
213+
?go_deeper_stmt: bool option;
214+
?implicit_deep_exprstmt: bool option;
215+
?implicit_ellipsis: bool option;
216+
?xml_singleton_loose_matching: bool option;
217+
?xml_attrs_implicit_ellipsis: bool option;
218+
?xml_children_ordered: bool option;
219+
?generic_engine: generic_engine option;
220+
?cpp_parsing_pref: cpp_parsing_opt option;
221+
?generic_multiline: bool option;
222+
?generic_braces: (string * string) list option;
223+
~generic_extra_braces: (string * string) list;
224+
~generic_extra_word_characters: string list;
225+
~generic_caseless: bool;
226+
?generic_ellipsis_max_span: int option;
227+
?generic_comment_style: generic_comment_style option;
228+
?interfile: bool option;
229+
}
230+
231+
type cpp_parsing_opt = [
232+
| AsFunDef <json name="as_fundef">
233+
| AsVarDefWithCtor <json name="as_vardef_with_ctor">
234+
]
235+
236+
type generic_engine = [
237+
| Aliengrep <json name="aliengrep">
238+
| Spacegrep <json name="spacegrep">
239+
]
240+
241+
type generic_comment_style = [
242+
| C <json name="c">
243+
| Cpp <json name="cpp">
244+
| Shell <json name="shell">
245+
]
246+
187247

188248
(*****************************************************************************)
189249
(* Formula *)
@@ -400,9 +460,43 @@ type propagator = {
400460
<json adapter.ocaml="Rule_schema_v2_adapter.Formula">
401461

402462
(*****************************************************************************)
403-
(* TODO: SSC *)
463+
(* Supply chain *)
404464
(*****************************************************************************)
405465

466+
(* need an adapter there too *)
467+
type project_depends_on = [
468+
| DependsBasic <json name="B"> of project_depends_on_basic
469+
| DependsEither <json name="E"> of project_depends_on_either
470+
]
471+
<json adapter.ocaml="Rule_schema_v2_adapter.ProjectDependsOn">
472+
473+
type project_depends_on_basic = {
474+
namespace: namespace;
475+
package: string;
476+
version: semver_range;
477+
}
478+
479+
type project_depends_on_either = {
480+
depends_on_either <json name="depends-on-either">:
481+
project_depends_on_basic list;
482+
}
483+
484+
(* coupling: semgrep_output_v1.ecosystem (better name than namespace) *)
485+
type namespace = [
486+
| Npm <json name="npm">
487+
| Pypi <json name="pypi">
488+
| Gem <json name="gem">
489+
| Gomod <json name="gomod">
490+
| Cargo <json name="cargo">
491+
| Maven <json name="maven">
492+
| Composer <json name="composer">
493+
| Nuget <json name="nuget">
494+
| Pub <json name="pub">
495+
]
496+
497+
(* ex: < 0.0.8 *)
498+
type semver_range = string
499+
406500
(*****************************************************************************)
407501
(* TODO: Extract mode *)
408502
(*****************************************************************************)

rule_schema_v2_adapter.ml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ end
2020

2121
module Condition = struct
2222

23-
(** Convert from original json to ATD-compatible json *)
2423
let normalize (orig : Yojson.Safe.t ) : Yojson.Safe.t =
2524
match orig with
2625
| `Assoc (("comparison", cmp)::rest) ->
@@ -30,6 +29,7 @@ module Condition = struct
3029
(* TODO: check at least one of type/types/... is specified *)
3130
`List [`String "M";
3231
`Assoc (("metavariable", mvar)::rest)]
32+
(* alt: we could do the String vs List in a separate adapter *)
3333
| `Assoc [("focus", `String x)] ->
3434
`List [`String "F";
3535
`Assoc [("focus", `List [`String x])]]
@@ -38,23 +38,34 @@ module Condition = struct
3838
`Assoc [("focus", `List x)]]
3939
| x -> x
4040

41-
(** Convert from ATD-compatible json to original json *)
4241
let restore (_atd : Yojson.Safe.t) : Yojson.Safe.t =
43-
(* not needed for now; we care just about parsing *)
4442
failwith "Rule_schema_v2_adapter.Condition.restore not implemented"
4543
end
4644

4745
module BySideEffect = struct
4846

49-
(** Convert from original json to ATD-compatible json *)
5047
let normalize (orig : Yojson.Safe.t ) : Yojson.Safe.t =
5148
match orig with
5249
| `Bool true -> `String "true"
5350
| `Bool false -> `String "false"
5451
| x -> x
5552

56-
(** Convert from ATD-compatible json to original json *)
5753
let restore (_atd : Yojson.Safe.t) : Yojson.Safe.t =
58-
(* not needed for now; we care just about parsing *)
5954
failwith "Rule_schema_v2_adapter.BySideEffect.restore not implemented"
6055
end
56+
57+
module ProjectDependsOn = struct
58+
59+
let normalize (orig : Yojson.Safe.t ) : Yojson.Safe.t =
60+
match orig with
61+
| `Assoc [("depends-on-either", arr)] ->
62+
`List [`String "E";
63+
`Assoc [("depends-on-either", arr)]]
64+
| `Assoc (xs) ->
65+
`List [`String "B";
66+
`Assoc xs]
67+
| x -> x
68+
69+
let restore (_atd : Yojson.Safe.t) : Yojson.Safe.t =
70+
failwith "Rule_schema_v2_adapter.ProjectDependsOn.restore not implemented"
71+
end

0 commit comments

Comments
 (0)