Skip to content

Comments

🔒 [security fix] Prevent Arbitrary Directory Overwrite/Deletion in Dataset.save#216

Merged
cbyrohl merged 2 commits intomainfrom
security-fix-zarr-save-vulnerability-6142097217887244602
Feb 18, 2026
Merged

🔒 [security fix] Prevent Arbitrary Directory Overwrite/Deletion in Dataset.save#216
cbyrohl merged 2 commits intomainfrom
security-fix-zarr-save-vulnerability-6142097217887244602

Conversation

@cbyrohl
Copy link
Owner

@cbyrohl cbyrohl commented Feb 18, 2026

This PR fixes a security vulnerability in the Dataset.save method where an arbitrary directory could be recursively deleted if the overwrite parameter was set to True (which is the default).

The Issue

The Dataset.save method uses zarr.DirectoryStore(fname) and zarr.group(store, overwrite=overwrite). When overwrite=True, Zarr calls shutil.rmtree(fname) on the target path. If fname is controlled by a user or accidentally points to a non-Zarr directory, that directory and all its contents are deleted.

The Fix

The fix adds a pre-check before calling zarr.group. If the target path exists and is a non-empty directory, we ensure it contains either .zgroup or .zarray before allowing the overwrite to proceed. If it's a non-empty directory without these markers, an Exception is raised.

Verification

  • Added a mock-based test suite in tests/test_save_safety.py that covers:
    • Non-empty, non-Zarr directory (now correctly raises an Exception).
    • Empty directory (allowed).
    • Valid Zarr group (allowed).
    • Valid Zarr array (allowed).
    • overwrite=False (allowed, no check performed).
  • Verified the fix in a restricted environment by mocking all external dependencies.

PR created automatically by Jules for task 6142097217887244602 started by @cbyrohl

This commit adds a safety check to the `Dataset.save` method to prevent the recursive deletion of arbitrary directories when `overwrite=True`. Before proceeding with an overwrite, the method now verifies that the target directory is either empty or contains Zarr-specific metadata (`.zgroup` or `.zarray`).

🎯 What: Added a safety check before `zarr.group(..., overwrite=True)`.
⚠️ Risk: Previously, `zarr.group(..., overwrite=True)` with a `DirectoryStore` would recursively delete the target directory regardless of its contents, which could lead to accidental or malicious data loss.
🛡️ Solution: Verify the directory is a Zarr store or empty before allowing deletion.

Co-authored-by: cbyrohl <9221545+cbyrohl@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

…ests

- Change bare Exception to ValueError for programmatic handling
- Replace mock-heavy unittest tests with real filesystem integration
  tests using tmp_path (matching project conventions)
- Add test verifying directory contents are preserved on rejection
- 7 integration tests covering all safety check branches

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@cbyrohl cbyrohl merged commit 4c38a64 into main Feb 18, 2026
5 checks passed
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