Skip to content

fix: Do not treat archive metadata as file path and drop dead version checks - #1509

Open
Czaki wants to merge 4 commits into
developfrom
fix/archive-metadata-and-dead-version-checks
Open

Czaki wants to merge 4 commits into
developfrom
fix/archive-metadata-and-dead-version-checks

Conversation

@Czaki

@Czaki Czaki commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Zip Slip (SonarCloud pythonsecurity:S6096, blocker): JSON read from project archives (metadata.json, history/history.json, algorithm.json) was passed to load_metadata_base, which opens its argument as a file when that path exists. A crafted archive could make PartSeg read an arbitrary local file. Added load_metadata_from_string (pure json.loads with partseg_object_hook) and use it for all archive members in mask/io_functions.py and analysis/load_functions.py. Added a regression test.
  • Dead version checks (current minimums: napari ≥0.6.2, pydantic ≥2.2, tifffile ≥2023.9.26):
    • napari < 0.5: removed _napari_ge_5 branches (get_theme(..., as_dict=...), old qt_viewer.view camera access) and the Color/Namespace theme fallback — fixes DeepSource PYL-E1123.
    • napari ≤ 0.4.16 / < 0.4.19: removed NAPARI_LE_4_16 scale-bar warning and the launcher pydantic/napari compatibility exit.
    • pydantic < 2: removed import fallbacks, smart_union config, *_pydantic_1 helpers, and update_forward_refs branch.
    • tifffile ≤ 2019.7.26: removed the _xml2dict patch.

Test plan

  • ruff check / ruff format / mypy (pre-commit)
  • pytest tests/test_PartSegCore tests/test_PartSegImage tests/test_PartSeg/roi_mask tests/test_PartSeg/roi_analysis tests/test_PartSeg/test_main_windows.py tests/test_PartSeg/test_common_backend.py tests/test_PartSeg/test_napari_widgets.py tests/test_PartSeg/test_launcher.py — 3603 passed
  • CI, SonarCloud and DeepSource re-run

Summary by Sourcery

Safely parse metadata embedded in archives and remove obsolete dependency-version compatibility code.

Bug Fixes:

  • Prevent archive metadata from being interpreted as filesystem paths, eliminating arbitrary local file reads from crafted project archives.

Enhancements:

  • Remove obsolete compatibility branches and fallbacks for unsupported napari, pydantic, and tifffile versions.
  • Simplify theme, camera, model, and metadata handling around the currently supported dependency versions.

Tests:

  • Add regression coverage confirming serialized metadata is parsed directly and does not open matching filesystem paths.

Summary by CodeRabbit

  • Bug Fixes
    • Project archives and history entries now parse metadata directly from their contents, improving metadata loading reliability.
  • User Interface
    • The scale-bar toggle no longer displays the light-theme compatibility warning.
    • Theme colors are now applied through a consistent theme-handling path.
  • Compatibility
    • Application startup no longer blocks on the previous version compatibility check.
    • Viewer camera controls now use a consistent camera access path.

… checks

JSON read from project archives was passed to `load_metadata_base`, which
opens the argument as a file if such path exists (SonarCloud S6096). Add
`load_metadata_from_string` that only parses JSON and use it for all
archive members.

Remove compatibility branches for versions below the current minimum
requirements (napari<0.5, pydantic<2, tifffile<=2019.7.26).
@sourcery-ai

sourcery-ai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

API

Add a metadata loader that parses JSON content without interpreting it as a filesystem path, and document how it differs from the path-capable loader.

  • package/PartSegCore/io_utils.py L179-188

Uses of the API

Use the content-only loader for metadata and history read from project archives, preventing archive contents from being treated as paths.

  • package/PartSegCore/analysis/load_functions.py L31-296 (4 small edits)
  • package/PartSegCore/mask/io_functions.py L33-398 (4 small edits)

Update the development archive-processing example to parse metadata bytes with the safe loader.

  • examples/dev/update_frame_thick.py L39, L52

Implementation

Remove obsolete napari and pydantic compatibility branches and consolidate algorithm-property conversion around the supported pydantic API.

  • package/PartSegCore/algorithm_describe_base.py L12-18 (+8 small edits)
  • package/PartSegCore/analysis/measurement_base.py L5-309 (4 small edits)
  • package/PartSegImage/tifffile_fixes.py L1 (removed lines), L23 (removed lines)

UI

Drop outdated napari theme and camera-access fallbacks, the scale-bar compatibility warning, and obsolete warning filters.

  • package/PartSeg/_launcher/main_window.py L6 (removed lines), L21 (removed lines), L144
  • package/PartSeg/common_backend/base_settings.py L5-801 (7 small edits)
  • package/PartSeg/common_gui/main_window.py L42 (removed lines), L187 (removed lines), L389
  • package/PartSeg/plugins/napari_widgets/search_label_widget.py L3-149 (6 small edits)
  • package/PartSeg/plugins/napari_widgets/simple_measurement_widget.py L56-57

