Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a144ef6
Add Json Payload Functionality for User Agent Feature Extension
samsharma2700 Aug 22, 2025
13090ce
Update truncation null checks
samsharma2700 Aug 22, 2025
8359565
Merge remote-tracking branch 'origin/main' into dev/samsharma2700/use…
samsharma2700 Sep 9, 2025
c450c61
Enable UserAgent Feature Extension
samsharma2700 Sep 9, 2025
c8897d3
Add new functional tests for UserAgent FE
samsharma2700 Sep 10, 2025
c0eea2b
Update functional test to verify driver behaviour
samsharma2700 Sep 11, 2025
26dd6f9
Assertion update
samsharma2700 Sep 11, 2025
8ad3c69
Remove unused flags and conditionals
samsharma2700 Sep 11, 2025
2184035
Remove IsUserAgentSupportEnabled flag
samsharma2700 Sep 11, 2025
af1eeb1
Merge remote-tracking branch 'origin/main' into dev/samsharma2700/use…
samsharma2700 Sep 11, 2025
3b51844
Test cleanup and identifier update
samsharma2700 Sep 11, 2025
ff39552
Merge remote-tracking branch 'origin/main' into dev/samsharma2700/use…
samsharma2700 Sep 17, 2025
2394a2f
Merge remote-tracking branch 'origin/main' into dev/samsharma2700/use…
samsharma2700 Sep 23, 2025
d673104
Fix server side throw issue
samsharma2700 Oct 7, 2025
90c97c5
Resolve merge conflicts
samsharma2700 Oct 7, 2025
d494b66
Add useragent payload in parser
samsharma2700 Oct 8, 2025
4b59a62
Update Test
samsharma2700 Oct 10, 2025
bd038a2
Remove stray comment
samsharma2700 Oct 10, 2025
92452b8
Cleanup
samsharma2700 Oct 10, 2025
74027ae
Make Uagent flag session based
samsharma2700 Oct 10, 2025
792a721
Merge remote-tracking branch 'origin/main' into dev/samsharma2700/use…
samsharma2700 Oct 10, 2025
4fc5055
Minor fix
samsharma2700 Oct 10, 2025
45b7132
Add LocalAppContextSwitch and CI fix
samsharma2700 Oct 13, 2025
d88b970
Merge remote-tracking branch 'origin/main' into dev/samsharma2700/use…
samsharma2700 Oct 13, 2025
f427f5d
Minor fix
samsharma2700 Oct 13, 2025
eb5410a
Remove stray debug
samsharma2700 Oct 21, 2025
feec76f
Merge remote-tracking branch 'origin/main' into dev/samsharma2700/use…
samsharma2700 Oct 21, 2025
e043d15
Merge remote-tracking branch 'origin/main' into dev/samsharma2700/use…
samsharma2700 Oct 22, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,6 @@ internal bool IsDNSCachingBeforeRedirectSupported
// Json Support Flag
internal bool IsJsonSupportEnabled = false;

// User Agent Flag
internal bool IsUserAgentEnabled = true;

// Vector Support Flag
internal bool IsVectorSupportEnabled = false;

Expand Down Expand Up @@ -1417,10 +1414,7 @@ private void Login(ServerInfo server, TimeoutTimer timeout, string newPassword,
requestedFeatures |= TdsEnums.FeatureExtension.SQLDNSCaching;
requestedFeatures |= TdsEnums.FeatureExtension.JsonSupport;
requestedFeatures |= TdsEnums.FeatureExtension.VectorSupport;

#if DEBUG
requestedFeatures |= TdsEnums.FeatureExtension.UserAgent;
#endif

_parser.TdsLogin(login, requestedFeatures, _recoverySessionData, _fedAuthFeatureExtensionData, encrypt);
}
Expand Down Expand Up @@ -3000,6 +2994,12 @@ internal void OnFeatureExtAck(int featureId, byte[] data)
IsVectorSupportEnabled = true;
break;
}
case TdsEnums.FEATUREEXT_USERAGENT:
{
// Unexpected ack from server but we ignore it entirely
SqlClientEventSource.Log.TryAdvancedTraceEvent("<sc.SqlInternalConnectionTds.OnFeatureExtAck|ADV> {0}, Received feature extension acknowledgement for USERAGENTSUPPORT (ignored)", ObjectID);
break;
}

default:
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
using Microsoft.Data.SqlClient.DataClassification;
using Microsoft.Data.SqlClient.LocalDb;
using Microsoft.Data.SqlClient.Server;
using Microsoft.Data.SqlClient.UserAgent;

#if NETFRAMEWORK
using Microsoft.Data.SqlTypes;
#endif
Expand Down Expand Up @@ -8868,7 +8870,15 @@ private void WriteLoginData(SqlLogin rec,
}
}

