Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

test: stabilize DOCX namespace CPU growth check (#7267)

LearningCircuitedf15e00f635
diff --git a/tests/security/test_ooxml_decompression_caps.py b/tests/security/test_ooxml_decompression_caps.py
index 51f8bd5475..567deff943 100644
--- a/tests/security/test_ooxml_decompression_caps.py
+++ b/tests/security/test_ooxml_decompression_caps.py
@@ -4891,23 +4891,43 @@ class TestDocxAbnormalMarkupIsRefused:
assert peak < 6 * 1024 * 1024
def test_guard_time_at_the_uri_cap_stays_linear(self):
- # 100,000 short elements named through a URI at the cap cost
- # the guard about a third more than through Word's own (~0.5 s
- # against ~0.4 s measured): each name is read a bounded number
- # of times.
+ # Compare the CPU work for 100,000 elements through a short URI
+ # and one at the cap: each name should be read a bounded number
+ # of times. The guard (including native XML parsing) runs on the
+ # calling thread, so thread_time excludes scheduler waits and
+ # unrelated background threads on a busy CI runner.
guard = _guard()
- def elapsed(length: int) -> float:
+ def document(length: int) -> bytes:
uri = b"urn:" + b"u" * (length - 4)
- data = _blank_with(
+ return _blank_with(
b"<w:p xmlns:u='%s'>" % uri + b"<u:x/>" * 100_000 + b"</w:p>"
)
- started = time.perf_counter()
+
+ short = document(71)
+ capped = document(guard.MAX_DOCX_NAMESPACE_URI_CHARS)
+
+ # Warm lazy initialization with a small document. Keep fixture
+ # construction outside the samples, too.
+ guard.validate_zip_container(_blank_with(b"<w:p/>"), ".docx")
+
+ def cpu_time(data: bytes) -> float:
+ started = time.thread_time()
guard.validate_zip_container(data, ".docx")
- return time.perf_counter() - started
+ return time.thread_time() - started
- word = elapsed(71)
- assert elapsed(guard.MAX_DOCX_NAMESPACE_URI_CHARS) < 3 * word + 1
+ short_times = []
+ capped_times = []
+ # Interleave two samples per input and take the faster one so a
+ # single CPU-time outlier does not decide the growth assertion.
+ # More full-size repeats can exhaust the timeout under coverage.
+ for _ in range(2):
+ short_times.append(cpu_time(short))
+ capped_times.append(cpu_time(capped))
+
+ assert min(capped_times) < 3 * min(short_times) + 1, (
+ f"Thread CPU seconds: short={short_times}, capped={capped_times}"
+ )
# libxml2 re-homes a removed subtree's namespace declarations through
# a cache it searches linearly, so lxml's remove(), which