Commit 887128a
committed
Fix (CVE-2025-58367)
deepdiff/serialization.py — Added a _SafeConstructor wrapper class that intercepts calls to
size-sensitive constructors (like bytes and bytearray) during pickle deserialization. When
find_class returns one of these types, it wraps it in _SafeConstructor, which validates that no
integer argument exceeds _MAX_ALLOC_SIZE (128MB) before allowing the call. This prevents payloads
like bytes(10**10) from causing memory exhaustion while still allowing legitimate small
allocations.
Tests
tests/test_serialization.py — Added TestPicklingSecurity class with two tests:
1. test_restricted_unpickler_memory_exhaustion_cve — Reproduces the attack with a crafted payload
attempting bytes(10_000_000_000), using resource.RLIMIT_AS to cap memory at 500MB as a safety net.
Verifies the fix raises UnpicklingError before any allocation.
2. test_restricted_unpickler_allows_small_bytes — Ensures legitimate bytes(100) payloads still
deserialize correctly.1 parent 60ac5b9 commit 887128a
2 files changed
Lines changed: 86 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
331 | 331 | | |
332 | 332 | | |
333 | 333 | | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
334 | 363 | | |
335 | 364 | | |
336 | 365 | | |
| |||
355 | 384 | | |
356 | 385 | | |
357 | 386 | | |
358 | | - | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
359 | 392 | | |
360 | 393 | | |
361 | 394 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
155 | 155 | | |
156 | 156 | | |
157 | 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 | + | |
158 | 210 | | |
159 | 211 | | |
160 | 212 | | |
| |||
0 commit comments