Debian Codegraph @

pkg/cel/escaping_test.go

raw
1 /*
2 Copyright 2021 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package cel
18
19 import (
20 "fmt"
21 "regexp"
22 "strings"
23 "testing"
24
25 "sigs.k8s.io/randfill"
26 )
27
28 // TestEscaping tests that property names are escaped as expected.
29 func TestEscaping(t *testing.T) {
30 cases := []struct {
31 unescaped string
32 escaped string
33 unescapable bool
34 }{
35 // '.', '-', '/' and '__' are escaped since
36 // CEL only allows identifiers of the form: [a-zA-Z_][a-zA-Z0-9_]*
37 {unescaped: "a.a", escaped: "a__dot__a"},
38 {unescaped: "a-a", escaped: "a__dash__a"},
39 {unescaped: "a__a", escaped: "a__underscores__a"},
40 {unescaped: "a.-/__a", escaped: "a__dot____dash____slash____underscores__a"},
41 {unescaped: "a._a", escaped: "a__dot___a"},
42 {unescaped: "a__.__a", escaped: "a__underscores____dot____underscores__a"},
43 {unescaped: "a___a", escaped: "a__underscores___a"},
44 {unescaped: "a____a", escaped: "a__underscores____underscores__a"},
45 {unescaped: "a__dot__a", escaped: "a__underscores__dot__underscores__a"},
46 {unescaped: "a__underscores__a", escaped: "a__underscores__underscores__underscores__a"},
47 // CEL lexer RESERVED keywords must be escaped
48 {unescaped: "true", escaped: "__true__"},
49 {unescaped: "false", escaped: "__false__"},
50 {unescaped: "null", escaped: "__null__"},
51 {unescaped: "in", escaped: "__in__"},
52 {unescaped: "as", escaped: "__as__"},
53 {unescaped: "break", escaped: "__break__"},
54 {unescaped: "const", escaped: "__const__"},
55 {unescaped: "continue", escaped: "__continue__"},
56 {unescaped: "else", escaped: "__else__"},
57 {unescaped: "for", escaped: "__for__"},
58 {unescaped: "function", escaped: "__function__"},
59 {unescaped: "if", escaped: "__if__"},
60 {unescaped: "import", escaped: "__import__"},
61 {unescaped: "let", escaped: "__let__"},
62 {unescaped: "loop", escaped: "__loop__"},
63 {unescaped: "package", escaped: "__package__"},
64 {unescaped: "namespace", escaped: "__namespace__"},
65 {unescaped: "return", escaped: "__return__"},
66 {unescaped: "var", escaped: "__var__"},
67 {unescaped: "void", escaped: "__void__"},
68 {unescaped: "while", escaped: "__while__"},
69 // Not all property names are escapable
70 {unescaped: "@", unescapable: true},
71 {unescaped: "1up", unescapable: true},
72 {unescaped: "👑", unescapable: true},
73 // CEL macro and function names do not need to be escaped because the parser keeps identifiers in a
74 // different namespace than function and macro names.
75 {unescaped: "has", escaped: "has"},
76 {unescaped: "all", escaped: "all"},
77 {unescaped: "exists", escaped: "exists"},
78 {unescaped: "exists_one", escaped: "exists_one"},
79 {unescaped: "filter", escaped: "filter"},
80 {unescaped: "size", escaped: "size"},
81 {unescaped: "contains", escaped: "contains"},
82 {unescaped: "startsWith", escaped: "startsWith"},
83 {unescaped: "endsWith", escaped: "endsWith"},
84 {unescaped: "matches", escaped: "matches"},
85 {unescaped: "duration", escaped: "duration"},
86 {unescaped: "timestamp", escaped: "timestamp"},
87 {unescaped: "getDate", escaped: "getDate"},
88 {unescaped: "getDayOfMonth", escaped: "getDayOfMonth"},
89 {unescaped: "getDayOfWeek", escaped: "getDayOfWeek"},
90 {unescaped: "getFullYear", escaped: "getFullYear"},
91 {unescaped: "getHours", escaped: "getHours"},
92 {unescaped: "getMilliseconds", escaped: "getMilliseconds"},
93 {unescaped: "getMinutes", escaped: "getMinutes"},
94 {unescaped: "getMonth", escaped: "getMonth"},
95 {unescaped: "getSeconds", escaped: "getSeconds"},
96 // we don't escape a single _
97 {unescaped: "_if", escaped: "_if"},
98 {unescaped: "_has", escaped: "_has"},
99 {unescaped: "_int", escaped: "_int"},
100 {unescaped: "_anything", escaped: "_anything"},
101 }
102
103 for _, tc := range cases {
104 t.Run(tc.unescaped, func(t *testing.T) {
105 e, escapable := Escape(tc.unescaped)
106 if tc.unescapable {
107 if escapable {
108 t.Errorf("Expected escapable=false, but got %t", escapable)
109 }
110 return
111 }
112 if !escapable {
113 t.Fatalf("Expected escapable=true, but got %t", escapable)
114 }
115 if tc.escaped != e {
116 t.Errorf("Expected %s to escape to %s, but got %s", tc.unescaped, tc.escaped, e)
117 }
118
119 if !validCelIdent.MatchString(e) {
120 t.Errorf("Expected %s to escape to a valid CEL identifier, but got %s", tc.unescaped, e)
121 }
122
123 u, ok := Unescape(tc.escaped)
124 if !ok {
125 t.Fatalf("Expected %s to be escapable, but it was not", tc.escaped)
126 }
127 if tc.unescaped != u {
128 t.Errorf("Expected %s to unescape to %s, but got %s", tc.escaped, tc.unescaped, u)
129 }
130 })
131 }
132 }
133
134 func TestUnescapeMalformed(t *testing.T) {
135 for _, s := range []string{"__int__extra", "__illegal__"} {
136 t.Run(s, func(t *testing.T) {
137 e, ok := Unescape(s)
138 if ok {
139 t.Fatalf("Expected %s to be unescapable, but it escaped to: %s", s, e)
140 }
141 })
142 }
143 }
144
145 func TestEscapingFuzz(t *testing.T) {
146 fuzzer := randfill.New()
147 for i := 0; i < 1000; i++ {
148 var unescaped string
149 fuzzer.Fill(&unescaped)
150 t.Run(fmt.Sprintf("%d - '%s'", i, unescaped), func(t *testing.T) {
151 if len(unescaped) == 0 {
152 return
153 }
154 escaped, ok := Escape(unescaped)
155 if !ok {
156 return
157 }
158
159 if !validCelIdent.MatchString(escaped) {
160 t.Errorf("Expected %s to escape to a valid CEL identifier, but got %s", unescaped, escaped)
161 }
162 u, ok := Unescape(escaped)
163 if !ok {
164 t.Fatalf("Expected %s to be unescapable, but it was not", escaped)
165 }
166 if unescaped != u {
167 t.Errorf("Expected %s to unescape to %s, but got %s", escaped, unescaped, u)
168 }
169 })
170 }
171 }
172
173 var validCelIdent = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*$`)
174
175 func TestCanSkipRegex(t *testing.T) {
176 cases := []struct {
177 unescaped string
178 canSkip bool
179 invalidCharFound bool
180 }{
181 {unescaped: "a.a", canSkip: false},
182 {unescaped: "a-a", canSkip: false},
183 {unescaped: "a__a", canSkip: false},
184 {unescaped: "a.-/__a", canSkip: false},
185 {unescaped: "a_a", canSkip: true},
186 {unescaped: "a_a_a", canSkip: true},
187 {unescaped: "@", invalidCharFound: true},
188 {unescaped: "👑", invalidCharFound: true},
189 // if escape keyword is detected before invalid character, invalidCharFound
190 {unescaped: "/👑", canSkip: false},
191 }
192
193 for _, tc := range cases {
194 t.Run(tc.unescaped, func(t *testing.T) {
195 escapeCheck := skipRegexCheck(tc.unescaped)
196 if escapeCheck.invalidCharFound {
197 if escapeCheck.invalidCharFound != tc.invalidCharFound {
198 t.Errorf("Expected input validation to be %v, but got %t", tc.invalidCharFound, escapeCheck.invalidCharFound)
199 }
200 } else {
201 if escapeCheck.canSkipRegex != tc.canSkip {
202 t.Errorf("Expected %v, but got %t", tc.canSkip, escapeCheck.canSkipRegex)
203 }
204 }
205 })
206 }
207 }
208
209 func TestCELReservedSymbolsNoDoubleUnderscore(t *testing.T) {
210 for symbol := range celReservedSymbols {
211 if strings.Contains(symbol, "__") {
212 t.Errorf("CEL reserved symbol '%s' contains '__', which is not allowed as it would interfere with escaping", symbol)
213 }
214 }
215 }