// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.IO; namespace osu.Game.Utils { public static class FilesystemSanityCheckHelpers { /// /// Returns whether is potentially susceptible to path traversal style attacks. /// public static bool IncursPathTraversalRisk(string path) => path.Contains("../", StringComparison.Ordinal) || path.Contains("..\\", StringComparison.Ordinal) || Path.IsPathRooted(path); /// /// Returns whether is a subdirectory (direct or nested) of . /// public static bool IsSubDirectory(string parent, string child) { // `Path.GetFullPath()` invocations are required to fully resolve the paths to unambiguous downwards-traversal-only paths. var parentInfo = new DirectoryInfo(Path.GetFullPath(parent)); var childInfo = new DirectoryInfo(Path.GetFullPath(child)); while (childInfo != null) { if (parentInfo.FullName == childInfo.FullName) return true; childInfo = childInfo.Parent; } return false; } } }