fix: skill_pack — makra wykrywane także po typach treści OOXML (E2.2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
d0061dc517
commit
1081a1dba8
@@ -7,3 +7,4 @@
|
||||
- Pułapki: nazwy sprawdzane na `orig_filename` (na Windows `zipfile` zamienia `\` na `/`); ZIP64 przez `force_zip64` widać tylko w nagłówku lokalnym, więc `_is_zip64` czyta też go. Odrzucenie `pack_not_zip` łapie też błąd CRC/szyfrowanie przy odczycie.
|
||||
- 2.2: reguły plików w `read_pack` — `_open_archive` (reguły archiwum) wspólne dla paczki i wnętrza DOCX/XLSX; nowe kody `pack_file_type`, `pack_signature_mismatch`, `pack_macros`, `pack_nested_zip`. Wnętrze dokumentu sprawdzane tylko po katalogu centralnym (nic nie rozpakowane). Plik „tekstowy” = bez bajtu NUL (CSV w cp1250 przechodzi, UTF-16 odpada). Osadzone dokumenty w DOCX nie są sprawdzane rekurencyjnie (ponytail).
|
||||
- Dla 2.3: pliki wtyczki (`.sh`, `.py`) odpadają już na `pack_file_type` z listą plików — 2.3 musi sprawdzić wtyczkę PRZED regułą rozszerzeń, jeśli chce własnego kodu odrzucenia. Pułapka narzędziowa: heredoc bash→python zjada `\x03`/`\0` w źródle — sekwencje bajtów wpisuj narzędziem Edit.
|
||||
- 2.2 (dopisek po hooku security): makra wykrywane też po `[Content_Types].xml` (podciągi vbaproject/macroenabled/macrosheet/activex, bez parsowania XML, manifest ≤ 1 MB) — przemianowana część VBA nie przechodzi.
|
||||
|
||||
@@ -58,6 +58,9 @@ MESSAGES = {
|
||||
TEXT_TYPES = {".md", ".txt", ".csv", ".json", ".yaml", ".yml"}
|
||||
OFFICE_TYPES = {".docx", ".xlsx"}
|
||||
ALLOWED_TYPES = TEXT_TYPES | OFFICE_TYPES | {".pdf"}
|
||||
MAX_MANIFEST_BYTES = 1024 * 1024
|
||||
#: Content types that make an OOXML document carry code (VBA, macro sheets, ActiveX), lower-cased.
|
||||
_MACRO_TYPES = (b"vbaproject", b"macroenabled", b"macrosheet", b"activex")
|
||||
_ZIP_MAGIC = (b"PK\x03\x04", b"PK\x05\x06", b"PK\x07\x08")
|
||||
|
||||
|
||||
@@ -164,17 +167,24 @@ def _suffix(path: str) -> str:
|
||||
|
||||
|
||||
def _check_office(path: str, data: bytes) -> None:
|
||||
# Names only: a document's parts are never inflated, so its archive rules cost no decompression.
|
||||
# Only `[Content_Types].xml` is ever inflated (capped first); no other part is read. The name
|
||||
# `vbaProject.bin` is a convention, not a rule — a renamed part still has to declare its content
|
||||
# type, so the manifest is searched too (as bytes: nothing here parses XML).
|
||||
# ponytail: an embedded document (word/embeddings/*.docm) is not recursed into; Word itself asks
|
||||
# before running its macros. Recurse here if that stops being enough.
|
||||
try:
|
||||
_, parts = _open_archive(data)
|
||||
zf, parts = _open_archive(data)
|
||||
by_name = {p.orig_filename: p for p in parts}
|
||||
manifest = by_name.get("[Content_Types].xml")
|
||||
if manifest is None or manifest.file_size > MAX_MANIFEST_BYTES:
|
||||
raise PackRejected("pack_signature_mismatch")
|
||||
types = zf.read(manifest).lower()
|
||||
except PackRejected as inner:
|
||||
raise PackRejected(inner.code, [path]) from None
|
||||
names = {p.orig_filename for p in parts}
|
||||
if "[Content_Types].xml" not in names:
|
||||
raise PackRejected("pack_signature_mismatch", [path])
|
||||
if any(n.rsplit("/", 1)[-1].lower() == "vbaproject.bin" for n in names):
|
||||
except (zipfile.BadZipFile, RuntimeError, NotImplementedError, EOFError, zlib.error):
|
||||
raise PackRejected("pack_not_zip", [path]) from None
|
||||
if (any(n.rsplit("/", 1)[-1].lower() == "vbaproject.bin" for n in by_name)
|
||||
or any(marker in types for marker in _MACRO_TYPES)):
|
||||
raise PackRejected("pack_macros", [path])
|
||||
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import os
|
||||
import stat
|
||||
import warnings
|
||||
import zipfile
|
||||
@@ -184,10 +185,28 @@ def test_the_inside_of_an_office_file_obeys_the_path_rules():
|
||||
assert _files_code("s/r/a.xlsx", data) == ("pack_bad_path", ["s/r/a.xlsx"])
|
||||
|
||||
|
||||
def test_the_inside_of_an_office_file_is_never_inflated(monkeypatch):
|
||||
@pytest.mark.parametrize("types", [
|
||||
b'<Default Extension="bin" ContentType="application/vnd.ms-office.vbaProject"/>',
|
||||
b'<Override PartName="/word/document.xml" ContentType="application/vnd.ms-word.document.'
|
||||
b'macroEnabled.main+xml"/>',
|
||||
b'<Override PartName="/word/activeX/a.xml" ContentType="application/vnd.ms-office.activeX+xml"/>',
|
||||
])
|
||||
def test_macros_declared_only_in_the_content_types_are_refused(types):
|
||||
data = _zip([("[Content_Types].xml", b"<Types>" + types + b"</Types>"),
|
||||
("word/renamed.dat", b"code")])
|
||||
assert _files_code("s/r/a.docx", data) == ("pack_macros", ["s/r/a.docx"])
|
||||
|
||||
|
||||
def test_an_oversized_content_types_part_is_refused():
|
||||
data = _zip([("[Content_Types].xml", os.urandom(skill_pack.MAX_MANIFEST_BYTES + 1))],
|
||||
compression=zipfile.ZIP_STORED)
|
||||
assert _files_code("s/r/a.docx", data)[0] == "pack_signature_mismatch"
|
||||
|
||||
|
||||
def test_only_the_content_types_of_an_office_file_are_inflated(monkeypatch):
|
||||
calls = []
|
||||
real = zipfile.ZipFile.read
|
||||
monkeypatch.setattr(zipfile.ZipFile, "read", lambda self, name, *a: calls.append(name)
|
||||
or real(self, name, *a))
|
||||
read_pack(_zip([("s/r/a.docx", _office("word/document.xml"))]))
|
||||
assert [getattr(c, "filename", c) for c in calls] == ["s/r/a.docx"]
|
||||
assert [getattr(c, "filename", c) for c in calls] == ["s/r/a.docx", "[Content_Types].xml"]
|
||||
|
||||
Reference in New Issue
Block a user