Skip to content

fix(media): check storage against MediaLit's sealed size - #933

Merged
s3rot0n1n merged 2 commits into
platform-based-rewritefrom
fix/medialit-v080-sizes
Nov 26, 2026
Merged

s3rot0n1n merged 2 commits into
platform-based-rewritefrom
fix/medialit-v080-sizes

Conversation

@s3rot0n1n

Copy link
Copy Markdown
Contributor

Problem

MediaLit v0.8.0 moved WebP conversion from upload to seal. inspectAdmitSeal admits an upload into the school's storage using the size inspectUpload reports (the original, e.g. a PNG), then seals it, and the media row stores the size the seal reports (the WebP). The two now differ, and the WebP can be larger: MediaLit doesn't compare the two sizes, and its only check at seal is against its own account-wide limit, not the school's.

Fix

  • Admission still uses the original's size, so an upload that can't fit is never sealed.
  • After the seal, if the sealed asset is larger than what was admitted, a new gate.resize (resizeHold in storage.ts) re-checks the sealed size against the limit, leaving out the current hold, and grows the hold. It locks the school row the same way admitUpload does.
  • If it no longer fits, the sealed asset is deleted with deleteAsset, the hold is released, and the upload gets the usual refusal (plan_limit_reached for staff, upload_not_allowed for learners).
  • If it's smaller or the same, nothing else changes: all three finalize paths already save the sealed size and release the hold.

MemoryMediaLitClient gains an optional convertOnSeal setting so tests can model a seal that returns a different name, type, size and URL. The default is unchanged.

The second check after the seal sees the WebP; all three paths accept image/webp (staff: completeness only; learner avatar: image/*; community: isLearnerCommunityMime), so nothing changed there.

Testing

  • Four new tests in storage.test.ts: a PNG that seals to a smaller WebP stores the WebP's values and usage equals its size; a larger WebP that fits is kept and counted; a larger WebP that doesn't fit is refused, deleted from MediaLit, has no row, and its hold is released; a learner whose upload grows past their budget is refused. With the re-check disabled, the last two fail.
  • apps/api: bun run typecheck passes; bun run test 813 pass, 0 fail.
  • bun run lint fails with 11 errors in files this PR doesn't touch; the base commit has the same counts.
  • Not run: an end-to-end upload against MediaLit v0.8.0.

🤖 Generated with Claude Code

MediaLit v0.8.0 converts images to WebP at seal, so the sealed asset's
size can differ from the unsealed upload, and it keeps a WebP even when
it is larger than the original. Uploads are still admitted at their
unsealed size, so nothing that can't fit gets sealed; when the sealed
asset is larger, the finalize hold is re-checked against the limit and
grown, or the sealed asset is deleted and the upload refused. Rows and
storage usage already use the sealed size.

The memory MediaLit client can now model a seal-time conversion, and
the storage tests cover a smaller WebP, a larger one that fits, and a
larger one that doesn't for staff and learners.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T12:41:17.813602Z ba86658 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ba86658859

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread apps/api/src/storage.ts Outdated
if (refused) return { ok: false as const, error: refused };
await db
.update(schema.mediaUploadReservations)
.set({ bytes })

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Extend the hold when resizing it

If finalizeUpload takes longer than the 15-minute HOLD_MS, this update changes only bytes: usage() ignores expired reservations, and resizeHold does not verify that the hold still exists or renew its expiry. It can therefore return success without reserving space until the media row is saved, allowing a concurrent upload to pass its limit check in that gap.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8ba88ad. resizeHold now upserts the hold with a fresh expiresAt (now + HOLD_MS), so a seal that outran the hold renews it, and a hold the worker already pruned is recreated. inspectAdmitSeal also resizes after every seal, not only when the asset grew, so a slow seal that shrank the file is covered too. If other uploads took the space while the hold was expired, the resize refuses and the sealed asset is deleted as before. New tests in storage.test.ts ("resizing a finalize hold") cover the renewal, the recreate after pruning, and the refusal; the first two fail against the previous code.

A seal that outlasts HOLD_MS left the hold expired, so usage() stopped
counting it, and the worker could prune it, until the media row was
saved. resizeHold now upserts the hold with a fresh expiry, recreating it
if it was pruned. inspectAdmitSeal resizes after every seal, not only when
the asset grew, so a slow seal that shrank the file is covered too, and an
upload is refused when other uploads took the space in the meantime.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@s3rot0n1n
s3rot0n1n merged commit 0e761c7 into platform-based-rewrite Nov 26, 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