Skip to content

TEST: Regression test for issue #594 (access-token UAF) - #598

Draft
Saurabh Singh (saurabh500) wants to merge 1 commit into
microsoft:mainfrom
saurabh500:test/issue-594-access-token-uaf
Draft

Saurabh Singh (saurabh500) wants to merge 1 commit into
microsoft:mainfrom
saurabh500:test/issue-594-access-token-uaf

Conversation

@saurabh500

Copy link
Copy Markdown
Contributor

Adds a deterministic single-run regression test for issue #594.

Depends on: #596 (the fix). This test will FAIL on main until #596 merges.

What it does

Drives a FedAuth connect through a local mock TDS 7.4 server using SQL_COPT_SS_ACCESS_TOKEN (1256) via attrs_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

Build Result Time
Stock 1.7.1 (md5 1a7c…) FAIL — helper rc=-10 SIGBUS 0.1s
Fixed build from #596 (md5 366a…) PASS 0.2s

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 point
  • tests/tools/_issue_594_helper.py — subprocess driver, captures and verifies tokens
  • tests/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
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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants