mail-parser 4.11.0__tar.gz → 4.12.0__tar.gz
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.
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.github/copilot-instructions.md +13 -12
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.github/instructions/python.instructions.md +2 -1
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.github/workflows/main.yml +8 -8
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.markdownlint.json +1 -1
- {mail_parser-4.11.0 → mail_parser-4.12.0}/AGENTS.md +111 -11
- mail_parser-4.12.0/GLOSSARY.md +60 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/PKG-INFO +298 -115
- {mail_parser-4.11.0 → mail_parser-4.12.0}/README.md +295 -113
- mail_parser-4.12.0/SECURITY.md +117 -0
- mail_parser-4.12.0/docs/adr/0002-mime-tree-as-flat-parent-indexed-list.md +15 -0
- mail_parser-4.12.0/docs/features/message-structure/issues/01-mime-tree-property.md +44 -0
- mail_parser-4.12.0/docs/features/message-structure/issues/02-render-structure.md +45 -0
- mail_parser-4.12.0/docs/features/message-structure/issues/03-command-line-and-readme.md +39 -0
- mail_parser-4.12.0/docs/features/message-structure/spec.md +202 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/pyproject.toml +5 -1
- {mail_parser-4.11.0 → mail_parser-4.12.0}/src/mailparser/__init__.py +2 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/src/mailparser/__main__.py +63 -5
- {mail_parser-4.11.0 → mail_parser-4.12.0}/src/mailparser/addresses.py +23 -8
- {mail_parser-4.11.0 → mail_parser-4.12.0}/src/mailparser/const.py +3 -1
- {mail_parser-4.11.0 → mail_parser-4.12.0}/src/mailparser/core.py +389 -47
- {mail_parser-4.11.0 → mail_parser-4.12.0}/src/mailparser/exceptions.py +5 -1
- {mail_parser-4.11.0 → mail_parser-4.12.0}/src/mailparser/received.py +17 -7
- mail_parser-4.12.0/src/mailparser/structure.py +444 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/src/mailparser/utils.py +584 -65
- {mail_parser-4.11.0 → mail_parser-4.12.0}/src/mailparser/version.py +1 -1
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/test_address_recovery.py +38 -12
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/test_corpus_regression.py +5 -2
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/test_date_recovery.py +48 -14
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/test_encoded_word_context.py +31 -10
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/test_improved_received_patterns.py +3 -1
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/test_inline_body.py +36 -11
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/test_international_addresses.py +25 -7
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/test_mail_parser.py +1465 -96
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/test_main.py +31 -4
- mail_parser-4.12.0/tests/test_message_structure.py +897 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/test_mime_unicode_metadata.py +15 -6
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/test_received_corpus.py +20 -9
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/test_received_envelope.py +5 -2
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/test_received_lexical.py +27 -7
- mail_parser-4.12.0/tests/test_rfc2231_parameter_recovery.py +1170 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/test_subject_unfolding.py +7 -2
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/test_utils.py +88 -35
- {mail_parser-4.11.0 → mail_parser-4.12.0}/uv.lock +347 -277
- mail_parser-4.11.0/GLOSSARY.md +0 -21
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.agents/rfc-review/report-template.md +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.agents/rfc-review/rfc-scope.md +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.agents/rfc-review/workflow.md +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.agents/skills/mail-rfc-assessment/SKILL.md +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.agents/skills/mail-rfc-assessment/agents/openai.yaml +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.agents/skills/mail-rfc-diff/SKILL.md +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.agents/skills/mail-rfc-diff/agents/openai.yaml +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.claude/agents/security-reviewer.md +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.github/FUNDING.yml +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.github/ISSUE_TEMPLATE/feature_request.md +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.github/instructions/containerization-docker-best-practices.instructions.md +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.github/instructions/github-actions-ci-cd-best-practices.instructions.md +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.github/instructions/markdown.instructions.md +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.gitignore +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/.pre-commit-config.yaml +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/CLAUDE.md +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/Dockerfile +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/LICENSE.txt +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/Makefile +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/NOTICE.txt +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/docker-compose.yml +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/docs/adr/0001-bounded-forensic-recovery.md +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/docs/images/Bitcoin SpamScope.jpg +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/docs/rfc-review-skills.md +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/src/mailparser/dates.py +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/src/mailparser/serialization.py +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/fixtures/corpus_expected.json +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_malformed_1 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_malformed_2 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_malformed_3 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_outlook_1 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_test_1 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_test_10 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_test_11 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_test_12 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_test_13 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_test_14 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_test_15 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_test_16 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_test_17 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_test_18 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_test_19 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_test_2 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_test_3 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_test_4 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_test_5 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_test_6 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_test_7 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_test_8 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/mails/mail_test_9 +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/test_input_octet_preservation.py +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/test_security_recovery.py +0 -0
- {mail_parser-4.11.0 → mail_parser-4.12.0}/tests/work_budget.py +0 -0
|
@@ -12,10 +12,11 @@ it excels at security analysis, forensics, and RFC-compliant email processing.
|
|
|
12
12
|
|
|
13
13
|
```python
|
|
14
14
|
import mailparser
|
|
15
|
-
|
|
16
|
-
mail = mailparser.
|
|
17
|
-
mail = mailparser.
|
|
18
|
-
mail = mailparser.
|
|
15
|
+
|
|
16
|
+
mail = mailparser.parse_from_file(filepath) # Standard email files
|
|
17
|
+
mail = mailparser.parse_from_string(raw_email) # Email as string
|
|
18
|
+
mail = mailparser.parse_from_bytes(email_bytes) # Email as bytes
|
|
19
|
+
mail = mailparser.parse_from_file_msg(msg_file) # Outlook .msg files
|
|
19
20
|
```
|
|
20
21
|
|
|
21
22
|
### Triple-Format Property Access
|
|
@@ -23,9 +24,9 @@ mail = mailparser.parse_from_file_msg(msg_file) # Outlook .msg files
|
|
|
23
24
|
Every parsed component offers **three access patterns** (`src/mailparser/core.py:550-570`):
|
|
24
25
|
|
|
25
26
|
```python
|
|
26
|
-
mail.subject
|
|
27
|
-
mail.subject_raw
|
|
28
|
-
mail.subject_json
|
|
27
|
+
mail.subject # Python object (decoded string)
|
|
28
|
+
mail.subject_raw # Raw header value (JSON list)
|
|
29
|
+
mail.subject_json # JSON-serialized version
|
|
29
30
|
```
|
|
30
31
|
|
|
31
32
|
This pattern applies to all properties via `__getattr__` magic in `core.py`.
|
|
@@ -35,8 +36,8 @@ This pattern applies to all properties via `__getattr__` magic in `core.py`.
|
|
|
35
36
|
Headers with hyphens use **underscore substitution** (`core.py:__getattr__`):
|
|
36
37
|
|
|
37
38
|
```python
|
|
38
|
-
mail.X_MSMail_Priority
|
|
39
|
-
mail.Content_Type
|
|
39
|
+
mail.X_MSMail_Priority # Accesses "X-MSMail-Priority" header
|
|
40
|
+
mail.Content_Type # Accesses "Content-Type" header
|
|
40
41
|
```
|
|
41
42
|
|
|
42
43
|
## Development Workflows
|
|
@@ -94,9 +95,9 @@ Version is **dynamically loaded** from `src/mailparser/version.py` (see
|
|
|
94
95
|
The parser identifies RFC violations that could indicate malicious intent (`core.py:240-268`):
|
|
95
96
|
|
|
96
97
|
```python
|
|
97
|
-
mail.has_defects
|
|
98
|
-
mail.defects
|
|
99
|
-
mail.defects_categories
|
|
98
|
+
mail.has_defects # Boolean flag
|
|
99
|
+
mail.defects # List of defect dicts by content type
|
|
100
|
+
mail.defects_categories # Set of defect class names (e.g., "StartBoundaryNotFoundDefect")
|
|
100
101
|
```
|
|
101
102
|
|
|
102
103
|
**Epilogue defect handling** (`core.py:320-335`): When `EPILOGUE_DEFECTS` are detected, parser extracts hidden
|
|
@@ -15,12 +15,12 @@ jobs:
|
|
|
15
15
|
python-version: ['3.10', '3.11', '3.12', '3.13', '3.14', '3.15']
|
|
16
16
|
|
|
17
17
|
steps:
|
|
18
|
-
- uses: actions/checkout@
|
|
18
|
+
- uses: actions/checkout@v5
|
|
19
19
|
with:
|
|
20
20
|
fetch-depth: 0
|
|
21
21
|
|
|
22
22
|
- name: Set up Python ${{ matrix.python-version }}
|
|
23
|
-
uses: actions/setup-python@
|
|
23
|
+
uses: actions/setup-python@v6
|
|
24
24
|
with:
|
|
25
25
|
python-version: ${{ matrix.python-version }}
|
|
26
26
|
allow-prereleases: true
|
|
@@ -62,7 +62,7 @@ jobs:
|
|
|
62
62
|
|
|
63
63
|
- name: Upload artifacts
|
|
64
64
|
if: matrix.python-version == '3.10'
|
|
65
|
-
uses: actions/upload-artifact@
|
|
65
|
+
uses: actions/upload-artifact@v6
|
|
66
66
|
with:
|
|
67
67
|
name: build-artifacts
|
|
68
68
|
path: |
|
|
@@ -80,26 +80,26 @@ jobs:
|
|
|
80
80
|
runs-on: ubuntu-latest
|
|
81
81
|
needs: build
|
|
82
82
|
steps:
|
|
83
|
-
- uses: actions/checkout@
|
|
83
|
+
- uses: actions/checkout@v5
|
|
84
84
|
|
|
85
85
|
- name: Download build artifacts
|
|
86
|
-
uses: actions/download-artifact@
|
|
86
|
+
uses: actions/download-artifact@v7
|
|
87
87
|
with:
|
|
88
88
|
name: build-artifacts
|
|
89
89
|
path: dist/
|
|
90
90
|
|
|
91
91
|
- name: Set up Docker Buildx
|
|
92
|
-
uses: docker/setup-buildx-action@
|
|
92
|
+
uses: docker/setup-buildx-action@v4
|
|
93
93
|
|
|
94
94
|
- name: Log in to GitHub Container Registry
|
|
95
|
-
uses: docker/login-action@
|
|
95
|
+
uses: docker/login-action@v4
|
|
96
96
|
with:
|
|
97
97
|
registry: ghcr.io
|
|
98
98
|
username: ${{ github.actor }}
|
|
99
99
|
password: ${{ secrets.GITHUB_TOKEN }}
|
|
100
100
|
|
|
101
101
|
- name: Log in to Docker Hub
|
|
102
|
-
uses: docker/login-action@
|
|
102
|
+
uses: docker/login-action@v4
|
|
103
103
|
with:
|
|
104
104
|
registry: docker.io
|
|
105
105
|
username: ${{ secrets.DOCKER_USERNAME }}
|
|
@@ -67,17 +67,21 @@ mail = mailparser.parse_from_string(
|
|
|
67
67
|
)
|
|
68
68
|
assert mail.from_ == [("billing@trusted.example", "billing@vendor.example")]
|
|
69
69
|
assert mail.has_defects is True
|
|
70
|
-
assert mail.address_header_defects == [
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
"
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
70
|
+
assert mail.address_header_defects == [
|
|
71
|
+
{
|
|
72
|
+
"reason": "invalid-display-name",
|
|
73
|
+
"raw": "billing@trusted.example < billing@vendor.example >",
|
|
74
|
+
"recovered": True,
|
|
75
|
+
"candidates": [
|
|
76
|
+
{
|
|
77
|
+
"display_name": "billing@trusted.example",
|
|
78
|
+
"address": "billing@vendor.example",
|
|
79
|
+
}
|
|
80
|
+
],
|
|
81
|
+
"header": "from",
|
|
82
|
+
"occurrence": 0,
|
|
83
|
+
}
|
|
84
|
+
]
|
|
81
85
|
```
|
|
82
86
|
|
|
83
87
|
## Python Style
|
|
@@ -116,6 +120,7 @@ optional Outlook dependencies are installed with `uv sync --extra outlook`).
|
|
|
116
120
|
| `addresses.py` | Forensic address scanner and recovery diagnostics |
|
|
117
121
|
| `received.py` | Context-aware lexical scanning of SMTP trace clauses |
|
|
118
122
|
| `dates.py` | Date semantics, obsolete forms and occurrence-aware diagnostics |
|
|
123
|
+
| `structure.py` | Text drawing of the message structure (headers, hops, MIME tree) |
|
|
119
124
|
| `utils.py` | Stateless parsing helpers (address, received headers, date, encoding) |
|
|
120
125
|
| `const.py` | Compiled regexes, header sets, clause splitter |
|
|
121
126
|
| `exceptions.py` | Custom exception hierarchy |
|
|
@@ -165,6 +170,101 @@ header occurrence while retaining first-occurrence semantics for address
|
|
|
165
170
|
properties. `address_header_defects` is reserved computed metadata; literal
|
|
166
171
|
wire headers with that name remain accessible through `headers`/`message`.
|
|
167
172
|
|
|
173
|
+
**MIME tree and drawing**: `mime_tree` is a flat list; entry `i` is the `part N` of defect
|
|
174
|
+
messages and names its `parent` (ADR 0002). `parse()` keeps one small `_PartRecord` per part
|
|
175
|
+
(roles, attachment position, size) and the entries are built on first read: most callers never
|
|
176
|
+
read the tree, and the number of parts is chosen by the sender. It is a computed property kept
|
|
177
|
+
out of `mail`/`mail_json`, whose output must not change. Walk, build and draw iteratively
|
|
178
|
+
(`_walk_with_parents()`): nesting depth is chosen by the sender, so never recurse over parts.
|
|
179
|
+
Every string in the tree must encode as UTF-8; a value decoded with a sender-chosen RFC 2231
|
|
180
|
+
charset can hold lone surrogates (`mime_boundary()`). Reading the tree must not change the
|
|
181
|
+
message (`_decoded_size()` discards the defects decoding adds). In `structure.py`
|
|
182
|
+
every value taken from the message reaches the output only through `_visible()`, which escapes
|
|
183
|
+
control, ANSI, bidirectional and zero-width characters and stops at a column limit; never format
|
|
184
|
+
message text into the drawing directly. A warning mark means a recorded defect, never a heuristic.
|
|
185
|
+
|
|
186
|
+
**Parameter recovery**: every message the parser builds is a `utils.RecoveringMessage`, never a
|
|
187
|
+
plain `Message`: `from_string()`, `from_bytes()`, `from_file()` (Outlook included), the epilogue
|
|
188
|
+
recovery and `mime_header_view()`. The email package uses parameters on its own, where no caller
|
|
189
|
+
can catch what an RFC 2231 value raises: `get_boundary()` in the feed parser, `get_boundary()`,
|
|
190
|
+
`get_param()`, `get_payload()` and `set_payload()` in the generator. Four values raise: a charset
|
|
191
|
+
that refuses the octets (`boundary*=undefined''x`), sections with and without a number (`name*`
|
|
192
|
+
next to `name*1`, before 3.13.18, 3.14.10 and 3.15.2), a section number longer than `int()`
|
|
193
|
+
converts, and the `(charset, language, value)` tuple of an RFC 2231 charset where a codec name
|
|
194
|
+
is expected. The class overrides `get_boundary()`, `get_payload()`, `set_payload()` and
|
|
195
|
+
`_get_params_preserve()`; the last is private, but it is the one method every parameter accessor
|
|
196
|
+
reads through, and a test fails if a Python release changes that. A well formed value is read
|
|
197
|
+
as `Message` reads it, a malformed one as `Message` of 3.13.18, 3.14.10 and 3.15.2 reads it.
|
|
198
|
+
`set_payload()` raises `LookupError` when the tuple names no codec, so that `as_string_safe()`
|
|
199
|
+
falls back to bytes instead of writing a lossy body under an empty transfer encoding.
|
|
200
|
+
|
|
201
|
+
A parameter is malformed, on every Python, when its RFC 2231 sections come with and without a
|
|
202
|
+
number, repeat a number, or come more than once without one: releases differ in what they
|
|
203
|
+
raise and in how they order equal numbers. `_renumbered()` orders the sections as the fixed
|
|
204
|
+
releases do and numbers them again from 0, which every release reads the same way. The defect
|
|
205
|
+
must not depend on the Python version either: `mime_header_view()` reports
|
|
206
|
+
`invalid-parameter-continuation` from the structure of the list, `mime_boundary()` reports
|
|
207
|
+
`invalid-boundary-parameter`, both as the `MimeHeaderDefect` of that part, never recorded on
|
|
208
|
+
the message. An undecodable boundary is the percent-decoded value read as Latin-1, which is
|
|
209
|
+
what the email package returns for a charset it does not know. The sub-parts of a part rebuilt
|
|
210
|
+
from the epilogue are no entries of the tree: `parse()` reads each through the same two
|
|
211
|
+
helpers and reports it on that part as `sub-part N: ...`, `N` being its position in the walk.
|
|
212
|
+
|
|
213
|
+
All parameter lists go through `_read_params()`, behind a cache of 8 header values. The cache
|
|
214
|
+
spares the split, which is quadratic in some Python releases, to accessors that read one value
|
|
215
|
+
in a row: the feed parser, the helpers of one `parse()` iteration, the generator. It does not
|
|
216
|
+
make a value split once per message: an entry is evicted when 8 other values are read in
|
|
217
|
+
between, and a message with many containers splits a value again in a later phase. Never
|
|
218
|
+
return the cached tuple itself. The cache holds text of the message, so `_forgets_parameters`
|
|
219
|
+
empties it when `_parse_guarded()`, `parse()`, `_build_mime_tree()` or `message_as_string`
|
|
220
|
+
ends, whether it returns or raises; an accessor the caller runs later on `mail.message` fills
|
|
221
|
+
it again, with at most 8 values.
|
|
222
|
+
|
|
223
|
+
Read a filename, charset or boundary through `mime_filename()`, `mime_charset()` and
|
|
224
|
+
`mime_boundary()`: `get_filename()` and `get_content_charset()` still raise on an undecodable
|
|
225
|
+
value, which is how the helpers detect it. The helpers accept a plain `Message` from
|
|
226
|
+
`MailParser(message)` and read it through `RecoveringMessage.reading()`; the caller's parts are
|
|
227
|
+
not replaced. Reading or flattening the payload of such a part therefore still runs the plain
|
|
228
|
+
accessors, which raise `TypeError` for an RFC 2231 charset on an 8-bit body and, before
|
|
229
|
+
3.13.18, for sections with and without a number. `raw_payload()` catches it and returns the
|
|
230
|
+
bytes. `as_string_safe()` must not: its raw-bytes fallback is not text, and a charset that
|
|
231
|
+
could have been applied must never reach it.
|
|
232
|
+
|
|
233
|
+
**Flattening fallback**: when a part refuses its charset, `as_string_safe()` writes the containers
|
|
234
|
+
around it by hand and flattens their sub-parts one by one. A pass that fails must not be repeated
|
|
235
|
+
at every level: that cost the size of the message times its depth on `message_as_string`.
|
|
236
|
+
`_refusing_parts()` reads the parts a failed pass went through from the frames of
|
|
237
|
+
`Generator.flatten` in the traceback (locals `msg` and `policy`), and those parts skip the pass.
|
|
238
|
+
A header the generator will not fold is refused the same way, and is no charset error:
|
|
239
|
+
`Header.encode()` takes a form feed for a line break, which the feed parser does not
|
|
240
|
+
(`HeaderWriteError`, `HeaderParseError`). Catch `_FLATTEN_ERRORS` around a pass, never the
|
|
241
|
+
charset errors alone: the error left `parse_from_bytes()` below an attached container. A part
|
|
242
|
+
refused for a header skips the bytes pass too: before its own fix the bytes generator writes
|
|
243
|
+
that header with the line break, which a sender can make a delimiter.
|
|
244
|
+
Three rules keep the output what it was:
|
|
245
|
+
|
|
246
|
+
- A part is named only when flattening it alone repeats the failed pass: same class and same
|
|
247
|
+
policy object as the part the pass started from, and the same line length. Below
|
|
248
|
+
`multipart/signed` the generator drops the length limit, and a policy other than `compat32` may
|
|
249
|
+
refuse there a header it folds when the part is flattened alone.
|
|
250
|
+
- Nothing is skipped when a `SerializationBudget` is passed: at parse time every pass is debited,
|
|
251
|
+
also one that fails, and the debit decides which containers reach the resource limit.
|
|
252
|
+
- Add no stack frame per nesting level and catch no `RecursionError` there: the depth at which
|
|
253
|
+
`message_as_string` raises `MailParserRecursionError` must not move. A frame without the
|
|
254
|
+
expected locals names no part; it costs a pass, never another output.
|
|
255
|
+
|
|
256
|
+
A pass must not rewrite the message: the generator takes an empty boundary (`boundary=""`,
|
|
257
|
+
`boundary*=utf-8''`, only whitespace) for a missing one, invents another and stores it with
|
|
258
|
+
`set_boundary()`, which changed `mail.message` on a read and during `parse()` below an attached
|
|
259
|
+
container. Run every generator pass inside `_keeping_empty_boundaries()`: there
|
|
260
|
+
`RecoveringMessage.get_boundary()` returns the empty boundary as a string that is true, so the
|
|
261
|
+
delimiters written are the `--` and `----` the message has, and outside a pass it returns the
|
|
262
|
+
plain `""`. Do not save and restore headers around a pass instead (finding the parts is a scan
|
|
263
|
+
of the subtree at every call, and the text holds a random boundary); a plain `Message` of the
|
|
264
|
+
caller has no such accessor and is still rewritten. Keeping the empty boundary is recovery, not
|
|
265
|
+
compliance: `mime_boundary()` reports it as `invalid-boundary-parameter: empty` on that part
|
|
266
|
+
(RFC 2046 section 5.1.1), and only for a `multipart/*` part.
|
|
267
|
+
|
|
168
268
|
**Received header parsing**: `utils.receiveds_parsing()` uses the shared scanner in
|
|
169
269
|
`received.py` for RFC 5321 clause keywords and the timestamp separator. Delimiters
|
|
170
270
|
inside comments, quoted strings, domain literals and angle addresses are not
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Mail forensic evidence
|
|
2
|
+
|
|
3
|
+
Language used when describing the evidence recovered from an email.
|
|
4
|
+
|
|
5
|
+
## Language
|
|
6
|
+
|
|
7
|
+
**Original evidence**:
|
|
8
|
+
The message headers, MIME structure and payloads received for analysis, before
|
|
9
|
+
display decoding or recovery.
|
|
10
|
+
|
|
11
|
+
**Recovered value**:
|
|
12
|
+
A usable interpretation of damaged message content that remains distinguishable
|
|
13
|
+
from its original evidence.
|
|
14
|
+
|
|
15
|
+
**Container representation**:
|
|
16
|
+
A textual copy of the content enclosed by a multipart or attached message,
|
|
17
|
+
which can overlap the evidence in its child attachments.
|
|
18
|
+
|
|
19
|
+
**Partial recovery**:
|
|
20
|
+
A result containing the safely recovered content and explicit diagnostics for
|
|
21
|
+
representations that could not be produced within the analysis budget.
|
|
22
|
+
|
|
23
|
+
## Structure
|
|
24
|
+
|
|
25
|
+
**MIME part**:
|
|
26
|
+
One entity of the message body as delimited on the wire: either a container
|
|
27
|
+
enclosing other parts or a leaf carrying content.
|
|
28
|
+
|
|
29
|
+
**MIME tree**:
|
|
30
|
+
The nesting of the MIME parts of a message as found in the original evidence,
|
|
31
|
+
containers included, independent of how each part is later classified.
|
|
32
|
+
_Avoid_: Graph, body structure
|
|
33
|
+
|
|
34
|
+
**Hop**:
|
|
35
|
+
One transfer of the message between two servers, as declared by a single
|
|
36
|
+
trace header.
|
|
37
|
+
|
|
38
|
+
**Delivery path**:
|
|
39
|
+
The ordered sequence of hops a message declares, first hop first. It is the
|
|
40
|
+
sender's and relays' claim, not a verified route.
|
|
41
|
+
_Avoid_: Route, graph
|
|
42
|
+
|
|
43
|
+
**Message structure**:
|
|
44
|
+
The combined view of a message: its main headers, its delivery path and its
|
|
45
|
+
MIME tree. The delivery path and the MIME tree are separate structures with no
|
|
46
|
+
links between them.
|
|
47
|
+
_Avoid_: Graph, email graph
|
|
48
|
+
|
|
49
|
+
## Security
|
|
50
|
+
|
|
51
|
+
**Message defect**:
|
|
52
|
+
A violation of email standards found in the analysed message itself; it is
|
|
53
|
+
evidence to report, not a flaw in the library.
|
|
54
|
+
_Avoid_: Bug, vulnerability
|
|
55
|
+
|
|
56
|
+
**Parser vulnerability**:
|
|
57
|
+
A flaw in the library that lets attacker-controlled message content crash,
|
|
58
|
+
exhaust, mislead or escape the parser, including output that diverges from
|
|
59
|
+
the original evidence.
|
|
60
|
+
_Avoid_: Defect, security bug
|