Skip to content

Fix/2491 unclosed file handles#2522

Open
immortal71 wants to merge 27 commits intoOWASP:masterfrom
immortal71:fix/2491-unclosed-file-handles
Open

Fix/2491 unclosed file handles#2522
immortal71 wants to merge 27 commits intoOWASP:masterfrom
immortal71:fix/2491-unclosed-file-handles

Conversation

@immortal71
Copy link
Contributor

Proposed fix

def create_level_summary(level: int, arr: List[dict[str, Any]]) -> None:
    topic = ""
    category = ""
    os.mkdir(Path(convert_vars.args.output_path, f"level-{level}-controls"))
    with open(Path(convert_vars.args.output_path, f"level-{level}-controls/index.md"), "w", encoding="utf-8") as f:
        f.write(f"# Level {level} controls\n\n")
        f.write(f"Level {level} contains {len(arr)} controls listed below: \n\n")
        for link in arr:
            if link["topic"] != topic:
                topic = link["topic"]
                f.write(f"## {topic}\n\n")
            ...

Describe the bug

In scripts/convert_asvs.py, the create_level_summary() function opens a file using f = open(...) without a with context manager. If any exception is raised inside the loop body (e.g., a missing dictionary key during iteration), the file handle f is never explicitly closed, leaking the OS file descriptor. On batch runs that generate many ASVS taxonomy pages, this can exhaust system file handle limits.

Accessing catalog['Categories']['Category'] without guards caused an
unhandled KeyError if the CAPEC JSON had no Categories section.

Added defensive checks consistent with existing guards for
Attack_Patterns and Attack_Pattern. Logs a warning and skips the
categories block if the key is absent or malformed.

Fixes OWASP#2488
…te_limiter prod bypass, schema changeset tests
Copilot AI review requested due to automatic review settings March 5, 2026 11:10
Copy link
Contributor

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 unclosed file handles in Python scripts (convert_asvs.py and convert_capec.py) by converting manual open()/close() patterns to with context managers, ensuring files are properly closed even when exceptions occur. The PR also includes several other fixes and test coverage improvements across both the Python scripts and the Elixir (copi.owasp.org) codebase.

Changes:

  • Replaced manual file open/close with with context managers in convert_asvs.py and convert_capec.py, and refactored capec_map_enricher.py to extract a reusable _extract_names_from_items helper with improved error handling for missing categories.
  • Fixed LiveView callback return values in player_live/show.ex (changed {:ok, redirect(...)} to {:noreply, redirect(...)}) and added a nil guard in index.html.heex to prevent rendering FormComponent when @player is nil.
  • Added extensive test coverage across Python and Elixir codebases, including file handle closure verification tests, pure function tests, edge case tests, and model changeset tests.

Reviewed changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
scripts/convert_asvs.py Converted file open/close to with context manager
scripts/convert_capec.py Converted two file open/close blocks to with context managers
scripts/capec_map_enricher.py Extracted _extract_names_from_items helper; added defensive checks for Categories
tests/scripts/convert_asvs_utest.py Added file handle closure assertions and error-handling test
tests/scripts/convert_capec_utest.py Added file handle closure test for CAPEC page generation
copi.owasp.org/lib/copi_web/live/player_live/show.ex Fixed handle_params/handle_info return values from {:ok, ...} to {:noreply, ...}
copi.owasp.org/lib/copi_web/live/player_live/index.html.heex Added nil guard around FormComponent rendering
copi.owasp.org/test/copi_web/live/player_live/show_test.exs Added tests for nonexistent player redirect and validation errors
copi.owasp.org/test/copi_web/live/player_live/show_pure_test.exs New pure function tests for PlayerLive.Show helpers
copi.owasp.org/test/copi_web/live/game_live/show_test.exs Added tests for edge cases including different game id, finished game, and nonexistent game
copi.owasp.org/test/copi_web/live/game_live/show_pure_test.exs New pure function tests for GameLive.Show helpers
copi.owasp.org/test/copi_web/controllers/card_controller_test.exs Added test for format_capec/1
copi.owasp.org/test/copi_web/plugs/rate_limiter_plug_test.exs Improved test for no-IP scenario; added init/1 test
copi.owasp.org/test/copi/rate_limiter_test.exs Added tests for normalize_ip fallback, prod env bypass, and cleanup
copi.owasp.org/test/copi/ip_helper_test.exs Added tests for various socket/connect_info edge cases
copi.owasp.org/test/copi/cornucopia/vote_test.exs New changeset test for Vote
copi.owasp.org/test/copi/cornucopia/player_test.exs New changeset tests for Player
copi.owasp.org/test/copi/cornucopia/dealt_card_test.exs New changeset and find tests for DealtCard

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.

2 participants