The Free Software Media System - Server Backend & API
Harden remaining path-construction sinks against traversal
Shadowghost21801e8ba138
diff --git a/Jellyfin.Api/Controllers/HlsSegmentController.cs b/Jellyfin.Api/Controllers/HlsSegmentController.csindex b5365cd632..1b432aaf44 100644--- a/Jellyfin.Api/Controllers/HlsSegmentController.cs+++ b/Jellyfin.Api/Controllers/HlsSegmentController.cs@@ -5,6 +5,7 @@ using System.IO;using System.Threading.Tasks;using Jellyfin.Api.Attributes;using Jellyfin.Api.Helpers;+using Jellyfin.Extensions;using MediaBrowser.Common.Configuration;using MediaBrowser.Controller.Configuration;using MediaBrowser.Controller.MediaEncoding;@@ -63,8 +64,7 @@ public class HlsSegmentController : BaseJellyfinApiControllervar file = string.Concat(segmentId, Path.GetExtension(Request.Path.Value.AsSpan()));var transcodePath = _serverConfigurationManager.GetTranscodePath();file = Path.GetFullPath(Path.Combine(transcodePath, file));- var fileDir = Path.GetDirectoryName(file);- if (string.IsNullOrEmpty(fileDir) || !fileDir.StartsWith(transcodePath, StringComparison.InvariantCulture))+ if (!PathHelper.IsContainedIn(transcodePath, file)){return BadRequest("Invalid segment.");}@@ -89,8 +89,7 @@ public class HlsSegmentController : BaseJellyfinApiControllervar file = string.Concat(playlistId, Path.GetExtension(Request.Path.Value.AsSpan()));var transcodePath = _serverConfigurationManager.GetTranscodePath();file = Path.GetFullPath(Path.Combine(transcodePath, file));- var fileDir = Path.GetDirectoryName(file);- if (string.IsNullOrEmpty(fileDir) || !fileDir.StartsWith(transcodePath, StringComparison.InvariantCulture)+ if (!PathHelper.IsContainedIn(transcodePath, file)|| Path.GetExtension(file.AsSpan()).Equals(".m3u8", StringComparison.OrdinalIgnoreCase)){return BadRequest("Invalid segment.");@@ -144,8 +143,7 @@ public class HlsSegmentController : BaseJellyfinApiControllervar transcodeFolderPath = _serverConfigurationManager.GetTranscodePath();file = Path.GetFullPath(Path.Combine(transcodeFolderPath, file));- var fileDir = Path.GetDirectoryName(file);- if (string.IsNullOrEmpty(fileDir) || !fileDir.StartsWith(transcodeFolderPath, StringComparison.InvariantCulture))+ if (!PathHelper.IsContainedIn(transcodeFolderPath, file)){return BadRequest("Invalid segment.");}diff --git a/Jellyfin.Api/Controllers/ImageController.cs b/Jellyfin.Api/Controllers/ImageController.csindex 52d8b4dad1..d492a2f5ba 100644--- a/Jellyfin.Api/Controllers/ImageController.cs+++ b/Jellyfin.Api/Controllers/ImageController.cs@@ -125,7 +125,13 @@ public class ImageController : BaseJellyfinApiController{// Handle image/png; charset=utf-8var mimeType = Request.ContentType?.Split(';').FirstOrDefault();- var userDataPath = Path.Combine(_serverConfigurationManager.ApplicationPaths.UserConfigurationDirectoryPath, user.Username);+ var userConfigurationDirectoryPath = _serverConfigurationManager.ApplicationPaths.UserConfigurationDirectoryPath;+ var userDataPath = Path.Combine(userConfigurationDirectoryPath, user.Username);+ if (!PathHelper.IsContainedIn(userConfigurationDirectoryPath, userDataPath))+ {+ return BadRequest("Invalid user.");+ }+if (user.ProfileImage is not null){await _userManager.ClearProfileImageAsync(user).ConfigureAwait(false);diff --git a/Jellyfin.Api/Controllers/PluginsController.cs b/Jellyfin.Api/Controllers/PluginsController.csindex 0105ecf7a7..3bb8bb7077 100644--- a/Jellyfin.Api/Controllers/PluginsController.cs+++ b/Jellyfin.Api/Controllers/PluginsController.cs@@ -6,6 +6,7 @@ using System.Linq;using System.Text.Json;using System.Threading.Tasks;using Jellyfin.Api.Attributes;+using Jellyfin.Extensions;using Jellyfin.Extensions.Json;using MediaBrowser.Common.Api;using MediaBrowser.Common.Plugins;@@ -228,8 +229,8 @@ public class PluginsController : BaseJellyfinApiControllerif (!string.IsNullOrEmpty(plugin.Manifest.ImagePath)){- var imagePath = Path.Combine(plugin.Path, plugin.Manifest.ImagePath);- if (!System.IO.File.Exists(imagePath))+ var imagePath = Path.GetFullPath(Path.Combine(plugin.Path, plugin.Manifest.ImagePath));+ if (!PathHelper.IsContainedIn(plugin.Path, imagePath) || !System.IO.File.Exists(imagePath)){return NotFound();}diff --git a/Jellyfin.Server.Implementations/Users/UserManager.cs b/Jellyfin.Server.Implementations/Users/UserManager.csindex 583f29f94f..136d2d6029 100644--- a/Jellyfin.Server.Implementations/Users/UserManager.cs+++ b/Jellyfin.Server.Implementations/Users/UserManager.cs@@ -893,7 +893,7 @@ namespace Jellyfin.Server.Implementations.Usersinternal static void ThrowIfInvalidUsername(string name){- if (!string.IsNullOrWhiteSpace(name) && ValidUsernameRegex().IsMatch(name))+ if (!string.IsNullOrWhiteSpace(name) && ValidUsernameRegex().IsMatch(name) && name != "." && name != ".."){return;}diff --git a/tests/Jellyfin.Server.Implementations.Tests/Users/UserManagerTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Users/UserManagerTests.csindex 4cea53bd3d..2bf1d1d05b 100644--- a/tests/Jellyfin.Server.Implementations.Tests/Users/UserManagerTests.cs+++ b/tests/Jellyfin.Server.Implementations.Tests/Users/UserManagerTests.cs@@ -27,6 +27,8 @@ namespace Jellyfin.Server.Implementations.Tests.Users[InlineData(" thishasaspaceatthestart")][InlineData(" thishasaspaceatbothends ")][InlineData(" this has a space at both ends and inbetween ")]+ [InlineData(".")]+ [InlineData("..")]public void ThrowIfInvalidUsername_WhenInvalidUsername_ThrowsArgumentException(string username){Assert.Throws<ArgumentException>(() => UserManager.ThrowIfInvalidUsername(username));