cog_mirror() records a file as downloaded without checking its sha256 #83

Open
opened 2026-11-13 08:05:15 -05:00 by jared · 0 comments
Owner

What happens

.mirror_one_file() in R/mirror.R writes the HTTP response to disk and returns "downloaded" without hashing the file. cog_mirror() then reports the manifest's sha256 for that row, as though the bytes had been checked.

The sha256 is checked only on the way in: a file already present is skipped as "cached" when its hash matches. A truncated download, a proxy error page or a file republished mid-mirror lands on disk as "downloaded". The next cog_mirror() call fetches it again because the hash fails, but a reader who points uscogdata.url at the mirror in between reads the bad file.

test-mirror.R's "verifies sha256 against manifest" test hashes the files after the fact against a local fixture, so it can't see this.

Fix

Download to a temporary file beside the destination, compare its sha256 with the manifest, and move it into place only on a match; abort naming the file otherwise. .cache_one_table() in R/cache.R (#64) already does this for the metadata cache and could be shared.

Found while building #64.

## What happens `.mirror_one_file()` in `R/mirror.R` writes the HTTP response to disk and returns `"downloaded"` without hashing the file. `cog_mirror()` then reports the manifest's `sha256` for that row, as though the bytes had been checked. The sha256 is checked only on the way in: a file already present is skipped as `"cached"` when its hash matches. A truncated download, a proxy error page or a file republished mid-mirror lands on disk as `"downloaded"`. The next `cog_mirror()` call fetches it again because the hash fails, but a reader who points `uscogdata.url` at the mirror in between reads the bad file. `test-mirror.R`'s "verifies sha256 against manifest" test hashes the files after the fact against a local fixture, so it can't see this. ## Fix Download to a temporary file beside the destination, compare its sha256 with the manifest, and move it into place only on a match; abort naming the file otherwise. `.cache_one_table()` in `R/cache.R` (#64) already does this for the metadata cache and could be shared. Found while building #64.
jared added the
ws
corpus
type
defect
labels 2026-11-13 08:05:15 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Civilytics/uscogdata#83