Close data leaks: remove anonymous user creation, hide private playlists, validate uploads and clamp gateway ranges

This commit is contained in:
2026-09-24 05:00:21 +02:00
parent c8fe28b07b
commit c8e9c7790e
34 changed files with 374 additions and 553 deletions
@@ -1,20 +1,20 @@
using FluentValidation;
using Musify.Api.DataTransferObjects.Albums;
using Musify.Application.Configuration;
namespace Musify.Api.Validators.Albums;
public sealed class RequestAlbumPictureUploadRequestValidator : AbstractValidator<RequestAlbumPictureUploadRequest>
{
public RequestAlbumPictureUploadRequestValidator()
public RequestAlbumPictureUploadRequestValidator(UploadIntentConfiguration uploadConfiguration)
{
RuleFor(x => x.FileType)
.NotEmpty();
.MustBeFileType(Uploads.PictureFileTypes);
RuleFor(x => x.ContentType)
.NotEmpty();
.MustBeContentType(Uploads.PictureContentTypes);
RuleFor(x => x.ExpectedSizeBytes)
.GreaterThan(0)
.When(x => x.ExpectedSizeBytes.HasValue);
.MustBeValidUploadSize(uploadConfiguration.MaxUploadBytes);
}
}
@@ -1,20 +1,20 @@
using FluentValidation;
using Musify.Api.DataTransferObjects.PlayLists;
using Musify.Application.Configuration;
namespace Musify.Api.Validators.PlayLists;
public sealed class RequestPlayListPictureUploadRequestValidator : AbstractValidator<RequestPlayListPictureUploadRequest>
{
public RequestPlayListPictureUploadRequestValidator()
public RequestPlayListPictureUploadRequestValidator(UploadIntentConfiguration uploadConfiguration)
{
RuleFor(x => x.FileType)
.NotEmpty();
.MustBeFileType(Uploads.PictureFileTypes);
RuleFor(x => x.ContentType)
.NotEmpty();
.MustBeContentType(Uploads.PictureContentTypes);
RuleFor(x => x.ExpectedSizeBytes)
.GreaterThan(0)
.When(x => x.ExpectedSizeBytes.HasValue);
.MustBeValidUploadSize(uploadConfiguration.MaxUploadBytes);
}
}
@@ -1,30 +1,29 @@
using FluentValidation;
using Musify.Api.DataTransferObjects.Tracks;
using Musify.Application.Configuration;
namespace Musify.Api.Validators.Tracks;
public sealed class RequestTrackUploadUrlsRequestValidator : AbstractValidator<RequestTrackUploadUrlsRequest>
{
public RequestTrackUploadUrlsRequestValidator()
public RequestTrackUploadUrlsRequestValidator(UploadIntentConfiguration uploadConfiguration)
{
RuleFor(x => x.PictureFileType)
.NotEmpty();
.MustBeFileType(Uploads.PictureFileTypes);
RuleFor(x => x.PictureContentType)
.NotEmpty();
.MustBeContentType(Uploads.PictureContentTypes);
RuleFor(x => x.AudioFileType)
.NotEmpty();
.MustBeFileType(Uploads.AudioFileTypes);
RuleFor(x => x.AudioContentType)
.NotEmpty();
.MustBeContentType(Uploads.AudioContentTypes);
RuleFor(x => x.ExpectedPictureSizeBytes)
.GreaterThan(0)
.When(x => x.ExpectedPictureSizeBytes.HasValue);
.MustBeValidUploadSize(uploadConfiguration.MaxUploadBytes);
RuleFor(x => x.ExpectedAudioSizeBytes)
.GreaterThan(0)
.When(x => x.ExpectedAudioSizeBytes.HasValue);
.MustBeValidUploadSize(uploadConfiguration.MaxUploadBytes);
}
}
@@ -0,0 +1,42 @@
using FluentValidation;
namespace Musify.Api.Validators;
public static class Uploads
{
public static readonly IReadOnlySet<string> PictureFileTypes =
new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "jpg", "jpeg", "png", "webp" };
public static readonly IReadOnlySet<string> PictureContentTypes =
new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "image/jpeg", "image/png", "image/webp" };
public static readonly IReadOnlySet<string> AudioFileTypes =
new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "mp3", "wav", "flac", "ogg", "m4a", "aac" };
public static readonly IReadOnlySet<string> AudioContentTypes =
new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"audio/mpeg", "audio/mp3", "audio/wav", "audio/x-wav", "audio/wave", "audio/flac", "audio/x-flac",
"audio/ogg", "audio/mp4", "audio/x-m4a", "audio/m4a", "audio/aac"
};
public static IRuleBuilderOptions<T, string> MustBeFileType<T>(
this IRuleBuilder<T, string> rule, IReadOnlySet<string> allowed) =>
rule
.NotEmpty()
.Length(1, 10)
.Matches("^[A-Za-z0-9]+$").WithMessage("'{PropertyName}' must contain only letters and digits.")
.Must(allowed.Contains).WithMessage("'{PropertyName}' must be one of: " + string.Join(", ", allowed) + ".");
public static IRuleBuilderOptions<T, string> MustBeContentType<T>(
this IRuleBuilder<T, string> rule, IReadOnlySet<string> allowed) =>
rule
.NotEmpty()
.Must(allowed.Contains).WithMessage("'{PropertyName}' is not an allowed content type.");
public static IRuleBuilderOptions<T, long?> MustBeValidUploadSize<T>(
this IRuleBuilder<T, long?> rule, long maxUploadBytes) =>
rule
.Must(size => size is null || (size > 0 && size <= maxUploadBytes))
.WithMessage($"'{{PropertyName}}' must be greater than 0 and at most {maxUploadBytes} bytes.");
}
@@ -1,25 +0,0 @@
using FluentValidation;
using Musify.Api.DataTransferObjects.Users;
namespace Musify.Api.Validators.Users;
public sealed class CreateUserRequestValidator : AbstractValidator<CreateUserRequest>
{
public CreateUserRequestValidator()
{
RuleFor(x => x.Id)
.NotEmpty();
RuleFor(x => x.Name)
.NotEmpty()
.MaximumLength(48);
RuleFor(x => x.FirstName)
.MaximumLength(48)
.When(x => !string.IsNullOrWhiteSpace(x.FirstName));
RuleFor(x => x.SecondName)
.MaximumLength(48)
.When(x => !string.IsNullOrWhiteSpace(x.SecondName));
}
}