Skip to content

Fix mutable default argument in merge_logs_to_list#1470

Closed
juandiego-bmu wants to merge 1 commit intoOWASP:masterfrom
juandiego-bmu:fix-mutable-default-arg
Closed

Fix mutable default argument in merge_logs_to_list#1470
juandiego-bmu wants to merge 1 commit intoOWASP:masterfrom
juandiego-bmu:fix-mutable-default-arg

Conversation

@juandiego-bmu
Copy link
Copy Markdown

Summary

Replace log_list=[] with log_list=None and initialize inside the function body.

The mutable default list was shared across all calls. Since it's mutated via .append() on line 41, log entries from previous invocations leak into subsequent results and the list grows without bound.

Split from #1466 as requested by @pUrGe12.

Fixes #1464

Replace log_list=[] with log_list=None and initialize inside the
function body. The mutable default was shared across all calls,
causing log entries from previous invocations to leak into subsequent
results and growing without bound.

Fixes OWASP#1464
Copilot AI review requested due to automatic review settings March 30, 2026 11:30
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 30, 2026

Summary by CodeRabbit

  • Chores
    • Code quality improvement to internal utilities—no user-facing changes.

Walkthrough

Fixes a mutable default argument bug in merge_logs_to_list by changing the default parameter from an empty list to None and initializing the list inside the function to prevent log contamination across calls.

Changes

Cohort / File(s) Summary
Bug Fix
nettacker/core/utils/common.py
Changed merge_logs_to_list function signature to use log_list=None as default parameter instead of log_list=[], with initialization logic added inside the function. Prevents mutable default argument anti-pattern that was causing log entries to persist across multiple function calls.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: fixing a mutable default argument in the merge_logs_to_list function, which is the exact focus of the changeset.
Description check ✅ Passed The description clearly explains the bug (mutable default list being shared and mutated), its impact on log contamination, and the fix applied, all directly related to the changeset.
Linked Issues check ✅ Passed The PR implements the exact fix suggested in issue #1464: replacing log_list=[] with log_list=None and initializing it inside the function body to prevent shared mutable state.
Out of Scope Changes check ✅ Passed All changes are scoped to the merge_logs_to_list function signature and initialization logic, directly addressing the mutable default argument issue in #1464 with no extraneous modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a Python mutable default argument bug in merge_logs_to_list, preventing log entries from leaking across calls and avoiding unbounded growth of a shared default list.

Changes:

  • Change merge_logs_to_list(result, log_list=[]) to default log_list=None.
  • Initialize log_list = [] inside the function when None is provided.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
nettacker/core/utils/common.py (1)

34-36: Consider adding type hints (and a short docstring) for this public utility.

This function is in a core shared utility path; typing the parameters/return improves readability and safer callsites.

♻️ Suggested refactor
-def merge_logs_to_list(result, log_list=None):
+def merge_logs_to_list(result: object, log_list: list[str] | None = None) -> list[str]:
+    """Collect and deduplicate nested `log` values from a result structure."""
     if log_list is None:
         log_list = []

As per coding guidelines, "Keep functions small, use type hints where practical, and add docstrings for public APIs".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@nettacker/core/utils/common.py` around lines 34 - 36, Update
merge_logs_to_list to include type hints and a short docstring: annotate
parameters as result: Dict[str, Any] (or appropriate specific type) and
log_list: Optional[List[Dict[str, Any]]] = None, and the return type as
List[Dict[str, Any]]; add a one- or two-line docstring describing the function
purpose, parameters, and return value; ensure you import Optional, List, Dict,
Any from typing at the top and keep the existing None-default pattern to avoid
mutable defaults.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@nettacker/core/utils/common.py`:
- Around line 34-36: Update merge_logs_to_list to include type hints and a short
docstring: annotate parameters as result: Dict[str, Any] (or appropriate
specific type) and log_list: Optional[List[Dict[str, Any]]] = None, and the
return type as List[Dict[str, Any]]; add a one- or two-line docstring describing
the function purpose, parameters, and return value; ensure you import Optional,
List, Dict, Any from typing at the top and keep the existing None-default
pattern to avoid mutable defaults.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e2df119d-d3f1-4316-aa1c-c657e9978a33

📥 Commits

Reviewing files that changed from the base of the PR and between f4c1bbf and 90a6481.

📒 Files selected for processing (1)
  • nettacker/core/utils/common.py

@juandiego-bmu
Copy link
Copy Markdown
Author

Closing to recreate with proper PR template and signed commits as requested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Mutable default argument in merge_logs_to_list causes log contamination

2 participants