Debian Codegraph @

podofo/test/unit/TokenizerTest.cpp

raw
1 // SPDX-FileCopyrightText: 2007 Dominik Seichter <[email protected]>
2 // SPDX-FileCopyrightText: 2021 Francesco Pretto <[email protected]>
3 // SPDX-License-Identifier: MIT-0
4
5 #include <PdfTest.h>
6
7 using namespace std;
8 using namespace PoDoFo;
9
10 static void Test(const string_view& buffer, PdfDataType dataType, string_view expected = { });
11 static void TestStream(const string_view& buffer, const char* tokens[]);
12 static void TestStreamIsNextToken(const string_view& buffer, const char* tokens[]);
13
14 TEST_CASE("TestArrays")
15 {
16 Test("[]", PdfDataType::Array);
17 Test("[ ]", PdfDataType::Array, "[]");
18 Test("[ / ]", PdfDataType::Array, "[/]"); // empty names are legal, too!
19 Test("[ / [ ] ]", PdfDataType::Array, "[/[]]"); // empty names are legal, too!
20 Test("[/[]]", PdfDataType::Array, "[/[]]"); // empty names are legal, too!
21 Test("[ 1 2 3 4 ]", PdfDataType::Array, "[ 1 2 3 4]");
22 Test("[1 2 3 4]", PdfDataType::Array, "[ 1 2 3 4]");
23 Test("[ 2 (Hallo Welt!) 3.500000 /FMC ]", PdfDataType::Array, "[ 2(Hallo Welt!) 3.5/FMC]");
24 Test("[ [ 1 2 ] (Hallo Welt!) 3.500000 /FMC ]", PdfDataType::Array, "[[ 1 2](Hallo Welt!) 3.5/FMC]");
25 Test("[/ImageA/ImageB/ImageC]", PdfDataType::Array, "[/ImageA/ImageB/ImageC]");
26 Test("[<530464995927cef8aaf46eb953b93373><530464995927cef8aaf46eb953b93373>]", PdfDataType::Array, "[<530464995927CEF8AAF46EB953B93373><530464995927CEF8AAF46EB953B93373>]");
27 Test("[ 2 0 R (Test Data) 4 << /Key /Data >> 5 0 R ]", PdfDataType::Array, "[ 2 0 R(Test Data) 4<</Key/Data>> 5 0 R]");
28 Test("[<</key/name>>2 0 R]", PdfDataType::Array, "[<</key/name>> 2 0 R]");
29 Test("[<<//name>>2 0 R]", PdfDataType::Array, "[<<//name>> 2 0 R]");
30 Test("[ 27.673200 27.673200 566.256000 651.295000 ]", PdfDataType::Array, "[ 27.6732 27.6732 566.256 651.295]");
31 }
32
33 TEST_CASE("TestBool")
34 {
35 Test("false", PdfDataType::Bool);
36 Test("true", PdfDataType::Bool);
37 }
38
39 TEST_CASE("TestHexString")
40 {
41 Test("<FFEB0400A0CC>", PdfDataType::String);
42 Test("<FFEB0400A0C>", PdfDataType::String, "<FFEB0400A0C0>");
43 Test("<>", PdfDataType::String);
44 }
45
46 TEST_CASE("TestName")
47 {
48 Test("/Type", PdfDataType::Name);
49 Test("/Length", PdfDataType::Name);
50 Test("/Adobe#20Green", PdfDataType::Name);
51 Test("/$$", PdfDataType::Name);
52 Test("/1.2", PdfDataType::Name);
53 Test("/.notdef", PdfDataType::Name);
54 Test("/@pattern", PdfDataType::Name);
55 Test("/A;Name_With-Various***Characters?", PdfDataType::Name);
56 Test("/", PdfDataType::Name); // empty names are legal, too!
57 }
58
59 TEST_CASE("TestName2")
60 {
61 // Some additional tests, which cause errors for Sebastian Loch
62 string_view buffer = "/CheckBox#C3#9Cbersetzungshinweis";
63 SpanStreamDevice device(buffer);
64
65 PdfTokenizer tokenizer;
66 PdfVariant variant;
67 REQUIRE(tokenizer.TryReadNextVariant(device, variant));
68
69 PdfName name(variant.GetName());
70 auto nameStr = name.GetString();
71
72 REQUIRE(variant.GetName() == name);
73 REQUIRE(name == nameStr);
74
75 INFO(utls::Format("!!! Name=[{}]\n", variant.GetName().GetString()));
76 INFO(utls::Format("!!! Name2=[{}]\n", name.GetString()));
77 }
78
79 TEST_CASE("TestNull")
80 {
81 Test("null", PdfDataType::Null);
82 }
83
84 TEST_CASE("TestNumbers")
85 {
86 Test("145", PdfDataType::Number);
87 Test("-12", PdfDataType::Number);
88 Test("3.141230", PdfDataType::Real, "3.14123");
89 Test("-2.970000", PdfDataType::Real, "-2.97");
90 Test("0", PdfDataType::Number);
91 Test("4.", PdfDataType::Real, "4");
92 }
93
94 TEST_CASE("TestReference")
95 {
96 Test("2 0 R", PdfDataType::Reference);
97 Test("3 0 R", PdfDataType::Reference);
98 Test("4 1 R", PdfDataType::Reference);
99 }
100
101 TEST_CASE("TestString")
102 {
103 // testing strings
104 Test("(Hallo Welt!)", PdfDataType::String);
105 Test("(Hallo \\(schöne\\) Welt!)", PdfDataType::String);
106 Test("(Balanced () brackets are (ok ()) in PDF Strings)", PdfDataType::String,
107 "(Balanced \\(\\) brackets are \\(ok \\(\\)\\) in PDF Strings)");
108 Test("()", PdfDataType::String);
109
110 // Test octal strings
111 Test("(Test: \\064)", PdfDataType::String, "(Test: \064)");
112 Test("(Test: \\064\\064)", PdfDataType::String, "(Test: \064\064)");
113 Test("(Test: \\0645)", PdfDataType::String, "(Test: 45)");
114 Test("(Test: \\478)", PdfDataType::String, "(Test: '8)");
115
116 // Test line breaks
117 Test("(Hallo\nWelt!)", PdfDataType::String, "(Hallo\\nWelt!)");
118 Test("(These \\\ntwo strings \\\nare the same.)", PdfDataType::String,
119 "(These two strings are the same.)");
120
121 // Test escape sequences
122 Test("(Hallo\\nWelt!)", PdfDataType::String, "(Hallo\\nWelt!)");
123 Test("(Hallo\\rWelt!)", PdfDataType::String, "(Hallo\\rWelt!)");
124 Test("(Hallo\\tWelt!)", PdfDataType::String, "(Hallo\\tWelt!)");
125 Test("(Hallo\\bWelt!)", PdfDataType::String, "(Hallo\\bWelt!)");
126 Test("(Hallo\\fWelt!)", PdfDataType::String, "(Hallo\\fWelt!)");
127 }
128
129 TEST_CASE("TestDictionary")
130 {
131 string_view dictIn =
132 "<< /CheckBox#C3#9Cbersetzungshinweis(False)/Checkbox#C3#9Cbersetzungstabelle(False) >>";
133 string_view dictOut =
134 "<</CheckBox#C3#9Cbersetzungshinweis(False)/Checkbox#C3#9Cbersetzungstabelle(False)>>";
135
136 Test(dictIn, PdfDataType::Dictionary, dictOut);
137 }
138
139 TEST_CASE("TestTokens")
140 {
141 const char* pszBuffer = "613 0 obj"
142 "<< /Length 141 /Filter [ /ASCII85Decode /FlateDecode ] >>"
143 "endobj";
144
145 const char* pszTokens[] = {
146 "613", "0", "obj", "<<", "/", "Length", "141", "/", "Filter", "[", "/",
147 "ASCII85Decode", "/", "FlateDecode", "]", ">>", "endobj", NULL
148 };
149
150 TestStream(pszBuffer, pszTokens);
151 TestStreamIsNextToken(pszBuffer, pszTokens);
152 }
153
154 TEST_CASE("TestComments")
155 {
156 const char* pszBuffer = "613 0 obj\n"
157 "% A comment that should be ignored\n"
158 "<< /Length 141 /Filter\n% A comment in a dictionary\n[ /ASCII85Decode /FlateDecode ] >>"
159 "endobj";
160
161 const char* pszTokens[] = {
162 "613", "0", "obj", "<<", "/", "Length", "141", "/", "Filter", "[", "/",
163 "ASCII85Decode", "/", "FlateDecode", "]", ">>", "endobj", NULL
164 };
165
166 TestStream(pszBuffer, pszTokens);
167 TestStreamIsNextToken(pszBuffer, pszTokens);
168 }
169
170 TEST_CASE("TestLocale")
171 {
172 // Test with a locale that uses "," instead of "." for doubles
173 char* old = setlocale(LC_ALL, "de_DE");
174
175 Test("3.140000", PdfDataType::Real, "3.14");
176
177 setlocale(LC_ALL, old);
178 }
179
180 TEST_CASE("TestInvalidRealTokenNocrash")
181 {
182 // CVE-2025-9394 regression: invalid real token after a PdfName must not
183 // cause use-after-free. The bug: DetermineDataType() calls variant.~PdfVariant()
184 // (destroying the PdfName's shared_ptr), then returns Unknown WITHOUT
185 // reinitializing the union. The variant's destructor later reads stale PdfName
186 // data and calls ~shared_ptr() on freed memory.
187 //
188 // To trigger the actual vulnerability, the variant must hold a PdfName
189 // (which has a non-trivial destructor with shared_ptr) before the invalid
190 // token is parsed. A default-constructed Null variant has a trivial destructor
191 // and would not trigger the use-after-free.
192 SpanStreamDevice device("/SomeName -.");
193 PdfTokenizer tokenizer;
194 PdfVariant variant;
195
196 // Name arms the variant with a non-trivial destructor (shared_ptr in union)
197 REQUIRE(tokenizer.TryReadNextVariant(device, variant));
198 REQUIRE(variant.GetDataType() == PdfDataType::Name);
199
200 // "-." triggers the Real recovery path; without the fix, the variant's
201 // PdfName union member is destroyed but never reinitialized, so the
202 // subsequent ~PdfVariant() double-frees the shared_ptr
203 (void)tokenizer.TryReadNextVariant(device, variant);
204 }
205
206 TEST_CASE("TestInvalidNumberTokenNocrash")
207 {
208 // CVE-2025-9394 regression: same mechanism as TestInvalidRealTokenNocrash
209 // but exercises the Number recovery path (integer overflow fails from_chars).
210 SpanStreamDevice device("/SomeName 99999999999999999999999999999999");
211 PdfTokenizer tokenizer;
212 PdfVariant variant;
213
214 // Name arms the variant with a non-trivial destructor
215 REQUIRE(tokenizer.TryReadNextVariant(device, variant));
216 REQUIRE(variant.GetDataType() == PdfDataType::Name);
217
218 // Overflow triggers the Number recovery path
219 (void)tokenizer.TryReadNextVariant(device, variant);
220 }
221
222 TEST_CASE("TestInvalidRealInArrayNocrash")
223 {
224 // CVE-2025-9394 regression: invalid numeric values inside arrays
225 // must not cause use-after-free during array destruction.
226 // "+." fails std::from_chars but passes the character scan as Real.
227 // ReadArray internally calls ReadNextVariant (throwing), so we catch the
228 // expected exception — the important thing is no crash or ASAN violation.
229 SpanStreamDevice device("[+. ..]");
230 PdfTokenizer tokenizer;
231 PdfVariant variant;
232 try
233 {
234 (void)tokenizer.TryReadNextVariant(device, variant);
235 }
236 catch (const PdfError&)
237 {
238 // Exception is acceptable error handling — not a crash
239 }
240 }
241
242 TEST_CASE("TestInvalidNumberInDictionaryNocrash")
243 {
244 // CVE-2025-9394 / issue #275 regression: invalid numeric values inside
245 // dictionaries must not cause use-after-free during dictionary destruction.
246 // ReadDictionary internally calls ReadNextVariant (throwing), so we catch
247 // the expected exception.
248 SpanStreamDevice device("<< /Key -. >>");
249 PdfTokenizer tokenizer;
250 PdfVariant variant;
251 try
252 {
253 (void)tokenizer.TryReadNextVariant(device, variant);
254 }
255 catch (const PdfError&)
256 {
257 // Exception is acceptable error handling — not a crash
258 }
259 }
260
261 TEST_CASE("TestVariantReuseAfterInvalidToken")
262 {
263 // CVE-2025-9394 regression: the recovery path must leave the variant in a
264 // fully usable state, not just a non-crashing one. Verifies the variant
265 // can be reassigned and used after the fix reinitializes it as NullMember.
266 {
267 SpanStreamDevice device("/TestName -. 42");
268 PdfTokenizer tokenizer;
269 PdfVariant variant;
270
271 // Arm with Name, then trigger Real recovery path
272 REQUIRE(tokenizer.TryReadNextVariant(device, variant));
273 REQUIRE(variant.GetDataType() == PdfDataType::Name);
274 (void)tokenizer.TryReadNextVariant(device, variant);
275
276 // Reassignment must work — proves variant is in a valid state, not just
277 // that it avoided a crash during destruction
278 variant = PdfVariant(static_cast<int64_t>(42));
279 REQUIRE(variant.GetDataType() == PdfDataType::Number);
280 REQUIRE(variant.GetNumber() == 42);
281 }
282 {
283 SpanStreamDevice device("/TestName 99999999999999999999999999999999 99");
284 PdfTokenizer tokenizer;
285 PdfVariant variant;
286
287 // Arm with Name, then trigger Number recovery path
288 REQUIRE(tokenizer.TryReadNextVariant(device, variant));
289 REQUIRE(variant.GetDataType() == PdfDataType::Name);
290 (void)tokenizer.TryReadNextVariant(device, variant);
291
292 variant = PdfVariant(static_cast<int64_t>(99));
293 REQUIRE(variant.GetDataType() == PdfDataType::Number);
294 REQUIRE(variant.GetNumber() == 99);
295 }
296 }
297
298 TEST_CASE("TestMultipleInvalidTokenVariantsNocrash")
299 {
300 // CVE-2025-9394 regression: exercises multiple distinct invalid-token patterns
301 // that all reach the recovery path. Each variant is armed with a PdfName first
302 // because the use-after-free only manifests with non-trivial union members.
303 const string_view sequences[] = {
304 "/Name1 -.", // Real: sign-dot
305 "/Name2 +.", // Real: sign-dot
306 "/Name3 ..", // Real: double-dot
307 "/Name4 99999999999999999999999999999999" // Number: overflow
308 };
309 for (const string_view& input : sequences)
310 {
311 SpanStreamDevice device(input);
312 PdfTokenizer tokenizer;
313 PdfVariant variant;
314
315 // Arm with Name so the destructor path is non-trivial
316 REQUIRE(tokenizer.TryReadNextVariant(device, variant));
317 REQUIRE(variant.GetDataType() == PdfDataType::Name);
318
319 // Trigger recovery; variant destruction at end of scope is the real test
320 (void)tokenizer.TryReadNextVariant(device, variant);
321 }
322 }
323
324 void Test(const string_view& buffer, PdfDataType dataType, string_view expected)
325 {
326 expected = expected.empty() ? buffer : expected;
327
328 INFO(utls::Format("Testing with value: {}", buffer));
329
330 SpanStreamDevice device(buffer);
331 PdfTokenizer tokenizer;
332 PdfVariant variant;
333 REQUIRE(tokenizer.TryReadNextVariant(device, variant));
334
335 INFO(utls::Format(" -> Expected Datatype: {}", (int)dataType));
336 INFO(utls::Format(" -> Got Datatype: {}", (int)variant.GetDataType()));
337 REQUIRE(variant.GetDataType() == dataType);
338
339 string variantStr;
340 variant.ToString(variantStr);
341 INFO(utls::Format(" -> Convert To String: {}", variantStr));
342
343 REQUIRE(variantStr == expected);
344 }
345
346 void TestStream(const string_view& buffer, const char* tokens[])
347 {
348 SpanStreamDevice device(buffer);
349 PdfTokenizer tokenizer;
350 string_view token;
351 unsigned i = 0;
352 while (tokens[i] != nullptr)
353 {
354 REQUIRE(tokenizer.TryReadNextToken(device, token));
355 REQUIRE(token == tokens[i]);
356
357 i++;
358 }
359
360 // We are at the end, so GetNextToken has to return false!
361 REQUIRE(!tokenizer.TryReadNextToken(device, token));
362 }
363
364 void TestStreamIsNextToken(const string_view& buffer, const char* tokens[])
365 {
366 SpanStreamDevice device(buffer);
367 PdfTokenizer tokenizer;
368
369 unsigned i = 0;
370 string_view token;
371 while (tokens[i] != nullptr)
372 REQUIRE((tokenizer.TryReadNextToken(device, token) && token == tokens[i++]));
373 }