-
-
Notifications
You must be signed in to change notification settings - Fork 9
[AvatarController] Restrict UploadAvatar to current user #3839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
0e7ed75
aa1321e
5e7ed4c
c4fef0c
4ca1440
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,8 +25,10 @@ public AvatarController(IUserRepository userRepo, IPermissionService permissionS | |
|
||
/// <summary> Get user's avatar on disk </summary> | ||
/// <returns> Stream of local avatar file </returns> | ||
[AllowAnonymous] | ||
[HttpGet("download", Name = "DownloadAvatar")] | ||
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(FileContentResult))] | ||
[ProducesResponseType(StatusCodes.Status404NotFound)] | ||
public async Task<IActionResult> DownloadAvatar(string userId) | ||
{ | ||
// SECURITY: Omitting authentication so the frontend can use the API endpoint directly as a URL. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment feels somewhat superfluous with the method now explicitly being marked with |
||
|
@@ -35,27 +37,28 @@ public async Task<IActionResult> DownloadAvatar(string userId) | |
// return Forbid(); | ||
// } | ||
|
||
var user = await _userRepo.GetUser(userId, false); | ||
var avatar = string.IsNullOrEmpty(user?.Avatar) ? null : user.Avatar; | ||
|
||
if (avatar is null) | ||
var avatar = (await _userRepo.GetUser(userId, false))?.Avatar; | ||
if (string.IsNullOrEmpty(avatar)) | ||
{ | ||
return NotFound(userId); | ||
return NotFound(); | ||
} | ||
|
||
var imageFile = System.IO.File.OpenRead(avatar); | ||
return File(imageFile, "application/octet-stream"); | ||
} | ||
|
||
/// <summary> | ||
/// Adds an avatar image to a <see cref="User"/> and saves locally to ~/.CombineFiles/{ProjectId}/Avatars | ||
/// Adds an avatar image to current <see cref="User"/> and saves locally to ~/.CombineFiles/{ProjectId}/Avatars | ||
/// </summary> | ||
/// <returns> Path to local avatar file </returns> | ||
[HttpPost("upload", Name = "UploadAvatar")] | ||
[ProducesResponseType(StatusCodes.Status200OK)] | ||
public async Task<IActionResult> UploadAvatar(string userId, IFormFile? file) | ||
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(string))] | ||
[ProducesResponseType(StatusCodes.Status403Forbidden)] | ||
[ProducesResponseType(StatusCodes.Status404NotFound)] | ||
public async Task<IActionResult> UploadAvatar(IFormFile? file) | ||
{ | ||
if (!_permissionService.IsUserIdAuthorized(HttpContext, userId)) | ||
if (!_permissionService.IsCurrentUserAuthorized(HttpContext)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's intriguing to me that this isn't already handled via the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be superfluous. I'll look into that, thank you. |
||
{ | ||
return Forbid(); | ||
} | ||
|
@@ -72,10 +75,11 @@ public async Task<IActionResult> UploadAvatar(string userId, IFormFile? file) | |
} | ||
|
||
// Get user to apply avatar to. | ||
var userId = _permissionService.GetUserId(HttpContext); | ||
var user = await _userRepo.GetUser(userId, false); | ||
if (user is null) | ||
{ | ||
return NotFound(userId); | ||
return NotFound(); | ||
} | ||
|
||
// Generate path to store avatar file. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks forgotten or incomplete