TEST: Regression test for issue #594 (access-token UAF)#598
Draft
saurabh500 wants to merge 1 commit into
Draft
Conversation
Adds a deterministic single-run regression test that drives a FedAuth connect through a local mock TDS server using SQL_COPT_SS_ACCESS_TOKEN (1256) via attrs_before, and asserts the bytes the server actually receives in the Login7 FedAuth feature extension match the bytes the test passed in. On a buggy build (PR microsoft#568 / pre-microsoft#594-fix) the helper subprocess crashes with SIGBUS (rc=-10 on macOS arm64) or, on platforms with different allocator behavior, the captured token differs from the sentinel. Both signals are caught by the same single test run. Files: tests/test_021_issue_594_access_token_uaf.py - pytest entry point tests/tools/_issue_594_helper.py - subprocess driver tests/tools/mock_tds_server.py - mock TDS 7.4 server (verbatim port from microsoft/msphpsql, same MIT license) tests/tools/__init__.py - package marker Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
| self.ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) | ||
| self.ssl_context.load_cert_chain(certfile=cert_file, keyfile=key_file) | ||
| # Don't require client certs | ||
| self.ssl_context.check_hostname = False |
| key = sys.argv[2] | ||
| iters = int(sys.argv[3]) if len(sys.argv) > 3 else 3 | ||
|
|
||
| srv = _CapturingMockTdsServer(host="127.0.0.1", port=0, cert_file=cert, key_file=key) |
| sentinel = "MSSQL-PYTHON-ISSUE-594-SENTINEL-" + ("A" * 1500) | ||
| attrs = {SQL_COPT_SS_ACCESS_TOKEN: _pack_access_token(sentinel)} | ||
| cs = ( | ||
| f"Server=127.0.0.1,{srv.port};Database=mockdb;" |
| class MockTdsServer: | ||
| """Mock TDS server for testing SQL driver connectivity.""" | ||
|
|
||
| def __init__(self, host="127.0.0.1", port=1433, cert_file=None, key_file=None): |
| self.enable_session_recovery = False # include session recovery in FeatureExtAck | ||
|
|
||
| if cert_file and key_file: | ||
| self.ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) |
| "-out", cert_path, | ||
| "-days", "30", | ||
| "-nodes", | ||
| "-subj", "/CN=localhost", |
|
|
||
| key = rsa.generate_private_key(public_exponent=65537, key_size=2048) | ||
| subject = issuer = x509.Name([ | ||
| x509.NameAttribute(NameOID.COMMON_NAME, "localhost"), |
| parser = argparse.ArgumentParser( | ||
| description="Mock TDS server for testing SQL driver connectivity", | ||
| ) | ||
| parser.add_argument("--host", default="127.0.0.1", help="Bind address") |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds a deterministic single-run regression test for issue #594.
Depends on: #596 (the fix). This test will FAIL on
mainuntil #596 merges.What it does
Drives a FedAuth connect through a local mock TDS 7.4 server using
SQL_COPT_SS_ACCESS_TOKEN(1256) viaattrs_before, and asserts that the bytes the server actually receives in the Login7 FedAuth feature extension match the bytes the test passed in.Why a subprocess
On a buggy build the helper crashes with SIGBUS (rc=-10 on macOS arm64); on platforms with different allocator behavior the captured token differs from the sentinel. Running the helper as a subprocess lets the same test catch both signals (native crash AND silent corruption) without taking down the pytest worker.
Verification
1a7c…)366a…)A single connect with a natural-AAD-token-sized (~1500-char) sentinel deterministically triggers the UAF — no loop, no artificial network delay, no malloc-churn shim needed.
Files
tests/test_021_issue_594_access_token_uaf.py— pytest entry pointtests/tools/_issue_594_helper.py— subprocess driver, captures and verifies tokenstests/tools/mock_tds_server.py— verbatim port from microsoft/msphpsql, same MIT license. Implements PreLogin + TDS 7.4 TLS upgrade + Login7 FedAuth parsing.tests/tools/__init__.py— package marker