kreuzberg 4.0.0.pre.rc.6 → 4.0.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -6
  3. data/.rubocop.yaml +534 -1
  4. data/Gemfile +2 -1
  5. data/Gemfile.lock +11 -11
  6. data/README.md +5 -10
  7. data/examples/async_patterns.rb +0 -1
  8. data/ext/kreuzberg_rb/extconf.rb +0 -10
  9. data/ext/kreuzberg_rb/native/Cargo.toml +15 -23
  10. data/ext/kreuzberg_rb/native/build.rs +2 -0
  11. data/ext/kreuzberg_rb/native/include/ieeefp.h +1 -1
  12. data/ext/kreuzberg_rb/native/include/msvc_compat/strings.h +1 -1
  13. data/ext/kreuzberg_rb/native/include/strings.h +2 -2
  14. data/ext/kreuzberg_rb/native/include/unistd.h +1 -1
  15. data/ext/kreuzberg_rb/native/src/lib.rs +16 -75
  16. data/kreuzberg.gemspec +14 -57
  17. data/lib/kreuzberg/cache_api.rb +0 -1
  18. data/lib/kreuzberg/cli.rb +2 -2
  19. data/lib/kreuzberg/config.rb +2 -9
  20. data/lib/kreuzberg/errors.rb +7 -75
  21. data/lib/kreuzberg/extraction_api.rb +0 -1
  22. data/lib/kreuzberg/setup_lib_path.rb +0 -1
  23. data/lib/kreuzberg/version.rb +1 -1
  24. data/lib/kreuzberg.rb +0 -21
  25. data/pkg/kreuzberg-4.0.0.rc1.gem +0 -0
  26. data/sig/kreuzberg.rbs +3 -55
  27. data/spec/binding/cli_proxy_spec.rb +4 -2
  28. data/spec/binding/cli_spec.rb +11 -12
  29. data/spec/examples.txt +104 -0
  30. data/spec/fixtures/config.yaml +1 -0
  31. data/spec/spec_helper.rb +1 -1
  32. data/vendor/kreuzberg/Cargo.toml +42 -112
  33. data/vendor/kreuzberg/README.md +2 -2
  34. data/vendor/kreuzberg/build.rs +4 -18
  35. data/vendor/kreuzberg/src/bin/profile_extract.rs +455 -0
  36. data/vendor/kreuzberg/src/cache/mod.rs +3 -27
  37. data/vendor/kreuzberg/src/core/batch_mode.rs +0 -60
  38. data/vendor/kreuzberg/src/core/extractor.rs +81 -202
  39. data/vendor/kreuzberg/src/core/io.rs +2 -4
  40. data/vendor/kreuzberg/src/core/mime.rs +12 -2
  41. data/vendor/kreuzberg/src/core/mod.rs +1 -4
  42. data/vendor/kreuzberg/src/core/pipeline.rs +33 -111
  43. data/vendor/kreuzberg/src/embeddings.rs +16 -125
  44. data/vendor/kreuzberg/src/error.rs +1 -1
  45. data/vendor/kreuzberg/src/extraction/docx.rs +1 -1
  46. data/vendor/kreuzberg/src/extraction/image.rs +13 -13
  47. data/vendor/kreuzberg/src/extraction/libreoffice.rs +1 -0
  48. data/vendor/kreuzberg/src/extraction/mod.rs +5 -9
  49. data/vendor/kreuzberg/src/extraction/office_metadata/mod.rs +0 -2
  50. data/vendor/kreuzberg/src/extraction/pandoc/batch.rs +275 -0
  51. data/vendor/kreuzberg/src/extraction/pandoc/mime_types.rs +178 -0
  52. data/vendor/kreuzberg/src/extraction/pandoc/mod.rs +491 -0
  53. data/vendor/kreuzberg/src/extraction/pandoc/server.rs +496 -0
  54. data/vendor/kreuzberg/src/extraction/pandoc/subprocess.rs +1188 -0
  55. data/vendor/kreuzberg/src/extraction/pandoc/version.rs +162 -0
  56. data/vendor/kreuzberg/src/extractors/archive.rs +0 -21
  57. data/vendor/kreuzberg/src/extractors/docx.rs +128 -16
  58. data/vendor/kreuzberg/src/extractors/email.rs +0 -14
  59. data/vendor/kreuzberg/src/extractors/excel.rs +20 -19
  60. data/vendor/kreuzberg/src/extractors/html.rs +154 -137
  61. data/vendor/kreuzberg/src/extractors/image.rs +4 -7
  62. data/vendor/kreuzberg/src/extractors/mod.rs +9 -106
  63. data/vendor/kreuzberg/src/extractors/pandoc.rs +201 -0
  64. data/vendor/kreuzberg/src/extractors/pdf.rs +15 -12
  65. data/vendor/kreuzberg/src/extractors/pptx.rs +3 -17
  66. data/vendor/kreuzberg/src/extractors/structured.rs +0 -14
  67. data/vendor/kreuzberg/src/extractors/text.rs +5 -23
  68. data/vendor/kreuzberg/src/extractors/xml.rs +0 -7
  69. data/vendor/kreuzberg/src/keywords/rake.rs +1 -0
  70. data/vendor/kreuzberg/src/lib.rs +1 -4
  71. data/vendor/kreuzberg/src/mcp/mod.rs +1 -1
  72. data/vendor/kreuzberg/src/mcp/server.rs +3 -5
  73. data/vendor/kreuzberg/src/ocr/processor.rs +2 -18
  74. data/vendor/kreuzberg/src/pdf/error.rs +1 -1
  75. data/vendor/kreuzberg/src/pdf/table.rs +44 -17
  76. data/vendor/kreuzberg/src/pdf/text.rs +3 -0
  77. data/vendor/kreuzberg/src/plugins/extractor.rs +5 -8
  78. data/vendor/kreuzberg/src/plugins/ocr.rs +11 -2
  79. data/vendor/kreuzberg/src/plugins/processor.rs +1 -2
  80. data/vendor/kreuzberg/src/plugins/registry.rs +0 -13
  81. data/vendor/kreuzberg/src/plugins/validator.rs +8 -9
  82. data/vendor/kreuzberg/src/stopwords/mod.rs +2 -2
  83. data/vendor/kreuzberg/src/types.rs +12 -42
  84. data/vendor/kreuzberg/tests/batch_orchestration.rs +5 -19
  85. data/vendor/kreuzberg/tests/batch_processing.rs +3 -15
  86. data/vendor/kreuzberg/tests/chunking_offset_demo.rs +92 -0
  87. data/vendor/kreuzberg/tests/concurrency_stress.rs +1 -17
  88. data/vendor/kreuzberg/tests/config_features.rs +0 -18
  89. data/vendor/kreuzberg/tests/config_loading_tests.rs +39 -15
  90. data/vendor/kreuzberg/tests/core_integration.rs +7 -24
  91. data/vendor/kreuzberg/tests/csv_integration.rs +81 -71
  92. data/vendor/kreuzberg/tests/docx_metadata_extraction_test.rs +25 -23
  93. data/vendor/kreuzberg/tests/pandoc_integration.rs +503 -0
  94. data/vendor/kreuzberg/tests/pipeline_integration.rs +1 -0
  95. data/vendor/kreuzberg/tests/plugin_postprocessor_test.rs +1 -0
  96. data/vendor/kreuzberg/tests/registry_integration_tests.rs +22 -1
  97. data/vendor/kreuzberg/tests/security_validation.rs +1 -12
  98. metadata +25 -90
  99. data/.rubocop.yml +0 -538
  100. data/ext/kreuzberg_rb/native/Cargo.lock +0 -6535
  101. data/lib/kreuzberg/error_context.rb +0 -32
  102. data/vendor/kreuzberg/benches/otel_overhead.rs +0 -48
  103. data/vendor/kreuzberg/src/extraction/markdown.rs +0 -213
  104. data/vendor/kreuzberg/src/extraction/office_metadata/odt_properties.rs +0 -287
  105. data/vendor/kreuzberg/src/extractors/bibtex.rs +0 -469
  106. data/vendor/kreuzberg/src/extractors/docbook.rs +0 -502
  107. data/vendor/kreuzberg/src/extractors/epub.rs +0 -707
  108. data/vendor/kreuzberg/src/extractors/fictionbook.rs +0 -491
  109. data/vendor/kreuzberg/src/extractors/fictionbook.rs.backup2 +0 -738
  110. data/vendor/kreuzberg/src/extractors/jats.rs +0 -1051
  111. data/vendor/kreuzberg/src/extractors/jupyter.rs +0 -367
  112. data/vendor/kreuzberg/src/extractors/latex.rs +0 -652
  113. data/vendor/kreuzberg/src/extractors/markdown.rs +0 -700
  114. data/vendor/kreuzberg/src/extractors/odt.rs +0 -628
  115. data/vendor/kreuzberg/src/extractors/opml.rs +0 -634
  116. data/vendor/kreuzberg/src/extractors/orgmode.rs +0 -528
  117. data/vendor/kreuzberg/src/extractors/rst.rs +0 -576
  118. data/vendor/kreuzberg/src/extractors/rtf.rs +0 -810
  119. data/vendor/kreuzberg/src/extractors/security.rs +0 -484
  120. data/vendor/kreuzberg/src/extractors/security_tests.rs +0 -367
  121. data/vendor/kreuzberg/src/extractors/typst.rs +0 -650
  122. data/vendor/kreuzberg/src/panic_context.rs +0 -154
  123. data/vendor/kreuzberg/tests/api_extract_multipart.rs +0 -52
  124. data/vendor/kreuzberg/tests/bibtex_parity_test.rs +0 -421
  125. data/vendor/kreuzberg/tests/docbook_extractor_tests.rs +0 -498
  126. data/vendor/kreuzberg/tests/docx_vs_pandoc_comparison.rs +0 -370
  127. data/vendor/kreuzberg/tests/epub_native_extractor_tests.rs +0 -275
  128. data/vendor/kreuzberg/tests/fictionbook_extractor_tests.rs +0 -228
  129. data/vendor/kreuzberg/tests/html_table_test.rs +0 -551
  130. data/vendor/kreuzberg/tests/instrumentation_test.rs +0 -139
  131. data/vendor/kreuzberg/tests/jats_extractor_tests.rs +0 -639
  132. data/vendor/kreuzberg/tests/jupyter_extractor_tests.rs +0 -704
  133. data/vendor/kreuzberg/tests/latex_extractor_tests.rs +0 -496
  134. data/vendor/kreuzberg/tests/markdown_extractor_tests.rs +0 -490
  135. data/vendor/kreuzberg/tests/odt_extractor_tests.rs +0 -695
  136. data/vendor/kreuzberg/tests/opml_extractor_tests.rs +0 -616
  137. data/vendor/kreuzberg/tests/orgmode_extractor_tests.rs +0 -822
  138. data/vendor/kreuzberg/tests/rst_extractor_tests.rs +0 -692
  139. data/vendor/kreuzberg/tests/rtf_extractor_tests.rs +0 -776
  140. data/vendor/kreuzberg/tests/typst_behavioral_tests.rs +0 -1259
  141. data/vendor/kreuzberg/tests/typst_extractor_tests.rs +0 -647
  142. data/vendor/rb-sys/.cargo-ok +0 -1
  143. data/vendor/rb-sys/.cargo_vcs_info.json +0 -6
  144. data/vendor/rb-sys/Cargo.lock +0 -393
  145. data/vendor/rb-sys/Cargo.toml +0 -70
  146. data/vendor/rb-sys/Cargo.toml.orig +0 -57
  147. data/vendor/rb-sys/LICENSE-APACHE +0 -190
  148. data/vendor/rb-sys/LICENSE-MIT +0 -21
  149. data/vendor/rb-sys/bin/release.sh +0 -21
  150. data/vendor/rb-sys/build/features.rs +0 -108
  151. data/vendor/rb-sys/build/main.rs +0 -246
  152. data/vendor/rb-sys/build/stable_api_config.rs +0 -153
  153. data/vendor/rb-sys/build/version.rs +0 -48
  154. data/vendor/rb-sys/readme.md +0 -36
  155. data/vendor/rb-sys/src/bindings.rs +0 -21
  156. data/vendor/rb-sys/src/hidden.rs +0 -11
  157. data/vendor/rb-sys/src/lib.rs +0 -34
  158. data/vendor/rb-sys/src/macros.rs +0 -371
  159. data/vendor/rb-sys/src/memory.rs +0 -53
  160. data/vendor/rb-sys/src/ruby_abi_version.rs +0 -38
  161. data/vendor/rb-sys/src/special_consts.rs +0 -31
  162. data/vendor/rb-sys/src/stable_api/compiled.c +0 -179
  163. data/vendor/rb-sys/src/stable_api/compiled.rs +0 -257
  164. data/vendor/rb-sys/src/stable_api/ruby_2_6.rs +0 -316
  165. data/vendor/rb-sys/src/stable_api/ruby_2_7.rs +0 -316
  166. data/vendor/rb-sys/src/stable_api/ruby_3_0.rs +0 -324
  167. data/vendor/rb-sys/src/stable_api/ruby_3_1.rs +0 -317
  168. data/vendor/rb-sys/src/stable_api/ruby_3_2.rs +0 -315
  169. data/vendor/rb-sys/src/stable_api/ruby_3_3.rs +0 -326
  170. data/vendor/rb-sys/src/stable_api/ruby_3_4.rs +0 -327
  171. data/vendor/rb-sys/src/stable_api.rs +0 -261
  172. data/vendor/rb-sys/src/symbol.rs +0 -31
  173. data/vendor/rb-sys/src/tracking_allocator.rs +0 -332
  174. data/vendor/rb-sys/src/utils.rs +0 -89
  175. data/vendor/rb-sys/src/value_type.rs +0 -7
