-
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 all 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,92 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Runtime.InteropServices; | ||
| using System.Security.Cryptography; | ||
| using System.Text; | ||
| using LiteDB.Engine; | ||
|
|
||
| namespace LiteDB.Client.Shared; | ||
|
|
||
| internal static class SharedMutexNameFactory | ||
| { | ||
| // Effective Windows limit for named mutexes (conservative). | ||
| private const int WINDOWS_MUTEX_NAME_MAX = 250; | ||
|
|
||
| // If the caller adds "Global\" (7 chars) + the name + ".Mutex" (7 chars) to the mutex name, | ||
| // we account for it conservatively here without baking it into the return value. | ||
| // Adjust if your caller prepends something longer. | ||
| private const int CONSERVATIVE_EXTERNAL_PREFIX_LENGTH = 13; // e.g., "Global\\" + name + ".Mutex" | ||
|
|
||
| internal static string Create(string fileName, SharedMutexNameStrategy strategy) | ||
| { | ||
| return strategy switch | ||
| { | ||
| SharedMutexNameStrategy.Default => CreateUsingUriEncodingWithFallback(fileName), | ||
| SharedMutexNameStrategy.UriEscape => CreateUsingUriEncoding(fileName), | ||
| SharedMutexNameStrategy.Sha1Hash => CreateUsingSha1(fileName), | ||
| _ => throw new ArgumentOutOfRangeException(nameof(strategy), strategy, null) | ||
| }; | ||
| } | ||
|
|
||
| private static string CreateUsingUriEncodingWithFallback(string fileName) | ||
| { | ||
| var normalized = Normalize(fileName); | ||
| var uri = Uri.EscapeDataString(normalized); | ||
|
|
||
| if (IsWindows() && | ||
| uri.Length + CONSERVATIVE_EXTERNAL_PREFIX_LENGTH > WINDOWS_MUTEX_NAME_MAX) | ||
| { | ||
| // Short, stable fallback well under the limit. | ||
| return "sha1-" + ComputeSha1Hex(normalized); | ||
| } | ||
|
|
||
| return uri; | ||
| } | ||
|
|
||
| private static string CreateUsingUriEncoding(string fileName) | ||
| { | ||
| var normalized = Normalize(fileName); | ||
| var uri = Uri.EscapeDataString(normalized); | ||
|
|
||
| if (IsWindows() && | ||
| uri.Length + CONSERVATIVE_EXTERNAL_PREFIX_LENGTH > WINDOWS_MUTEX_NAME_MAX) | ||
| { | ||
| // Fallback to SHA to avoid ArgumentException on Windows. | ||
| return "sha1-" + ComputeSha1Hex(normalized); | ||
| } | ||
|
|
||
| return uri; | ||
| } | ||
|
|
||
| private static bool IsWindows() | ||
| { | ||
| return RuntimeInformation.IsOSPlatform(OSPlatform.Windows); | ||
| } | ||
|
|
||
| internal static string CreateUsingSha1(string value) | ||
| { | ||
| var normalized = Normalize(value); | ||
| return ComputeSha1Hex(normalized); | ||
| } | ||
|
|
||
| private static string Normalize(string path) | ||
| { | ||
| // Invariant casing + absolute path yields stable identity. | ||
| return Path.GetFullPath(path).ToLowerInvariant(); | ||
| } | ||
|
|
||
| private static string ComputeSha1Hex(string input) | ||
| { | ||
| var data = Encoding.UTF8.GetBytes(input); | ||
| using var sha = SHA1.Create(); | ||
| var hashData = sha.ComputeHash(data); | ||
|
|
||
| var sb = new StringBuilder(hashData.Length * 2); | ||
| foreach (var b in hashData) | ||
| { | ||
| sb.Append(b.ToString("X2")); | ||
| } | ||
|
|
||
| return sb.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 default mutex name builder only switches to a SHA‑1 hash when the URI-encoded path exceeds 250 characters and the runtime is Windows. However
SharedMutexFactory.Createpasses the name tonew Mutex(false, fullName)on every platform, and the POSIX implementation also rejects names longer than 251 bytes. With a moderately long database path (≈90+ characters after encoding the/separators) the Linux/OSX runtime will throwArgumentException: name too long, makingSharedEngineunusable for that file. The previous SHA‑1 approach always produced a short, safe name. Consider applying the length check on all platforms or hashing when the encoded value exceeds the POSIX limit as well.Useful? React with 👍 / 👎.