Skip to content

Commit 32023b2

Browse files
committed
track safe function calls instead of checking parents on calling UntrustedInputChecker.OnVisitNodeLeave
1 parent e25a8b8 commit 32023b2

5 files changed

+67
-210
lines changed

expr_ast.go

+20-115
Original file line numberDiff line numberDiff line change
@@ -5,121 +5,83 @@ package actionlint
55
type ExprNode interface {
66
// Token returns the first token of the node. This method is useful to get position of this node.
77
Token() *Token
8-
// Parent returns the parent node of this node.
9-
Parent() ExprNode
108
}
119

1210
// Variable
1311

1412
// VariableNode is node for variable access.
1513
type VariableNode struct {
1614
// Name is name of the variable
17-
Name string
18-
tok *Token
19-
parent ExprNode
15+
Name string
16+
tok *Token
2017
}
2118

2219
// Token returns the first token of the node. This method is useful to get position of this node.
2320
func (n *VariableNode) Token() *Token {
2421
return n.tok
2522
}
2623

27-
// Parent returns the parent node of this node.
28-
func (n *VariableNode) Parent() ExprNode {
29-
return n.parent
30-
}
31-
3224
// Literals
3325

3426
// NullNode is node for null literal.
3527
type NullNode struct {
36-
tok *Token
37-
parent ExprNode
28+
tok *Token
3829
}
3930

4031
// Token returns the first token of the node. This method is useful to get position of this node.
4132
func (n *NullNode) Token() *Token {
4233
return n.tok
4334
}
4435

45-
// Parent returns the parent node of this node.
46-
func (n *NullNode) Parent() ExprNode {
47-
return n.parent
48-
}
49-
5036
// BoolNode is node for boolean literal, true or false.
5137
type BoolNode struct {
5238
// Value is value of the boolean literal.
53-
Value bool
54-
tok *Token
55-
parent ExprNode
39+
Value bool
40+
tok *Token
5641
}
5742

5843
// Token returns the first token of the node. This method is useful to get position of this node.
5944
func (n *BoolNode) Token() *Token {
6045
return n.tok
6146
}
6247

63-
// Parent returns the parent node of this node.
64-
func (n *BoolNode) Parent() ExprNode {
65-
return n.parent
66-
}
67-
6848
// IntNode is node for integer literal.
6949
type IntNode struct {
7050
// Value is value of the integer literal.
71-
Value int
72-
tok *Token
73-
parent ExprNode
51+
Value int
52+
tok *Token
7453
}
7554

7655
// Token returns the first token of the node. This method is useful to get position of this node.
7756
func (n *IntNode) Token() *Token {
7857
return n.tok
7958
}
8059

81-
// Parent returns the parent node of this node.
82-
func (n *IntNode) Parent() ExprNode {
83-
return n.parent
84-
}
85-
8660
// FloatNode is node for float literal.
8761
type FloatNode struct {
8862
// Value is value of the float literal.
89-
Value float64
90-
tok *Token
91-
parent ExprNode
63+
Value float64
64+
tok *Token
9265
}
9366

9467
// Token returns the first token of the node. This method is useful to get position of this node.
9568
func (n *FloatNode) Token() *Token {
9669
return n.tok
9770
}
9871

99-
// Parent returns the parent node of this node.
100-
func (n *FloatNode) Parent() ExprNode {
101-
return n.parent
102-
}
103-
10472
// StringNode is node for string literal.
10573
type StringNode struct {
10674
// Value is value of the string literal. Escapes are resolved and quotes at both edges are
10775
// removed.
108-
Value string
109-
tok *Token
110-
parent ExprNode
76+
Value string
77+
tok *Token
11178
}
11279

11380
// Token returns the first token of the node. This method is useful to get position of this node.
11481
func (n *StringNode) Token() *Token {
11582
return n.tok
11683
}
11784

118-
// Parent returns the parent node of this node.
119-
func (n *StringNode) Parent() ExprNode {
120-
return n.parent
121-
}
122-
12385
// Operators
12486

12587
// ObjectDerefNode represents property dereference of object like 'foo.bar'.
@@ -128,76 +90,52 @@ type ObjectDerefNode struct {
12890
Receiver ExprNode
12991
// Property is a name of property to access.
13092
Property string
131-
parent ExprNode
13293
}
13394

