summaryrefslogtreecommitdiffstats
path: root/meta/recipes-devtools/python/python3-pygments/CVE-2022-40896-0002.patch
blob: 61ebe5dad5f1daa8990fa38968a50a8695875967 (plain)
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
From 45ff8eabe0363f829c397372aefc3b23aeb135b3 Mon Sep 17 00:00:00 2001
From: Narpat Mali <narpat.mali@windriver.com>
Date: Tue, 29 Aug 2023 10:45:34 +0000
Subject: [PATCH] Improve Java properties lexer (#2404)

Use special lexer rules for escapes; fixes catastrophic backtracking,
and highlights them too.

Fixes #2356

CVE: CVE-2022-40896

Upstream-Status: Backport [https://github.com/pygments/pygments/commit/fdf182a7af85b1deeeb637ca970d31935e7c9d52]

Signed-off-by: Narpat Mali <narpat.mali@windriver.com>
---
 pygments/lexers/configs.py                    |  50 +++++---
 tests/examplefiles/properties/java.properties |  11 ++
 .../properties/java.properties.output         | 110 +++++++++++++++---
 .../test_escaped_space_in_value.txt           |   4 +-
 .../properties/test_just_key_with_space.txt   |   4 +-
 5 files changed, 143 insertions(+), 36 deletions(-)

diff --git a/pygments/lexers/configs.py b/pygments/lexers/configs.py
index e04c722..b28b56a 100644
--- a/pygments/lexers/configs.py
+++ b/pygments/lexers/configs.py
@@ -129,26 +129,42 @@ class PropertiesLexer(RegexLexer):

     tokens = {
         'root': [
-            (r'\s+', Whitespace),
+            # comments
             (r'[!#].*|/{2}.*', Comment.Single),
-            # search for first separator
-            (r'([^\\\n]|\\.)*?(?=[ \f\t=:])', Name.Attribute, "separator"),
-            # empty key
-            (r'.+?$', Name.Attribute),
+            # ending a comment or whitespace-only line
+            (r'\n', Whitespace),
+            # eat whitespace at the beginning of a line
+            (r'^[^\S\n]+', Whitespace),
+            # start lexing a key
+            default('key'),
         ],
-        'separator': [
-            # search for line continuation escape
-            (r'([ \f\t]*)([=:]*)([ \f\t]*)(.*(?<!\\)(?:\\{2})*)(\\)(?!\\)$',
-             bygroups(Whitespace, Operator, Whitespace, String, Text), "value", "#pop"),
-            (r'([ \f\t]*)([=:]*)([ \f\t]*)(.*)',
-             bygroups(Whitespace, Operator, Whitespace, String), "#pop"),
+        'key': [
+            # non-escaped key characters
+            (r'[^\\:=\s]+', Name.Attribute),
+            # escapes
+            include('escapes'),
+            # separator is the first non-escaped whitespace or colon or '=' on the line;
+            # if it's whitespace, = and : are gobbled after it
+            (r'([^\S\n]*)([:=])([^\S\n]*)',
+             bygroups(Whitespace, Operator, Whitespace),
+             ('#pop', 'value')),
+            (r'[^\S\n]+', Whitespace, ('#pop', 'value')),
+            # maybe we got no value after all
+            (r'\n', Whitespace, '#pop'),
         ],
-        'value': [     # line continuation
-            (r'\s+', Whitespace),
-            # search for line continuation escape
-            (r'(\s*)(.*(?<!\\)(?:\\{2})*)(\\)(?!\\)([ \t]*)',
-             bygroups(Whitespace, String, Text, Whitespace)),
-            (r'.*$', String, "#pop"),
+        'value': [
+            # non-escaped value characters
+            (r'[^\\\n]+', String),
+            # escapes
+            include('escapes'),
+            # end the value on an unescaped newline
+            (r'\n', Whitespace, '#pop'),
+        ],
+        'escapes': [
+            # line continuations; these gobble whitespace at the beginning of the next line
+            (r'(\\\n)([^\S\n]*)', bygroups(String.Escape, Whitespace)),
+            # other escapes
+            (r'\\(.|\n)', String.Escape),
         ],
     }

diff --git a/tests/examplefiles/properties/java.properties b/tests/examplefiles/properties/java.properties
index d5b594e..7fe915c 100644
--- a/tests/examplefiles/properties/java.properties
+++ b/tests/examplefiles/properties/java.properties
@@ -14,6 +14,8 @@ key = \
     and value2\\
 key\ 2 = value
 key\\ 3 = value3
+key \
+  = value

 ! empty keys and edge cases
 key1 =
@@ -22,3 +24,12 @@ key3 the value3
 key4 the:value4
 key5 the=value5
 key6=the value6
+
+! escapes in keys
+key\ with\ spaces = value
+key\nwith\nnewlines = value\nwith\nnewlines
+
+   ! indented comment
+
+! line continuations do \
+not = work for comments
diff --git a/tests/examplefiles/properties/java.properties.output b/tests/examplefiles/properties/java.properties.output
index 0c1fdee..4822575 100644
--- a/tests/examplefiles/properties/java.properties.output
+++ b/tests/examplefiles/properties/java.properties.output
@@ -2,13 +2,17 @@
 '\n'          Text.Whitespace

 '# mixing spaces' Comment.Single
-'\n\t'        Text.Whitespace
+'\n'          Text.Whitespace
+
+'\t'          Text.Whitespace
 'Truth'       Name.Attribute
 ' '           Text.Whitespace
 '='           Operator
 ' '           Text.Whitespace
 'Beauty'      Literal.String
-'\n  '        Text.Whitespace
+'\n'          Text.Whitespace
+
+'  '          Text.Whitespace
 'Truth'       Name.Attribute
 ':'           Operator
 'Beauty'      Literal.String
@@ -23,18 +27,24 @@
 '                    ' Text.Whitespace
 ':'           Operator
 'Beauty'      Literal.String
-'\n \n'       Text.Whitespace
+'\n'          Text.Whitespace
+
+'\n'          Text.Whitespace

 '! line continuations and escapes' Comment.Single
-'\n '         Text.Whitespace
+'\n'          Text.Whitespace
+
+' '           Text.Whitespace
 'fruits'      Name.Attribute
 '                           ' Text.Whitespace
 'apple, banana, pear, ' Literal.String
-'\\'          Text
-'\n                                  ' Text.Whitespace
+'\\\n'        Literal.String.Escape
+
+'                                  ' Text.Whitespace
 'cantaloupe, watermelon, ' Literal.String
-'\\'          Text
-'\n                                  ' Text.Whitespace
+'\\\n'        Literal.String.Escape
+
+'                                  ' Text.Whitespace
 'kiwi, mango' Literal.String
 '\n'          Text.Whitespace

@@ -42,25 +52,42 @@
 ' '           Text.Whitespace
 '='           Operator
 ' '           Text.Whitespace
-'\\'          Text
-'\n    '      Text.Whitespace
-'value1 \\\\' Literal.String
-'\\'          Text
-'\n    '      Text.Whitespace
-'and value2\\\\' Literal.String
+'\\\n'        Literal.String.Escape
+
+'    '        Text.Whitespace
+'value1 '     Literal.String
+'\\\\'        Literal.String.Escape
+'\\\n'        Literal.String.Escape
+
+'    '        Text.Whitespace
+'and value2'  Literal.String
+'\\\\'        Literal.String.Escape
 '\n'          Text.Whitespace

-'key\\ 2'     Name.Attribute
+'key'         Name.Attribute
+'\\ '         Literal.String.Escape
+'2'           Name.Attribute
 ' '           Text.Whitespace
 '='           Operator
 ' '           Text.Whitespace
 'value'       Literal.String
 '\n'          Text.Whitespace

-'key\\\\'     Name.Attribute
+'key'         Name.Attribute
+'\\\\'        Literal.String.Escape
 ' '           Text.Whitespace
 '3 = value3'  Literal.String
-'\n\n'        Text.Whitespace
+'\n'          Text.Whitespace
+
+'key'         Name.Attribute
+' '           Text.Whitespace
+'\\\n'        Literal.String.Escape
+
+'  '          Text.Whitespace
+'= value'     Literal.String
+'\n'          Text.Whitespace
+
+'\n'          Text.Whitespace

 '! empty keys and edge cases' Comment.Single
 '\n'          Text.Whitespace
@@ -92,3 +119,52 @@
 '='           Operator
 'the value6'  Literal.String
 '\n'          Text.Whitespace
+
+'\n'          Text.Whitespace
+
+'! escapes in keys' Comment.Single
+'\n'          Text.Whitespace
+
+'key'         Name.Attribute
+'\\ '         Literal.String.Escape
+'with'        Name.Attribute
+'\\ '         Literal.String.Escape
+'spaces'      Name.Attribute
+' '           Text.Whitespace
+'='           Operator
+' '           Text.Whitespace
+'value'       Literal.String
+'\n'          Text.Whitespace
+
+'key'         Name.Attribute
+'\\n'         Literal.String.Escape
+'with'        Name.Attribute
+'\\n'         Literal.String.Escape
+'newlines'    Name.Attribute
+' '           Text.Whitespace
+'='           Operator
+' '           Text.Whitespace
+'value'       Literal.String
+'\\n'         Literal.String.Escape
+'with'        Literal.String
+'\\n'         Literal.String.Escape
+'newlines'    Literal.String
+'\n'          Text.Whitespace
+
+'\n'          Text.Whitespace
+
+'   '         Text.Whitespace
+'! indented comment' Comment.Single
+'\n'          Text.Whitespace
+
+'\n'          Text.Whitespace
+
+'! line continuations do \\' Comment.Single
+'\n'          Text.Whitespace
+
+'not'         Name.Attribute
+' '           Text.Whitespace
+'='           Operator
+' '           Text.Whitespace
+'work for comments' Literal.String
+'\n'          Text.Whitespace
diff --git a/tests/snippets/properties/test_escaped_space_in_value.txt b/tests/snippets/properties/test_escaped_space_in_value.txt
index f76507f..44772d8 100644
--- a/tests/snippets/properties/test_escaped_space_in_value.txt
+++ b/tests/snippets/properties/test_escaped_space_in_value.txt
@@ -6,5 +6,7 @@ key = doubleword\ value
 ' '           Text.Whitespace
 '='           Operator
 ' '           Text.Whitespace
-'doubleword\\ value' Literal.String
+'doubleword'  Literal.String
+'\\ '         Literal.String.Escape
+'value'       Literal.String
 '\n'          Text.Whitespace
diff --git a/tests/snippets/properties/test_just_key_with_space.txt b/tests/snippets/properties/test_just_key_with_space.txt
index 660c37c..833fe40 100644
--- a/tests/snippets/properties/test_just_key_with_space.txt
+++ b/tests/snippets/properties/test_just_key_with_space.txt
@@ -2,5 +2,7 @@
 just\ key

 ---tokens---
-'just\\ key'  Name.Attribute
+'just'        Name.Attribute
+'\\ '         Literal.String.Escape
+'key'         Name.Attribute
 '\n'          Text.Whitespace
--
2.40.0