ApplyFeatureExData(requestedFeatures, recoverySessionData, fedAuthFeatureExtensionData, useFeatureExt, length, true);
ApplyFeatureExData(
requestedFeatures,
recoverySessionData,
fedAuthFeatureExtensionData,
UserAgentInfo.UserAgentCachedJsonPayload.ToArray(),
useFeatureExt,
length,
true
);
}
catch (Exception e)
{
Expand All @@ -8887,6 +8897,7 @@ private void WriteLoginData(SqlLogin rec,
private int ApplyFeatureExData(TdsEnums.FeatureExtension requestedFeatures,
SessionData recoverySessionData,
FederatedAuthenticationFeatureExtensionData fedAuthFeatureExtensionData,
byte[] userAgentJsonPayload,
bool useFeatureExt,
int length,
bool write = false)
Expand Down Expand Up @@ -8939,6 +8950,11 @@ private int ApplyFeatureExData(TdsEnums.FeatureExtension requestedFeatures,
length += WriteVectorSupportFeatureRequest(write);
}

if ((requestedFeatures & TdsEnums.FeatureExtension.UserAgent) != 0)
{
length += WriteUserAgentFeatureRequest(userAgentJsonPayload, write);
}

length++; // for terminator
if (write)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,6 @@ internal bool IsDNSCachingBeforeRedirectSupported
// Vector Support Flag
internal bool IsVectorSupportEnabled = false;

// User Agent Flag
internal bool IsUserAgentEnabled = true;

// TCE flags
internal byte _tceVersionSupported;

Expand Down Expand Up @@ -3038,7 +3035,14 @@ internal void OnFeatureExtAck(int featureId, byte[] data)
IsVectorSupportEnabled = true;
break;
}

case TdsEnums.FEATUREEXT_USERAGENT:
{
// TODO: define comment, TDS spec doesnot define an ack
// Unexpected ack from server but we ignore it entirely
// TODO for tfuture if we can find and verify this log message
Comment thread
samsharma2700 marked this conversation as resolved.
Outdated
SqlClientEventSource.Log.TryAdvancedTraceEvent("<sc.SqlInternalConnectionTds.OnFeatureExtAck|ADV> {0}, Received feature extension acknowledgement for USERAGENTSUPPORT (ignored)", ObjectID);
break;
}
default:
{
// Unknown feature ack
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
using Microsoft.Data.SqlClient.DataClassification;
using Microsoft.Data.SqlClient.LocalDb;
using Microsoft.Data.SqlClient.Server;
using Microsoft.Data.SqlClient.UserAgent;

#if NETFRAMEWORK
using Microsoft.Data.SqlTypes;
#endif
Expand Down Expand Up @@ -9035,7 +9037,15 @@ private void WriteLoginData(SqlLogin rec,
}
}

ApplyFeatureExData(requestedFeatures, recoverySessionData, fedAuthFeatureExtensionData, useFeatureExt, length, true);
ApplyFeatureExData(
requestedFeatures,
recoverySessionData,
fedAuthFeatureExtensionData,
UserAgentInfo.UserAgentCachedJsonPayload.ToArray(),
useFeatureExt,
length,
true
);
}
catch (Exception e)
{
Expand All @@ -9054,6 +9064,7 @@ private void WriteLoginData(SqlLogin rec,
private int ApplyFeatureExData(TdsEnums.FeatureExtension requestedFeatures,
SessionData recoverySessionData,
FederatedAuthenticationFeatureExtensionData fedAuthFeatureExtensionData,
byte[] userAgentJsonPayload,
bool useFeatureExt,
int length,
bool write = false)
Expand Down Expand Up @@ -9108,6 +9119,11 @@ private int ApplyFeatureExData(TdsEnums.FeatureExtension requestedFeatures,
length += WriteVectorSupportFeatureRequest(write);
}

if ((requestedFeatures & TdsEnums.FeatureExtension.UserAgent) != 0)
{
length += WriteUserAgentFeatureRequest(userAgentJsonPayload, write);
}

length++; // for terminator
if (write)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Buffers;
using System.Diagnostics;
using System.Text;
using Microsoft.Data.SqlClient.UserAgent;
using Microsoft.Data.SqlClient.Utilities;

#nullable enable
Expand Down Expand Up @@ -191,8 +192,21 @@ internal void TdsLogin(
}

int feOffset = length;

// NOTE: This approach of pre-calculating the packet length is inefficient.
// We're making 2 passes over the data to be written.
// Instead, we should be writing everything to the buffer once,
// leaving a hole where the header length goes.

// calculate and reserve the required bytes for the featureEx
length = ApplyFeatureExData(requestedFeatures, recoverySessionData, fedAuthFeatureExtensionData, useFeatureExt, length);
length = ApplyFeatureExData(
Comment thread
samsharma2700 marked this conversation as resolved.
Outdated
requestedFeatures,
recoverySessionData,
fedAuthFeatureExtensionData,
UserAgentInfo.UserAgentCachedJsonPayload.ToArray(),
useFeatureExt,
length
);

WriteLoginData(rec,
requestedFeatures,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@
<PackageReference Include="Microsoft.SqlServer.Types" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="System.Security.Cryptography.Pkcs" />
<PackageReference Include="System.Configuration.ConfigurationManager" />
Comment thread
paulmedynski marked this conversation as resolved.
<PackageReference Include="Microsoft.Data.SqlClient.SNI.runtime" />
</ItemGroup>
<ItemGroup Condition="'$(TargetGroup)' == 'netcoreapp'">
<PackageReference Include="System.Data.Odbc" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.SqlServer.TDS;
using Microsoft.SqlServer.TDS.FeatureExtAck;
using Microsoft.SqlServer.TDS.Login7;
using Microsoft.SqlServer.TDS.PreLogin;
using Microsoft.SqlServer.TDS.Servers;
using Xunit;

Expand Down Expand Up @@ -620,5 +618,117 @@ public void TestConnWithVectorFeatExtVersionNegotiation(bool expectedConnectionR
Assert.Throws<InvalidOperationException>(() => connection.Open());
}
}

// Test to verify client sends a UserAgent version
// and driver behaviour if server sends an Ack or not
[Theory]
[InlineData(false, false)] // We do not receive any Ack from the server
[InlineData(true, true)] // Server sends an Ack
public void TestConnWithUserAgentFeatureExtension(bool forceAck, bool expectAck)
{
using var server = TestTdsServer.StartTestServer();

// Configure the server to support UserAgent version 0x01
server.ServerSupportedUserAgentFeatureExtVersion = 0x01;

// Opt in to forced ACK for UserAgentSupport (no negotiation)
server.EmitUserAgentFeatureExtAck = forceAck;

bool loginFound = false;
bool responseFound = false;

// Captured from LOGIN7 as parsed by the test server
byte observedVersion = 0;
byte[] observedJsonBytes = Array.Empty<byte>();

// Inspect what the client sends in the LOGIN7 packet
server.OnLogin7Validated = loginToken =>
{
var token = loginToken.FeatureExt
.OfType<TDSLogin7GenericOptionToken>()
.FirstOrDefault(t => t.FeatureID == TDSFeatureID.UserAgentSupport);


// Test should fail if no UserAgent FE token is found
Assert.NotNull(token);

Assert.Equal((byte)TDSFeatureID.UserAgentSupport, (byte)token.FeatureID);

// Layout: [0] = version byte, rest = UTF-8 JSON blob
Assert.True(token.Data.Length >= 2, "UserAgent token is too short");

observedVersion = token.Data[0];
Assert.Equal(0x1, observedVersion);

observedJsonBytes = token.Data.AsSpan(1).ToArray();
loginFound = true;
};

// TODO: Confirm the server sent an Ack by reading log message from SqlInternalConnectionTds
// Inspect whether the server ever sends back an ACK
server.OnAuthenticationResponseCompleted = response =>
Comment thread
paulmedynski marked this conversation as resolved.
Outdated
{
var uaAckOptions = response
.OfType<TDSFeatureExtAckToken>()
.SelectMany(t => t.Options)
.OfType<TDSFeatureExtAckGenericOption>()
.Where(o => o.FeatureID == TDSFeatureID.UserAgentSupport)
.ToList();

if (uaAckOptions.Count > 0)
{
responseFound = true;
}

if (expectAck)
{
Assert.True(uaAckOptions.Count >= 1, "Expected an ACK for UserAgentSupport");
}
};

using var connection = new SqlConnection(server.ConnectionString);
connection.Open();

// Verify client did offer UserAgent
Assert.True(loginFound, "Expected UserAgent extension in LOGIN7");

// Verify server ACK presence or absence per scenario
if (expectAck)
{
Assert.True(responseFound, "Server should acknowledge UserAgent when forced");
}
else
{
Assert.False(responseFound, "Server should not acknowledge UserAgent");
}

// Verify the connection itself succeeded
Assert.Equal(ConnectionState.Open, connection.State);

// Note: Accessing UserAgentInfo via Reflection.
// We cannot use InternalsVisibleTo here because making internals visible to FunctionalTests
// causes the *.TestHarness.cs stubs to clash with the real internal types in SqlClient.
var asm = typeof(SqlConnection).Assembly;
var userAgentInfoType =
asm.GetTypes().FirstOrDefault(t => string.Equals(t.Name, "UserAgentInfo", StringComparison.Ordinal)) ??
asm.GetTypes().FirstOrDefault(t => t.FullName?.EndsWith(".UserAgentInfo", StringComparison.Ordinal) == true);

Assert.True(userAgentInfoType != null,
Comment thread
paulmedynski marked this conversation as resolved.
Outdated
$"Unable to find UserAgentInfo type in assembly {asm.FullName}");

// Try to get the property
var prop = userAgentInfoType.GetProperty("UserAgentCachedJsonPayload",
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

Assert.True(prop != null,
"Unable to find property 'UserAgentCachedJsonPayload' on UserAgentInfo");

ReadOnlyMemory<byte> cachedPayload = (ReadOnlyMemory<byte>)prop.GetValue(null)!;

Assert.False(cachedPayload.IsEmpty);
Comment thread
paulmedynski marked this conversation as resolved.
Outdated
Assert.True(observedJsonBytes.AsSpan().SequenceEqual(cachedPayload.Span),
"Observed UserAgent JSON does not match the cached payload bytes");

}
}
}
Loading
Loading