13495
// Token returns the first token of the node. This method is useful to get position of this node.
135-
func (n *ObjectDerefNode) Token() *Token {
96+
func (n ObjectDerefNode) Token() *Token {
13697
return n.Receiver.Token()
13798
}
13899

139-
// Parent returns the parent node of this node.
140-
func (n *ObjectDerefNode) Parent() ExprNode {
141-
return n.parent
142-
}
143-
144100
// ArrayDerefNode represents elements dereference of arrays like '*' in 'foo.bar.*.piyo'.
145101
type ArrayDerefNode struct {
146102
// Receiver is an expression at receiver of array element dereference.
147103
Receiver ExprNode
148-
parent ExprNode
149104
}
150105

151106
// Token returns the first token of the node. This method is useful to get position of this node.
152-
func (n *ArrayDerefNode) Token() *Token {
107+
func (n ArrayDerefNode) Token() *Token {
153108
return n.Receiver.Token()
154109
}
155110

156-
// Parent returns the parent node of this node.
157-
func (n *ArrayDerefNode) Parent() ExprNode {
158-
return n.parent
159-
}
160-
161111
// IndexAccessNode is node for index access, which represents dynamic object property access or
162112
// array index access.
163113
type IndexAccessNode struct {
164114
// Operand is an expression at operand of index access, which should be array or object.
165115
Operand ExprNode
166116
// Index is an expression at index, which should be integer or string.
167-
Index ExprNode
168-
parent ExprNode
117+
Index ExprNode
169118
}
170119

171120
// Token returns the first token of the node. This method is useful to get position of this node.
172121
func (n *IndexAccessNode) Token() *Token {
173122
return n.Operand.Token()
174123
}
175124

176-
// Parent returns the parent node of this node.
177-
func (n *IndexAccessNode) Parent() ExprNode {
178-
return n.parent
179-
}
180-
181125
// Note: Currently only ! is a logical unary operator
182126

183127
// NotOpNode is node for unary ! operator.
184128
type NotOpNode struct {
185129
// Operand is an expression at operand of ! operator.
186130
Operand ExprNode
187131
tok *Token
188-
parent ExprNode
189132
}
190133

191134
// Token returns the first token of the node. This method is useful to get position of this node.
192135
func (n *NotOpNode) Token() *Token {
193136
return n.tok
194137
}
195138

196-
// Parent returns the parent node of this node.
197-
func (n *NotOpNode) Parent() ExprNode {
198-
return n.parent
199-
}
200-
201139
// CompareOpNodeKind is a kind of compare operators; ==, !=, <, <=, >, >=.
202140
type CompareOpNodeKind int
203141

@@ -249,20 +187,14 @@ type CompareOpNode struct {
249187
// Left is an expression for left hand side of the binary operator.
250188
Left ExprNode
251189
// Right is an expression for right hand side of the binary operator.
252-
Right ExprNode
253-
parent ExprNode
190+
Right ExprNode
254191
}
255192

256193
// Token returns the first token of the node. This method is useful to get position of this node.
257194
func (n *CompareOpNode) Token() *Token {
258195
return n.Left.Token()
259196
}
260197

261-
// Parent returns the parent node of this node.
262-
func (n *CompareOpNode) Parent() ExprNode {
263-
return n.parent
264-
}
265-
266198
// LogicalOpNodeKind is a kind of logical operators; && and ||.
267199
type LogicalOpNodeKind int
268200

@@ -293,42 +225,30 @@ type LogicalOpNode struct {
293225
// Left is an expression for left hand side of the binary operator.
294226
Left ExprNode
295227
// Right is an expression for right hand side of the binary operator.
296-
Right ExprNode
297-
parent ExprNode
228+
Right ExprNode
298229
}
299230

300231
// Token returns the first token of the node. This method is useful to get position of this node.
301232
func (n *LogicalOpNode) Token() *Token {
302233
return n.Left.Token()
303234
}
304235

305-
// Parent returns the parent node of this node.
306-
func (n *LogicalOpNode) Parent() ExprNode {
307-
return n.parent
308-
}
309-
310236
// FuncCallNode represents function call in expression.
311237
// Note that currently only calling builtin functions is supported.
312238
type FuncCallNode struct {
313239
// Callee is a name of called function. This is string value because currently only built-in
314240
// functions can be called.
315241
Callee string
316242
// Args is arguments of the function call.
317-
Args []ExprNode
318-
tok *Token
319-
parent ExprNode
243+
Args []ExprNode
244+
tok *Token
320245
}
321246

322247
// Token returns the first token of the node. This method is useful to get position of this node.
323248
func (n *FuncCallNode) Token() *Token {
324249
return n.tok
325250
}
326251

327-
// Parent returns the parent node of this node.
328-
func (n *FuncCallNode) Parent() ExprNode {
329-
return n.parent
330-
}
331-
332252
// VisitExprNodeFunc is a visitor function for VisitExprNode(). The entering argument is set to
333253
// true when it is called before visiting children. It is set to false when it is called after
334254
// visiting children. It means that this function is called twice for the same node. The parent
@@ -355,8 +275,8 @@ func visitExprNode(n, p ExprNode, f VisitExprNodeFunc) {
355275
visitExprNode(n.Left, n, f)
356276
visitExprNode(n.Right, n, f)
357277
case *FuncCallNode:
358-
for i := range n.Args {
359-
visitExprNode(n.Args[i], n, f)
278+
for _, a := range n.Args {
279+
visitExprNode(a, n, f)
360280
}
361281
}
362282
f(n, p, false)
@@ -366,18 +286,3 @@ func visitExprNode(n, p ExprNode, f VisitExprNodeFunc) {
366286
func VisitExprNode(n ExprNode, f VisitExprNodeFunc) {
367287
visitExprNode(n, nil, f)
368288
}
369-
370-
// FindParent applies predicate to each parent of this node until predicate returns true.
371-
// Then it returns result of predicate. If no parent found, returns nil, false.
372-
func FindParent[T ExprNode](n ExprNode, predicate func(n ExprNode) (T, bool)) (T, bool) {
373-
parent := n.Parent()
374-
for parent != nil {
375-
t, ok := predicate(parent)
376-
if ok {
377-
return t, true
378-
}
379-
parent = parent.Parent()
380-
}
381-
var zero T
382-
return zero, false
383-
}

0 commit comments

Comments
 (0)