Skip to content

Enhance metadata processing security, stability, and documentation#95

Open
glebtarasov-creator wants to merge 5 commits into
openscilab:mainfrom
glebtarasov-creator:main
Open

Enhance metadata processing security, stability, and documentation#95
glebtarasov-creator wants to merge 5 commits into
openscilab:mainfrom
glebtarasov-creator:main

Conversation

@glebtarasov-creator

Copy link
Copy Markdown

Summary of Changes

This PR enhances the security and stability of the metadata processing functionality in DMeta.

Changes Implemented

  1. Security Enhancement:

    • Replaced defusedxml.lxml with defusedxml.ElementTree for safer XML parsing
  2. Resource Management Improvements:

    • Improved resource management in extract_metadata function with proper file handle closing
    • Added retry mechanism for temporary directory cleanup on Windows to address file locking issues
    • Enhanced PNG metadata clearing by using img.copy() to preserve image properties while removing metadata
  3. API Consistency Fixes:

    • Fixed return value consistency in clear() function to match its documentation
    • Updated docstring to accurately reflect return values (path to file, None for unsupported formats, or original path when metadata already cleared)
  4. Test Suite Improvements:

    • Added assertions in test functions to verify return values
    • Implemented proper cleanup of created test files to prevent filesystem pollution
    • Improved test isolation with proper working directory restoration
  5. Code Quality:

    • Improved code readability and formatting throughout

Motivation

These changes address Windows-specific file handling issues and improve the overall security posture of the library by using safer XML parsing methods. The enhanced resource management prevents potential PermissionError exceptions during temporary directory cleanup on Windows systems. Additionally, fixing the return value consistency ensures the API behaves predictably according to its documentation.

Backward Compatibility

All changes maintain full backward compatibility with existing functionality. The API remains the same, but now behaves consistently with its documented contract.

- Replace defusedxml.lxml with defusedxml.ElementTree for safer XML parsing
- Improve resource management in extract_metadata function with proper file handle closing
- Add retry mechanism for temporary directory cleanup on Windows
- Enhance PNG metadata clearing by using img.copy() to preserve image properties
- Add assertions and proper cleanup in test functions
- Improve code readability and formatting
…tion

- Update return type documentation to accurately reflect actual return values
- Return the file path even when metadata is already cleared to maintain consistency
- This ensures the function behavior matches its documented contract
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 12 complexity · 0 duplication

Metric Results
Complexity 12
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

- Remove unused imports: 'art' and 'errors' modules are no longer used
- Remove unused functions: 'extract_metadata', 'dmeta_help', and 'run_dmeta' are deleted
- Improve return value documentation in clear function
- Add missing returns for unsupported formats in clear and update functions
- Format code to improve readability, especially in exception handling
- Refactor clear and update functions to use a shared _process_metadata function
- Add extract_metadata function to retrieve metadata from Microsoft files
- Implement _cleanup_extracted function to handle cleanup after processing
- Update clear_all and update_all functions to use new _process_metadata
- Improve error handling and verbose output in various functions
- Add _output_path function to determine the output file path
- Implement _extract_and_prepare for consistent file extraction
- Create _repack_and_cleanup function to handle file repacking
- Simplify _process_metadata by using new helper functions
- Improve code readability and reduce duplication
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.

1 participant