Skip to content

Commit

Permalink
Fix arbitrary image file reads in ImageByNameController
Browse files Browse the repository at this point in the history
GHSL-2021-050: Issue 4 Arbitrary image file read and directory traversal.
  • Loading branch information
EraYaN committed Mar 20, 2021
1 parent f61d186 commit 239a715
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions Jellyfin.Api/Controllers/ImageByNameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,19 @@ public ActionResult GetGeneralImage([FromRoute, Required] string name, [FromRout
: type;

var path = BaseItem.SupportedImageExtensions
.Select(i => Path.Combine(_applicationPaths.GeneralPath, name, filename + i))
.Select(i => Path.GetFullPath(Path.Combine(_applicationPaths.GeneralPath, name, filename + i)))
.FirstOrDefault(System.IO.File.Exists);

if (path == null)
{
return NotFound();
}

if (!path.StartsWith(_applicationPaths.GeneralPath))
{
return BadRequest("Invalid image path.");
}

var contentType = MimeTypes.GetMimeType(path);
return File(System.IO.File.OpenRead(path), contentType);
}
Expand Down Expand Up @@ -163,27 +168,39 @@ public ActionResult<IEnumerable<ImageByNameInfo>> GetMediaInfoImages()
/// <returns>A <see cref="FileStreamResult"/> containing the image contents on success, or a <see cref="NotFoundResult"/> if the image could not be found.</returns>
private ActionResult GetImageFile(string basePath, string theme, string? name)
{
var themeFolder = Path.Combine(basePath, theme);
var themeFolder = Path.GetFullPath(Path.Combine(basePath, theme));

if (Directory.Exists(themeFolder))
{
var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(themeFolder, name + i))
.FirstOrDefault(System.IO.File.Exists);

if (!string.IsNullOrEmpty(path) && System.IO.File.Exists(path))
{
if (!path.StartsWith(basePath))
{
return BadRequest("Invalid image path.");
}

var contentType = MimeTypes.GetMimeType(path);

return PhysicalFile(path, contentType);
}
}

var allFolder = Path.Combine(basePath, "all");
var allFolder = Path.GetFullPath(Path.Combine(basePath, "all"));
if (Directory.Exists(allFolder))
{
var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(allFolder, name + i))
.FirstOrDefault(System.IO.File.Exists);

if (!string.IsNullOrEmpty(path) && System.IO.File.Exists(path))
{
if (!path.StartsWith(basePath))
{
return BadRequest("Invalid image path.");
}

var contentType = MimeTypes.GetMimeType(path);
return PhysicalFile(path, contentType);
}
Expand Down

0 comments on commit 239a715

Please sign in to comment.