-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix/url encoded mutex #2709
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
Fix/url encoded mutex #2709
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
274d5e1
Replace Mutex name hashing with URI escaping
Joy-less 1da88ed
Merge branch 'mbdavid:master' into fix-2543
Joy-less 6864862
Merge remote-tracking branch 'upstream/dev' into fix/url-encoded-mutex
JKamsker f945f10
Make SharedMutexNameStrategy configurable to switch back to legacy if…
JKamsker 3096f35
Adjust member visibility
JKamsker 95c943f
Re-introduce normalization in sha1 hash
JKamsker 5b53384
Falling back to sha1 if uri encode is not safe
JKamsker 8d8b411
Merge remote-tracking branch 'origin/dev' into fix/url-encoded-mutex
JKamsker a7fb560
Refactor SharedMutexNameFactory to remove NETSTANDARD2_0 preprocessor…
JKamsker 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
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,43 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Security.Cryptography; | ||
| using System.Text; | ||
| using LiteDB.Engine; | ||
|
|
||
| namespace LiteDB.Client.Shared; | ||
|
|
||
| internal static class SharedMutexNameFactory | ||
| { | ||
| internal static string Create(string fileName, SharedMutexNameStrategy strategy) | ||
| { | ||
| return strategy switch | ||
| { | ||
| SharedMutexNameStrategy.UriEscape or SharedMutexNameStrategy.Default => CreateUsingUriEncoding(fileName), | ||
| SharedMutexNameStrategy.Sha1Hash => CreateUsingSha1(fileName), | ||
| _ => throw new ArgumentOutOfRangeException(nameof(strategy), strategy, null) | ||
| }; | ||
| } | ||
|
|
||
| private static string CreateUsingUriEncoding(string fileName) | ||
| { | ||
| var normalized = Path.GetFullPath(fileName).ToLowerInvariant(); | ||
| return Uri.EscapeDataString(normalized); | ||
| } | ||
|
|
||
| internal static string CreateUsingSha1(string value) | ||
| { | ||
| var normalized = Path.GetFullPath(value).ToLower(); | ||
| var data = Encoding.UTF8.GetBytes(normalized); | ||
|
|
||
| using var sha = SHA1.Create(); | ||
| var hashData = sha.ComputeHash(data); | ||
| var hash = new StringBuilder(); | ||
|
|
||
| foreach (var b in hashData) | ||
| { | ||
| hash.Append(b.ToString("X2")); | ||
| } | ||
|
|
||
| return hash.ToString(); | ||
| } | ||
| } | ||
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,9 @@ | ||
| | ||
| namespace LiteDB.Engine; | ||
|
|
||
| public enum SharedMutexNameStrategy | ||
| { | ||
| Default, | ||
| UriEscape, | ||
| Sha1Hash | ||
| } |
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
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.
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.
Switching the default mutex name generation to
CreateUsingUriEncodingencodes the entire absolute path rather than hashing it. On Windows a named mutex cannot exceed 260 characters, but an escaped full path grows by two extra characters for every directory separator and other reserved characters. Valid database files whose paths are already near MAX_PATH now causenew Mutex(false, …)to throwArgumentException, whereas the previous SHA1 hash was always 40 characters. This makes some databases unusable when their path length is only moderately long.Useful? React with 👍 / 👎.