Upload Store Review
Three stages on a small Rust store for user uploads: keep every file inside its base directory, then make saves safe on a busy server (a size limit, an atomic write, honest content types), then hold a quota while many uploads arrive at once. The assistant's first version passes the happy path; the work is deciding what the store must refuse.
On this page
The codebase
The crate keeps the files users upload under one base directory. A web layer outside the crate calls save with the name the client sent, the type the client declared and the bytes, and later calls read, list and delete.
Each stage adds a read-only test file that fails on the code you start with, and hidden tests that run after Continue or Submit. Your own tests go in a file of your own and run with Test.
The assistant explains the code and Rust on request and makes the changes you direct.
Where things live
src/lib.rsThe public types and methods. Their signatures are fixed; fields and private code are yours.src/store.rsThe paths, save, read, list and delete: where every stage's decision lands.src/limits.rsAdded in stage 2: the size limit and the content type.src/quota.rsAdded in stage 3: the quota.src/candidate_tests.rsYour tests. Test runs them after the visible tests in tests/, which are read-only.Rules that shape every fix
- Keep the public API: the types, the methods and their signatures.
- Build on the code you have rather than rewriting the crate.
- Rust standard library only.
How to work each stage
- Press Test first and read which supplied test fails and why.
- Name the cause in the code before you ask for any change.
- Ask for your own tests first, each case with the answer the stage expects, and watch them fail.
- Decide the design yourself and say it whole: the rule, the mechanism, and what happens on every failure.
- Press Test again after the last change, then Continue or Submit.
Uploads stay inside the base directory
What the ticket asks
Two supplied tests fail on the code you start with: a client name that starts with two dots and a slash is written, read and deleted outside the base directory. Find out why and make the store stay inside its base.
A plain name is refused or accepted as it is, never repaired, and only a regular file under a name counts as a stored file.
Where to look
src/store.rsThe path for a name is built by joining the client's name to the base as given, and read and save open whatever sits there.tests/store_test.rsThe two failing tests show the escape on save, read and delete.What the stage pins down
InvalidName with the name as given, before the disk is touched.NotFound for anything that is not a regular file, save answers AlreadyExists, and neither the entry nor its target changes.Where the generated store goes wrong
- The name is joined to the base as given, so two dots, a separator or an absolute path leave the base.
- Reading and creating a file follow a symlink planted under a name, so a read reaches its target and a save writes through it.
The fix
- Refuse a name that is not plain with one check that every method calls first.
- Look at the entry itself, without following it, before reading, deleting or saving.
src/store.rsOne name check, called by save, read and delete before any path is built.+pub(crate) fn validate_name(name: &str) -> Result<(), StoreError> {+ let bytes = name.as_bytes();+ let plain = !bytes.is_empty()+ && bytes.len() <= MAX_NAME_BYTES+ && bytes[0] != b'.'+ && !bytes.iter().any(|&byte| byte == b'/' || byte == b'\\' || byte == 0);+ if plain {+ Ok(())+ } else {+ Err(StoreError::InvalidName(name.to_string()))+ }+}src/store.rsread and delete look at the entry without following it; save refuses anything already there.+ let metadata = fs::symlink_metadata(&path).map_err(|error| not_found_or_io(name, error))?;+ if !metadata.is_file() {+ return Err(StoreError::NotFound(name.to_string()));+ }Tests that prove it
- An absolute path, a backslash, a leading dot, an empty name and a NUL byte, each refused with the base left empty.
- A 255-byte name stored, a 256-byte one refused, and ordinary names with dots and spaces kept.
- A planted link refused by read, delete and save, with its target untouched.
The conversation
Only your side is shown: the assistant reads, writes and runs between your messages. Each chip is a rubric item the step earns.
DirectionPrompt qualityVerificationCorrectnessCode quality
- Press Test. A supplied test fails: a name with two dots reaches outside the base.
- Press Test. Still red: your tests run after the supplied ones, which fail first.
- Verification: Checks own, 6 points
- Press Test. Every test passes after the last change, your own included.
- Verification: Run after last change, 6 points
- Continue. The stage closes. Scope, the hidden tests and code quality are judged on the final code.
- Direction: Scope, 6 points
- Correctness: Hidden tests, 20 points
- Code quality: Code quality, 10 points
Where the stage lands
Measured when this conversation was played on the platform. A+ needs 92, S needs 95.
Limits, atomic writes, honest types
What the ticket asks
A new supplied test file fails on the code you start with: make save safe on a busy server. An upload over the limit is refused and read no further than needed, a half-written upload is never visible, and a failure leaves nothing behind.
The content type a stored file reports comes from its first bytes, the same from save and list.
Where to look
src/store.rssave copies the upload straight to its final name and checks the size after the whole copy.src/limits.rsAdded by this stage: where the limit and the content type belong.What the stage pins down
TooLarge and nothing is stored; exactly the limit is accepted; reading stops at most 64 KiB past it.AlreadyExists.application/octet-stream; the declared type and the extension play no part.Where the generated store goes wrong
- The upload is copied under the client's name, so it is listed while it arrives and left behind when it fails.
- The size is compared only after the whole copy, so a huge upload is read to the end.
- The reported type comes from the declared type and the extension, not from the bytes.
The fix
- Read through a reader capped one byte past the limit and stop as soon as the count passes it.
- Write to a temporary file that a guard removes unless it is committed, then rename it into place.
- Sniff the first bytes as they stream, and sniff stored files the same way in list.
src/store.rsThe upload is read through a cap one byte past the limit, in fixed chunks.+ let mut limited = data.take(limit.map_or(u64::MAX, |limit| limit.saturating_add(1)));+ let mut buffer = vec![0u8; CHUNK_BYTES];src/store.rsA temporary file removes itself on every failure unless it was committed.+impl Drop for TempFile {+ fn drop(&mut self) {+ if !self.committed {+ let _ = fs::remove_file(&self.path);+ }+ }+}src/limits.rsThe type comes from the first bytes.+pub fn sniff(head: &[u8]) -> &'static str {+ const PNG: &[u8] = &[0x89, b'P', b'N', b'G', b'\r', b'\n', 0x1a, b'\n'];+ const JPEG: &[u8] = &[0xff, 0xd8, 0xff];+ if head.starts_with(PNG) {+ "image/png"+ } else if head.starts_with(JPEG) {+ "image/jpeg"Tests that prove it
- The limit exactly and one byte past it, and a counting reader that shows how much of a large upload was read.
- A reader that fails midway, leaving nothing behind and the name free.
- A repeated name refused with the first file kept, and types that follow the bytes, not the name.
The conversation
Only your side is shown: the assistant reads, writes and runs between your messages. Each chip is a rubric item the step earns.
DirectionPrompt qualityVerificationCorrectnessCode quality
- Press Test. The new supplied test file fails on the start code.
- Press Test. Still red at the first supplied test.
- Verification: Checks own, 6 points
- Press Test. Every test passes after the last change, your own included.
- Verification: Run after last change, 6 points
- Continue. The stage closes. Scope, the hidden tests and code quality are judged on the final code.
- Direction: Scope, 5 points
- Correctness: Hidden tests, 20 points
- Code quality: Code quality, 10 points
Where the stage lands
Measured when this conversation was played on the platform. A+ needs 92, S needs 95.
A quota that holds under concurrent uploads
What the ticket asks
A new supplied test file fails on the code you start with: make the quota hold when many uploads arrive at once.
Saves in flight count, a failed save gives back every byte it took, a delete gives back its file's bytes, and files already in the base count from the start.
Where to look
src/quota.rsAdded by this stage: the quota is recorded and never read.src/store.rssave and delete, where the room is taken, settled and given back.What the stage pins down
QuotaExceeded.Where the generated store goes wrong
- The quota is recorded and nothing reads it, so a save past it succeeds.
- A check of the stored total made after the write, or without a reservation, lets saves in flight pass the quota together.
The fix
- Keep the used count behind a mutex in the store and reserve the limit under it before writing.
- Release the lock for the copy, settle the reservation to the stored size after the rename, and give it back through a guard on any failure.
- Count the files already in the base on first use, and give a deleted file's size back.
src/quota.rsA reservation gives its room back on drop unless it was settled.+impl Drop for Reservation<'_> {+ fn drop(&mut self) {+ if self.amount > 0 {+ self.quota.give_back(self.amount);+ }+ }+}src/quota.rsSettling keeps only the bytes actually stored.+ pub(crate) fn settle(mut self, stored: u64) {+ let mut used = self.quota.used();+ *used = Some(used.unwrap_or(0).saturating_sub(self.amount).saturating_add(stored));+ self.amount = 0;+ }Tests that prove it
- Two saves fill the quota, a third is refused, and a delete makes room again.
- A failed save followed by one that fits, and files already in the base counting.
- Eight threads released together against a quota of four uploads: exactly four succeed and every file reads back as its own bytes.
The conversation
Only your side is shown: the assistant reads, writes and runs between your messages. Each chip is a rubric item the step earns.
DirectionPrompt qualityVerificationCorrectnessCode quality
- Press Test. The new supplied test file fails on the start code.
- Press Test. Still red at the first supplied test.
- Verification: Checks own, 6 points
- Press Test. Every test passes after the last change, your own included.
- Verification: Run after last change, 6 points
- Submit. The last stage closes. Scope, the hidden tests and code quality are judged on the final code.
- Direction: Scope, 5 points
- Correctness: Hidden tests, 20 points
- Code quality: Code quality, 10 points
Where the stage lands
Measured when this conversation was played on the platform. A+ needs 92, S needs 95.