Skip to content

Commit

Permalink
Secure local playlist path handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadowghost committed May 16, 2024
1 parent 8aee500 commit 56a158e
Showing 1 changed file with 42 additions and 40 deletions.
82 changes: 42 additions & 40 deletions MediaBrowser.Providers/Playlists/PlaylistItemsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private IEnumerable<LinkedChild> GetItems(string path, string extension)

if (string.Equals(".m3u8", extension, StringComparison.OrdinalIgnoreCase))
{
return GetM3u8Items(stream, path);
return GetM3uItems(stream, path);
}

if (string.Equals(".pls", extension, StringComparison.OrdinalIgnoreCase))
Expand All @@ -95,73 +95,75 @@ private IEnumerable<LinkedChild> GetPlsItems(Stream stream, string path)
var content = new PlsContent();
var playlist = content.GetFromStream(stream);

return playlist.PlaylistEntries.Select(i => new LinkedChild
{
Path = GetPlaylistItemPath(i.Path, path),
Type = LinkedChildType.Manual
});
}

private IEnumerable<LinkedChild> GetM3u8Items(Stream stream, string path)
{
var content = new M3uContent();
var playlist = content.GetFromStream(stream);

return playlist.PlaylistEntries.Select(i => new LinkedChild
{
Path = GetPlaylistItemPath(i.Path, path),
Type = LinkedChildType.Manual
});
return playlist.PlaylistEntries
.Select(i => GetLinkedChild(i.Path, path))
.Where(i => i is not null);
}

private IEnumerable<LinkedChild> GetM3uItems(Stream stream, string path)
{
var content = new M3uContent();
var playlist = content.GetFromStream(stream);

return playlist.PlaylistEntries.Select(i => new LinkedChild
{
Path = GetPlaylistItemPath(i.Path, path),
Type = LinkedChildType.Manual
});
return playlist.PlaylistEntries
.Select(i => GetLinkedChild(i.Path, path))
.Where(i => i is not null);
}

private IEnumerable<LinkedChild> GetZplItems(Stream stream, string path)
{
var content = new ZplContent();
var playlist = content.GetFromStream(stream);

return playlist.PlaylistEntries.Select(i => new LinkedChild
{
Path = GetPlaylistItemPath(i.Path, path),
Type = LinkedChildType.Manual
});
return playlist.PlaylistEntries
.Select(i => GetLinkedChild(i.Path, path))
.Where(i => i is not null);
}

private IEnumerable<LinkedChild> GetWplItems(Stream stream, string path)
{
var content = new WplContent();
var playlist = content.GetFromStream(stream);

return playlist.PlaylistEntries.Select(i => new LinkedChild
{
Path = GetPlaylistItemPath(i.Path, path),
Type = LinkedChildType.Manual
});
return playlist.PlaylistEntries
.Select(i => GetLinkedChild(i.Path, path))
.Where(i => i is not null);
}

private string GetPlaylistItemPath(string itemPath, string containingPlaylistFolder)
private LinkedChild GetLinkedChild(string itemPath, string playlistPath)
{
if (!File.Exists(itemPath))
if (TryGetPlaylistItemPath(itemPath, playlistPath, out var parsedPath))
{
var path = Path.Combine(Path.GetDirectoryName(containingPlaylistFolder), itemPath);
if (File.Exists(path))
return new LinkedChild
{
return path;
}
Path = parsedPath,
Type = LinkedChildType.Manual
};
}

return null;
}

private bool TryGetPlaylistItemPath(string itemPath, string playlistPath, out string path)
{
path = null;
var baseFolder = Path.GetDirectoryName(playlistPath);

if (itemPath.StartsWith(baseFolder, StringComparison.OrdinalIgnoreCase) && File.Exists(itemPath))
{
path = itemPath;
return true;
}

var basePath = Path.Combine(baseFolder, itemPath);
var fullPath = Path.GetFullPath(basePath);
if (fullPath.StartsWith(baseFolder, StringComparison.OrdinalIgnoreCase) && File.Exists(fullPath))
{
path = fullPath;
return true;
}

return itemPath;
return false;
}

public bool HasChanged(BaseItem item, IDirectoryService directoryService)
Expand Down

0 comments on commit 56a158e

Please sign in to comment.