Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 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
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 @@ -210,9 +210,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 @@ -1414,10 +1411,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 @@ -3023,6 +3017,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 @@ -214,9 +214,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 @@ -3068,7 +3065,18 @@ internal void OnFeatureExtAck(int featureId, byte[] data)
IsVectorSupportEnabled = true;
break;
}
case TdsEnums.FEATUREEXT_USERAGENT:
{
// TODO: Verify that the server sends an acknowledgment (Ack)
// using this log message in the future.

// This Ack from the server is unexpected and is ignored completely.
// According to the TDS specification, an Ack is not defined/expected
// for this scenario. We handle it only for completeness
// and to support testing.
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 @@ -241,8 +241,7 @@ public enum EnvChangeType : byte
public const byte FEATUREEXT_SQLDNSCACHING = 0x0B;
public const byte FEATUREEXT_JSONSUPPORT = 0x0D;
public const byte FEATUREEXT_VECTORSUPPORT = 0x0E;
// TODO: re-verify if this byte competes with another feature
public const byte FEATUREEXT_USERAGENT = 0x0F;
public const byte FEATUREEXT_USERAGENT = 0x10;

[Flags]
public enum FeatureExtension : uint
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 @@ -192,7 +193,19 @@ internal void TdsLogin(

int feOffset = length;
// calculate and reserve the required bytes for the featureEx
length = ApplyFeatureExData(requestedFeatures, recoverySessionData, fedAuthFeatureExtensionData, useFeatureExt, 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.
length = ApplyFeatureExData(
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 @@ -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 @@ -9235,7 +9237,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 @@ -9254,6 +9264,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 All @@ -9262,6 +9273,11 @@ private int ApplyFeatureExData(TdsEnums.FeatureExtension requestedFeatures,
{
checked
{
// NOTE: As part of TDS spec UserAgent feature extension should be the first feature extension in the list.
if ((requestedFeatures & TdsEnums.FeatureExtension.UserAgent) != 0)
{
length += WriteUserAgentFeatureRequest(userAgentJsonPayload, write);
}
if ((requestedFeatures & TdsEnums.FeatureExtension.SessionRecovery) != 0)
{
length += WriteSessionRecoveryFeatureRequest(recoverySessionData, write);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -828,5 +828,81 @@ public void TestConnWithVectorFeatExtVersionNegotiation(bool expectedConnectionR
Assert.Throws<InvalidOperationException>(() => connection.Open());
}
}

// Test to verify that the client sends a UserAgent version
// and driver behaves correctly even if server sent an Ack
[Theory]
[InlineData(false)] // We do not force test server to send an Ack
[InlineData(true)] // Server is forced to send an Ack
public void TestConnWithUserAgentFeatureExtension(bool forceAck)
{
using var server = new TdsServer();
server.Start();

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

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

bool loginFound = 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 tdsFeatureExt = loginToken.FeatureExt
.OfType<TDSLogin7GenericOptionToken>().ToArray();
var token = tdsFeatureExt?.FirstOrDefault(t => t.FeatureID == TDSFeatureID.UserAgentSupport);

Assert.Equal(TDSFeatureID.UserAgentSupport, tdsFeatureExt?[0].FeatureID);
Assert.NotNull(token);
var data = token.Data;
Assert.True(data.Length >= 2);

observedVersion = data[0];
observedJsonBytes = data.AsSpan(1).ToArray();
loginFound = true;
};

// Connect to the test TDS server.
var connStr = new SqlConnectionStringBuilder
{
DataSource = $"localhost,{server.EndPoint.Port}",
Encrypt = SqlConnectionEncryptOption.Optional,
}.ConnectionString;

// TODO: Confirm the server sent an Ack by reading log message from SqlInternalConnectionTds
using var connection = new SqlConnection(connStr);
connection.Open();

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

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

// 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.NotNull(userAgentInfoType);

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

ReadOnlyMemory<byte> cachedPayload = (ReadOnlyMemory<byte>)prop.GetValue(null)!;
Assert.Equal(cachedPayload.ToArray(), observedJsonBytes.ToArray());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,10 @@ public interface ITDSServerSession
/// Indicates whether the client supports Vector column type
/// </summary>
bool IsVectorSupportEnabled { get; set; }

/// <summary>
/// Indicates whether the client supports UserAgent
/// </summary>
bool IsUserAgentSupportEnabled { get; set; }
}
}
Loading
Loading