-
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 5 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,38 @@ | ||
| 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 => Uri.EscapeDataString(Path.GetFullPath(fileName).ToLowerInvariant()), | ||
| SharedMutexNameStrategy.Sha1Hash => Sha1(fileName), | ||
| _ => throw new ArgumentOutOfRangeException(nameof(strategy), strategy, null) | ||
| }; | ||
| } | ||
|
|
||
| internal static string Sha1(string value) | ||
| { | ||
| var data = Encoding.UTF8.GetBytes(value); | ||
|
|
||
| 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.
The
Sha1Hashbranch no longer normalizes the file path the way the legacy code did. The previous implementation hashedPath.GetFullPath(settings.Filename).ToLower()so the mutex name was identical regardless of relative versus absolute paths or path casing. Here the rawfileNameis hashed, so two processes that point at the same file with different path representations will compute different mutex names and both acquire their own mutexes, effectively removing mutual exclusion and defeating the purpose of the compatibility switch.Useful? React with 👍 / 👎.