Mini lesson · English

How to let users upload images

Upload, reopen and delete supplied test images while checking size limits and account ownership.

Start the interactive lessonRead the lesson below

Who is this for?

People who want visitors to upload images. You can use the ready project and a coding assistant without writing storage policies or upload code.

Before you start

A separate Supabase practice project with two controlled test accounts. The small, oversized and unsupported files are supplied. Ask the assistant to prepare the private avatars storage and provided policies in this disposable project.

Estimated time: 25–42 min

Estimates for the supplied starter files with prerequisite accounts and tools ready. Setup, deployment queues and delivery/DNS waits take additional time. This lab covers restricted image storage and ownership. Client checks and MIME settings do not constitute a complete malware/content-validation pipeline for arbitrary public uploads.

What you will learn

  • Distinguish a local preview from a stored upload.
  • Test allowed and rejected supplied files.
  • Check owner-only deletion and another account’s limits.

Workshop files

Step 1

Understand what the preview proves

A selected filename, a stored upload and a readable image are three different observations.

Open the supplied app with your assistant and choose fixtures/avatar-small.webp. Before pressing Upload image, the file control shows your selected filename. This starter does not create a local image preview at selection time; its image appears only after upload and signed access succeed.

Other apps can preview a local image without storing it. In this app too, selecting a filename alone proves no remote upload. Keep selection, provider storage and the later image read as separate checks.

A concrete example

Selecting avatar-small.webp can show the filename before the storage account is configured. To establish remote storage later, find the uploaded object in the provider and check the signed preview that reads it.

Use the complete image-upload workshop and my disposable Supabase project. Help configure sign-in, a private avatars bucket, the stated 2 MB limit and allowed image formats with the provided policies. Keep private keys out of public files. Run the page and guide me through small/oversized/unsupported-file tests and separate-account deletion checks. Handle code changes for me.

Files for this step

fixtures/avatar-small.webp
fixtures/avatar-small.webpDownload this file

Start hereWorkshop files

Open or download the files below. When saving a project, keep the folder paths shown in the filenames.

How it works (optional)

Store the image, not just a local preview

A preview created from a selected file is not evidence that a storage service received it.

The browser first holds the selected file. Uploading sends its bytes to an object-storage service, which gives it a path and applies access rules.

We will use a private Supabase bucket with one owner folder per account. The app uploads a file, then asks for a short-lived signed preview URL. It does not make every upload publicly readable.

Worked example

A local blob: preview can work without any network request. Our test will inspect the actual object and retrieve it through an authorized route.

Extra implementation exercise

The selected image appears instantly through a blob URL. Has the upload necessarily happened?

Show a worked solution

No. Verify the storage operation and matching object before claiming an upload.

  • Separates local preview from remote storage.
  • Checks the upload result and stored object.

Step 2

Prepare private test storage

Use the supplied settings and an assistant for the setup.

In your disposable project, have the assistant configure the private avatars bucket, size/format limits and policies.sql. Launch the complete sign-in/upload app over HTTP.

Confirm the bucket is private and the account/project is the intended one. Do not remove storage protections to make a failed request succeed.

A concrete example

The visitor interface can help reject a file, but the storage service must enforce its own rules too.

Supabase: storage access control

Files for this step

Start hereWorkshop files

Open or download the files below. When saving a project, keep the folder paths shown in the filenames.

How it works (optional)

Create a private bucket and owner rules

Bucket configuration and access policies enforce different parts of the task.

Create the avatars bucket as private. Configure its 2MB size limit and allowed MIME types, then apply policies.sql. Browser roles should only insert and read paths under their verified user ID.

The client’s accept attribute is a convenience, not a security check. Provider size/type rules and ownership policies still apply to direct requests. MIME labels alone do not prove arbitrary file content is safe; do not present this lab as a complete scanning service.

For an object named USER_A/avatar-1.webp, storage.foldername(name) returns the folder components; [1] selects the first one in this PostgreSQL array. It must equal the authenticated user's ID. USER_B/avatar-1.webp fails for account A. This storage-side rule is distinct from the browser's file picker and MIME precheck.

The first path segment remains the user ID even with nested folders: USER_A/subfolder/avatar.webp still has USER_A as folder component [1]. A path beginning subfolder/USER_A/... would not match. Keep the owner ID first; do not loosen the policy just to accommodate a mistaken path.

Worked example

The folder prefix comes from the signed-in user, while the database policy compares it with auth.uid(). Changing a browser path to another user’s folder should not grant permission.

create policy "upload own avatar" on storage.objects for insert to authenticated
with check (bucket_id='avatars' and (storage.foldername(name))[1]=auth.uid()::text);
create policy "read own avatar" on storage.objects for select to authenticated
using (bucket_id='avatars' and (storage.foldername(name))[1]=auth.uid()::text);

Supabase: storage access control

Extra implementation exercise

Why is checking the filename extension only in JavaScript insufficient?

Show a worked solution

A caller can send a direct request with different metadata or a different path. Enforce the intended bucket and owner rules at the service boundary.

  • Recognizes the caller can bypass JavaScript.
  • Requires storage-side restrictions and authorization.

Step 3

Upload the small test image

Check the stored object as well as the visible picture.

