Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle InvalidOperationException in StringsHelper.CleanUri() #2373 #2374

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 12 additions & 5 deletions src/Agent/NewRelic/Agent/Parsing/StringsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ public static string CleanUri(Uri uri)
if (!uri.IsAbsoluteUri)
return CleanUri(uri.ToString());

return uri.GetComponents(
UriComponents.Scheme |
UriComponents.HostAndPort |
UriComponents.Path,
UriFormat.UriEscaped);
try
tippmar-nr marked this conversation as resolved.
Show resolved Hide resolved
{
return uri.GetComponents(
UriComponents.Scheme |
UriComponents.HostAndPort |
UriComponents.Path,
UriFormat.UriEscaped);
}
catch (InvalidOperationException) // can throw in .NET 6+ if the uri was created with UriCreationOptions.DangerousDisablePathAndQueryCanonicalization = true
{
return CleanUri(uri.ToString());
}
}

public static string FixDatabaseObjectName(string s)
Expand Down
6 changes: 4 additions & 2 deletions tests/Agent/UnitTests/ParsingTests/ParsingTests.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
<PropertyGroup>
<TargetFrameworks>net462</TargetFrameworks>
<TargetFrameworks>net462;net8.0</TargetFrameworks>
tippmar-nr marked this conversation as resolved.
Show resolved Hide resolved
<RootNamespace>ParsingTests</RootNamespace>
<AssemblyName>ParsingTests</AssemblyName>
<TargetPlatformIdentifier>windows</TargetPlatformIdentifier>
<DebugType Condition="'$(TargetFramework)' != '' AND '$(TargetFramework)' != 'netcoreapp1.0'">Full</DebugType>
<RunSettingsFilePath>$(SolutionDir)test.runsettings</RunSettingsFilePath>
</PropertyGroup>
Expand All @@ -22,8 +23,9 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="JustMock" Version="2023.3.1122.188" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
</ItemGroup>
<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Data.OracleClient" />
Expand Down
7 changes: 6 additions & 1 deletion tests/Agent/UnitTests/ParsingTests/SqlParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,7 @@ public void SqlParserTest_FixParameterizedSql_CorrectlyParsesParameters_Object_A
});
}

#if NETFRAMEWORK
[TestCaseSource(nameof(BinaryTestDatas))]
public void SqlParserTest_FixParameterizedSql_DoesNotParse_Binary(string originalSql, string expectedSql, string sqlParameterName, object sqlParameterValue)
{
Expand All @@ -834,6 +835,7 @@ public void SqlParserTest_FixParameterizedSql_DoesNotParse_Binary(string origina
Assert.That(shouldGeneratePlan, Is.False, "FixParameterizedSql should return false if it is not parsing a statement");
});
}
#endif

[TestCaseSource(nameof(CustomObjectTestDatas))]
public void SqlParserTest_FixParameterizedSql_DoesNotParse_CustomObject(string originalSql, string expectedSql, string sqlParameterName, object sqlParameterValue)
Expand All @@ -856,6 +858,7 @@ public void SqlParserTest_FixParameterizedSql_DoesNotParse_CustomObject(string o
});
}

#if NETFRAMEWORK
public static IEnumerable<TestCaseData> BinaryTestDatas
{
get
Expand All @@ -874,6 +877,7 @@ public static IEnumerable<TestCaseData> BinaryTestDatas
ObjectToByteArray(new List<bool> { true, false }));
}
}
#endif

public static IEnumerable<TestCaseData> CustomObjectTestDatas
{
Expand Down Expand Up @@ -911,7 +915,7 @@ private string RandomString(int size)
}
return new string(buffer);
}

#if NETFRAMEWORK
private static byte[] ObjectToByteArray(object obj)
{
using (var ms = new MemoryStream())
Expand All @@ -921,5 +925,6 @@ private static byte[] ObjectToByteArray(object obj)
return ms.ToArray();
}
}
#endif
}
}
9 changes: 6 additions & 3 deletions tests/Agent/UnitTests/ParsingTests/SqlWrapperHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

using System;
using System.Data;
#if NETFRAMEWORK
using System.Data.OleDb;
using System.Data.OracleClient;
#endif
using System.Data.SqlClient;
using NewRelic.Agent.Extensions.Providers.Wrapper;
using NewRelic.Parsing;
Expand All @@ -17,6 +19,7 @@ public class SqlWrapperHelperTests
{
#region GetVendorName

#if NETFRAMEWORK
[Test]
[TestCase("SQL Server", ExpectedResult = DatastoreVendor.MSSQL)]
[TestCase("MySql", ExpectedResult = DatastoreVendor.MySQL)]
Expand All @@ -33,7 +36,7 @@ public DatastoreVendor GetVendorName_ReturnsCorrectHost_IfOleDbConnectionProvide

return SqlWrapperHelper.GetVendorName(command);
}

#endif
[Test]
[TestCase("SqlCommand", ExpectedResult = DatastoreVendor.MSSQL)]
[TestCase("MySqlCommand", ExpectedResult = DatastoreVendor.MySQL)]
Expand All @@ -56,7 +59,7 @@ public void GetVendorName_ReturnsSqlServer_IfTypeNameIsNotProvidedAndCommandIsSq

Assert.That(datastoreName, Is.EqualTo(DatastoreVendor.MSSQL));
}

#if NETFRAMEWORK
[Test]
public void GetVendorName_ReturnsOracle_IfTypeNameIsNotProvidedAndCommandIsOracleCommand()
{
Expand All @@ -68,7 +71,7 @@ public void GetVendorName_ReturnsOracle_IfTypeNameIsNotProvidedAndCommandIsOracl

Assert.That(datastoreName, Is.EqualTo(DatastoreVendor.Oracle));
}

#endif
[Test]
public void GetVendorName_ReturnsUnknown_IfCommandIsOfUnknownType()
{
Expand Down
15 changes: 15 additions & 0 deletions tests/Agent/UnitTests/ParsingTests/StringsHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,20 @@ public void validate_CleanUri_String_Version(string uri, string expected)
var actual = StringsHelper.CleanUri(uri);
Assert.That(actual, Is.EqualTo(expected));
}

#if NET6_0_OR_GREATER
[Test]
public void validate_CleanUri_handles_invalidoperationexception()
{
var options = new UriCreationOptions
{
DangerousDisablePathAndQueryCanonicalization = true // only avaialable in .NET 6+
};
var uri = new Uri("http://www.example.com:8080/dir/?query=test", options);

var actual = StringsHelper.CleanUri(uri);
Assert.That(actual, Is.EqualTo("http://www.example.com:8080/dir/"));
}
#endif
}
}
Loading