From 6c86ff3fc1e8226b66f673e9664ff4e6f55d250d Mon Sep 17 00:00:00 2001 From: John Luo Date: Fri, 10 Jun 2016 11:49:36 -0700 Subject: [PATCH] Treat Path.GetFullPath exceptions as unknown files #201 #204 --- .../PhysicalFileProvider.cs | 11 +++++- .../PhysicalFileProviderTests.cs | 37 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.Extensions.FileProviders.Physical/PhysicalFileProvider.cs b/src/Microsoft.Extensions.FileProviders.Physical/PhysicalFileProvider.cs index dfa65bfd..aaa7e631 100644 --- a/src/Microsoft.Extensions.FileProviders.Physical/PhysicalFileProvider.cs +++ b/src/Microsoft.Extensions.FileProviders.Physical/PhysicalFileProvider.cs @@ -74,7 +74,16 @@ private string GetFullPath(string path) return null; } - var fullPath = Path.GetFullPath(Path.Combine(Root, path)); + string fullPath; + try + { + fullPath = Path.GetFullPath(Path.Combine(Root, path)); + } + catch + { + return null; + } + if (!IsUnderneathRoot(fullPath)) { return null; diff --git a/test/Microsoft.Extensions.FileProviders.Physical.Tests/PhysicalFileProviderTests.cs b/test/Microsoft.Extensions.FileProviders.Physical.Tests/PhysicalFileProviderTests.cs index a8357375..6888df1a 100644 --- a/test/Microsoft.Extensions.FileProviders.Physical.Tests/PhysicalFileProviderTests.cs +++ b/test/Microsoft.Extensions.FileProviders.Physical.Tests/PhysicalFileProviderTests.cs @@ -36,6 +36,32 @@ public void GetFileInfoReturnsNotFoundFileInfoForEmptyPath() } } + public static TheoryData InvalidPaths + { + get + { + return new TheoryData + { + Path.Combine(". .", "file"), + Path.Combine(" ..", "file"), + Path.Combine(".. ", "file"), + Path.Combine(" .", "file"), + Path.Combine(". ", "file"), + }; + } + } + + [Theory] + [MemberData(nameof(InvalidPaths))] + public void GetFileInfoReturnsNonExistentFileInfoForIllegalPath(string path) + { + using (var provider = new PhysicalFileProvider(Path.GetTempPath())) + { + var info = provider.GetFileInfo(path); + Assert.False(info.Exists); + } + } + [ConditionalFact] [OSSkipCondition(OperatingSystems.Linux, SkipReason = "Paths starting with / are considered relative.")] [OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "Paths starting with / are considered relative.")] @@ -419,6 +445,17 @@ public void GetDirectoryContentsReturnsNotFoundDirectoryContentsForNullPath() } } + [Theory] + [MemberData(nameof(InvalidPaths))] + public void GetDirectoryContentsReturnsNotFoundDirectoryContentsForInvalidPath(string path) + { + using (var provider = new PhysicalFileProvider(Path.GetTempPath())) + { + var contents = provider.GetDirectoryContents(path); + Assert.IsType(typeof(NotFoundDirectoryContents), contents); + } + } + [Fact] public void GetDirectoryContentsReturnsNotFoundDirectoryContentsForAbsolutePath() {