wolfdocx 0.1.0__py3-none-any.whl
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.
- wolfdocx/__init__.py +25 -0
- wolfdocx/_cache_root.py +24 -0
- wolfdocx/_comment_threads.py +278 -0
- wolfdocx/_inspection_cache.py +374 -0
- wolfdocx/_version.py +3 -0
- wolfdocx/admission.py +163 -0
- wolfdocx/assembly.py +463 -0
- wolfdocx/authoring.py +2773 -0
- wolfdocx/canonical.py +267 -0
- wolfdocx/content.py +963 -0
- wolfdocx/document.py +215 -0
- wolfdocx/errors.py +169 -0
- wolfdocx/find_replace.py +1210 -0
- wolfdocx/formatting.py +3160 -0
- wolfdocx/inspect.py +3999 -0
- wolfdocx/model.py +1038 -0
- wolfdocx/numbering_authoring.py +1422 -0
- wolfdocx/output.py +13 -0
- wolfdocx/package.py +2313 -0
- wolfdocx/package_profile.py +128 -0
- wolfdocx/page_setup.py +2483 -0
- wolfdocx/paragraph_authoring.py +1842 -0
- wolfdocx/properties.py +2728 -0
- wolfdocx/protection.py +1909 -0
- wolfdocx/regions.py +1993 -0
- wolfdocx/render_warnings.py +106 -0
- wolfdocx/reporting.py +943 -0
- wolfdocx/review.py +2862 -0
- wolfdocx/run_text.py +117 -0
- wolfdocx/section_break.py +1810 -0
- wolfdocx/session_ops/__init__.py +45 -0
- wolfdocx/session_ops/_bridge.py +151 -0
- wolfdocx/session_ops/lists.py +104 -0
- wolfdocx/session_ops/paragraphs.py +39 -0
- wolfdocx/session_ops/rich_objects.py +2598 -0
- wolfdocx/session_ops/run_edits.py +70 -0
- wolfdocx/session_ops/runs.py +313 -0
- wolfdocx/session_ops/sections.py +109 -0
- wolfdocx/session_ops/styling.py +99 -0
- wolfdocx/session_ops/tables.py +247 -0
- wolfdocx/styles_authoring.py +2822 -0
- wolfdocx/table_cell_properties.py +1458 -0
- wolfdocx/table_column.py +1501 -0
- wolfdocx/table_delete.py +942 -0
- wolfdocx/table_insert.py +1114 -0
- wolfdocx/table_merge.py +1863 -0
- wolfdocx/table_properties.py +2429 -0
- wolfdocx/table_row_properties.py +1095 -0
- wolfdocx/table_structure.py +1044 -0
- wolfdocx/text_effects.py +1438 -0
- wolfdocx-0.1.0.dist-info/METADATA +127 -0
- wolfdocx-0.1.0.dist-info/RECORD +54 -0
- wolfdocx-0.1.0.dist-info/WHEEL +4 -0
- wolfdocx-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
"""Persistent per-part inspection facts.
|
|
2
|
+
|
|
3
|
+
Structural inspection parses every XML part of a package. Whether one part parses
|
|
4
|
+
under the bounded parser, and how many elements it holds, depends only on the part's
|
|
5
|
+
bytes, its parse mode, the effective :class:`~wolfdocx.inspect.PackageLimits`, and the
|
|
6
|
+
inspection code. This store remembers that fact for the parts of packages that
|
|
7
|
+
inspected valid, so a later process can defer parsing a part whose bytes it has seen
|
|
8
|
+
until something reads its tree.
|
|
9
|
+
|
|
10
|
+
The descriptive stage (part texts, active-content facts, the document fingerprint) is a
|
|
11
|
+
pure function of parsed trees, so its results are stored too, as JSON values keyed by
|
|
12
|
+
the digests of every part they read.
|
|
13
|
+
|
|
14
|
+
The store is a SQLite database below the WolfDocx cache root
|
|
15
|
+
(``WOLFDOCX_CACHE_DIR`` or the platform cache directory). Set
|
|
16
|
+
``WOLFDOCX_INSPECTION_CACHE=off`` to disable it. Gates run with it suspended unless
|
|
17
|
+
``WOLFDOCX_GATE_INSPECTION_CACHE=on`` (see :func:`gate_scope`). Every failure to open,
|
|
18
|
+
read, or write it disables it for the rest of the process, so inspection falls back to
|
|
19
|
+
parsing every part.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import contextlib
|
|
25
|
+
import contextvars
|
|
26
|
+
import hashlib
|
|
27
|
+
import json
|
|
28
|
+
import os
|
|
29
|
+
import stat
|
|
30
|
+
import sys
|
|
31
|
+
import threading
|
|
32
|
+
import zlib
|
|
33
|
+
from collections.abc import Iterator, Mapping
|
|
34
|
+
from pathlib import Path
|
|
35
|
+
from typing import TYPE_CHECKING, Any
|
|
36
|
+
|
|
37
|
+
from ._cache_root import default_cache_root
|
|
38
|
+
|
|
39
|
+
if TYPE_CHECKING:
|
|
40
|
+
import sqlite3
|
|
41
|
+
|
|
42
|
+
_FORMAT = b"wolfdocx-inspection-part-facts-v2"
|
|
43
|
+
_DATABASE_NAME = "inspection-part-facts-v1.sqlite3"
|
|
44
|
+
_DISABLED_VALUES = frozenset({"0", "off", "false", "no"})
|
|
45
|
+
_ENABLED_VALUES = frozenset({"1", "on", "true", "yes"})
|
|
46
|
+
# Rows are about 60 bytes, so this bounds the database near 12 MB; the oldest rows go
|
|
47
|
+
# first once it is reached.
|
|
48
|
+
MAX_ROWS = 200_000
|
|
49
|
+
# Descriptive values are compressed JSON. A value above the per-value bound is not
|
|
50
|
+
# stored; once the total exceeds its bound the oldest values go first.
|
|
51
|
+
MAX_VALUE_BYTES = 4 * 1024 * 1024
|
|
52
|
+
MAX_VALUE_TOTAL_BYTES = 128 * 1024 * 1024
|
|
53
|
+
# Stored facts depend on the bounded parser, descriptive stage, hashing and model.
|
|
54
|
+
_IDENTITY_MODULES = ("inspect.py", "canonical.py", "model.py")
|
|
55
|
+
|
|
56
|
+
_lock = threading.Lock()
|
|
57
|
+
# One store (or ``None`` once it failed) per process and database path.
|
|
58
|
+
_stores: dict[tuple[int, Path], PartFactStore | None] = {}
|
|
59
|
+
_code_identity: bytes | None = None
|
|
60
|
+
# True inside a scope that must not trust the store (see ``gate_scope``).
|
|
61
|
+
_suspended: contextvars.ContextVar[bool] = contextvars.ContextVar(
|
|
62
|
+
"wolfdocx_inspection_store_suspended", default=False
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def enabled() -> bool:
|
|
67
|
+
"""False when ``WOLFDOCX_INSPECTION_CACHE`` disables the store or a scope suspends it."""
|
|
68
|
+
if _suspended.get():
|
|
69
|
+
return False
|
|
70
|
+
value = os.environ.get("WOLFDOCX_INSPECTION_CACHE")
|
|
71
|
+
return value is None or value.strip().lower() not in _DISABLED_VALUES
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@contextlib.contextmanager
|
|
75
|
+
def gate_scope() -> Iterator[None]:
|
|
76
|
+
"""Suspend the store for this thread's context unless gates opt in.
|
|
77
|
+
|
|
78
|
+
Inside the scope nothing is read from or written to the store, and in-process
|
|
79
|
+
inspections built with it are neither returned from the memo nor lent as reuse
|
|
80
|
+
donors, so every verdict and report derives from the package bytes alone. Set
|
|
81
|
+
``WOLFDOCX_GATE_INSPECTION_CACHE=on`` to let gates use the store.
|
|
82
|
+
"""
|
|
83
|
+
value = os.environ.get("WOLFDOCX_GATE_INSPECTION_CACHE", "")
|
|
84
|
+
if value.strip().lower() in _ENABLED_VALUES:
|
|
85
|
+
yield
|
|
86
|
+
return
|
|
87
|
+
token = _suspended.set(True)
|
|
88
|
+
try:
|
|
89
|
+
yield
|
|
90
|
+
finally:
|
|
91
|
+
_suspended.reset(token)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def database_path() -> Path:
|
|
95
|
+
return default_cache_root() / "inspection" / _DATABASE_NAME
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def open_store() -> PartFactStore | None:
|
|
99
|
+
"""Return this process's store, or ``None`` when it is disabled or unusable."""
|
|
100
|
+
if not enabled():
|
|
101
|
+
return None
|
|
102
|
+
path = database_path()
|
|
103
|
+
key = (os.getpid(), path)
|
|
104
|
+
with _lock:
|
|
105
|
+
if key in _stores:
|
|
106
|
+
return _stores[key]
|
|
107
|
+
store: PartFactStore | None = None
|
|
108
|
+
try:
|
|
109
|
+
import sqlite3
|
|
110
|
+
except ImportError:
|
|
111
|
+
pass
|
|
112
|
+
else:
|
|
113
|
+
try:
|
|
114
|
+
store = PartFactStore(path, _identity())
|
|
115
|
+
except sqlite3.DatabaseError as exc:
|
|
116
|
+
# A database that is not one (a torn or foreign file) is rebuilt once;
|
|
117
|
+
# busy or locked databases raise subclasses and are left alone.
|
|
118
|
+
if type(exc) is sqlite3.DatabaseError:
|
|
119
|
+
store = _rebuild(path)
|
|
120
|
+
except (sqlite3.Error, OSError):
|
|
121
|
+
pass
|
|
122
|
+
_stores[key] = store
|
|
123
|
+
return store
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _rebuild(path: Path) -> PartFactStore | None:
|
|
127
|
+
import sqlite3
|
|
128
|
+
|
|
129
|
+
try:
|
|
130
|
+
for suffix in ("", "-wal", "-shm"):
|
|
131
|
+
path.with_name(path.name + suffix).unlink(missing_ok=True)
|
|
132
|
+
return PartFactStore(path, _identity())
|
|
133
|
+
except (sqlite3.Error, OSError):
|
|
134
|
+
return None
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _identity() -> bytes:
|
|
138
|
+
"""Digest of everything besides the limits and the part that decides its facts."""
|
|
139
|
+
global _code_identity
|
|
140
|
+
if _code_identity is None:
|
|
141
|
+
import pyexpat
|
|
142
|
+
|
|
143
|
+
digest = hashlib.sha256(_FORMAT)
|
|
144
|
+
digest.update(f"\0{sys.version_info[0]}.{sys.version_info[1]}".encode())
|
|
145
|
+
digest.update(f"\0{pyexpat.EXPAT_VERSION}".encode())
|
|
146
|
+
package = Path(__file__).parent
|
|
147
|
+
for name in _IDENTITY_MODULES:
|
|
148
|
+
digest.update(b"\0" + name.encode() + b"\0")
|
|
149
|
+
digest.update((package / name).read_bytes())
|
|
150
|
+
_code_identity = digest.digest()
|
|
151
|
+
return _code_identity
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _private(mode: int, uid: int) -> bool:
|
|
155
|
+
"""True when the path belongs to this user and no one else may write to it."""
|
|
156
|
+
getuid = getattr(os, "getuid", None)
|
|
157
|
+
if getuid is None:
|
|
158
|
+
return True
|
|
159
|
+
return uid == getuid() and not mode & (stat.S_IWGRP | stat.S_IWOTH)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
class PartFactStore:
|
|
163
|
+
"""Element counts of XML parts that parsed cleanly inside valid packages, and the
|
|
164
|
+
descriptive values computed from them."""
|
|
165
|
+
|
|
166
|
+
def __init__(self, path: Path, identity: bytes) -> None:
|
|
167
|
+
import sqlite3
|
|
168
|
+
|
|
169
|
+
self._identity = identity
|
|
170
|
+
self._errors = (sqlite3.Error, OSError)
|
|
171
|
+
self._suppress = contextlib.suppress(sqlite3.Error)
|
|
172
|
+
directory = path.parent
|
|
173
|
+
directory.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
174
|
+
info = directory.stat()
|
|
175
|
+
if not _private(info.st_mode, info.st_uid):
|
|
176
|
+
raise PermissionError(f"{directory} is not private to this user")
|
|
177
|
+
created = not path.exists()
|
|
178
|
+
connection = sqlite3.connect(
|
|
179
|
+
path, timeout=0.25, isolation_level=None, check_same_thread=False
|
|
180
|
+
)
|
|
181
|
+
try:
|
|
182
|
+
if created:
|
|
183
|
+
os.chmod(path, 0o600)
|
|
184
|
+
info = path.stat()
|
|
185
|
+
if not _private(info.st_mode, info.st_uid):
|
|
186
|
+
raise PermissionError(f"{path} is not private to this user")
|
|
187
|
+
if connection.execute("PRAGMA journal_mode").fetchone()[0] != "wal":
|
|
188
|
+
connection.execute("PRAGMA journal_mode=WAL")
|
|
189
|
+
connection.execute("PRAGMA synchronous=NORMAL")
|
|
190
|
+
connection.execute(
|
|
191
|
+
"CREATE TABLE IF NOT EXISTS part_facts "
|
|
192
|
+
"(key BLOB NOT NULL UNIQUE, elements INTEGER NOT NULL)"
|
|
193
|
+
)
|
|
194
|
+
connection.execute(
|
|
195
|
+
"CREATE TABLE IF NOT EXISTS descriptive_facts "
|
|
196
|
+
"(key BLOB NOT NULL UNIQUE, value BLOB NOT NULL)"
|
|
197
|
+
)
|
|
198
|
+
except BaseException:
|
|
199
|
+
connection.close()
|
|
200
|
+
raise
|
|
201
|
+
self._connection: sqlite3.Connection | None = connection
|
|
202
|
+
self._lock = threading.Lock()
|
|
203
|
+
|
|
204
|
+
def facts(self, limits: Mapping[str, Any], mode: str) -> PartFacts:
|
|
205
|
+
"""Facts for parts parsed in ``mode`` under the given effective limits."""
|
|
206
|
+
prefix = hashlib.sha256(self._identity)
|
|
207
|
+
prefix.update(json.dumps(dict(limits), sort_keys=True).encode())
|
|
208
|
+
prefix.update(b"\0" + mode.encode() + b"\0")
|
|
209
|
+
return PartFacts(self, prefix)
|
|
210
|
+
|
|
211
|
+
def _disable(self) -> None:
|
|
212
|
+
connection, self._connection = self._connection, None
|
|
213
|
+
if connection is not None:
|
|
214
|
+
with self._suppress:
|
|
215
|
+
connection.close()
|
|
216
|
+
|
|
217
|
+
def lookup(self, key: bytes) -> int | None:
|
|
218
|
+
with self._lock:
|
|
219
|
+
if self._connection is None:
|
|
220
|
+
return None
|
|
221
|
+
try:
|
|
222
|
+
row = self._connection.execute(
|
|
223
|
+
"SELECT elements FROM part_facts WHERE key = ?", (key,)
|
|
224
|
+
).fetchone()
|
|
225
|
+
except self._errors:
|
|
226
|
+
self._disable()
|
|
227
|
+
return None
|
|
228
|
+
if row is None:
|
|
229
|
+
return None
|
|
230
|
+
elements = row[0]
|
|
231
|
+
if type(elements) is not int or elements < 0:
|
|
232
|
+
self.forget(key)
|
|
233
|
+
return None
|
|
234
|
+
return elements
|
|
235
|
+
|
|
236
|
+
def record(self, facts: Mapping[bytes, int]) -> None:
|
|
237
|
+
if not facts:
|
|
238
|
+
return
|
|
239
|
+
with self._lock:
|
|
240
|
+
connection = self._connection
|
|
241
|
+
if connection is None:
|
|
242
|
+
return
|
|
243
|
+
try:
|
|
244
|
+
connection.execute("BEGIN IMMEDIATE")
|
|
245
|
+
try:
|
|
246
|
+
connection.executemany(
|
|
247
|
+
"INSERT OR IGNORE INTO part_facts (key, elements) VALUES (?, ?)",
|
|
248
|
+
facts.items(),
|
|
249
|
+
)
|
|
250
|
+
newest = connection.execute("SELECT max(rowid) FROM part_facts").fetchone()[0]
|
|
251
|
+
if newest is not None and newest > MAX_ROWS:
|
|
252
|
+
connection.execute(
|
|
253
|
+
"DELETE FROM part_facts WHERE rowid <= ?", (newest - MAX_ROWS,)
|
|
254
|
+
)
|
|
255
|
+
connection.execute("COMMIT")
|
|
256
|
+
except BaseException:
|
|
257
|
+
connection.execute("ROLLBACK")
|
|
258
|
+
raise
|
|
259
|
+
except self._errors:
|
|
260
|
+
self._disable()
|
|
261
|
+
|
|
262
|
+
def forget(self, key: bytes) -> None:
|
|
263
|
+
with self._lock:
|
|
264
|
+
if self._connection is None:
|
|
265
|
+
return
|
|
266
|
+
try:
|
|
267
|
+
self._connection.execute("DELETE FROM part_facts WHERE key = ?", (key,))
|
|
268
|
+
except self._errors:
|
|
269
|
+
self._disable()
|
|
270
|
+
|
|
271
|
+
def lookup_value(self, key: bytes) -> Any:
|
|
272
|
+
"""Return the stored JSON value, or ``None``; an undecodable value is evicted."""
|
|
273
|
+
with self._lock:
|
|
274
|
+
if self._connection is None:
|
|
275
|
+
return None
|
|
276
|
+
try:
|
|
277
|
+
row = self._connection.execute(
|
|
278
|
+
"SELECT value FROM descriptive_facts WHERE key = ?", (key,)
|
|
279
|
+
).fetchone()
|
|
280
|
+
except self._errors:
|
|
281
|
+
self._disable()
|
|
282
|
+
return None
|
|
283
|
+
if row is None:
|
|
284
|
+
return None
|
|
285
|
+
try:
|
|
286
|
+
return json.loads(zlib.decompress(row[0]))
|
|
287
|
+
except (TypeError, ValueError, zlib.error):
|
|
288
|
+
self.forget_value(key)
|
|
289
|
+
return None
|
|
290
|
+
|
|
291
|
+
def record_values(self, values: Mapping[bytes, Any]) -> None:
|
|
292
|
+
encoded = []
|
|
293
|
+
for key, value in values.items():
|
|
294
|
+
blob = zlib.compress(
|
|
295
|
+
json.dumps(value, ensure_ascii=False, separators=(",", ":")).encode(), 1
|
|
296
|
+
)
|
|
297
|
+
if len(blob) <= MAX_VALUE_BYTES:
|
|
298
|
+
encoded.append((key, blob))
|
|
299
|
+
if not encoded:
|
|
300
|
+
return
|
|
301
|
+
with self._lock:
|
|
302
|
+
connection = self._connection
|
|
303
|
+
if connection is None:
|
|
304
|
+
return
|
|
305
|
+
try:
|
|
306
|
+
connection.execute("BEGIN IMMEDIATE")
|
|
307
|
+
try:
|
|
308
|
+
connection.executemany(
|
|
309
|
+
"INSERT OR IGNORE INTO descriptive_facts (key, value) VALUES (?, ?)",
|
|
310
|
+
encoded,
|
|
311
|
+
)
|
|
312
|
+
total = connection.execute(
|
|
313
|
+
"SELECT coalesce(sum(length(value)), 0) FROM descriptive_facts"
|
|
314
|
+
).fetchone()[0]
|
|
315
|
+
if total > MAX_VALUE_TOTAL_BYTES:
|
|
316
|
+
# Drop the oldest values until the rest fit in half the bound.
|
|
317
|
+
kept = 0
|
|
318
|
+
cutoff = None
|
|
319
|
+
for rowid, size in connection.execute(
|
|
320
|
+
"SELECT rowid, length(value) FROM descriptive_facts ORDER BY rowid DESC"
|
|
321
|
+
):
|
|
322
|
+
kept += size
|
|
323
|
+
if kept > MAX_VALUE_TOTAL_BYTES // 2:
|
|
324
|
+
cutoff = rowid
|
|
325
|
+
break
|
|
326
|
+
if cutoff is not None:
|
|
327
|
+
connection.execute(
|
|
328
|
+
"DELETE FROM descriptive_facts WHERE rowid <= ?", (cutoff,)
|
|
329
|
+
)
|
|
330
|
+
connection.execute("COMMIT")
|
|
331
|
+
except BaseException:
|
|
332
|
+
connection.execute("ROLLBACK")
|
|
333
|
+
raise
|
|
334
|
+
except self._errors:
|
|
335
|
+
self._disable()
|
|
336
|
+
|
|
337
|
+
def forget_value(self, key: bytes) -> None:
|
|
338
|
+
with self._lock:
|
|
339
|
+
if self._connection is None:
|
|
340
|
+
return
|
|
341
|
+
try:
|
|
342
|
+
self._connection.execute("DELETE FROM descriptive_facts WHERE key = ?", (key,))
|
|
343
|
+
except self._errors:
|
|
344
|
+
self._disable()
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
class PartFacts:
|
|
348
|
+
"""Lookups for one parse mode and limit set, keyed by part digest."""
|
|
349
|
+
|
|
350
|
+
__slots__ = ("_prefix", "store")
|
|
351
|
+
|
|
352
|
+
def __init__(self, store: PartFactStore, prefix: Any) -> None:
|
|
353
|
+
self.store = store
|
|
354
|
+
self._prefix = prefix
|
|
355
|
+
|
|
356
|
+
def key(self, part_sha256: str) -> bytes:
|
|
357
|
+
digest = self._prefix.copy()
|
|
358
|
+
digest.update(part_sha256.encode())
|
|
359
|
+
return digest.digest()
|
|
360
|
+
|
|
361
|
+
def lookup_value(self, key: bytes) -> Any:
|
|
362
|
+
return self.store.lookup_value(key)
|
|
363
|
+
|
|
364
|
+
def lookup(self, key: bytes) -> int | None:
|
|
365
|
+
return self.store.lookup(key)
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def _reset_for_tests() -> None:
|
|
369
|
+
"""Close and forget every store this process opened."""
|
|
370
|
+
with _lock:
|
|
371
|
+
for store in _stores.values():
|
|
372
|
+
if store is not None:
|
|
373
|
+
store._disable()
|
|
374
|
+
_stores.clear()
|
wolfdocx/_version.py
ADDED
wolfdocx/admission.py
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"""Shared DOCX safety admission, independent of optional import and recovery policies."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hashlib
|
|
6
|
+
import zipfile
|
|
7
|
+
import zlib
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from xml.etree import ElementTree as ET
|
|
10
|
+
|
|
11
|
+
from .inspect import PackageLimits, _parse_bounded_xml, _XmlSafetyError
|
|
12
|
+
from .package import PackageError, PackageGraph
|
|
13
|
+
|
|
14
|
+
_R = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
|
|
15
|
+
_STRICT_R = "http://purl.oclc.org/ooxml/officeDocument/relationships"
|
|
16
|
+
_OFFICE_DOCUMENT = f"{_R}/officeDocument"
|
|
17
|
+
_WML = "application/vnd.openxmlformats-officedocument.wordprocessingml"
|
|
18
|
+
_MAIN_CONTENT_TYPES = frozenset(
|
|
19
|
+
{
|
|
20
|
+
f"{_WML}.document.main+xml",
|
|
21
|
+
f"{_WML}.template.main+xml",
|
|
22
|
+
"application/vnd.ms-word.document.macroEnabled.main+xml",
|
|
23
|
+
"application/vnd.ms-word.template.macroEnabledTemplate.main+xml",
|
|
24
|
+
}
|
|
25
|
+
)
|
|
26
|
+
_CFB_SIGNATURE = b"\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class _Damage(Exception):
|
|
30
|
+
def __init__(self, code: str, target: str | None = None) -> None:
|
|
31
|
+
super().__init__(code)
|
|
32
|
+
self.code = code
|
|
33
|
+
self.target = target
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
# -- package loading -----------------------------------------------------------------
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class _DiskPackageGraph(PackageGraph):
|
|
40
|
+
"""Read lazy parts from the admitted snapshot, independent of the display path."""
|
|
41
|
+
|
|
42
|
+
def _open_archive(self) -> zipfile.ZipFile:
|
|
43
|
+
return self._open_zip(self._snapshot_path)
|
|
44
|
+
|
|
45
|
+
def _mutation_source(self) -> bytes:
|
|
46
|
+
from .package import PackageFormatError, PackageSafetyError
|
|
47
|
+
|
|
48
|
+
try:
|
|
49
|
+
data = self._snapshot_path.read_bytes()
|
|
50
|
+
except OSError as exc:
|
|
51
|
+
raise PackageFormatError(
|
|
52
|
+
"package_read_failed", "Cannot read immutable package source."
|
|
53
|
+
) from exc
|
|
54
|
+
if hashlib.sha256(data).hexdigest() != self.file_sha256:
|
|
55
|
+
raise PackageSafetyError(
|
|
56
|
+
"package_source_changed",
|
|
57
|
+
"Source bytes no longer match the immutable package graph.",
|
|
58
|
+
)
|
|
59
|
+
return data
|
|
60
|
+
|
|
61
|
+
@classmethod
|
|
62
|
+
def from_snapshot(cls, source: Path, path: str | Path, limits: PackageLimits) -> PackageGraph:
|
|
63
|
+
graph = cls.from_file(source, limits=limits)
|
|
64
|
+
graph._snapshot_path = source
|
|
65
|
+
graph.path = Path(path)
|
|
66
|
+
return graph
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _admit(
|
|
70
|
+
data: bytes | Path,
|
|
71
|
+
limits: PackageLimits,
|
|
72
|
+
path: str | Path,
|
|
73
|
+
*,
|
|
74
|
+
whole: bool = True,
|
|
75
|
+
defer_parts: bool = False,
|
|
76
|
+
) -> PackageGraph:
|
|
77
|
+
"""Check member integrity and main XML; explicit recovery checks every XML part."""
|
|
78
|
+
try:
|
|
79
|
+
graph = (
|
|
80
|
+
_DiskPackageGraph.from_snapshot(data, path, limits)
|
|
81
|
+
if isinstance(data, Path)
|
|
82
|
+
else PackageGraph.from_bytes(data, path=path, limits=limits)
|
|
83
|
+
)
|
|
84
|
+
except PackageError as exc:
|
|
85
|
+
if not whole:
|
|
86
|
+
raise
|
|
87
|
+
raise _Damage(exc.code, exc.entry_name) from None
|
|
88
|
+
try:
|
|
89
|
+
with graph._open_archive() as archive:
|
|
90
|
+
for info in archive.infolist():
|
|
91
|
+
try:
|
|
92
|
+
with archive.open(info) as member:
|
|
93
|
+
while member.read(64 * 1024):
|
|
94
|
+
pass
|
|
95
|
+
except zipfile.BadZipFile:
|
|
96
|
+
raise _Damage("member_crc_invalid", info.filename) from None
|
|
97
|
+
except (zipfile.BadZipFile, zlib.error, EOFError, OSError, NotImplementedError):
|
|
98
|
+
raise _Damage("member_corrupt") from None
|
|
99
|
+
if whole and not defer_parts:
|
|
100
|
+
for name in graph.parts:
|
|
101
|
+
extension = name.rpartition(".")[2].lower()
|
|
102
|
+
if (
|
|
103
|
+
name not in graph.content_type_overrides
|
|
104
|
+
and extension not in graph.content_type_defaults
|
|
105
|
+
):
|
|
106
|
+
raise _Damage("part_content_type_missing", name)
|
|
107
|
+
main = _main_part_name(graph)
|
|
108
|
+
if main is None:
|
|
109
|
+
raise _Damage("main_document_missing")
|
|
110
|
+
if defer_parts:
|
|
111
|
+
return graph
|
|
112
|
+
try:
|
|
113
|
+
graph.validate_relationship_graph()
|
|
114
|
+
except PackageError as exc:
|
|
115
|
+
if not whole:
|
|
116
|
+
raise
|
|
117
|
+
raise _Damage(exc.code, exc.entry_name) from None
|
|
118
|
+
total = [0]
|
|
119
|
+
for name, part in graph.parts.items():
|
|
120
|
+
if not whole and name != main:
|
|
121
|
+
continue
|
|
122
|
+
if not _is_xml_part(name, part.content_type):
|
|
123
|
+
continue
|
|
124
|
+
try:
|
|
125
|
+
root = _parse_bounded_xml(
|
|
126
|
+
graph.read_part_bytes(name),
|
|
127
|
+
limits=limits,
|
|
128
|
+
total_elements=total,
|
|
129
|
+
preserve_qnames=False,
|
|
130
|
+
)
|
|
131
|
+
except _XmlSafetyError as exc:
|
|
132
|
+
raise _Damage(exc.code, name) from None
|
|
133
|
+
except (ET.ParseError, PackageError):
|
|
134
|
+
raise _Damage("malformed_xml", name) from None
|
|
135
|
+
if not whole or name.endswith(".rels"):
|
|
136
|
+
continue
|
|
137
|
+
identifiers = {relationship.id for relationship in graph.relationships_for(name)}
|
|
138
|
+
for element in root.iter():
|
|
139
|
+
for key, value in element.attrib.items():
|
|
140
|
+
if (
|
|
141
|
+
key.startswith((f"{{{_R}}}", f"{{{_STRICT_R}}}"))
|
|
142
|
+
and value
|
|
143
|
+
and value not in identifiers
|
|
144
|
+
):
|
|
145
|
+
raise _Damage("relationship_reference_dangling", name)
|
|
146
|
+
return graph
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _main_part_name(graph: PackageGraph) -> str | None:
|
|
150
|
+
for relationship in graph.relationships:
|
|
151
|
+
if (
|
|
152
|
+
relationship.type in {_OFFICE_DOCUMENT, f"{_STRICT_R}/officeDocument"}
|
|
153
|
+
and not relationship.is_external
|
|
154
|
+
):
|
|
155
|
+
target = relationship.resolved_target()
|
|
156
|
+
part = graph.parts.get(target)
|
|
157
|
+
if part is not None and part.content_type in _MAIN_CONTENT_TYPES:
|
|
158
|
+
return target
|
|
159
|
+
return None
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _is_xml_part(name: str, content_type: str | None) -> bool:
|
|
163
|
+
return name.endswith((".xml", ".rels")) or (content_type or "").endswith("xml")
|