@@ -1,484 +0,0 @@
1
- //! Security utilities for document extractors.
2
- //!
3
- //! This module provides validation and protection mechanisms against common attacks:
4
- //! - ZIP bomb detection (decompression bombs)
5
- //! - XML entity expansion limits
6
- //! - Nesting depth limits
7
- //! - Input size limits
8
- //! - Entity length validation
9
-
10
- #[cfg(feature = "archives")]
11
- use std::io::{Read, Seek};
12
-
13
- /// Configuration for security limits across extractors.
14
- ///
15
- /// All limits are intentionally conservative to prevent DoS attacks
16
- /// while still supporting legitimate documents.
17
- #[derive(Clone, Debug)]
18
- pub struct SecurityLimits {
19
- /// Maximum uncompressed size for archives (500 MB)
20
- pub max_archive_size: usize,
21
-
22
- /// Maximum compression ratio before flagging as potential bomb (100:1)
23
- pub max_compression_ratio: usize,
24
-
25
- /// Maximum number of files in archive (10,000)
26
- pub max_files_in_archive: usize,
27
-
28
- /// Maximum nesting depth for structures (100)
29
- pub max_nesting_depth: usize,
30
-
31
- /// Maximum entity/string length (32)
32
- pub max_entity_length: usize,
33
-
34
- /// Maximum string growth per document (100 MB)
35
- pub max_content_size: usize,
36
-
37
- /// Maximum iterations per operation
38
- pub max_iterations: usize,
39
-
40
- /// Maximum XML depth (100 levels)
41
- pub max_xml_depth: usize,
42
-
43
- /// Maximum cells per table (100,000)
44
- pub max_table_cells: usize,
45
- }
46
-
47
- impl Default for SecurityLimits {
48
- fn default() -> Self {
49
- Self {
50
- max_archive_size: 500 * 1024 * 1024,
51
- max_compression_ratio: 100,
52
- max_files_in_archive: 10_000,
53
- max_nesting_depth: 100,
54
- max_entity_length: 32,
55
- max_content_size: 100 * 1024 * 1024,
56
- max_iterations: 10_000_000,
57
- max_xml_depth: 100,
58
- max_table_cells: 100_000,
59
- }
60
- }
61
- }
62
-
63
- /// Security validation errors.
64
- #[derive(Debug, Clone)]
65
- pub enum SecurityError {
66
- /// Potential ZIP bomb detected
67
- ZipBombDetected {
68
- compressed_size: u64,
69
- uncompressed_size: u64,
70
- ratio: f64,
71
- },
72
-
73
- /// Archive exceeds maximum size
74
- ArchiveTooLarge { size: u64, max: usize },
75
-
76
- /// Archive contains too many files
77
- TooManyFiles { count: usize, max: usize },
78
-
79
- /// Nesting too deep
80
- NestingTooDeep { depth: usize, max: usize },
81
-
82
- /// Content exceeds maximum size
83
- ContentTooLarge { size: usize, max: usize },
84
-
85
- /// Entity/string too long
86
- EntityTooLong { length: usize, max: usize },
87
-
88
- /// Too many iterations
89
- TooManyIterations { count: usize, max: usize },
90
-
91
- /// XML depth exceeded
92
- XmlDepthExceeded { depth: usize, max: usize },
93
-
94
- /// Too many table cells
95
- TooManyCells { cells: usize, max: usize },
96
- }
97
-
98
- impl std::fmt::Display for SecurityError {
99
- fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
100
- match self {
101
- SecurityError::ZipBombDetected {
102
- compressed_size,
103
- uncompressed_size,
104
- ratio,
105
- } => {
106
- write!(
107
- f,
108
- "Potential ZIP bomb detected: compressed {}B -> uncompressed {}B (ratio: {:.1}:1)",
109
- compressed_size, uncompressed_size, ratio
110
- )
111
- }
112
- SecurityError::ArchiveTooLarge { size, max } => {
113
- write!(f, "Archive too large: {} bytes (max: {} bytes)", size, max)
114
- }
115
- SecurityError::TooManyFiles { count, max } => {
116
- write!(f, "Archive has too many files: {} (max: {})", count, max)
117
- }
118
- SecurityError::NestingTooDeep { depth, max } => {
119
- write!(f, "Nesting too deep: {} levels (max: {})", depth, max)
120
- }
121
- SecurityError::ContentTooLarge { size, max } => {
122
- write!(f, "Content too large: {} bytes (max: {} bytes)", size, max)
123
- }
124
- SecurityError::EntityTooLong { length, max } => {
125
- write!(f, "Entity too long: {} chars (max: {})", length, max)
126
- }
127
- SecurityError::TooManyIterations { count, max } => {
128
- write!(f, "Too many iterations: {} (max: {})", count, max)
129
- }
130
- SecurityError::XmlDepthExceeded { depth, max } => {
131
- write!(f, "XML depth exceeded: {} (max: {})", depth, max)
132
- }
133
- SecurityError::TooManyCells { cells, max } => {
134
- write!(f, "Too many table cells: {} (max: {})", cells, max)
135
- }
136
- }
137
- }
138
- }
139
-
140
- impl std::error::Error for SecurityError {}
141
-
142
- /// Helper struct for validating ZIP archives for security issues.
143
- #[cfg(feature = "archives")]
144
- pub struct ZipBombValidator {
145
- limits: SecurityLimits,
146
- }
147
-
148
- #[cfg(feature = "archives")]
149
- impl ZipBombValidator {
150
- /// Create a new ZIP bomb validator.
151
- pub fn new(limits: SecurityLimits) -> Self {
152
- Self { limits }
153
- }
154
-
155
- /// Validate a ZIP archive for security issues.
156
- ///
157
- /// # Arguments
158
- /// * `archive` - Mutable ZIP archive to validate
159
- ///
160
- /// # Returns
161
- /// * `Ok(())` if archive is safe
162
- /// * `Err(SecurityError)` if security limit violated
163
- pub fn validate<R: Read + Seek>(&self, archive: &mut zip::ZipArchive<R>) -> Result<(), SecurityError> {
164
- let file_count = archive.len();
165
-
166
- if file_count > self.limits.max_files_in_archive {
167
- return Err(SecurityError::TooManyFiles {
168
- count: file_count,
169
- max: self.limits.max_files_in_archive,
170
- });
171
- }
172
-
173
- let mut total_uncompressed: u64 = 0;
174
- let mut total_compressed: u64 = 0;
175
-
176
- for i in 0..file_count {
177
- if let Ok(file) = archive.by_index(i) {
178
- let compressed_size = file.compressed_size();
179
- let uncompressed_size = file.size();
180
-
181
- total_uncompressed += uncompressed_size;
182
- total_compressed += compressed_size;
183
-
184
- if compressed_size > 0 && uncompressed_size > 0 {
185
- let ratio = uncompressed_size as f64 / compressed_size as f64;
186
- if ratio > self.limits.max_compression_ratio as f64 {
187
- return Err(SecurityError::ZipBombDetected {
188
- compressed_size,
189
- uncompressed_size,
190
- ratio,
191
- });
192
- }
193
- }
194
- }
195
- }
196
-
197
- if total_uncompressed > self.limits.max_archive_size as u64 {
198
- return Err(SecurityError::ArchiveTooLarge {
199
- size: total_uncompressed,
200
- max: self.limits.max_archive_size,
201
- });
202
- }
203
-
204
- if total_compressed > 0 {
205
- let ratio = total_uncompressed as f64 / total_compressed as f64;
206
- if ratio > self.limits.max_compression_ratio as f64 {
207
- return Err(SecurityError::ZipBombDetected {
208
- compressed_size: total_compressed,
209
- uncompressed_size: total_uncompressed,
210
- ratio,
211
- });
212
- }
213
- }
214
-
215
- Ok(())
216
- }
217
- }
218
-
219
- /// Helper struct for tracking and validating string growth.
220
- pub struct StringGrowthValidator {
221
- max_size: usize,
222
- current_size: usize,
223
- }
224
-
225
- impl StringGrowthValidator {
226
- /// Create a new string growth validator.
227
- pub fn new(max_size: usize) -> Self {
228
- Self {
229
- max_size,
230
- current_size: 0,
231
- }
232
- }
233
-
234
- /// Validate and update size after appending.
235
- ///
236
- /// # Returns
237
- /// * `Ok(())` if size is within limits
238
- /// * `Err(SecurityError)` if size exceeds limit
239
- pub fn check_append(&mut self, len: usize) -> Result<(), SecurityError> {
240
- self.current_size = self.current_size.saturating_add(len);
241
- if self.current_size > self.max_size {
242
- Err(SecurityError::ContentTooLarge {
243
- size: self.current_size,
244
- max: self.max_size,
245
- })
246
- } else {
247
- Ok(())
248
- }
249
- }
250
-
251
- /// Get current size.
252
- pub fn current_size(&self) -> usize {
253
- self.current_size
254
- }
255
- }
256
-
257
- /// Helper struct for validating iteration counts.
258
- pub struct IterationValidator {
259
- max_iterations: usize,
260
- current_count: usize,
261
- }
262
-
263
- impl IterationValidator {
264
- /// Create a new iteration validator.
265
- pub fn new(max_iterations: usize) -> Self {
266
- Self {
267
- max_iterations,
268
- current_count: 0,
269
- }
270
- }
271
-
272
- /// Validate and increment iteration count.
273
- ///
274
- /// # Returns
275
- /// * `Ok(())` if count is within limits
276
- /// * `Err(SecurityError)` if count exceeds limit
277
- pub fn check_iteration(&mut self) -> Result<(), SecurityError> {
278
- self.current_count += 1;
279
- if self.current_count > self.max_iterations {
280
- Err(SecurityError::TooManyIterations {
281
- count: self.current_count,
282
- max: self.max_iterations,
283
- })
284
- } else {
285
- Ok(())
286
- }
287
- }
288
-
289
- /// Get current iteration count.
290
- pub fn current_count(&self) -> usize {
291
- self.current_count
292
- }
293
- }
294
-
295
- /// Helper struct for validating nesting depth.
296
- pub struct DepthValidator {
297
- max_depth: usize,
298
- current_depth: usize,
299
- }
300
-
301
- impl DepthValidator {
302
- /// Create a new depth validator.
303
- pub fn new(max_depth: usize) -> Self {
304
- Self {
305
- max_depth,
306
- current_depth: 0,
307
- }
308
- }
309
-
310
- /// Push a level (increase depth).
311
- ///
312
- /// # Returns
313
- /// * `Ok(())` if depth is within limits
314
- /// * `Err(SecurityError)` if depth exceeds limit
315
- pub fn push(&mut self) -> Result<(), SecurityError> {
316
- self.current_depth += 1;
317
- if self.current_depth > self.max_depth {
318
- Err(SecurityError::NestingTooDeep {
319
- depth: self.current_depth,
320
- max: self.max_depth,
321
- })
322
- } else {
323
- Ok(())
324
- }
325
- }
326
-
327
- /// Pop a level (decrease depth).
328
- pub fn pop(&mut self) {
329
- if self.current_depth > 0 {
330
- self.current_depth -= 1;
331
- }
332
- }
333
-
334
- /// Get current depth.
335
- pub fn current_depth(&self) -> usize {
336
- self.current_depth
337
- }
338
- }
339
-
340
- /// Helper struct for validating entity/string length.
341
- pub struct EntityValidator {
342
- max_length: usize,
343
- }
344
-
345
- impl EntityValidator {
346
- /// Create a new entity validator.
347
- pub fn new(max_length: usize) -> Self {
348
- Self { max_length }
349
- }
350
-
351
- /// Validate entity length.
352
- ///
353
- /// # Returns
354
- /// * `Ok(())` if length is within limits
355
- /// * `Err(SecurityError)` if length exceeds limit
356
- pub fn validate(&self, content: &str) -> Result<(), SecurityError> {
357
- if content.len() > self.max_length {
358
- Err(SecurityError::EntityTooLong {
359
- length: content.len(),
360
- max: self.max_length,
361
- })
362
- } else {
363
- Ok(())
364
- }
365
- }
366
- }
367
-
368
- /// Helper struct for validating table cell counts.
369
- pub struct TableValidator {
370
- max_cells: usize,
371
- current_cells: usize,
372
- }
373
-
374
- impl TableValidator {
375
- /// Create a new table validator.
376
- pub fn new(max_cells: usize) -> Self {
377
- Self {
378
- max_cells,
379
- current_cells: 0,
380
- }
381
- }
382
-
383
- /// Add cells to table and validate.
384
- ///
385
- /// # Returns
386
- /// * `Ok(())` if cell count is within limits
387
- /// * `Err(SecurityError)` if cell count exceeds limit
388
- pub fn add_cells(&mut self, count: usize) -> Result<(), SecurityError> {
389
- self.current_cells = self.current_cells.saturating_add(count);
390
- if self.current_cells > self.max_cells {
391
- Err(SecurityError::TooManyCells {
392
- cells: self.current_cells,
393
- max: self.max_cells,
394
- })
395
- } else {
396
- Ok(())
397
- }
398
- }
399
-
400
- /// Get current cell count.
401
- pub fn current_cells(&self) -> usize {
402
- self.current_cells
403
- }
404
- }
405
-
406
- #[cfg(test)]
407
- mod tests {
408
- use super::*;
409
-
410
- #[test]
411
- fn test_depth_validator() {
412
- let mut validator = DepthValidator::new(3);
413
-
414
- assert!(validator.push().is_ok());
415
- assert_eq!(validator.current_depth(), 1);
416
-
417
- assert!(validator.push().is_ok());
418
- assert_eq!(validator.current_depth(), 2);
419
-
420
- assert!(validator.push().is_ok());
421
- assert_eq!(validator.current_depth(), 3);
422
-
423
- assert!(validator.push().is_err());
424
- assert_eq!(validator.current_depth(), 4);
425
-
426
- validator.pop();
427
- assert_eq!(validator.current_depth(), 3);
428
- }
429
-
430
- #[test]
431
- fn test_entity_validator() {
432
- let validator = EntityValidator::new(10);
433
-
434
- assert!(validator.validate("short").is_ok());
435
-
436
- assert!(validator.validate("0123456789").is_ok());
437
-
438
- assert!(validator.validate("01234567890").is_err());
439
- }
440
-
441
- #[test]
442
- fn test_string_growth_validator() {
443
- let mut validator = StringGrowthValidator::new(100);
444
-
445
- assert!(validator.check_append(50).is_ok());
446
- assert_eq!(validator.current_size(), 50);
447
-
448
- assert!(validator.check_append(50).is_ok());
449
- assert_eq!(validator.current_size(), 100);
450
-
451
- assert!(validator.check_append(1).is_err());
452
- }
453
-
454
- #[test]
455
- fn test_iteration_validator() {
456
- let mut validator = IterationValidator::new(3);
457
-
458
- assert!(validator.check_iteration().is_ok());
459
- assert!(validator.check_iteration().is_ok());
460
- assert!(validator.check_iteration().is_ok());
461
- assert!(validator.check_iteration().is_err());
462
- }
463
-
464
- #[test]
465
- fn test_table_validator() {
466
- let mut validator = TableValidator::new(10);
467
-
468
- assert!(validator.add_cells(5).is_ok());
469
- assert_eq!(validator.current_cells(), 5);
470
-
471
- assert!(validator.add_cells(5).is_ok());
472
- assert_eq!(validator.current_cells(), 10);
473
-
474
- assert!(validator.add_cells(1).is_err());
475
- }
476
-
477
- #[test]
478
- fn test_default_limits() {
479
- let limits = SecurityLimits::default();
480
- assert_eq!(limits.max_archive_size, 500 * 1024 * 1024);
481
- assert_eq!(limits.max_nesting_depth, 100);
482
- assert_eq!(limits.max_entity_length, 32);
483
- }
484
- }