SapientGuardian / mysql-connector-net-netstandard

ADO.NET driver for MySQL targeted against netstandard 1.3
GNU General Public License v2.0
46 stars 15 forks source link

Attempt by security transparent method to access security critical method #33

Closed Nick-Lucas closed 7 years ago

Nick-Lucas commented 7 years ago

I have a netstandard1.3 class library using SapientGuardian.MySQL which is consumed in a WPF .Net 4.6.1 application.

After upgrading to 6.9.815 I can no longer connect to MySQL with the below error.

After some digging it seems that .Net Standard and Core use Security Level 2 as standard, whereas .Net Framework uses Level 1.

It seems that the following commit has caused this regression: https://github.com/SapientGuardian/mysql-connector-net-netstandard/commit/95651617dea59470a155f0dfb9805a92154f2485

I have dropped down to 6.9.814 for now, since the difference is basically that one line of code.

'Attempt by security transparent method 'MySql.Data.MySqlClient.MySqlField..ctor(MySql.Data.MySqlClient.Driver)' to access security critical method 'System.Text.CodePagesEncodingProvider.get_Instance()' failed.

Assembly 'SapientGuardian.MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=null' is marked with the AllowPartiallyTrustedCallersAttribute, and uses the level 2 security transparency model.  Level 2 transparency causes all methods in AllowPartiallyTrustedCallers assemblies to become security transparent by default, which may be the cause of this exception.'

   at MySql.Data.MySqlClient.MySqlField..ctor(Driver driver)
   at MySql.Data.MySqlClient.Driver.GetColumns(Int32 count)
   at MySql.Data.MySqlClient.ResultSet.LoadColumns(Int32 numCols)
   at MySql.Data.MySqlClient.ResultSet..ctor(Driver d, Int32 statementId, Int32 numCols)
   at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
   at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
   at MySql.Data.MySqlClient.MySqlDataReader.Close()
   at MySql.Data.MySqlClient.MySqlCommand.ResetReader()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
   at MySql.Data.MySqlClient.Driver.LoadServerProperties(MySqlConnection connection)
   at MySql.Data.MySqlClient.Driver.Configure(MySqlConnection connection)
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at ReVal_GUI.Selectors.ConfigFactory.GetConnection() in B:\\Repositories\\DataAnalysisDesktop\\ReVal_GUI\\Selectors\\ConfigFactory.cs:line 15
   at ReVal_GUI.Selectors.AirfieldSelector.Window_Loaded(Object sender, RoutedEventArgs e) in B:\\Repositories\\DataAnalysisDesktop\\ReVal_GUI\\Selectors\\AirfieldSelector.xaml.cs:line 71
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
   at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
   at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
   at MS.Internal.LoadedOrUnloadedOperation.DoWork()
   at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
   at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)
   at System.Windows.Interop.HwndTarget.OnResize()
   at System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
SapientGuardian commented 7 years ago

This is probably a simple fix, much like #20. @kleberksms do you want to take a shot at it?

SapientGuardian commented 7 years ago

@Nick-Lucas would you mind checking if https://ci.appveyor.com/api/buildjobs/vrgpaoo3f4sg8537/artifacts/artifacts%2FSapientGuardian.MySql.Data%2FSapientGuardian.MySql.Data.6.9.816.nupkg resolves the issue for you?

Nick-Lucas commented 7 years ago

Sure, I will need to do this on Monday, as it's a work solution. Will set a reminder now :)

Nick-Lucas commented 7 years ago

Hey just wanted to say I haven't forgotten about this, I just need to get a few internal packages updated to run a test and should have some time tomorrow.

Nick-Lucas commented 7 years ago

@SapientGuardian

Yep this appears to be working. I can't 100% guarantee my packages environment was the same, but based on what I know of the problem and your fix, I'd say it's sorted.

Thanks!

SapientGuardian commented 7 years ago

Thanks for checking. 6.9.816 should publish to nuget soon with the fix.