Skip to content

File uploads need automatic ownership controls #922

Open

Description

@lukehutch

I am building a social app where a user can upload profile photos. I don't want user A to be able to overwrite user B's photos by directly calling the endpoint, and uploading files with the same path as user B's photos. I also don't want user A to be able to delete user B's photos (although user A should be able to delete their own). What I have so far is below.

It seems like this sort of thing should be built into ServerPod. Or if there are file ownership controls in ServerPod already, the docs need work.

file_ownership.yaml:

class: FileOwnership
table: file_ownership
fields:
  path: String
  userId: int
indexes:
  path_idx:
    fields: path
    unique: true
  userId_idx:
    fields: userId

upload_file.dart:

import 'package:myapp_server/src/generated/protocol.dart';
import 'package:serverpod/serverpod.dart';

class UploadFile extends Endpoint {
  @override
  bool get requireLogin => true;

  Future<int> _userId(Session session) async {
    final userId = await session.auth.authenticatedUserId;
    assert(userId != null);
    return userId!;
  }

  Future<Set<Scope>> _scopes(Session session) async {
    return await session.scopes ?? {};
  }

  Future<FileOwnership?> _pathOwner(Session session, String path) async {
    return await FileOwnership.findSingleRow(session,
        where: (t) => t.path.equals(path));
  }

  Future<String?> getUploadDescription(Session session, String path) async {
    // Ensure that the files uploaded by one user don't overwrite
    // photos already uploaded by another user
    final userId = await _userId(session);
    var pathOwner = await _pathOwner(session, path);
    if (pathOwner != null &&
        pathOwner.userId != userId &&
        // Admin can always upload
        !(await _scopes(session)).contains(Scope.admin)) {
      throw 'Tried to overwrite file owned by another user: $path';
    }
    // Get file upload description
    final uploadDescription =
        await session.storage.createDirectFileUploadDescription(
      storageId: 'public',
      path: path,
    );
    // Record that this path is now owned by the current user
    if (uploadDescription != null && pathOwner == null) {
      pathOwner = FileOwnership(path: path, userId: userId!);
      await FileOwnership.insert(session, pathOwner);
    }
    // Return file upload description
    return uploadDescription;
  }

  Future<bool> verifyUpload(Session session, String path) async {
    return await session.storage.verifyDirectFileUpload(
      storageId: 'public',
      path: path,
    );
  }

  Future<void> deleteUpload(Session session, String path) async {
    final userId = await _userId(session);
    var pathOwner = await _pathOwner(session, path);
    if (pathOwner != null &&
        pathOwner.userId != userId &&
        // Admin can always delete
        !(await _scopes(session)).contains(Scope.admin)) {
      throw 'Tried to delete file owned by another user: $path';
    }
    // Delete file
    await session.storage.deleteFile(
      storageId: 'public',
      path: path,
    );
    // Remove path ownership
    await FileOwnership.delete(session, where: (t) => t.path.equals(path));
  }
}

Activity

  1. added
    documentation
    on May 23, 2023
  2. vlidholt commented on May 23, 2023

    @vlidholt
    Collaborator

    We should add better documentation for this. There is code for securely uploading user images in the auth module.

  3. lukehutch commented on May 23, 2023

    @lukehutch
    ContributorAuthor

    @vlidholt it needs to be in Serverpod core, not Auth, and it needs to work for all filetypes, not just profile pictures.

  4. added
    enhancement
    on Dec 11, 2023
  5. added
    area: storage
    on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: storagedocumentationenhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions