-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathYaml.qll
More file actions
680 lines (608 loc) · 14.8 KB
/
Yaml.qll
File metadata and controls
680 lines (608 loc) · 14.8 KB
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
/**
* Provides classes for working with YAML data.
*
* YAML documents are represented as abstract syntax trees whose nodes
* are either YAML values or alias nodes referring to another YAML value.
*/
overlay[local?]
module;
/** Provides the input specification of YAML implementation. */
signature module InputSig {
class Container {
string getAbsolutePath();
Container getParentContainer();
}
class File extends Container;
class Location {
File getFile();
}
class LocatableBase {
/**
* Gets the `Location` of this node.
*
* Typically `yaml_locations(this, result)`.
*/
Location getLocation();
}
/**
* A base class for Nodes.
*
* Typically `@yaml_node`.
*/
class NodeBase extends LocatableBase {
/**
* Gets the `i`th child node of this node.
*
* Typically `yaml(result, _, this, i, _, _)`.
*/
NodeBase getChildNode(int i);
/**
* Gets the tag of this node.
*
* Typically `yaml(this, _, _, _, result, _)`.
*/
string getTag();
/**
* Gets the anchor associated with this node, if any.
*
* Typically `yaml_anchors(this, result)`.
*/
string getAnchor();
/**
* Gets a textual representation of this node.
*
* Typically `yaml(this, _, _, _, _, result)`.
*/
string toString();
}
/**
* A base class for scalar nodes.
*
* Typically `@yaml_scalar_node`.
*/
class ScalarNodeBase extends NodeBase {
/**
* Gets the style of this scalar.
*
* Typically `yaml_scalars(this, result, _)`.
*/
int getStyle();
/**
* Gets the value of this scalar, as a string.
*
* Typically `yaml_scalars(this, _, result)`.
*/
string getValue();
}
/**
* A base class for collections.
*
* Typically `@yaml_collection_node`.
*/
class CollectionNodeBase extends NodeBase;
/**
* A base class for objects.
*
* Typically `@yaml_mapping_node`.
*/
class MappingNodeBase extends CollectionNodeBase;
/**
* A base class for arrays.
*
* Typically `@yaml_sequence_node`.
*/
class SequenceNodeBase extends CollectionNodeBase;
/**
* A base class for aliases.
*
* Typically `@yaml_alias_node`.
*/
class AliasNodeBase extends NodeBase {
/**
* Gets the target anchor this alias refers to.
*
* Typically `yaml_aliases(this, result)`.
*/
string getTarget();
}
/**
* A base class for parse errors.
*
* Typically `@yaml_parse_error`.
*/
class ParseErrorBase extends LocatableBase {
/**
* Gets the message of this parse error.
*
* Typically `yaml_errors(this, result)`.
*/
string getMessage();
}
}
/** Provides a class hierarchy for working with YAML files. */
module Make<InputSig Input> {
/**
* A node in the AST representation of a YAML file, which may either be
* a YAML value (such as a scalar or a collection) or an alias node
* referring to some other YAML value.
*
* Examples:
*
* ```
* # a mapping
* x: 1
* << : *DEFAULTS # an alias node referring to anchor `DEFAULTS`
* ```
*/
class YamlNode instanceof Input::NodeBase {
/** Gets the file this node comes from. */
Input::File getFile() { result = this.getLocation().getFile() }
/**
* Gets the `Location` of this node.
*/
Input::Location getLocation() { result = super.getLocation() }
/**
* Gets the parent node of this node, which is always a collection.
*/
YamlCollection getParentNode() { this = result.getChildNode(_) }
/**
* Gets the `i`th child node of this node.
*
* _Note_: The index of a child node relative to its parent is considered
* an implementation detail and may change between versions of the extractor.
*/
YamlNode getChildNode(int i) { result = super.getChildNode(i) }
/**
* Gets a child node of this node.
*/
YamlNode getAChildNode() { result = this.getChildNode(_) }
/**
* Gets the number of child nodes of this node.
*/
int getNumChild() { result = count(this.getAChildNode()) }
/**
* Gets the `i`th child of this node, as a YAML value.
*/
YamlValue getChild(int i) { result = this.getChildNode(i).eval() }
/**
* Gets a child of this node, as a YAML value.
*/
YamlValue getAChild() { result = this.getChild(_) }
/**
* Gets the tag of this node.
*/
string getTag() { result = super.getTag() }
/**
* Holds if this node is tagged with a standard type tag of the form
* `tag:yaml.org,2002:<t>`.
*/
predicate hasStandardTypeTag(string t) {
t = this.getTag().regexpCapture("tag:yaml.org,2002:(.*)", 1)
}
/** Gets a textual representation of this node. */
string toString() { result = super.toString() }
/**
* Gets the anchor associated with this node, if any.
*/
string getAnchor() { result = super.getAnchor() }
/**
* Gets the toplevel document to which this node belongs.
*/
YamlDocument getDocument() { result = this.getParentNode*() }
/**
* Gets the YAML value this node corresponds to after resolving aliases and includes.
*/
YamlValue eval() { result = this }
/**
* Gets the primary QL class for this node.
*/
string getAPrimaryQlClass() { result = "YamlNode" }
}
/**
* A YAML value; that is, either a scalar or a collection.
*
* Examples:
*
* ```
* ---
* "a string"
* ---
* - a
* - sequence
* ```
*/
abstract class YamlValue extends YamlNode { }
/**
* A YAML scalar.
*
* Examples:
*
* ```
* 42
* 1.0
* 2001-12-15T02:59:43.1Z
* true
* null
* "hello"
* ```
*/
class YamlScalar extends YamlValue instanceof Input::ScalarNodeBase {
/**
* Gets the style of this scalar, which is one of the following:
*
* - `""` (empty string): plain style
* - `"\""` (double quote): double quoted style
* - `"'"` (single quote): single quoted style
* - `">"` (greater-than): folded style
* - `"|"` (pipe): literal style
*/
string getStyle() {
exists(int s | s = super.getStyle() |
s = 0 and result = ""
or
s = 34 and result = "\""
or
s = 39 and result = "'"
or
s = 62 and result = ">"
or
s = 124 and result = "|"
)
}
/**
* Gets the value of this scalar, as a string.
*/
string getValue() { result = super.getValue() }
override string getAPrimaryQlClass() { result = "YamlScalar" }
}
/**
* A YAML scalar representing an integer value.
*
* Examples:
*
* ```
* 42
* 0xffff
* ```
*/
class YamlInteger extends YamlScalar {
YamlInteger() { this.hasStandardTypeTag("int") }
/**
* Gets the value of this scalar, as an integer.
*/
int getIntValue() { result = this.getValue().toInt() }
}
/**
* A YAML scalar representing a floating point value.
*
* Examples:
*
* ```
* 1.0
* 6.626e-34
* ```
*/
class YamlFloat extends YamlScalar {
YamlFloat() { this.hasStandardTypeTag("float") }
/**
* Gets the value of this scalar, as a floating point number.
*/
float getFloatValue() { result = this.getValue().toFloat() }
}
/**
* A YAML scalar representing a time stamp.
*
* Example:
*
* ```
* 2001-12-15T02:59:43.1Z
* ```
*/
class YamlTimestamp extends YamlScalar {
YamlTimestamp() { this.hasStandardTypeTag("timestamp") }
/**
* Gets the value of this scalar, as a date.
*/
date getDateValue() { result = this.getValue().toDate() }
}
/**
* A YAML scalar representing a Boolean value.
*
* Example:
*
* ```
* true
* ```
*/
class YamlBool extends YamlScalar {
YamlBool() { this.hasStandardTypeTag("bool") }
/**
* Gets the value of this scalar, as a Boolean.
*/
boolean getBoolValue() { if this.getValue() = "true" then result = true else result = false }
}
/**
* A YAML scalar representing the null value.
*
* Example:
*
* ```
* null
* ```
*/
class YamlNull extends YamlScalar {
YamlNull() { this.hasStandardTypeTag("null") }
}
/**
* A YAML scalar representing a string value.
*
* Example:
*
* ```
* "hello"
* ```
*/
class YamlString extends YamlScalar {
YamlString() { this.hasStandardTypeTag("str") }
}
/**
* A YAML scalar representing a merge key.
*
* Example:
*
* ```
* x: 1
* << : *DEFAULTS # merge key
* ```
*/
class YamlMergeKey extends YamlScalar {
YamlMergeKey() { this.hasStandardTypeTag("merge") }
}
/**
* A YAML scalar representing an `!include` directive.
*
* ```
* !include common.yaml
* ```
*/
class YamlInclude extends YamlScalar {
YamlInclude() { this.getTag() = "!include" }
override YamlValue eval() {
exists(YamlDocument targetDoc |
targetDoc.getLocation().getFile().getAbsolutePath() = this.getTargetPath() and
result = targetDoc.eval()
)
}
/**
* Gets the absolute path of the file included by this directive.
*/
private string getTargetPath() {
result = this.getAbsolutePath()
or
result =
this.getDocument().getLocation().getFile().getParentContainer().getAbsolutePath() + "/" +
this.getRelativePath()
}
/** Join-order helper for `getTargetPath`. Gets the path but only if it is an absolute path. */
private string getAbsolutePath() {
result = this.getValue() and
result.matches("/%")
}
/** Join-order helper for `getTargetPath`. Gets the path, but only if it is a relative path. */
pragma[noinline]
private string getRelativePath() {
result = this.getValue() and
not result.matches("/%")
}
}
/**
* A YAML collection, that is, either a mapping or a sequence.
*
* Examples:
*
* ```
* ---
* # a mapping
* x: 0
* y: 1
* ---
* # a sequence
* - red
* - green
* - -blue
* ```
*/
class YamlCollection extends YamlValue instanceof Input::CollectionNodeBase {
override string getAPrimaryQlClass() { result = "YamlCollection" }
}
/**
* A YAML mapping.
*
* Example:
*
* ```
* x: 0
* y: 1
* ```
*/
class YamlMapping extends YamlCollection instanceof Input::MappingNodeBase {
/**
* Gets the `i`th key of this mapping.
*/
YamlNode getKeyNode(int i) {
i >= 0 and
exists(int j | i = j - 1 and result = this.getChildNode(j))
}
/**
* Gets the `i`th value of this mapping.
*/
YamlNode getValueNode(int i) {
i >= 0 and
exists(int j | i = -j - 1 and result = this.getChildNode(j))
}
/**
* Gets the `i`th key of this mapping, as a YAML value.
*/
YamlValue getKey(int i) { result = this.getKeyNode(i).eval() }
/**
* Gets the `i`th value of this mapping, as a YAML value.
*/
YamlValue getValue(int i) { result = this.getValueNode(i).eval() }
/**
* Holds if this mapping maps `key` to `value`.
*/
predicate maps(YamlValue key, YamlValue value) {
exists(int i | key = this.getKey(i) and value = this.getValue(i))
or
exists(YamlMergeKey merge, YamlMapping that | this.maps(merge, that) | that.maps(key, value))
}
/**
* Gets the value that this mapping maps `key` to.
*/
YamlValue lookup(string key) {
exists(YamlScalar s | s.getValue() = key | this.maps(s, result))
}
override string getAPrimaryQlClass() { result = "YamlMapping" }
}
/**
* A YAML sequence.
*
* Example:
*
* ```
* - red
* - green
* - blue
* ```
*/
class YamlSequence extends YamlCollection instanceof Input::SequenceNodeBase {
/**
* Gets the `i`th element in this sequence.
*/
YamlNode getElementNode(int i) { result = this.getChildNode(i) }
/**
* Gets the `i`th element in this sequence, as a YAML value.
*/
YamlValue getElement(int i) { result = this.getElementNode(i).eval() }
override string getAPrimaryQlClass() { result = "YamlSequence" }
}
/**
* A YAML alias node referring to a target anchor.
*
* Example:
*
* ```
* *DEFAULTS
* ```
*/
class YamlAliasNode extends YamlNode instanceof Input::AliasNodeBase {
override YamlValue eval() {
result.getAnchor() = this.getTarget() and
result.getDocument() = this.getDocument()
}
/**
* Gets the target anchor this alias refers to.
*/
string getTarget() { result = super.getTarget() }
override string getAPrimaryQlClass() { result = "YamlAliasNode" }
}
/**
* A YAML document.
*
* Example:
*
* ```
* ---
* x: 0
* y: 1
* ```
*/
class YamlDocument extends YamlNode {
YamlDocument() { not exists(this.getParentNode()) }
}
/**
* An error message produced by the YAML parser while processing a YAML file.
*/
class YamlParseError instanceof Input::ParseErrorBase {
/**
* Gets the `Location` of this error.
*/
Input::Location getLocation() { result = super.getLocation() }
/**
* Gets the message of this error.
*/
string getMessage() { result = super.getMessage() }
/**
* Get the string representation of this error.
*/
string toString() { result = super.getMessage() }
}
/**
* A YAML node that may contain sub-nodes that can be identified by a name.
* I.e. a mapping, sequence, or scalar.
*
* Is used in e.g. GithHub Actions, which is quite flexible in parsing YAML.
*
* For example:
* ```
* on: pull_request
* ```
* and
* ```
* on: [pull_request]
* ```
* and
* ```
* on:
* pull_request:
* ```
*
* are equivalent.
*/
class YamlMappingLikeNode extends YamlNode {
YamlMappingLikeNode() {
this instanceof YamlMapping
or
this instanceof YamlSequence
or
this instanceof YamlScalar
}
/** Gets sub-name identified by `name`. */
YamlNode getNode(string name) {
exists(YamlMapping mapping |
mapping = this and
result = mapping.lookup(name)
)
or
exists(YamlSequence sequence, YamlNode node |
sequence = this and
sequence.getAChildNode() = node and
node.eval().toString() = name and
result = node
)
or
exists(YamlScalar scalar |
scalar = this and
scalar.getValue() = name and
result = scalar
)
}
/** Gets the number of elements in this mapping or sequence. */
int getElementCount() {
exists(YamlMapping mapping |
mapping = this and
result = mapping.getNumChild() / 2
)
or
exists(YamlSequence sequence |
sequence = this and
result = sequence.getNumChild()
)
or
exists(YamlScalar scalar |
scalar = this and
result = 1
)
}
}
}