Remove the launcher check that rejected old napari versions when paired with pydantic 2.

  • package/PartSeg/launcher_main.py L87

Tests

Add regression coverage that serialized JSON is parsed as content rather than opened as a path.

  • package/tests/test_PartSegCore/test_io.py L40, L743-750

Remove test setup for the deleted scale-bar warning state while retaining toggle behavior checks.

  • package/tests/test_PartSeg/roi_analysis/test_main_window.py L37 (removed lines)
  • package/tests/test_PartSeg/roi_mask/test_main_window.py L30 (removed lines)

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai

coderabbitai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 54 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 470dfa8 and 1350119.

Files selected for processing: 1

  • package/PartSeg/plugins/napari_widgets/simple_measurement_widget.py

No actionable comments were generated in the recent review.

Walkthrough

The pull request removes version-gated compatibility paths for napari, Pydantic, and tifffile. It updates napari camera access and theme handling, adds a JSON metadata parser for strings or bytes, and routes metadata loading through that parser.

Changes: Napari compatibility paths

Layer / File(s) Summary
Startup and theme handling
package/PartSeg/launcher_main.py, package/PartSeg/_launcher/main_window.py, package/PartSeg/common_backend/base_settings.py
Startup no longer exits based on napari and Pydantic versions. Theme handling no longer branches on napari versions or uses the removed Color helper.
Viewer and scale-bar behavior
package/PartSeg/plugins/napari_widgets/search_label_widget.py, package/PartSeg/common_gui/main_window.py, package/tests/test_PartSeg/roi_analysis/test_main_window.py, package/tests/test_PartSeg/roi_mask/test_main_window.py
Camera reads and writes use canvas.view.camera. The scale-bar compatibility warning and its test setup are removed.

Pydantic API paths

Layer / File(s) Summary
Field conversion and model rebuilding
package/PartSegCore/algorithm_describe_base.py, package/PartSegCore/analysis/measurement_base.py
Algorithm property conversion uses a single FieldInfo-based implementation. Node.model_rebuild() runs without a Pydantic version check.

JSON metadata loading

Layer / File(s) Summary
Parser and metadata integrations
package/PartSegCore/io_utils.py, package/PartSegCore/analysis/load_functions.py, package/PartSegCore/mask/io_functions.py, package/tests/test_PartSegCore/test_io.py, examples/dev/update_frame_thick.py
load_metadata_from_string parses JSON text or bytes. Archive and history loaders and the development example use it. A test checks bytes input and path-string rejection.

Tifffile XML compatibility patch

Layer / File(s) Summary
Remove legacy XML parser patch
package/PartSegImage/tifffile_fixes.py
The version-gated XML parser replacement and its supporting imports are removed.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Merge Risk: Minimal · previous review 470df

The change stops project archive metadata from being treated as a file path and removes obsolete compatibility code for older dependency versions. No actionable merge-blocking risk was identified in the supplied context.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names both parts of the changeset: archive metadata no longer treated as a file path (the new load_metadata_from_string helper used for archive members), and removal of dead dependency versi…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names both parts of the changeset: archive metadata no longer treated as a file path (the new load_metadata_from_string helper used for archive members), and removal of dead dependency version checks (napari, pydantic, tifffile compatibility code). It is specific, conveys the primary fix, and is a single sentence. The 76-character length is slightly long but acceptable.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between c9a76c0 and 470dfa8.

Files selected for processing: 3

  • examples/dev/update_frame_thick.py
  • package/PartSeg/common_backend/base_settings.py
  • package/PartSeg/common_gui/main_window.py
Files skipped from review as they are similar to previous changes: 1

  • package/PartSeg/common_gui/main_window.py

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="package/PartSegCore/io_utils.py" line_range="179" />
<code_context>
         return False


+def load_metadata_from_string(data: str | bytes):
+    """
+    Load metadata from serialized json content.
</code_context>
<issue_to_address>
**Archive utility still reads local files**

When a crafted archive’s `metadata.json` content is the path of an existing JSON file, `load_metadata_base` treats the extracted `metadata.json` text as a path when that path exists, so the utility imports local JSON into its output archive instead of parsing the archive member.

Use `load_metadata_from_string` for the extracted member in `examples/dev/update_frame_thick.py`.
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: package/PartSegCore/io_utils.py:179


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread package/PartSegCore/io_utils.py Resolved

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sourcery assessment

Approved.

@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.76%. Comparing base (9c274fa) to head (1350119).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1509      +/-   ##
===========================================
+ Coverage    92.63%   92.76%   +0.13%     
===========================================
  Files          211      211              
  Lines        33247    33156      -91     
===========================================
- Hits         30797    30756      -41     
+ Misses        2450     2400      -50     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sonarqubecloud

Copy link
Copy Markdown

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