Sign in as account A and upload the supplied small image. Find its object in the provider and use the resulting preview or signed access route.

If storage succeeds but preview fails, investigate reading/preview before uploading another copy. Keep the recorded object reference.

A concrete example

A missing preview does not necessarily mean the upload itself failed.

Files for this step

Start hereWorkshop files

Open or download the files below. When saving a project, keep the folder paths shown in the filenames.

How it works (optional)

Upload to a unique owner path

Use a generated name instead of trusting a caller’s path or overwriting an existing file.

After sign-in, the app chooses a path using the verified user ID and a random filename. It checks the selected image’s size/type before requesting an upload, but those checks do not replace provider enforcement.

The upload does not enable upsert. If storage succeeds but signed-URL creation fails, the UI reports those as separate observations. It never says “nothing uploaded” when the object may already exist.

Worked example

A generated filename avoids accidental collisions with another picture named photo.jpg. It is not an authorization mechanism by itself.

Extra implementation exercise

The object exists, but preview creation failed. Should you immediately upload another copy?

Show a worked solution

Check the object path and read policy first. Re-uploading can create duplicates without repairing the preview problem.

  • Keeps upload success separate from preview authorization.
  • Investigates read policy/path before creating duplicates.

Step 4

Try the supplied failure cases

The exercise includes files that should not upload.

Use fixtures/avatar-too-large.png and fixtures/not-an-image.txt. Record how the page and storage respond. Ask the assistant to inspect a rejection that is confusing or an invalid file that slipped through.

Rejected files must not become stored objects. A valid signed URL can allow its holder to view an image for its lifetime; that alone is not proof that the bucket became public.

A concrete example

A file-picker restriction and real storage enforcement are different checks.

Files for this step

fixtures/avatar-small.webp
fixtures/avatar-small.webpDownload this file

Start hereWorkshop files

Open or download the files below. When saving a project, keep the folder paths shown in the filenames.

How it works (optional)

Verify rejection and temporary access

Test the service rules and understand what a signed link grants.

Upload fixtures/avatar-small.webp as account A. Inspect its storage path and open the authorized signed preview. Then try fixtures/avatar-too-large.png (over 2MiB) and fixtures/not-an-image.txt (unsupported type); both should be rejected. All three files are supplied with the lesson.

Use account B to request a signed link for A’s path; the owner policy should deny it. A signed URL legitimately issued to A can still be used by anyone who obtains it until expiry. Client sign-out does not instantly revoke every issued URL.

Worked example

A useful report records a redacted path and expiry duration, not the full temporary access URL.

Extra implementation exercise

Someone with a valid signed URL opens the image without signing in. Does that alone prove the private bucket policy failed?

Show a worked solution

No. Test whether an unauthorized account can create the URL or read without an authorized capability. Treat issued URLs as sensitive until they expire.

  • Recognizes signed URLs as temporary bearer capabilities.
  • Distinguishes URL access from permission to create one.

Step 5

Add owner-only deletion

Verify who can remove the uploaded object.

Ask the assistant to add the supplied owner-deletion behavior. As A, delete a first test object X and verify it is absent in storage. Then upload a new object Y as A; keep Y’s redacted reference for the next check.

In B’s separate session, have the assistant target Y for a direct deletion attempt. Record actor B, target Y and the provider result, then verify that A’s Y still exists. Checking only a hidden delete button, or trying to delete already-missing X again, would not establish this ownership boundary.

A concrete example

Removing an image from the screen is not the same as deleting the stored object.

Files for this step

fixtures/avatar-small.webp
fixtures/avatar-small.webpDownload this file

Start hereWorkshop files

Open or download the files below. When saving a project, keep the folder paths shown in the filenames.

How it works (optional)

Your assignment: add owner-only deletion

A new operation needs its own policy and negative tests.

Add a delete control for the uploaded object and a DELETE policy restricted to its owner folder. Do not broaden the existing bucket into a public shared area.

Verify that A can remove A’s object and B cannot remove it. Keep the storage state consistent with the UI after a failed deletion.

Worked example

The original policies intentionally allow insert/read only. Deletion is not automatically authorized by an upload permission.

The complete reference walkthrough is available in reference/assignment-walkthrough.txt. Compare its steps with your own result; fill the test record from observations in your own project.

Extra implementation exercise

Implement owner-only deletion and demonstrate both allowed and forbidden attempts.

Show a worked solution

Use a private avatars bucket with storage-side size/type settings and owner-folder policies. Upload an allowed image as account A; inspect the returned storage result, then open the short-lived signed preview. A blob preview alone proves no remote upload.

Try an unsupported or oversized file and attempt an upload/read under account B's folder. A must not gain access merely by changing a path in developer tools. Signed URLs intentionally grant temporary access until their expiry.

For deletion, add a DELETE policy using the same bucket/owner-folder condition and call remove with the exact stored path. Verify the object is absent afterward and a cross-owner deletion leaves the other owner's object intact. The reference deletion module is separate so you can inspect the path and policy before integrating it.

  • Delete permission checks the bucket and verified owner folder.
  • Failed attempts do not remove another account’s object or falsely clear the UI.

Was this lesson useful?

Leave a like, or share it with someone who could use it.

Continue learning

Explore the other lessons