Fix resource leaks in static asset loading#1344
Fix resource leaks in static asset loading#1344xovishnukosuri wants to merge 2 commits intoOWASP:masterfrom
Conversation
Summary by CodeRabbit
WalkthroughReplaces direct file reads with UTF-8-aware readers: adds Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@nettacker/lib/html_log/log_data.py`:
- Around line 6-7: Add a concise docstring to the public function
read_static_text(path: Path) describing its purpose (reads and returns the text
contents of a file), the parameter (path: Path — file path to read), the return
value (str — file contents decoded as UTF-8), and any noteworthy behavior (uses
encoding="utf-8"). Place the docstring immediately below the def
read_static_text(...) signature following standard triple-quoted style.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 87bc9d04-cb9b-4eae-bb8a-10b43a434ac0
📒 Files selected for processing (1)
nettacker/lib/html_log/log_data.py
| def read_static_text(path: Path) -> str: | ||
| return path.read_text(encoding="utf-8") |
There was a problem hiding this comment.
Add a docstring to read_static_text.
This is a public module-level function and should include a short docstring for maintainability and API clarity.
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/lib/html_log/log_data.py` around lines 6 - 7, Add a concise
docstring to the public function read_static_text(path: Path) describing its
purpose (reads and returns the text contents of a file), the parameter (path:
Path — file path to read), the return value (str — file contents decoded as
UTF-8), and any noteworthy behavior (uses encoding="utf-8"). Place the docstring
immediately below the def read_static_text(...) signature following standard
triple-quoted style.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
nettacker/core/messages.py (1)
24-25: UnnecessaryStringIOwrapper; consideryaml.safe_loadfor defense-in-depth.
The
StringIOwrapper is unnecessary sinceyaml.load()accepts strings directly:return yaml.load(yaml_text, Loader=yaml.FullLoader)Static analysis (Ruff S506) flags
yaml.FullLoaderas potentially unsafe because it allows instantiation of arbitrary objects. While these are internal locale files, usingyaml.safe_load()would be more secure:return yaml.safe_load(yaml_text)If
safe_loadis too restrictive for the YAML content, the current approach is acceptable given these are trusted internal files.♻️ Suggested simplification
def load_yaml(filename): yaml_text = Path(filename).read_text(encoding="utf-8") - return yaml.load(StringIO(yaml_text), Loader=yaml.FullLoader) + return yaml.safe_load(yaml_text)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@nettacker/core/messages.py` around lines 24 - 25, Replace the unnecessary StringIO wrapper and the unsafe yaml.FullLoader usage: read the file into yaml_text via Path(...).read_text and pass that string directly to yaml.safe_load instead of yaml.load(StringIO(...), Loader=yaml.FullLoader); update the call sites referencing yaml.load and StringIO in this module (e.g., the function that reads message YAML files) to use yaml.safe_load(yaml_text) to remove StringIO and harden against unsafe object construction while preserving existing behavior for trusted internal files.
🤖 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/messages.py`:
- Around line 24-25: Replace the unnecessary StringIO wrapper and the unsafe
yaml.FullLoader usage: read the file into yaml_text via Path(...).read_text and
pass that string directly to yaml.safe_load instead of yaml.load(StringIO(...),
Loader=yaml.FullLoader); update the call sites referencing yaml.load and
StringIO in this module (e.g., the function that reads message YAML files) to
use yaml.safe_load(yaml_text) to remove StringIO and harden against unsafe
object construction while preserving existing behavior for trusted internal
files.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: d116e376-e55c-4524-9454-c5ae63cec997
📒 Files selected for processing (6)
nettacker/core/app.pynettacker/core/fuzzer.pynettacker/core/messages.pynettacker/core/utils/common.pynettacker/lib/compare_report/engine.pynettacker/lib/graph/d3_tree_v1/engine.py
|
Hi @xovishnukosuri, |
Fixes #1320
Replaces bare open().read() calls with Path.read_text() in log_data.py to ensure proper file handle management and avoid potential resource leaks.
Changes: