mirror of
https://github.com/domainaware/parsedmarc.git
synced 2026-03-10 08:41:26 +00:00
Compare commits
12 Commits
9.0.7
...
copilot/op
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2192d0bfd4 | ||
|
|
f1c6ebef1d | ||
|
|
221bc332ef | ||
|
|
a2a75f7a81 | ||
|
|
50fcb51577 | ||
|
|
dd9ef90773 | ||
|
|
0e3a4b0f06 | ||
|
|
343b53ef18 | ||
|
|
792079a3e8 | ||
|
|
1f3a1fc843 | ||
|
|
34fa0c145d | ||
|
|
6719a06388 |
2
.github/workflows/python-tests.yml
vendored
2
.github/workflows/python-tests.yml
vendored
@@ -30,7 +30,7 @@ jobs:
|
|||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
|
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", "3.14"]
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v5
|
||||||
|
|||||||
17
CHANGELOG.md
17
CHANGELOG.md
@@ -1,5 +1,22 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 9.0.10
|
||||||
|
|
||||||
|
- Support Python 3.14+
|
||||||
|
|
||||||
|
## 9.0.9
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
|
||||||
|
- Validate that a string is base64-encoded before trying to base64 decode it. (PRs #648 and #649)
|
||||||
|
|
||||||
|
## 9.0.8
|
||||||
|
|
||||||
|
### Fixes
|
||||||
|
|
||||||
|
- Fix logging configuration not propagating to child parser processes (#646).
|
||||||
|
- Update `mailsuite` dependency to `?=1.11.1` to solve issues with iCloud IMAP (#493).
|
||||||
|
|
||||||
## 9.0.7
|
## 9.0.7
|
||||||
|
|
||||||
## Fixes
|
## Fixes
|
||||||
|
|||||||
@@ -61,4 +61,4 @@ for RHEL or Debian.
|
|||||||
| 3.11 | ✅ | Actively maintained; supported until June 2028 (Debian 12) |
|
| 3.11 | ✅ | Actively maintained; supported until June 2028 (Debian 12) |
|
||||||
| 3.12 | ✅ | Actively maintained; supported until May 2035 (RHEL 10) |
|
| 3.12 | ✅ | Actively maintained; supported until May 2035 (RHEL 10) |
|
||||||
| 3.13 | ✅ | Actively maintained; supported until June 2030 (Debian 13) |
|
| 3.13 | ✅ | Actively maintained; supported until June 2030 (Debian 13) |
|
||||||
| 3.14 | ❌ | Not currently supported due to [this imapclient bug](https://github.com/mjs/imapclient/issues/618)|
|
| 3.14 | ✅ | Actively maintained |
|
||||||
|
|||||||
1
ci.ini
1
ci.ini
@@ -3,6 +3,7 @@ save_aggregate = True
|
|||||||
save_forensic = True
|
save_forensic = True
|
||||||
save_smtp_tls = True
|
save_smtp_tls = True
|
||||||
debug = True
|
debug = True
|
||||||
|
offline = True
|
||||||
|
|
||||||
[elasticsearch]
|
[elasticsearch]
|
||||||
hosts = http://localhost:9200
|
hosts = http://localhost:9200
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ for RHEL or Debian.
|
|||||||
| 3.11 | ✅ | Actively maintained; supported until June 2028 (Debian 12) |
|
| 3.11 | ✅ | Actively maintained; supported until June 2028 (Debian 12) |
|
||||||
| 3.12 | ✅ | Actively maintained; supported until May 2035 (RHEL 10) |
|
| 3.12 | ✅ | Actively maintained; supported until May 2035 (RHEL 10) |
|
||||||
| 3.13 | ✅ | Actively maintained; supported until June 2030 (Debian 13) |
|
| 3.13 | ✅ | Actively maintained; supported until June 2030 (Debian 13) |
|
||||||
| 3.14 | ❌ | Not currently supported due to [this imapclient bug](https://github.com/mjs/imapclient/issues/618)|
|
| 3.14 | ✅ | Actively maintained |
|
||||||
|
|
||||||
```{toctree}
|
```{toctree}
|
||||||
:caption: 'Contents'
|
:caption: 'Contents'
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -751,8 +751,8 @@ def parse_aggregate_report_xml(
|
|||||||
new_report_metadata["report_id"] = report_id
|
new_report_metadata["report_id"] = report_id
|
||||||
date_range = report["report_metadata"]["date_range"]
|
date_range = report["report_metadata"]["date_range"]
|
||||||
|
|
||||||
begin_ts = int(date_range["begin"])
|
begin_ts = int(date_range["begin"].split(".")[0])
|
||||||
end_ts = int(date_range["end"])
|
end_ts = int(date_range["end"].split(".")[0])
|
||||||
span_seconds = end_ts - begin_ts
|
span_seconds = end_ts - begin_ts
|
||||||
|
|
||||||
normalize_timespan = span_seconds > normalize_timespan_threshold_hours * 3600
|
normalize_timespan = span_seconds > normalize_timespan_threshold_hours * 3600
|
||||||
@@ -892,7 +892,11 @@ def extract_report(content: Union[bytes, str, BinaryIO]) -> str:
|
|||||||
try:
|
try:
|
||||||
if isinstance(content, str):
|
if isinstance(content, str):
|
||||||
try:
|
try:
|
||||||
file_object = BytesIO(b64decode(content))
|
file_object = BytesIO(
|
||||||
|
b64decode(
|
||||||
|
content.replace("\n", "").replace("\r", ""), validate=True
|
||||||
|
)
|
||||||
|
)
|
||||||
except binascii.Error:
|
except binascii.Error:
|
||||||
return content
|
return content
|
||||||
header = file_object.read(6)
|
header = file_object.read(6)
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ from parsedmarc.mail import (
|
|||||||
MSGraphConnection,
|
MSGraphConnection,
|
||||||
)
|
)
|
||||||
from parsedmarc.mail.graph import AuthMethod
|
from parsedmarc.mail.graph import AuthMethod
|
||||||
|
from parsedmarc.types import ParsingResults
|
||||||
from parsedmarc.utils import get_base_domain, get_reverse_dns, is_mbox
|
from parsedmarc.utils import get_base_domain, get_reverse_dns, is_mbox
|
||||||
|
|
||||||
# Increase the max header limit for very large emails. `_MAXHEADERS` is a
|
# Increase the max header limit for very large emails. `_MAXHEADERS` is a
|
||||||
@@ -67,6 +68,48 @@ def _str_to_list(s):
|
|||||||
return list(map(lambda i: i.lstrip(), _list))
|
return list(map(lambda i: i.lstrip(), _list))
|
||||||
|
|
||||||
|
|
||||||
|
def _configure_logging(log_level, log_file=None):
|
||||||
|
"""
|
||||||
|
Configure logging for the current process.
|
||||||
|
This is needed for child processes to properly log messages.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
log_level: The logging level (e.g., logging.DEBUG, logging.WARNING)
|
||||||
|
log_file: Optional path to log file
|
||||||
|
"""
|
||||||
|
# Get the logger
|
||||||
|
from parsedmarc.log import logger
|
||||||
|
|
||||||
|
# Set the log level
|
||||||
|
logger.setLevel(log_level)
|
||||||
|
|
||||||
|
# Add StreamHandler with formatter if not already present
|
||||||
|
# Check if we already have a StreamHandler to avoid duplicates
|
||||||
|
# Use exact type check to distinguish from FileHandler subclass
|
||||||
|
has_stream_handler = any(type(h) is logging.StreamHandler for h in logger.handlers)
|
||||||
|
|
||||||
|
if not has_stream_handler:
|
||||||
|
formatter = logging.Formatter(
|
||||||
|
fmt="%(levelname)8s:%(filename)s:%(lineno)d:%(message)s",
|
||||||
|
datefmt="%Y-%m-%d:%H:%M:%S",
|
||||||
|
)
|
||||||
|
handler = logging.StreamHandler()
|
||||||
|
handler.setFormatter(formatter)
|
||||||
|
logger.addHandler(handler)
|
||||||
|
|
||||||
|
# Add FileHandler if log_file is specified
|
||||||
|
if log_file:
|
||||||
|
try:
|
||||||
|
fh = logging.FileHandler(log_file, "a")
|
||||||
|
formatter = logging.Formatter(
|
||||||
|
"%(asctime)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s"
|
||||||
|
)
|
||||||
|
fh.setFormatter(formatter)
|
||||||
|
logger.addHandler(fh)
|
||||||
|
except (IOError, OSError, PermissionError) as error:
|
||||||
|
logger.warning("Unable to write to log file: {}".format(error))
|
||||||
|
|
||||||
|
|
||||||
def cli_parse(
|
def cli_parse(
|
||||||
file_path,
|
file_path,
|
||||||
sa,
|
sa,
|
||||||
@@ -79,8 +122,29 @@ def cli_parse(
|
|||||||
reverse_dns_map_url,
|
reverse_dns_map_url,
|
||||||
normalize_timespan_threshold_hours,
|
normalize_timespan_threshold_hours,
|
||||||
conn,
|
conn,
|
||||||
|
log_level=logging.ERROR,
|
||||||
|
log_file=None,
|
||||||
):
|
):
|
||||||
"""Separated this function for multiprocessing"""
|
"""Separated this function for multiprocessing
|
||||||
|
|
||||||
|
Args:
|
||||||
|
file_path: Path to the report file
|
||||||
|
sa: Strip attachment payloads flag
|
||||||
|
nameservers: List of nameservers
|
||||||
|
dns_timeout: DNS timeout
|
||||||
|
ip_db_path: Path to IP database
|
||||||
|
offline: Offline mode flag
|
||||||
|
always_use_local_files: Always use local files flag
|
||||||
|
reverse_dns_map_path: Path to reverse DNS map
|
||||||
|
reverse_dns_map_url: URL to reverse DNS map
|
||||||
|
normalize_timespan_threshold_hours: Timespan threshold
|
||||||
|
conn: Pipe connection for IPC
|
||||||
|
log_level: Logging level for this process
|
||||||
|
log_file: Optional path to log file
|
||||||
|
"""
|
||||||
|
# Configure logging in this child process
|
||||||
|
_configure_logging(log_level, log_file)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
file_results = parse_report_file(
|
file_results = parse_report_file(
|
||||||
file_path,
|
file_path,
|
||||||
@@ -1461,6 +1525,10 @@ def _main():
|
|||||||
if n_procs < 1:
|
if n_procs < 1:
|
||||||
n_procs = 1
|
n_procs = 1
|
||||||
|
|
||||||
|
# Capture the current log level to pass to child processes
|
||||||
|
current_log_level = logger.level
|
||||||
|
current_log_file = opts.log_file
|
||||||
|
|
||||||
for batch_index in range((len(file_paths) + n_procs - 1) // n_procs):
|
for batch_index in range((len(file_paths) + n_procs - 1) // n_procs):
|
||||||
processes = []
|
processes = []
|
||||||
connections = []
|
connections = []
|
||||||
@@ -1486,6 +1554,8 @@ def _main():
|
|||||||
opts.reverse_dns_map_url,
|
opts.reverse_dns_map_url,
|
||||||
opts.normalize_timespan_threshold_hours,
|
opts.normalize_timespan_threshold_hours,
|
||||||
child_conn,
|
child_conn,
|
||||||
|
current_log_level,
|
||||||
|
current_log_file,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
processes.append(process)
|
processes.append(process)
|
||||||
@@ -1688,13 +1758,13 @@ def _main():
|
|||||||
logger.exception("Mailbox Error")
|
logger.exception("Mailbox Error")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
results = {
|
parsing_results: ParsingResults = {
|
||||||
"aggregate_reports": aggregate_reports,
|
"aggregate_reports": aggregate_reports,
|
||||||
"forensic_reports": forensic_reports,
|
"forensic_reports": forensic_reports,
|
||||||
"smtp_tls_reports": smtp_tls_reports,
|
"smtp_tls_reports": smtp_tls_reports,
|
||||||
}
|
}
|
||||||
|
|
||||||
process_reports(results)
|
process_reports(parsing_results)
|
||||||
|
|
||||||
if opts.smtp_host:
|
if opts.smtp_host:
|
||||||
try:
|
try:
|
||||||
@@ -1708,7 +1778,7 @@ def _main():
|
|||||||
else _str_to_list(str(opts.smtp_to))
|
else _str_to_list(str(opts.smtp_to))
|
||||||
)
|
)
|
||||||
email_results(
|
email_results(
|
||||||
results,
|
parsing_results,
|
||||||
opts.smtp_host,
|
opts.smtp_host,
|
||||||
opts.smtp_from,
|
opts.smtp_from,
|
||||||
smtp_to_value,
|
smtp_to_value,
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
__version__ = "9.0.7"
|
__version__ = "9.0.10"
|
||||||
|
|
||||||
USER_AGENT = f"parsedmarc/{__version__}"
|
USER_AGENT = f"parsedmarc/{__version__}"
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ classifiers = [
|
|||||||
"Operating System :: OS Independent",
|
"Operating System :: OS Independent",
|
||||||
"Programming Language :: Python :: 3"
|
"Programming Language :: Python :: 3"
|
||||||
]
|
]
|
||||||
requires-python = ">=3.9, <3.14"
|
requires-python = ">=3.9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"azure-identity>=1.8.0",
|
"azure-identity>=1.8.0",
|
||||||
"azure-monitor-ingestion>=1.0.0",
|
"azure-monitor-ingestion>=1.0.0",
|
||||||
@@ -48,7 +48,7 @@ dependencies = [
|
|||||||
"imapclient>=2.1.0",
|
"imapclient>=2.1.0",
|
||||||
"kafka-python-ng>=2.2.2",
|
"kafka-python-ng>=2.2.2",
|
||||||
"lxml>=4.4.0",
|
"lxml>=4.4.0",
|
||||||
"mailsuite>=1.11.0",
|
"mailsuite>=1.11.2",
|
||||||
"msgraph-core==0.2.2",
|
"msgraph-core==0.2.2",
|
||||||
"opensearch-py>=2.4.2,<=3.0.0",
|
"opensearch-py>=2.4.2,<=3.0.0",
|
||||||
"publicsuffixlist>=0.10.0",
|
"publicsuffixlist>=0.10.0",
|
||||||
|
|||||||
19
tests.py
19
tests.py
@@ -12,6 +12,9 @@ from lxml import etree
|
|||||||
import parsedmarc
|
import parsedmarc
|
||||||
import parsedmarc.utils
|
import parsedmarc.utils
|
||||||
|
|
||||||
|
# Detect if running in GitHub Actions to skip DNS lookups
|
||||||
|
OFFLINE_MODE = os.environ.get("GITHUB_ACTIONS", "false").lower() == "true"
|
||||||
|
|
||||||
|
|
||||||
def minify_xml(xml_string):
|
def minify_xml(xml_string):
|
||||||
parser = etree.XMLParser(remove_blank_text=True)
|
parser = etree.XMLParser(remove_blank_text=True)
|
||||||
@@ -121,7 +124,7 @@ class Test(unittest.TestCase):
|
|||||||
continue
|
continue
|
||||||
print("Testing {0}: ".format(sample_path), end="")
|
print("Testing {0}: ".format(sample_path), end="")
|
||||||
parsed_report = parsedmarc.parse_report_file(
|
parsed_report = parsedmarc.parse_report_file(
|
||||||
sample_path, always_use_local_files=True
|
sample_path, always_use_local_files=True, offline=OFFLINE_MODE
|
||||||
)["report"]
|
)["report"]
|
||||||
parsedmarc.parsed_aggregate_reports_to_csv(parsed_report)
|
parsedmarc.parsed_aggregate_reports_to_csv(parsed_report)
|
||||||
print("Passed!")
|
print("Passed!")
|
||||||
@@ -129,7 +132,7 @@ class Test(unittest.TestCase):
|
|||||||
def testEmptySample(self):
|
def testEmptySample(self):
|
||||||
"""Test empty/unparasable report"""
|
"""Test empty/unparasable report"""
|
||||||
with self.assertRaises(parsedmarc.ParserError):
|
with self.assertRaises(parsedmarc.ParserError):
|
||||||
parsedmarc.parse_report_file("samples/empty.xml")
|
parsedmarc.parse_report_file("samples/empty.xml", offline=OFFLINE_MODE)
|
||||||
|
|
||||||
def testForensicSamples(self):
|
def testForensicSamples(self):
|
||||||
"""Test sample forensic/ruf/failure DMARC reports"""
|
"""Test sample forensic/ruf/failure DMARC reports"""
|
||||||
@@ -139,8 +142,12 @@ class Test(unittest.TestCase):
|
|||||||
print("Testing {0}: ".format(sample_path), end="")
|
print("Testing {0}: ".format(sample_path), end="")
|
||||||
with open(sample_path) as sample_file:
|
with open(sample_path) as sample_file:
|
||||||
sample_content = sample_file.read()
|
sample_content = sample_file.read()
|
||||||
parsed_report = parsedmarc.parse_report_email(sample_content)["report"]
|
parsed_report = parsedmarc.parse_report_email(
|
||||||
parsed_report = parsedmarc.parse_report_file(sample_path)["report"]
|
sample_content, offline=OFFLINE_MODE
|
||||||
|
)["report"]
|
||||||
|
parsed_report = parsedmarc.parse_report_file(
|
||||||
|
sample_path, offline=OFFLINE_MODE
|
||||||
|
)["report"]
|
||||||
parsedmarc.parsed_forensic_reports_to_csv(parsed_report)
|
parsedmarc.parsed_forensic_reports_to_csv(parsed_report)
|
||||||
print("Passed!")
|
print("Passed!")
|
||||||
|
|
||||||
@@ -152,7 +159,9 @@ class Test(unittest.TestCase):
|
|||||||
if os.path.isdir(sample_path):
|
if os.path.isdir(sample_path):
|
||||||
continue
|
continue
|
||||||
print("Testing {0}: ".format(sample_path), end="")
|
print("Testing {0}: ".format(sample_path), end="")
|
||||||
parsed_report = parsedmarc.parse_report_file(sample_path)["report"]
|
parsed_report = parsedmarc.parse_report_file(
|
||||||
|
sample_path, offline=OFFLINE_MODE
|
||||||
|
)["report"]
|
||||||
parsedmarc.parsed_smtp_tls_reports_to_csv(parsed_report)
|
parsedmarc.parsed_smtp_tls_reports_to_csv(parsed_report)
|
||||||
print("Passed!")
|
print("Passed!")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user