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

HtmlWeb.GetExtensionForContentType throws an exception on .NET Framework 4.5.2 #294

Closed
alex-jitbit opened this issue Apr 25, 2019 · 5 comments
Assignees

Comments

@alex-jitbit
Copy link

Exception

Exception message:

System.MethodAccessException: Attempt by security transparent method 'HtmlAgilityPack.PermissionHelper.GetIsRegistryAvailable()' to access security critical method 'System.AppDomain.get_PermissionSet()' failed.

Assembly 'HtmlAgilityPack, Version=1.11.0.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a' 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 HtmlAgilityPack.PermissionHelper.GetIsRegistryAvailable() in C:\Users\Jonathan\source\repos\HtmlAgilityPack\HtmlAgilityPack.Shared\HtmlWeb.cs:line 2570
   at HtmlAgilityPack.HtmlWeb.GetExtensionForContentType(String contentType, String def) in C:\Users\Jonathan\source\repos\HtmlAgilityPack\HtmlAgilityPack.Shared\HtmlWeb.cs:line 993

@alex-jitbit alex-jitbit changed the title HtmlWeb.GetExtensionForContentType thorwc n exception on .NET Framework 4.5.2 HtmlWeb.GetExtensionForContentType throws an exception on .NET Framework 4.5.2 Apr 25, 2019
@JonathanMagnan JonathanMagnan self-assigned this Apr 25, 2019
@JonathanMagnan
Copy link
Member

JonathanMagnan commented Apr 25, 2019

Hello @alex-jitbit ,

A runnable project/solution or Fiddle is now required if you would like we look at it.

We now always ask for a project sample even if the issue is easy to reproduce. As a free product, we must find some way to save time to offer an overall better experience for everyone and release fixes faster.

You can send it to: info@zzzprojects.com if you need to keep the source private
Best Regards,

Jonathan


Performance Libraries
context.BulkInsert(list, options => options.BatchSize = 1000);
Entity Framework ExtensionsEntity Framework ClassicBulk OperationsDapper Plus

Runtime Evaluation
Eval.Execute("x + y", new {x = 1, y = 2}); // return 3
C# Eval FunctionSQL Eval Function

@alex-jitbit
Copy link
Author

@JonathanMagnan here's a dotnetfiddle

https://dotnetfiddle.net/qjSYgD

@JonathanMagnan
Copy link
Member

Hello @alex-jitbit ,

Thank you, my developer looked at it but there is not so much we can do.

This method requires additional permission to check in the key registry available mime type:

var helper = new PermissionHelper();
if (!helper.GetIsRegistryAvailable())
{
//if (MimeTypes.ContainsValue(contentType))
//{
// foreach (KeyValuePair<string, string> pair in MimeTypes)
// if (pair.Value == contentType)
// return pair.Value;
//}
return def;
}
if (helper.GetIsRegistryAvailable())
{
try
{
RegistryKey reg = Registry.ClassesRoot;
reg = reg.OpenSubKey(@"MIME\Database\Content Type\" + contentType, false);
if (reg != null) ext = (string) reg.GetValue("Extension", def);
}
catch (Exception)
{
ext = def;
}
}

Honestly, I'm not really sure why it has been done like that as using registry key is mostly always a bad idea for a third party library.

What we could do on our side is doing a try/catch and in the catch using a dictionary of all common mime know mime type.

Is it something that will work for your scenario?

@alex-jitbit
Copy link
Author

I'm not heavily dependent on this bug, just something I stumbled upon.

You can simply steal code from MS https://github.com/Microsoft/referencesource/blob/master/System.Web/MimeMapping.cs

Or even simply call their method (not sure its good idea since it would add System.Web dependency

@JonathanMagnan
Copy link
Member

Hello @VBWebprofi ,

The v1.11.4 has been released.

Could you try it and let me know if everything is now working as expected?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants