-
Notifications
You must be signed in to change notification settings - Fork 0
VPR-138 fix(cms): harden temp file path in DownloadZip #184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rlorenzo
wants to merge
1
commit into
main
Choose a base branch
from
VPR-138-cms-download-cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+520
−33
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,298 @@ | ||
| using CmsData = Viper.Areas.CMS.Services.CmsFilePathSafety; | ||
|
|
||
| namespace Viper.test.CMS; | ||
|
|
||
| /// <summary> | ||
| /// Security tests for the helpers that back <c>CMS.DownloadZip</c>. | ||
| /// Covers the filename sanitizer used for the Content-Disposition header | ||
| /// and the per-request temp-archive path builder (VPR-138). | ||
| /// </summary> | ||
| public sealed class DownloadZipSecurityTests | ||
| { | ||
| #region SanitizeDownloadName | ||
|
|
||
| [Theory] | ||
| [InlineData(@"\..\..\evil.zip")] | ||
| [InlineData("../../evil.zip")] | ||
| [InlineData(@"C:\Windows\Temp\evil.zip")] | ||
| [InlineData("../evil")] | ||
| public void SanitizeDownloadName_StripsPathSeparatorsAndTraversal(string input) | ||
| { | ||
| var result = CmsData.SanitizeDownloadName(input); | ||
|
|
||
| Assert.DoesNotContain('\\', result); | ||
| Assert.DoesNotContain('/', result); | ||
| Assert.DoesNotContain("..", result); | ||
| Assert.EndsWith(".zip", result, StringComparison.OrdinalIgnoreCase); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(null)] | ||
| [InlineData("")] | ||
| [InlineData(" ")] | ||
| [InlineData("!!!")] | ||
| [InlineData("///")] | ||
| [InlineData(@"\\\")] | ||
| [InlineData(".")] | ||
| [InlineData("..")] | ||
| [InlineData("...")] | ||
| [InlineData(". .")] | ||
| public void SanitizeDownloadName_NullEmptyOrJunk_ReturnsDefault(string? input) | ||
| { | ||
| var result = CmsData.SanitizeDownloadName(input!); | ||
|
|
||
| Assert.Equal("FileDownload.zip", result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SanitizeDownloadName_NameWithoutExtension_AppendsZip() | ||
| { | ||
| var result = CmsData.SanitizeDownloadName("export"); | ||
|
|
||
| Assert.Equal("export.zip", result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SanitizeDownloadName_NonZipExtension_ForcesZipSuffix() | ||
| { | ||
| // Documented choice: preserve the original name and append .zip so the | ||
| // response MIME (application/zip) always matches the filename. | ||
| var result = CmsData.SanitizeDownloadName("export.exe"); | ||
|
|
||
| Assert.EndsWith(".zip", result, StringComparison.OrdinalIgnoreCase); | ||
| Assert.Equal("export.exe.zip", result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SanitizeDownloadName_AlreadyZip_IsPreserved() | ||
| { | ||
| var result = CmsData.SanitizeDownloadName("monthly-report.zip"); | ||
|
|
||
| Assert.Equal("monthly-report.zip", result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SanitizeDownloadName_CaseInsensitiveZipExtension_IsPreserved() | ||
| { | ||
| var result = CmsData.SanitizeDownloadName("REPORT.ZIP"); | ||
|
|
||
| Assert.Equal("REPORT.ZIP", result); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("CON")] | ||
| [InlineData("PRN")] | ||
| [InlineData("AUX")] | ||
| [InlineData("NUL")] | ||
| [InlineData("COM1")] | ||
| [InlineData("LPT1")] | ||
| [InlineData("con")] | ||
| [InlineData("CON.txt")] | ||
| public void SanitizeDownloadName_ReservedWindowsDeviceNames_ReturnsDefault(string input) | ||
| { | ||
| var result = CmsData.SanitizeDownloadName(input); | ||
|
|
||
| Assert.Equal("FileDownload.zip", result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SanitizeDownloadName_AllowsSafeCharacters() | ||
| { | ||
| var result = CmsData.SanitizeDownloadName("My_File-01 v2.zip"); | ||
|
|
||
| Assert.Equal("My_File-01 v2.zip", result); | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region Regression guard (VPR-138) | ||
|
|
||
| // Before the fix, DownloadZip built the on-disk temp archive path by | ||
| // concatenating GetRootFileFolder() + ticks + user-supplied fileName. | ||
| // A traversal payload in fileName (e.g. \..\..\evil.zip) escaped the | ||
| // CMS root. The fix splits the flow: user input feeds only the | ||
| // Content-Disposition name (SanitizeDownloadName); the on-disk path | ||
| // is generated from a server-side GUID under a dedicated temp root | ||
| // (BuildTempArchivePath). | ||
| // | ||
| // This test exercises both helpers as DownloadZip wires them and | ||
| // asserts the traversal payload cannot influence the on-disk path, | ||
| // even if a future refactor reintroduces the old concatenation. | ||
| [Theory] | ||
| [InlineData(@"\..\..\evil.zip")] | ||
| [InlineData("../../evil.zip")] | ||
| [InlineData(@"C:\Windows\System32\evil.zip")] | ||
| [InlineData(@"..\..\..\..\Windows\evil.zip")] | ||
| [InlineData("../../../../etc/passwd")] | ||
| public void Regression_Vpr138_TraversalPayload_CannotEscapeTempRoot(string attackPayload) | ||
| { | ||
| var tempRoot = CreateIsolatedTempRoot(); | ||
| try | ||
| { | ||
| var responseName = CmsData.SanitizeDownloadName(attackPayload); | ||
| var tempPath = CmsData.BuildTempArchivePath(tempRoot); | ||
|
|
||
| Assert.DoesNotContain('\\', responseName); | ||
| Assert.DoesNotContain('/', responseName); | ||
| Assert.DoesNotContain("..", responseName); | ||
| Assert.EndsWith(".zip", responseName, StringComparison.OrdinalIgnoreCase); | ||
|
|
||
| var resolvedRoot = Path.GetFullPath(tempRoot); | ||
| var resolvedPath = Path.GetFullPath(tempPath); | ||
| Assert.StartsWith( | ||
| resolvedRoot + Path.DirectorySeparatorChar, | ||
| resolvedPath, | ||
| StringComparison.OrdinalIgnoreCase); | ||
| Assert.DoesNotContain("..", resolvedPath); | ||
| } | ||
| finally | ||
| { | ||
| Cleanup(tempRoot); | ||
| } | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region SanitizeZipEntryName | ||
|
|
||
| [Theory] | ||
| [InlineData(@"..\..\evil.txt", "evil.txt")] | ||
| [InlineData("../../evil.txt", "evil.txt")] | ||
| [InlineData(@"nested\folder\file.pdf", "file.pdf")] | ||
| [InlineData("nested/folder/file.pdf", "file.pdf")] | ||
| [InlineData("Annual Report 2024.pdf", "Annual Report 2024.pdf")] | ||
| public void SanitizeZipEntryName_StripsPathComponents(string friendlyName, string expected) | ||
| { | ||
| var result = CmsData.SanitizeZipEntryName(friendlyName, fallback: "fallback.bin"); | ||
|
|
||
| Assert.Equal(expected, result); | ||
| Assert.DoesNotContain('\\', result); | ||
| Assert.DoesNotContain('/', result); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(null)] | ||
| [InlineData("")] | ||
| [InlineData(" ")] | ||
| public void SanitizeZipEntryName_EmptyFriendlyName_FallsBackToFilePath(string? friendlyName) | ||
| { | ||
| var result = CmsData.SanitizeZipEntryName(friendlyName!, fallback: @"C:\storage\real-file.bin"); | ||
|
|
||
| Assert.Equal("real-file.bin", result); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("..")] | ||
| [InlineData(".")] | ||
| [InlineData("dir/..")] | ||
| [InlineData(@"nested\.")] | ||
| [InlineData(". .")] | ||
| public void SanitizeZipEntryName_DotsOnly_FallsBackToFilePath(string friendlyName) | ||
| { | ||
| var result = CmsData.SanitizeZipEntryName(friendlyName, fallback: @"C:\storage\real-file.bin"); | ||
|
|
||
| Assert.Equal("real-file.bin", result); | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region BuildTempArchivePath | ||
|
|
||
| [Fact] | ||
| public void BuildTempArchivePath_ReturnsPathUnderTempRoot() | ||
| { | ||
| var tempRoot = CreateIsolatedTempRoot(); | ||
| try | ||
| { | ||
| var path = CmsData.BuildTempArchivePath(tempRoot); | ||
|
|
||
| var resolved = Path.GetFullPath(path); | ||
| var normalizedRoot = Path.GetFullPath(tempRoot); | ||
|
|
||
| Assert.StartsWith( | ||
| normalizedRoot + Path.DirectorySeparatorChar, | ||
| resolved, | ||
| StringComparison.OrdinalIgnoreCase); | ||
| Assert.EndsWith(".zip", resolved, StringComparison.OrdinalIgnoreCase); | ||
| } | ||
| finally | ||
| { | ||
| Cleanup(tempRoot); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void BuildTempArchivePath_AcceptsRootWithTrailingSeparator() | ||
| { | ||
| var tempRoot = CreateIsolatedTempRoot(); | ||
| var withTrailing = tempRoot + Path.DirectorySeparatorChar; | ||
| try | ||
| { | ||
| var path = CmsData.BuildTempArchivePath(withTrailing); | ||
|
|
||
| var resolved = Path.GetFullPath(path); | ||
| var normalizedRoot = Path.TrimEndingDirectorySeparator(Path.GetFullPath(withTrailing)); | ||
|
|
||
| Assert.StartsWith( | ||
| normalizedRoot + Path.DirectorySeparatorChar, | ||
| resolved, | ||
| StringComparison.OrdinalIgnoreCase); | ||
| } | ||
| finally | ||
| { | ||
| Cleanup(tempRoot); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void BuildTempArchivePath_GeneratesUniquePaths_OnSuccessiveCalls() | ||
| { | ||
| var tempRoot = CreateIsolatedTempRoot(); | ||
| try | ||
| { | ||
| var a = CmsData.BuildTempArchivePath(tempRoot); | ||
| var b = CmsData.BuildTempArchivePath(tempRoot); | ||
|
|
||
| Assert.NotEqual(a, b); | ||
| } | ||
| finally | ||
| { | ||
| Cleanup(tempRoot); | ||
| } | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("")] | ||
| [InlineData(" ")] | ||
| public void BuildTempArchivePath_RejectsEmptyRoot(string tempRoot) | ||
| { | ||
| Assert.Throws<ArgumentException>(() => CmsData.BuildTempArchivePath(tempRoot)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void BuildTempArchivePath_RejectsNullRoot() | ||
| { | ||
| Assert.Throws<ArgumentException>(() => CmsData.BuildTempArchivePath(null!)); | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region Helpers | ||
|
|
||
| private static string CreateIsolatedTempRoot() | ||
| { | ||
| var root = Path.Join(Path.GetTempPath(), "Viper-CMS-Test-" + Guid.NewGuid().ToString("N")); | ||
| Directory.CreateDirectory(root); | ||
| return root; | ||
| } | ||
|
|
||
| private static void Cleanup(string root) | ||
| { | ||
| if (Directory.Exists(root)) | ||
| { | ||
| Directory.Delete(root, recursive: true); | ||
| } | ||
| } | ||
|
|
||
| #endregion | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.