Repository navigation
Open
Labels
area: storageIssues related to file storage.documentationImprovements or additions to documentationenhancementNew feature or request
Description
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: userIdupload_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));
}
}Metadata
Metadata
Assignees
Labels
area: storageIssues related to file storage.documentationImprovements or additions to documentationenhancementNew feature or request
Type
Projects
- StatusShow more project fieldsBacklog 🗂️
Activity
vlidholt commentedon May 23, 2023
We should add better documentation for this. There is code for securely uploading user images in the auth module.
lukehutch commentedon May 23, 2023
@vlidholt it needs to be in Serverpod core, not Auth, and it needs to work for all filetypes, not just profile pictures.