From 565c415280030253246ecccd4ab086114a21e895 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Fri, 20 Mar 2026 18:54:06 -0400 Subject: [PATCH] Fix resource leak when HEC config is invalid in `_init_output_clients()` (#703) * Initial plan * Fix resource leak: validate HEC settings before creating any output clients Co-authored-by: seanthegeek <44679+seanthegeek@users.noreply.github.com> Agent-Logs-Url: https://github.com/domainaware/parsedmarc/sessions/38c73e09-789d-4d41-b75e-bbc61418859d --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: seanthegeek <44679+seanthegeek@users.noreply.github.com> --- parsedmarc/cli.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/parsedmarc/cli.py b/parsedmarc/cli.py index 2c9b420..6c62ff1 100644 --- a/parsedmarc/cli.py +++ b/parsedmarc/cli.py @@ -832,6 +832,13 @@ def _init_output_clients(opts): Raises: ConfigurationError: If a required output client cannot be created. """ + # Validate all required settings before creating any clients so that a + # ConfigurationError does not leave partially-created clients un-closed. + if opts.hec and (opts.hec_token is None or opts.hec_index is None): + raise ConfigurationError( + "HEC token and HEC index are required when using HEC URL" + ) + clients = {} if opts.save_aggregate or opts.save_forensic or opts.save_smtp_tls: @@ -940,10 +947,6 @@ def _init_output_clients(opts): ) if opts.hec: - if opts.hec_token is None or opts.hec_index is None: - raise ConfigurationError( - "HEC token and HEC index are required when using HEC URL" - ) verify = True if opts.hec_skip_certificate_verification: verify = False