Skip to content

Fix fetching signature verification result from cache#4339

Open
cheenamalhotra wants to merge 3 commits into
mainfrom
dev/cheena/fix-cmk-sign-cache
Open

Fix fetching signature verification result from cache#4339
cheenamalhotra wants to merge 3 commits into
mainfrom
dev/cheena/fix-cmk-sign-cache

Conversation

@cheenamalhotra
Copy link
Copy Markdown
Member

@cheenamalhotra cheenamalhotra commented Jun 4, 2026

Fixes AB#45439

Problem
GetSignatureVerificationResult returned the boolean from MemoryCache.TryGetValue(whether the key existed in cache) instead of the cached value itself. Once a CMK signature verification failure was cached result: 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:

return _cache.TryGetValue<bool>(cacheLookupKey, out bool value) && value;

Tests
Added SignatureVerificationCacheTests covering both cached true and cached false cases (regression test for the bug).

Copilot AI review requested due to automatic review settings June 4, 2026 00:11
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Jun 4, 2026
@cheenamalhotra cheenamalhotra added this to the 7.1.0-preview2 milestone Jun 4, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 GetSignatureVerificationResult to return true only when the cached value exists and is true.
  • 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
Copy link
Copy Markdown

codecov Bot commented Jun 4, 2026

Codecov Report

❌ Patch coverage is 63.46154% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.67%. Comparing base (bfbdd30) to head (d8f8b7a).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
...osoft/Data/SqlClient/SignatureVerificationCache.cs 75.00% 11 Missing ⚠️
...src/Microsoft/Data/SqlClient/SqlSecurityUtility.cs 0.00% 8 Missing ⚠️
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     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 64.67% <63.46%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cheenamalhotra cheenamalhotra marked this pull request as ready for review June 4, 2026 02:51
@cheenamalhotra cheenamalhotra requested a review from a team as a code owner June 4, 2026 02:51
@cheenamalhotra cheenamalhotra moved this from To triage to In review in SqlClient Board Jun 4, 2026
@cheenamalhotra cheenamalhotra added Hotfix 7.0.2 PRs targeting main that should be backported to release/7.0 for the 7.0.2 release. Hotfix 6.1.6 PRs targeting main that should be backported to release/6.1 for the 6.1.6 release. labels Jun 4, 2026
@github-project-automation github-project-automation Bot moved this from In review to Waiting for customer in SqlClient Board Jun 4, 2026
@paulmedynski paulmedynski added the Author attention needed PRs that require author to respond or make updates to PR. label Jun 4, 2026
Copilot AI review requested due to automatic review settings June 4, 2026 17:22
@cheenamalhotra cheenamalhotra removed the Author attention needed PRs that require author to respond or make updates to PR. label Jun 4, 2026
@cheenamalhotra cheenamalhotra moved this from Waiting for customer to In review in SqlClient Board Jun 4, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment on lines +179 to +194
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();
Copy link
Copy Markdown
Contributor

@paulmedynski paulmedynski Jun 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an existing bug that should have its own PR. Can you make a work item to track it?

@paulmedynski paulmedynski added the Author attention needed PRs that require author to respond or make updates to PR. label Jun 5, 2026
@paulmedynski paulmedynski moved this from In review to Waiting for customer in SqlClient Board Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Author attention needed PRs that require author to respond or make updates to PR. Hotfix 6.1.6 PRs targeting main that should be backported to release/6.1 for the 6.1.6 release. Hotfix 7.0.2 PRs targeting main that should be backported to release/7.0 for the 7.0.2 release.

Projects

Status: Waiting for customer

Development

Successfully merging this pull request may close these issues.

5 participants