Skip to content

fix(s3): a directory marker must not touch the object mirror (#534) - #541

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/534-directory-marker-delete
Aug 3, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/534-directory-marker-delete

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #534.

PUT dir/, PUT dir/a.txt, DELETE dir/, DELETE dir/a.txt — the last call
returned HTTP 500, reproduced end to end against a binary built from v0.88.0:

$ aws s3api delete-object --bucket probe534 --key dir/a.txt
aws: [ERROR]: An error occurred (500) when calling the DeleteObject operation
  (reached max retries: 2): Internal Server Error

Why

S3 keys are opaque strings, but the afero filesystem the plugin mirrors object
bodies into is hierarchical, and filepath.Clean normalizes /bucket/dir/ to
/bucket/dir — the same path as the directory node MkdirAll created to hold
dir/a.txt. Removing the marker removed that node, orphaning its children, and the
child's own delete then hit log.Panic in unRegisterWithParent
(memmap.go:73), recovered as a 500. DeleteObjects inherited it, since it
delegates per key.

I verified the mechanism against afero v1.15.0 using substrate's actual call
pattern rather than the issue's simplified one, which matters: the marker is
already never written to the mirror, so the node that gets destroyed is the one
created implicitly for the child, not the marker's own file.

The fix — option 1 from the issue, not option 2

The write path already had the right guard; the delete and copy paths did not. All
six sites now go through one predicate:

// s3ObjectHasBody reports whether an object key has a body stored in the afero
// filesystem the plugin mirrors object contents into.
//
// The mirror holds bodies only. A key ending in "/" is a directory marker: it has
// no body, and its filesystem path collides with the directory node MkdirAll
// created for the keys beneath it …

One function rather than a fourth hand-copied strings.HasSuffix — the drift
between three guarded sites and three unguarded ones is what produced this bug. No
new encoding is introduced, which is why option 2 (a distinct on-disk
representation for markers) is not needed: skipping the mirror restores the
invariant the write path already assumed.

Two adjacent cases the reproduction does not name

Both surfaced from mutation testing, and both are reachable:

  1. dir and dir/ are distinct S3 keys that collide on a single mirror path.
    Before this change, putting, deleting, or copying onto the marker dir/
    truncated or deleted the body of the ordinary object at dir. This is the
    sharper test: it needs no children at all.
  2. A marker's versioned path collides too. .versions/dir//<versionID>
    normalizes onto the directory holding the versions of a child key named
    dir/<versionID>. That is not theoretical — a client reads the version ID off
    the marker's own PUT response, so the sequence is constructible.

CopyObject over a marker now succeeds in both directions, yielding the empty
object a marker is; reading one as a source used to fail outright on ReadFile.
Source and destination are guarded independently so a marker→regular copy produces
an empty object rather than an error.

Tests

emulator/s3_directory_marker_test.go, all failing before the change:

Test Asserts
DeleteDoesNotStrandChildren the issue's reproduction: the child's delete is 204, was a panic
DeleteObjectsDoesNotStrandChildren the same within one multi-object delete, marker first in the batch
DeleteThenReadChild a child outlives its marker — deleting dir/ deletes one zero-byte object
DeleteLeavesFilesystemIntact the mirror directly: the child's directory node survives
WritesNoFileToTheMirror the invariant itself, walked from the root, versioned and not
VersionedDeleteDoesNotStrandChildVersions case 2 above, end to end through the API
SiblingKeyIsNotClobbered case 1 above, table-driven over put / delete / delete-absent / copy
Copy marker→marker, marker→key, key→marker; each leaves an unrelated child intact and deletable

Mutation testing

Ten mutants across the predicate and all six guard sites — inverted, always-true,
always-false, and each guard dropped in turn. All caught, no survivors.

Three survived initially and each was a genuine test-design gap rather than an
equivalence, so the tests were strengthened rather than the mutants excused:
dropping the copyObject destination guard and the putObject write guard were
both invisible until SiblingKeyIsNotClobbered existed (the corruption only bites
on a later Remove, which no test performed), and dropping the versioned-delete
guard needed VersionedDeleteDoesNotStrandChildVersions asserting on the child's
permanent version delete — the call that actually reaches fs.Remove.

Verification

gofmt -l ., go build ./..., go vet ./..., golangci-lint run ./... → 0
issues
, make test (race), make docs-reference docs-reference-check docs-versions (no diff — no plugin added), npm --prefix docs run build,
go vet -C test/e2e ./..., go test -C test/e2e ./.... Plus the end-to-end
sequence above against substrate server, which now returns a clean 204 and an
empty bucket listing.

Compatibility

No state-format change, so existing snapshots and recorded event logs replay
identically. A caller that was receiving a 500 now receives the documented
response; nothing that previously succeeded changes behaviour.

PUT dir/, PUT dir/a.txt, DELETE dir/, DELETE dir/a.txt returned HTTP 500 on
the last call. S3 keys are opaque strings, but the afero filesystem the plugin
mirrors object bodies into is hierarchical, and filepath.Clean normalizes
/bucket/dir/ to /bucket/dir -- the directory node MkdirAll created to hold
dir/a.txt. Removing the marker removed that node, orphaning its children, and
the child's delete then panicked inside MemMapFs. DeleteObjects inherited it,
delegating per key.

The write path already guarded this: a marker's body is always empty and is
never written to the mirror. The delete and copy paths did not. All six sites
now share one s3ObjectHasBody predicate instead of four hand-copied HasSuffix
checks -- the drift that produced the bug.

Two adjacent cases fall out, neither in the reproduction: "dir" and "dir/" are
distinct S3 keys colliding on one mirror path, so a marker operation truncated
or deleted the body of the object one character away from it; and a marker's
versioned path .versions/dir//<versionID> normalizes onto the directory holding
the versions of a child key named dir/<versionID>, which is reachable because a
client reads that version ID off the marker's own PUT. CopyObject over a marker
now succeeds in both directions, yielding the empty object a marker is.

Ten mutants applied across the predicate and all six guards; all caught.
@scttfrdmn
scttfrdmn merged commit 441d28a into main Aug 3, 2026
12 checks passed
@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 71.42857% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/s3_plugin.go 71.42% 3 Missing and 3 partials ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn deleted the fix/534-directory-marker-delete branch August 9, 2026 17:48
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.

S3: deleting a "prefix/" marker object strands every key beneath it, and the next delete panics

1 participant