Commit d8047c3
committed
fix(cache-middleware): bound cache size and enforce safe fuzzy threshold
CacheMiddleware previously:
1. Used an unbounded dict for storage — sustained unique inputs would
grow the cache without limit, a memory-exhaustion DoS surface.
2. Accepted any similarity_threshold in [0, 1]. Low values make cache
poisoning trivial: a crafted input whose difflib ratio exceeds the
threshold against a legitimate cached key hijacks the legitimate
user's response. At threshold=0.5, most short/structured inputs
collide on first try.
Changes:
- Back the cache with an OrderedDict. Evict the oldest entry (LRU)
whenever the cache exceeds max_entries. A cache hit moves the entry
to the MRU end so frequently-useful results are preferred on eviction.
- Add max_entries to __init__ (default 1024, must be a positive int).
- Reject similarity_threshold values below 0.85 at construct time with
an actionable error. 1.0 remains recommended (exact match only).
- Non-numeric threshold values now raise instead of failing later.
Breaking change: existing configs that pass similarity_threshold < 0.85
will fail at construct time. This is intentional — any such value is a
cache-poisoning foot-gun. The error message points at the two safe
choices (use 1.0, or use a value >= 0.85).
Test updates:
- test_similarity_computation_for_different_thresholds / _fuzzy_match_caching
/ test_multiple_similar_entries / test_custom_initialization: bumped
from 0.5/0.7/0.8 to values >= 0.85 so they exercise fuzzy matching
without tripping the new floor.
- Added TestSimilarityThresholdFloor:
* below-floor values (0.0, 0.3, 0.5, 0.7, 0.84) are rejected
* at/above-floor values (0.85, 0.9, 0.95, 1.0) are accepted
* threshold > 1.0 is rejected
* non-numeric threshold is rejected
- Added TestMaxEntriesLruEviction:
* default max_entries is positive
* zero or negative max_entries is rejected
* cache stays at max_entries under sustained unique input
* hit promotes entry to MRU; later insert evicts the older one
CWE-400 (resource exhaustion) + CWE-345 (insufficient data authenticity /
cache-key confusion).
Signed-off-by: ColinM-sys <cmcdonough@50words.com>1 parent 7beda93 commit d8047c3
2 files changed
Lines changed: 191 additions & 15 deletions
File tree
- packages/nvidia_nat_core
- src/nat/middleware/cache
- tests/nat/middleware
Lines changed: 66 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
| |||
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
46 | 62 | | |
47 | 63 | | |
48 | 64 | | |
| |||
67 | 83 | | |
68 | 84 | | |
69 | 85 | | |
70 | | - | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
71 | 93 | | |
72 | 94 | | |
73 | 95 | | |
74 | 96 | | |
75 | 97 | | |
76 | | - | |
77 | | - | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
78 | 110 | | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
79 | 124 | | |
80 | 125 | | |
81 | 126 | | |
82 | | - | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
83 | 131 | | |
84 | 132 | | |
85 | 133 | | |
| |||
199 | 247 | | |
200 | 248 | | |
201 | 249 | | |
202 | | - | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
203 | 253 | | |
204 | 254 | | |
205 | 255 | | |
| 256 | + | |
206 | 257 | | |
207 | 258 | | |
208 | 259 | | |
209 | 260 | | |
210 | 261 | | |
211 | 262 | | |
212 | 263 | | |
213 | | - | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
214 | 267 | | |
215 | | - | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
216 | 275 | | |
217 | 276 | | |
218 | 277 | | |
| |||
Lines changed: 125 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | | - | |
| 65 | + | |
| 66 | + | |
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
| |||
108 | 109 | | |
109 | 110 | | |
110 | 111 | | |
111 | | - | |
112 | | - | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
113 | 118 | | |
114 | 119 | | |
115 | 120 | | |
| |||
267 | 272 | | |
268 | 273 | | |
269 | 274 | | |
270 | | - | |
271 | | - | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
272 | 279 | | |
273 | 280 | | |
274 | 281 | | |
| |||
278 | 285 | | |
279 | 286 | | |
280 | 287 | | |
281 | | - | |
| 288 | + | |
282 | 289 | | |
283 | 290 | | |
284 | 291 | | |
285 | 292 | | |
286 | 293 | | |
287 | | - | |
288 | | - | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
289 | 300 | | |
290 | 301 | | |
291 | 302 | | |
| |||
306 | 317 | | |
307 | 318 | | |
308 | 319 | | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 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 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
0 commit comments