Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 5s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 8m33s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 10m40s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
S4 stored-XSS + S9 upload-size DoS from the #197 cold API review. The artwork path trusted client-supplied content types at both ends: upload validated only the declared multipart Content-Type (never decoded the bytes), and serving reflected a client `?contentType=` straight into the response Content-Type on unauthenticated GET sinks (/iptv/logos, /artwork/watermarks). Chain: upload <script> bytes as image/png -> GET ...?contentType=text/html serves them as HTML in-origin. nosniff (#279) does not help because the server explicitly declares text/html. - Upload: derive the content type from the bytes via SkiaSharp SKCodec (header-only, no decode -> no decompression-bomb path); reject non-images 422. New ErsatzTV.Core/Images/ImageContentTypes as the single allow-list source. Dropped the untrusted declared Content-Type from the UploadArtwork command. - Serve: removed the ?contentType= reflection structurally -- dropped ContentType from GetCachedImagePath and the [FromQuery] binding on GetImage/GetWatermark; the handler always sniffs the file, defaulting application/octet-stream. ArtworkContentTypeModel.UrlWithContentType is now the bare path; SPA previews no longer append the query. - Defense-in-depth: channel-logo / watermark {path, contentType} DTOs run through ArtworkContentTypeModel.Sanitized(), blanking non-allow-listed types on write. - S9: Kestrel MaxRequestBodySize from ETV_MAXIMUM_UPLOAD_MB rejects oversized bodies during read (controller file.Length check kept as friendly-error backstop). Both serve sinks are IgnoreApi, so no OpenAPI change. Tests: byte-sniff accept/ reject, Sanitized() allow-list, Location no longer carries ?contentType=. Docs: api-conventions §4a + decisions.md 2026-07-12. Refs #283 #197 #66 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
61 lines
2.5 KiB
C#
61 lines
2.5 KiB
C#
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Api.Artwork;
|
|
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Core.Images;
|
|
using ErsatzTV.Core.Interfaces.Images;
|
|
|
|
namespace ErsatzTV.Application.Artworks;
|
|
|
|
public class UploadArtworkHandler : IRequestHandler<UploadArtwork, Either<BaseError, ArtworkUploadResponseModel>>
|
|
{
|
|
private readonly IImageCache _imageCache;
|
|
|
|
public UploadArtworkHandler(IImageCache imageCache) => _imageCache = imageCache;
|
|
|
|
public async Task<Either<BaseError, ArtworkUploadResponseModel>> Handle(
|
|
UploadArtwork request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
// Buffer the upload so we can sniff its true format before storing it. The request body is
|
|
// already bounded by the Kestrel MaxRequestBodySize / the controller's size check, so this
|
|
// is a bounded read.
|
|
byte[] bytes;
|
|
await using (var buffer = new MemoryStream())
|
|
{
|
|
await request.Stream.CopyToAsync(buffer, cancellationToken);
|
|
bytes = buffer.ToArray();
|
|
}
|
|
|
|
// Derive the content type from the actual bytes, never from the client-declared value
|
|
// (issue #283 — a spoofed image/png header let a <script> payload be stored and later served
|
|
// as HTML). A payload that isn't a supported raster image is rejected here.
|
|
Option<string> maybeContentType = ImageContentTypes.DetectContentType(bytes);
|
|
if (maybeContentType.IsNone)
|
|
{
|
|
return BaseError.New(
|
|
$"Uploaded file is not a supported image; supported types are: {string.Join(", ", ImageContentTypes.Accepted)}");
|
|
}
|
|
|
|
string contentType = maybeContentType.IfNone(string.Empty);
|
|
|
|
using var toCache = new MemoryStream(bytes, writable: false);
|
|
Either<BaseError, string> maybeFileName = await _imageCache.SaveArtworkToCache(
|
|
toCache,
|
|
request.ArtworkKind);
|
|
|
|
return maybeFileName.Map(fileName => new ArtworkUploadResponseModel(
|
|
BuildPath(request.ArtworkKind, fileName),
|
|
contentType));
|
|
}
|
|
|
|
// Mirror the on-disk conventions the Blazor editors use so the returned path is a drop-in
|
|
// for ArtworkContentTypeModel.Path: channel logos are addressed as "iptv/logos/{file}"
|
|
// (see ChannelEditor.UploadLogo), watermarks by the bare cache file name (see WatermarkEditor).
|
|
private static string BuildPath(ArtworkKind artworkKind, string fileName) =>
|
|
artworkKind switch
|
|
{
|
|
ArtworkKind.Logo => $"iptv/logos/{fileName}",
|
|
_ => fileName
|
|
};
|
|
}
|