Fix fetching signature verification result from cache#4339
Fix fetching signature verification result from cache#4339cheenamalhotra wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes a correctness bug in the Always Encrypted column master key (CMK) signature verification cache where GetSignatureVerificationResult previously returned the cache hit flag (from MemoryCache.TryGetValue) instead of the cached boolean result, which could incorrectly treat a cached verification failure (false) as a success.
Changes:
- Correct
GetSignatureVerificationResultto returntrueonly when the cached value exists and istrue. - Add unit tests to cover both cached-success (
true) and cached-failure (false) scenarios to prevent regression.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SignatureVerificationCache.cs | Fixes cache lookup logic to return the cached verification result (not just presence). |
| src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/SignatureVerificationCacheTests.cs | Adds regression tests validating correct behavior for cached true and cached false. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4339 +/- ##
==========================================
- Coverage 66.69% 64.67% -2.02%
==========================================
Files 284 279 -5
Lines 43238 66067 +22829
==========================================
+ Hits 28836 42732 +13896
- Misses 14402 23335 +8933
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| int cacheCapacity = | ||
| keyStoreName.Length + | ||
| masterKeyPath.Length + | ||
| SqlSecurityUtility.GetBase64LengthFromByteLength(signature.Length) + | ||
| 4 * _cacheLookupKeySeparator.Length + | ||
| 10 /* boolean value + buffer */; | ||
|
|
||
| return new StringBuilder(keyStoreName, capacity: cacheCapacity) | ||
| .Append(_cacheLookupKeySeparator) | ||
| .Append(masterKeyPath) | ||
| .Append(_cacheLookupKeySeparator) | ||
| .Append(allowEnclaveComputations) | ||
| .Append(_cacheLookupKeySeparator) | ||
| .Append(Convert.ToBase64String(signature)) | ||
| .Append(_cacheLookupKeySeparator) | ||
| .ToString(); |
There was a problem hiding this comment.
This is an existing bug that should have its own PR. Can you make a work item to track it?
Fixes AB#45439
Problem
GetSignatureVerificationResultreturned the boolean fromMemoryCache.TryGetValue(whether the key existed in cache) instead of the cached value itself. Once a CMK signature verification failure was cachedresult: false, subsequent lookups returned true, causing the caller to skip re-verification and treat the column master key as having a valid signature.Fix
Return the actual cached value:
Tests
Added
SignatureVerificationCacheTestscovering both cachedtrueand cachedfalsecases (regression test for the bug).