-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
generated_code_validation.rb
277 lines (240 loc) 路 10.1 KB
/
generated_code_validation.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
# frozen_string_literal: true
# typed: true
module T::Props
# Helper to validate generated code, to mitigate security concerns around
# `class_eval`. Not called by default; the expectation is this will be used
# in a test iterating over all T::Props::Serializable subclasses.
#
# We validate the exact expected structure of the generated methods as far
# as we can, and then where cloning produces an arbitrarily nested structure,
# we just validate a lack of side effects.
module GeneratedCodeValidation
extend Private::Parse
class ValidationError < RuntimeError; end
def self.validate_deserialize(source)
parsed = parse(source)
# def %<name>(hash)
# ...
# end
assert_equal(:def, parsed.type)
name, args, body = parsed.children
assert_equal(:__t_props_generated_deserialize, name)
assert_equal(s(:args, s(:arg, :hash)), args)
assert_equal(:begin, body.type)
init, *prop_clauses, ret = body.children
# found = %<prop_count>
# ...
# found
assert_equal(:lvasgn, init.type)
init_name, init_val = init.children
assert_equal(:found, init_name)
assert_equal(:int, init_val.type)
assert_equal(s(:lvar, :found), ret)
prop_clauses.each_with_index do |clause, i|
if i.even?
validate_deserialize_hash_read(clause)
else
validate_deserialize_ivar_set(clause)
end
end
end
def self.validate_serialize(source)
parsed = parse(source)
# def %<name>(strict)
# ...
# end
assert_equal(:def, parsed.type)
name, args, body = parsed.children
assert_equal(:__t_props_generated_serialize, name)
assert_equal(s(:args, s(:arg, :strict)), args)
assert_equal(:begin, body.type)
init, *prop_clauses, ret = body.children
# h = {}
# ...
# h
assert_equal(s(:lvasgn, :h, s(:hash)), init)
assert_equal(s(:lvar, :h), ret)
prop_clauses.each do |clause|
validate_serialize_clause(clause)
end
end
private_class_method def self.validate_serialize_clause(clause)
assert_equal(:if, clause.type)
condition, if_body, else_body = clause.children
# if @%<accessor_key>.nil?
assert_equal(:send, condition.type)
receiver, method = condition.children
assert_equal(:ivar, receiver.type)
assert_equal(:nil?, method)
unless if_body.nil?
# required_prop_missing_from_serialize(%<prop>) if strict
assert_equal(:if, if_body.type)
if_strict_condition, if_strict_body, if_strict_else = if_body.children
assert_equal(s(:lvar, :strict), if_strict_condition)
assert_equal(:send, if_strict_body.type)
on_strict_receiver, on_strict_method, on_strict_arg = if_strict_body.children
assert_equal(nil, on_strict_receiver)
assert_equal(:required_prop_missing_from_serialize, on_strict_method)
assert_equal(:sym, on_strict_arg.type)
assert_equal(nil, if_strict_else)
end
# h[%<serialized_form>] = ...
assert_equal(:send, else_body.type)
receiver, method, h_key, h_val = else_body.children
assert_equal(s(:lvar, :h), receiver)
assert_equal(:[]=, method)
assert_equal(:str, h_key.type)
validate_lack_of_side_effects(h_val, whitelisted_methods_for_serialize)
end
private_class_method def self.validate_deserialize_hash_read(clause)
# val = hash[%<serialized_form>s]
assert_equal(:lvasgn, clause.type)
name, val = clause.children
assert_equal(:val, name)
assert_equal(:send, val.type)
receiver, method, arg = val.children
assert_equal(s(:lvar, :hash), receiver)
assert_equal(:[], method)
assert_equal(:str, arg.type)
end
private_class_method def self.validate_deserialize_ivar_set(clause)
# %<accessor_key>s = if val.nil?
# found -= 1 unless hash.key?(%<serialized_form>s)
# %<nil_handler>s
# else
# %<serialized_val>s
# end
assert_equal(:ivasgn, clause.type)
ivar_name, deser_val = clause.children
unless ivar_name.is_a?(Symbol)
raise ValidationError.new("Unexpected ivar: #{ivar_name}")
end
assert_equal(:if, deser_val.type)
condition, if_body, else_body = deser_val.children
assert_equal(s(:send, s(:lvar, :val), :nil?), condition)
assert_equal(:begin, if_body.type)
update_found, handle_nil = if_body.children
assert_equal(:if, update_found.type)
found_condition, found_if_body, found_else_body = update_found.children
assert_equal(:send, found_condition.type)
receiver, method, arg = found_condition.children
assert_equal(s(:lvar, :hash), receiver)
assert_equal(:key?, method)
assert_equal(:str, arg.type)
assert_equal(nil, found_if_body)
assert_equal(s(:op_asgn, s(:lvasgn, :found), :-, s(:int, 1)), found_else_body)
validate_deserialize_handle_nil(handle_nil)
if else_body.type == :kwbegin
rescue_expression, = else_body.children
assert_equal(:rescue, rescue_expression.type)
try, rescue_body = rescue_expression.children
validate_lack_of_side_effects(try, whitelisted_methods_for_deserialize)
assert_equal(:resbody, rescue_body.type)
exceptions, assignment, handler = rescue_body.children
assert_equal(:array, exceptions.type)
exceptions.children.each {|c| assert_equal(:const, c.type)}
assert_equal(:lvasgn, assignment.type)
assert_equal([:e], assignment.children)
deserialization_error, val_return = handler.children
assert_equal(:send, deserialization_error.type)
receiver, method, *args = deserialization_error.children
assert_equal(nil, receiver)
assert_equal(:raise_deserialization_error, method)
args.each {|a| validate_lack_of_side_effects(a, whitelisted_methods_for_deserialize)}
validate_lack_of_side_effects(val_return, whitelisted_methods_for_deserialize)
else
validate_lack_of_side_effects(else_body, whitelisted_methods_for_deserialize)
end
end
private_class_method def self.validate_deserialize_handle_nil(node)
case node.type
when :hash, :array, :str, :sym, :int, :float, :true, :false, :nil, :const # rubocop:disable Lint/BooleanSymbol
# Primitives and constants are safe
when :send
receiver, method, arg = node.children
if receiver.nil?
# required_prop_missing_from_deserialize(%<prop>)
assert_equal(:required_prop_missing_from_deserialize, method)
assert_equal(:sym, arg.type)
elsif receiver == self_class_decorator
# self.class.decorator.raise_nil_deserialize_error(%<serialized_form>)
assert_equal(:raise_nil_deserialize_error, method)
assert_equal(:str, arg.type)
elsif method == :default
# self.class.decorator.props_with_defaults.fetch(%<prop>).default
assert_equal(:send, receiver.type)
inner_receiver, inner_method, inner_arg = receiver.children
assert_equal(
s(:send, self_class_decorator, :props_with_defaults),
inner_receiver,
)
assert_equal(:fetch, inner_method)
assert_equal(:sym, inner_arg.type)
else
raise ValidationError.new("Unexpected receiver in nil handler: #{node.inspect}")
end
else
raise ValidationError.new("Unexpected nil handler: #{node.inspect}")
end
end
private_class_method def self.self_class_decorator
@self_class_decorator ||= s(:send, s(:send, s(:self), :class), :decorator).freeze
end
private_class_method def self.validate_lack_of_side_effects(node, whitelisted_methods_by_receiver_type)
case node.type
when :const
# This is ok, because we'll have validated what method has been called
# if applicable
when :hash, :array, :str, :sym, :int, :float, :true, :false, :nil, :self # rubocop:disable Lint/BooleanSymbol
# Primitives & self are ok
when :lvar, :arg, :ivar
# Reading local & instance variables & arguments is ok
unless node.children.all? {|c| c.is_a?(Symbol)}
raise ValidationError.new("Unexpected child for #{node.type}: #{node.inspect}")
end
when :args, :mlhs, :block, :begin, :if
# Blocks etc are read-only if their contents are read-only
node.children.each {|c| validate_lack_of_side_effects(c, whitelisted_methods_by_receiver_type) if c}
when :send
# Sends are riskier so check a whitelist
receiver, method, *args = node.children
if receiver
if receiver.type == :send
key = receiver
else
key = receiver.type
validate_lack_of_side_effects(receiver, whitelisted_methods_by_receiver_type)
end
if !whitelisted_methods_by_receiver_type[key]&.include?(method)
raise ValidationError.new("Unexpected method #{method} called on #{receiver.inspect}")
end
end
args.each do |arg|
validate_lack_of_side_effects(arg, whitelisted_methods_by_receiver_type)
end
else
raise ValidationError.new("Unexpected node type #{node.type}: #{node.inspect}")
end
end
private_class_method def self.assert_equal(expected, actual)
if expected != actual
raise ValidationError.new("Expected #{expected}, got #{actual}")
end
end
# Method calls generated by SerdeTransform
private_class_method def self.whitelisted_methods_for_serialize
@whitelisted_methods_for_serialize ||= {
lvar: %i{dup map transform_values transform_keys each_with_object nil? []= serialize},
ivar: %i[dup map transform_values transform_keys each_with_object serialize],
const: %i[checked_serialize deep_clone_object],
}
end
# Method calls generated by SerdeTransform
private_class_method def self.whitelisted_methods_for_deserialize
@whitelisted_methods_for_deserialize ||= {
lvar: %i{dup map transform_values transform_keys each_with_object nil? []= to_f},
const: %i[deserialize from_hash deep_clone_object],
}
end
end
end