-
Notifications
You must be signed in to change notification settings - Fork 873
add case sensitivity check to download directory command #4236
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
Merged
Merged
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
11 changes: 11 additions & 0 deletions
11
generator/.DevConfigs/922d5cff-5eaf-43f8-82bc-6b8f6ffac4da.json
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,11 @@ | ||
| { | ||
| "services": [ | ||
| { | ||
| "serviceName": "S3", | ||
| "type": "minor", | ||
| "changeLogMessages": [ | ||
| "BREAKING CHANGE: A security fix has been applied to the Transfer Utility Download Directory command which now checks if the directory is case-sensitive before proceeding with the download. This prevents potential overwriting of files in case-insensitive file systems. If you would like to revert to the previous behavior, you can set the 'SkipDirectoryCaseSensitivityCheck' property on the 'TransferUtilityConfig' to true." | ||
| ] | ||
| } | ||
| ] | ||
| } |
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
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
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,89 @@ | ||
| using System; | ||
| using System.IO; | ||
|
|
||
| namespace Amazon.S3.Util | ||
| { | ||
| internal static class FileSystemHelper | ||
| { | ||
| /// <summary> | ||
| /// Determines if the specified directory path is case-sensitive. | ||
| /// </summary> | ||
| /// <param name="directoryPath">The full path to the directory to check.</param> | ||
| /// <remarks> | ||
| /// Case-sensitivity can vary by directory or volume depending on the platform and configuration: | ||
| /// <list type="bullet"> | ||
| /// <item> | ||
| /// <description> | ||
| /// On Windows, NTFS is case-preserving but usually case-insensitive; however, case-sensitivity | ||
| /// can be enabled per-directory (for example via WSL tooling), so two directories on the same | ||
| /// volume may behave differently. | ||
| /// </description> | ||
| /// </item> | ||
| /// <item> | ||
| /// <description> | ||
| /// On Unix-like systems and macOS, different mount points or volumes can have different | ||
| /// case-sensitivity semantics. | ||
| /// </description> | ||
| /// </item> | ||
| /// </list> | ||
| /// Because of this, the check is performed against the actual target directory instead of a | ||
| /// global temporary location. This ensures we detect collisions (such as S3 keys that differ | ||
| /// only by case) according to the behavior of the specific filesystem where we will create files. | ||
| /// </remarks> | ||
| /// <returns> | ||
| /// True if the file system at the path is case-sensitive; False if case-insensitive. | ||
| /// </returns> | ||
| /// <exception cref="ArgumentNullException">If the directoryPath is null or empty.</exception> | ||
| /// <exception cref="DirectoryNotFoundException">If the directory does not exist.</exception> | ||
| public static bool IsDirectoryCaseSensitive(string directoryPath) | ||
| { | ||
| if (string.IsNullOrEmpty(directoryPath)) | ||
| { | ||
| throw new ArgumentNullException(nameof(directoryPath)); | ||
| } | ||
|
|
||
| if (!Directory.Exists(directoryPath)) | ||
| { | ||
| throw new DirectoryNotFoundException($"The directory '{directoryPath}' does not exist."); | ||
| } | ||
|
|
||
GarrettBeatty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #pragma warning disable CA1308 // Normalize strings to uppercase | ||
| string tempFileLower = Path.Combine(directoryPath, Guid.NewGuid().ToString().ToLowerInvariant() + ".tmp"); | ||
| #pragma warning restore CA1308 // Normalize strings to uppercase | ||
| string tempFileUpper = Path.Combine(directoryPath, Path.GetFileName(tempFileLower).ToUpperInvariant()); | ||
| bool isCaseSensitive = true; // Assume sensitive by default | ||
|
|
||
| #pragma warning disable CA1031 // Do not catch general exception types | ||
| try | ||
| { | ||
| // Create the file with a lowercase name | ||
| using (File.Create(tempFileLower)) { } | ||
|
|
||
| // Check if the uppercase version of the file exists. | ||
| // If it does, the file system is case-insensitive. | ||
| if (File.Exists(tempFileUpper)) | ||
| { | ||
| isCaseSensitive = false; | ||
| } | ||
| } | ||
| catch (Exception) | ||
| { | ||
| // Fallback assumption is that the file system is case-sensitive | ||
| isCaseSensitive = true; | ||
| } | ||
| finally | ||
| { | ||
| try | ||
| { | ||
| // Ensure temporary files are cleaned up in all cases | ||
| if (File.Exists(tempFileLower)) File.Delete(tempFileLower); | ||
| if (File.Exists(tempFileUpper)) File.Delete(tempFileUpper); | ||
| } | ||
| catch (Exception) { } | ||
| } | ||
| #pragma warning restore CA1031 // Do not catch general exception types | ||
|
|
||
| return isCaseSensitive; | ||
| } | ||
| } | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there any way to do this where we are not repeating the logic for constructing the failure policy and creating the request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why cant we add a check in DownloadSingleFileAsync for each file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am trying to detect a collision before actually starting the download. we have the ability to do so, so why let the download start and then have an exception thrown.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
current code
suggested