Skip to content

Commit 1f00ba8

Browse files
committed
move YAMLMappingLikeNode to the standard library
1 parent 8e2b00d commit 1f00ba8

File tree

3 files changed

+74
-73
lines changed

3 files changed

+74
-73
lines changed

javascript/ql/lib/semmle/javascript/Actions.qll

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -20,76 +20,6 @@ module Actions {
2020
}
2121
}
2222

23-
/**
24-
* A YAML node that may contain sub-nodes.
25-
*
26-
* Actions are quite flexible in parsing YAML.
27-
*
28-
* For example:
29-
* ```
30-
* on: pull_request
31-
* ```
32-
* and
33-
* ```
34-
* on: [pull_request]
35-
* ```
36-
* and
37-
* ```
38-
* on:
39-
* pull_request:
40-
* ```
41-
*
42-
* are equivalent.
43-
*/
44-
class MappingOrSequenceOrScalar extends YAMLNode {
45-
MappingOrSequenceOrScalar() {
46-
this instanceof YAMLMapping
47-
or
48-
this instanceof YAMLSequence
49-
or
50-
this instanceof YAMLScalar
51-
}
52-
53-
/** Gets sub-name identified by `name`. */
54-
YAMLNode getNode(string name) {
55-
exists(YAMLMapping mapping |
56-
mapping = this and
57-
result = mapping.lookup(name)
58-
)
59-
or
60-
exists(YAMLSequence sequence, YAMLNode node |
61-
sequence = this and
62-
sequence.getAChildNode() = node and
63-
node.eval().toString() = name and
64-
result = node
65-
)
66-
or
67-
exists(YAMLScalar scalar |
68-
scalar = this and
69-
scalar.getValue() = name and
70-
result = scalar
71-
)
72-
}
73-
74-
/** Gets the number of elements in this mapping or sequence. */
75-
int getElementCount() {
76-
exists(YAMLMapping mapping |
77-
mapping = this and
78-
result = mapping.getNumChild() / 2
79-
)
80-
or
81-
exists(YAMLSequence sequence |
82-
sequence = this and
83-
result = sequence.getNumChild()
84-
)
85-
or
86-
exists(YAMLScalar scalar |
87-
scalar = this and
88-
result = 1
89-
)
90-
}
91-
}
92-
9323
/**
9424
* An Actions workflow. This is a mapping at the top level of an Actions YAML workflow file.
9525
* See https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions.
@@ -112,7 +42,7 @@ module Actions {
11242
* An Actions On trigger within a workflow.
11343
* See https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#on.
11444
*/
115-
class On extends YAMLNode, MappingOrSequenceOrScalar {
45+
class On extends YAMLNode, YAMLMappingLikeNode {
11646
Workflow workflow;
11747

11848
On() { workflow.lookup("on") = this }

javascript/ql/lib/semmle/javascript/YAML.qll

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,74 @@ class YAMLParseError extends @yaml_error, Error {
441441

442442
override string toString() { result = this.getMessage() }
443443
}
444+
445+
/**
446+
* A YAML node that may contain sub-nodes that can be identified by a name.
447+
* I.e. a mapping, sequence, or scalar.
448+
*
449+
* Is used in e.g. GithHub Actions, which is quite flexible in parsing YAML.
450+
*
451+
* For example:
452+
* ```
453+
* on: pull_request
454+
* ```
455+
* and
456+
* ```
457+
* on: [pull_request]
458+
* ```
459+
* and
460+
* ```
461+
* on:
462+
* pull_request:
463+
* ```
464+
*
465+
* are equivalent.
466+
*/
467+
class YAMLMappingLikeNode extends YAMLNode {
468+
YAMLMappingLikeNode() {
469+
this instanceof YAMLMapping
470+
or
471+
this instanceof YAMLSequence
472+
or
473+
this instanceof YAMLScalar
474+
}
475+
476+
/** Gets sub-name identified by `name`. */
477+
YAMLNode getNode(string name) {
478+
exists(YAMLMapping mapping |
479+
mapping = this and
480+
result = mapping.lookup(name)
481+
)
482+
or
483+
exists(YAMLSequence sequence, YAMLNode node |
484+
sequence = this and
485+
sequence.getAChildNode() = node and
486+
node.eval().toString() = name and
487+
result = node
488+
)
489+
or
490+
exists(YAMLScalar scalar |
491+
scalar = this and
492+
scalar.getValue() = name and
493+
result = scalar
494+
)
495+
}
496+
497+
/** Gets the number of elements in this mapping or sequence. */
498+
int getElementCount() {
499+
exists(YAMLMapping mapping |
500+
mapping = this and
501+
result = mapping.getNumChild() / 2
502+
)
503+
or
504+
exists(YAMLSequence sequence |
505+
sequence = this and
506+
result = sequence.getNumChild()
507+
)
508+
or
509+
exists(YAMLScalar scalar |
510+
scalar = this and
511+
result = 1
512+
)
513+
}
514+
}

javascript/ql/src/experimental/Security/CWE-094/UntrustedCheckout.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class ProbableJob extends Actions::Job {
7878
/**
7979
* An action step that doesn't contain `actor` or `label` check in `if:` or
8080
*/
81-
class ProbablePullRequestTarget extends Actions::On, Actions::MappingOrSequenceOrScalar {
81+
class ProbablePullRequestTarget extends Actions::On, YAMLMappingLikeNode {
8282
ProbablePullRequestTarget() {
8383
exists(YAMLNode prtNode |
8484
// The `on:` is triggered on `pull_request_target`
@@ -88,7 +88,7 @@ class ProbablePullRequestTarget extends Actions::On, Actions::MappingOrSequenceO
8888
not exists(prtNode.getAChild())
8989
or
9090
// or has the filter, that is something else than just [labeled]
91-
exists(Actions::MappingOrSequenceOrScalar prt, Actions::MappingOrSequenceOrScalar types |
91+
exists(YAMLMappingLikeNode prt, YAMLMappingLikeNode types |
9292
types = prt.getNode("types") and
9393
prtNode = prt and
9494
(

0 commit comments

Comments
 (0)