Azure Table sanitization code leads to key collisions
@hyha600 pointed to the fact that the current sanitization code will produce same keys from different inputs, e.g. "h//a and h##a will all sanitize to h__a".
A solution would be to replace illegal characters with their respective hex codes.
To aid casual observers, the code is here.
If someone goes to do this, there was some chatter about in Gitter. The full list of characters can be found here. It looks like one could also move the check to be first one and make less allocations using a loop (maybe with fixed, maybe even unrolled, see Gitter for further discussion). @attilah Even scribbled some code already. :grinning:
Another option would be use of an URL-Safe Base64 encode, there is a Microsoft implementation in System.Web.
@hyha Hmm, one would still need to replace /, I gather, and maybe + (I've used ATS rarely, so I'm not sure), see RFC 4648. Also, the resulting string can be at most three characters longer than the original and it becomes unrecognizable. Not to mention there could a little performance win here and the code would be, looks to me, intelligible still.
You might say I'd argue for a explict loop since I see it the most performant solution and as clean as any other solution, but that's perhaps only my opinion. :)
@veikkoeeva an URL-Safe Base64 will not produce / + = chars. You can take a look at HttpServerUtility.UrlTokenEncode() / Decode in System.Web.
Note: Besides the name of the method, its not urlencode, its a url-safe base64 encode.
I'll put my various tests into a gist. I would not depend on System.Web, specially because of the CoreCLR direction.
@hyha Hmm, good point. To complement the discussion, in CoreCLR it lives here (but it'd be one more depdency nevertheless).
- I wouldn't consider it's a bug, it's just a math nature.
- Careful, this is breaking change - all existing id's will stop working which makes all states unrecoverable.
- Yes, any map from a large set to a small set, due to a math nature of such mapping, will have these consequences. So there is no guaranteed solution that allows us to map all permutations of (string of any length) to (subset of strings that are valid rowkeys). converting single character to a hex representation will make our range of rowkeys even worse - we will reach the limit or rowkey length (1024 characters) much faster (as each forbidden char will multiply to X valid ones, rather than as one '_' as per current implementation.
- with the hex implementation there is still a possibility to have same keys produced for different input values - it just moves into different set of input keys: as per current implementation A/B and A#B produces the same key - A_B
Proposed implementation will produce A/B -> A0x1111B (not a real hex value :) ) and A#B -> A0x2222B
but now we have collusion in A#B and A0x2222B keys as well as with A(?)22B - where ? is a character which produces 0x22 and so on.
So proposed solution is not better than the current one
Also if someone concerned about overlapping IDs and we want to introduce breaking change - instead of hex encoding all characters - we can append the hash value (padded or non-padded with 0 to certain length) of original string to the sanitized result. That reduces the chance of collusion as well as allows old and new schemas of ID generation to co-exists, partially....
There are two issues need to deal with:
- Special characters : This is due limitations of Azure Table or Blob.
- Collision : Maybe encoding the full Grain ID/Key solves the issue.
Also the result length have limitations in AzureTable and Blob as well.
- Special characters are currently partially fixed ( partially - because it doesn't cater for non-asci range). Changing the current behaviour - means introducing breaking change and that breaking change is not something we can mitigate easily on the app level. We can make it stricter, but we should not change the algorithm.
If you already have the collision with current implementation - bad luck. If you foresee that for your current strategy around grain identities you'll have more collisions (e.g. in your domain area you have a lot of cases when the difference between one grain id and another is just a single forbidden symbol) - I'd advise you to reconsider your strategy and avoid collisions on the app level using some kind of shared helper for grainID generation, rather than proposing to fix it at the runtime level. Easier for you, less breaking changes for all others.
2.Collusion is unavoidable. You can't map larger set (all possible Unicode string combinations) into a smaller subset (string of limited length and characters range) uniquely without creating collusion for some values. The question in this part is only one - how to make collusion less probable ( thus how to make target subset as large as possible within the limitations
Let's cross-link: https://github.com/dotnet/orleans/commit/c2506ea16b0bbe59fdb67e457e276e8463396f86#commitcomment-16340522.