Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ uses `elements/` (the spec, `strings`, and the readers and writers that
`docs/reference/spec-zarr.md`.
- **`storage/`**: hides whether a store is h5py or zarr.
- `open_store(path, mode, zarr_format=...)` detects the backend from the
path and returns a `Store`.
path and returns a `Store`. It refuses an HDF5 file reading data from
elsewhere and a Zarr store naming a codec off the lists, before reading.
- On close, a writable Zarr `Store` rewrites the consolidated metadata.
Without that, anndata reads a stale index and new members look missing.
- Use the `is_group`/`is_dataset`/`create_dataset`/`copy_tree` helpers here
Expand Down Expand Up @@ -197,6 +198,14 @@ These are easy to break without any test noticing straight away:
incomplete store, so it must reach staging, which then discards it. Only a
close made while another exception propagates (`__exit__`) stays quiet.
Never catch and drop an error from closing a store you wrote.
- **zarr builds only the codecs on the lists.** Importing `adata.storage`
runs `restrict_zarr_codecs`, which keeps numcodecs and zarr, in this
process, from building a codec outside `ZARR_V2_CODECS` or
`ZARR_V3_CODECS`: numcodecs' `pickle` codec unpickles each chunk, which
runs whatever code the store's author put there. That is the layer that
holds; `open_store` and `copy_path` also refuse such a store up front, so
the message can name the array. Widen a list only after reading the
codec's `decode()`, and never for one that builds Python objects.
- **Check the output before opening anything**, with
`util/path.output_target` (commands with `--inplace`) or `check_output`.
They refuse an output that is the input under any alias (`same_path` asks
Expand Down
16 changes: 10 additions & 6 deletions STATUS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ moves; the plan of record for the current item is in the section below.

| Version | What | Issues |
|---|---|---|
| 0.17.9 | A Zarr store naming a codec adata does not decode is refused when opened: numcodecs' `pickle`, which a v2 store could name for any text array, ran its author's code on the first read | #132 |
| 0.17.8 | Sparse matrices read at least 32 MiB at a time (`subset` of a narrow matrix on Lustre in a quarter of the time); no numpy huge pages, which stalled writes on nodes with fragmented memory; Performance on cluster storage page and the `benchmarks/lustre/` probe | #129, #130 |
| 0.17.7 | `import dataframe --categorical` keeps numeric-looking labels (leiden's 0, 1, 10) as categoricals of their text; `convert --feature-type` / `--split-feature-types` take a 10x input with no feature types (Cell Ranger 2) as gene expression; every tag gets its GitHub release from the changelog | #124, #127, #114 |
| 0.17.6 | Audit fixes, waves 3 and 4 (streaming): reads and writes follow the source's chunks and Zarr v3 shards, each shard written once; `recast` checks and converts in `--chunk` blocks; `split`'s memory no longer grows with its groups; pickle arrays checked before use; 16-bit images export as they are; plain-line logs and names printed as stored; h5py >= 3.16 | #96, #97, #101, #103 |
Expand All @@ -28,10 +29,13 @@ The conversion epic's sub-issues (#21–#30) are all done and ticked in #20.

## In progress

Nothing. The fixes from the October 2026 audit (epic #87, 164 findings in 16
batches) finished with 0.17.6; the sub-issues (#88–#103) are all done and
ticked in #87. The batch names in the follow-ups below (B1 to B10) are
theirs.
The December 2026 audit of 0.17.8 (85 findings). Its hotfix, B0, is 0.17.9;
the epic with the other batches is filed once 0.17.9 is out, and the
follow-ups it covers move there.

The fixes from the October 2026 audit (epic #87, 164 findings in 16 batches)
finished with 0.17.6; the sub-issues (#88–#103) are all done and ticked in
#87. The batch names in the follow-ups below (B1 to B10) are theirs.

## Follow-ups noticed

Expand Down Expand Up @@ -110,8 +114,8 @@ theirs.
base image to Debian trixie; a Codecov token (B9).
- Repository settings: the default workflow token can write, and the
`pypi` environment has no protection rules (B9).
- Zarr stores need a counterpart to the HDF5 hardening in 0.17.5, outside
that fix's scope (B5).
- Zarr stores: their codecs are checked since 0.17.9; the rest of the
counterpart to the HDF5 hardening in 0.17.5 is in the December audit.
- Flex's `target_sets` group is not carried, and `--make-unique` names
export with their suffixes (B8a).
- `formats/textio.py` does not unquote a field that starts with `"`, as
Expand Down
30 changes: 30 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,36 @@ nav_order: 7
Notable changes to `adata-cli`. Versions are `MAJOR.MINOR.PATCH`; tags carry no
`v` prefix.

## 0.17.9

A security fix: upgrade if you open Zarr stores that someone else wrote. A
Zarr v2 store can tell zarr to decode any text array with numcodecs'
`pickle` codec, which unpickles each chunk and so runs whatever code the
store's author put there. `subset`, `split`, `concat`, `recast`, `convert`
and `export` read such a store as asked and ran that code, and `subset` to
Zarr carried the codec into its output. Every release that reads Zarr did.
A store is now checked when it is opened, and one naming a codec `adata`
does not decode is refused before any chunk is read.

### Fixed

- **A Zarr store naming the `pickle` codec, or any codec adata does not
decode, is refused** ([#132](https://github.com/cellgeni/adata-cli/pull/132)).
zarr decodes a chunk with whatever codecs its array's metadata names, and
zarr-python and anndata run a pickled chunk the same way. `adata` now
keeps numcodecs and zarr from building any codec outside a list of those
that compress or transform bytes, numbers and text, whatever metadata
names it and whenever it is read. A store is also checked when it is
opened, through the metadata each read uses, and every metadata file in it
before it is copied, and one naming such a codec is refused before
anything is read or copied, naming the array and the codec. `json2` and
`msgpack2`, which build Python objects in a shape the chunk names, are
refused too; anndata writes none of them. The
[Zarr specification](https://cellgeni.github.io/adata-cli/reference/spec-zarr/#codecs-read)
lists the codecs read, and
[troubleshooting](https://cellgeni.github.io/adata-cli/troubleshooting/)
what to do with a store that is refused.

## 0.17.8

Faster on a cluster's shared filesystem. A sparse matrix is read at least
Expand Down
12 changes: 10 additions & 2 deletions docs/developers.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ element faithfully).
`write.py` always writes the current one; `strings.py` reconciles how HDF5
and Zarr spell text.
- **`storage/`** hides the backend. `open_store(path, mode, zarr_format=...)`
returns a `Store`; use its `is_group`, `is_dataset`, `create_dataset` and
`copy_tree` rather than branching on h5py or zarr.
returns a `Store`, refusing an HDF5 file that reads data from elsewhere and
a Zarr store that names a codec off the lists; use its `is_group`,
`is_dataset`, `create_dataset` and `copy_tree` rather than branching on
h5py or zarr.

### Adding a format to `convert`

Expand Down Expand Up @@ -129,6 +131,12 @@ before changing anything that writes:
- **`Store.close` raises.** A failed HDF5 flush or Zarr consolidation leaves
an incomplete store; raising it is what lets staging discard it. It stays
quiet only while another exception propagates.
- **zarr builds only the codecs on the lists.** Importing `adata.storage`
keeps numcodecs and zarr from building any codec outside `ZARR_V2_CODECS`
or `ZARR_V3_CODECS` in the process (`restrict_zarr_codecs`): numcodecs'
`pickle` codec runs whatever code a store's author put in a chunk.
`open_store` and `copy_path` refuse such a store up front too, naming the
array. Widen a list only after reading the codec's `decode()`.
- **Check the output before opening anything**, with
`util/path.output_target` (for commands with `--inplace`) or
`check_output`. One check for every writer: the output may not be the
Expand Down
38 changes: 38 additions & 0 deletions docs/reference/spec-zarr.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,44 @@ produces) and `FixedLengthUTF32` (what `<U` produces) as having no v3
specification, and warns that other Zarr libraries may not be able to read
them. This is why text is always written as variable-length UTF-8.

### Codecs read

zarr decodes a chunk with whatever codecs its array's metadata names. One of
the codecs numcodecs registers for v2, `pickle`, unpickles each chunk, and
unpickling runs whatever code the store's author put there: reading such a
store with zarr-python, or with anndata, runs it. So this tool decodes only
codecs that compress or transform bytes, numbers and text, and keeps to
that at two levels:

- **Whenever a codec is built.** Once `adata` starts, numcodecs and zarr
cannot build any other codec in its process, whatever metadata names it
and whenever it is read: an array zarr reaches without listing it (a path
looked up in one go, an index a group's metadata embeds) or metadata
changed after the store was opened is refused as it is read, as `A Zarr
store is refused: one of its arrays names ...`.
- **When a store is opened or copied.** Every array zarr lists is checked
through the metadata that read uses (the consolidated index when it reads
through one, the hierarchy on disk when it writes), and a store copied as
files has every metadata file in it read first: the arrays' own, the
consolidated index, and any index a group's metadata embeds. A store
naming another codec is refused before any chunk is read, naming the
array and the codec, so no output carries the codec on.

The codecs read:

- **v2** (`compressor` and `filters`): `adler32`, `astype`, `base64`,
`bitround`, `blosc`, `bz2`, `categorize`, `crc32`, `crc32c`, `delta`,
`fixedscaleoffset`, `fletcher32`, `gzip`, `jenkins_lookup3`, `lz4`, `lzma`,
`packbits`, `pcodec`, `quantize`, `shuffle`, `vlen-array`, `vlen-bytes`,
`vlen-utf8`, `zfpy`, `zlib` and `zstd`. Besides `pickle`, numcodecs
registers `json2` and `msgpack2`, which build Python objects in a shape the
chunk itself names; anndata writes none of the three.
- **v3** (`codecs`, and the codecs a shard nests): zarr's own (`blosc`,
`bytes`, `cast_value`, `crc32c`, `endian`, `gzip`, `scale_offset`,
`sharding_indexed`, `transpose`, `vlen-bytes`, `vlen-utf8`, `zstd`) and
numcodecs' `numcodecs.*` wrappers of the v2 codecs above. zarr resolves no
v3 codec that runs code; the list keeps a plugin from adding one.

## Sources

- AnnData “on-disk format” prose docs (modern, ≥0.8): https://anndata.readthedocs.io/en/stable/fileformat-prose.html
Expand Down
1 change: 1 addition & 0 deletions docs/reference/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ inside every release before PyPI has it.
|---|---|
| `test_elements.py` | The element layer — encodings, string dtypes, readers and writers — run against HDF5, Zarr v2 and Zarr v3 |
| `test_storage.py` | Backend detection, copying, Zarr versions, consolidated metadata |
| `test_zarr_safety.py` | Zarr stores naming a codec adata does not decode, refused before the payload can run (it leaves a marker if it does): by every command when the store is opened, when an array no listing shows is read, when its metadata changes after opening, and before a copy, whichever metadata file names it; numcodecs and zarr unable to build such a codec once `adata.storage` is imported; every store anndata writes still opens |
| `test_storage_root_attrs.py` | The root's AnnData attributes: written when a store is opened for writing, and a warning on reading a store that lacks them, though not for anndata 0.7's layout, which never had them |
| `test_staging.py` | `util/staging.py` on its own: write beside and rename into place, several outputs together, a failed swap put back, leftovers refused rather than deleted |
| `test_write_safety.py` | Every way a command used to destroy or corrupt a store while reporting success, or leave a half-written one: outputs that alias the input, failed selections, `--inplace`, failed closes, a full disk, two runs on one output, SIGTERM |
Expand Down
16 changes: 16 additions & 0 deletions docs/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,22 @@ renamed into place with exit code 0.

## Zarr

### `'data.zarr' is refused: obs/_index names the 'pickle' codec, which runs code when a chunk is decoded.`

The store tells zarr to decode one of its arrays with numcodecs' `pickle`
codec, and unpickling a chunk runs whatever code its author put there.
anndata never writes it. Do not open the store with zarr-python or anndata
either: they decode it as asked. A codec named `json2`, `msgpack2` or one
adata does not know is refused the same way, as `... names the codec
'json2', which adata does not decode`. An array zarr reaches without
listing it is refused only when it is read, and the message cannot name it:
`A Zarr store is refused: one of its arrays names ...`. The codecs adata
reads are listed in the [Zarr specification](reference/spec-zarr.md#codecs-read).

If the store is your own, from a pipeline that once wrote text with that
codec, rewrite the array with `numcodecs.VLenUTF8()` in Python, where you
trust what runs, and adata reads it.

### anndata cannot open a Zarr store `adata` wrote

A Zarr store written from an input that is not Zarr is v3, which anndata
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# so the distribution is published as `pyadata-cli`. The import package
# and the command are both still `adata`.
name = "pyadata-cli"
version = "0.17.8"
version = "0.17.9"
description = "Streaming CLI for exploring and editing large AnnData .h5ad and .zarr stores"
readme = "README.md"
requires-python = ">=3.12"
Expand Down
12 changes: 10 additions & 2 deletions src/adata/commands/import_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@
from adata.formats.mtx import read_header
from adata.formats.sparse import import_mtx
from adata.formats.validate import check_target, validate_dimensions
from adata.storage import copy_path, copy_store_contents, detect_backend, open_store
from adata.storage import (
check_zarr_contents,
copy_path,
copy_store_contents,
detect_backend,
open_store,
)
from adata.util.markup import literal
from adata.util.path import output_target
from adata.util.staging import check_free_space, disk_size, staged_outputs
Expand Down Expand Up @@ -74,7 +80,9 @@ def _source_root(file: Path) -> Iterator[Any]:
if detect_backend(file) == "zarr":
import zarr

yield zarr.open_group(str(file), mode="r", use_consolidated=False)
root = zarr.open_group(str(file), mode="r", use_consolidated=False)
check_zarr_contents(root, file)
yield root
return
with h5py.File(file, "r") as handle:
yield handle
Expand Down
Loading
Loading