smsohan / MvcMailer

A Mailer for ASP.Net MVC that forms the Email Body using MVC Views (Razor etc.) following Ruby on Rails ActionMailer style
MIT License
584 stars 178 forks source link

godaddy - Security Exception when embedded image inlined #25

Open m-tretyak opened 12 years ago

m-tretyak commented 12 years ago

I've got this error on godaddy hosting, only. It works correct on my internal server. I've created simple template with three images included as described on https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide

But it seems that Godaddy has strong security restriction and this call

Mvc.Mailer.LinkedResourceProvider.GetContentType(String fileName) +53

wan't work…

Security Exception
Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.RegistryPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[SecurityException: Request for the permission of type 'System.Security.Permissions.RegistryPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
   System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
   System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark) +31
   System.Security.CodeAccessPermission.Demand() +46
   Microsoft.Win32.RegistryKey.CheckPermission(RegistryInternalCheck check, String item, Boolean subKeyWritable, RegistryKeyPermissionCheck subKeyCheck) +9629642
   Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable) +70
   Microsoft.Win32.RegistryKey.OpenSubKey(String name) +6
   Mvc.Mailer.LinkedResourceProvider.GetContentType(String fileName) +53
   Mvc.Mailer.LinkedResourceProvider.Get(String contentId, String filePath) +34
   Mvc.Mailer.LinkedResourceProvider.GetAll(Dictionary`2 resources) +168
   Mvc.Mailer.MailerBase.PopulateLinkedResources(AlternateView mailPart, Dictionary`2 resources) +101
   Mvc.Mailer.MailerBase.PopulateHtmlPart(MailMessage mailMessage, String viewName, String masterName, Dictionary`2 linkedResources) +48
   Mvc.Mailer.MailerBase.PopulateBody(MailMessage mailMessage, String viewName, String masterName, Dictionary`2 linkedResources) +160
   Mvc.Mailer.MailerBase.PopulateBody(MailMessage mailMessage, String viewName, Dictionary`2 linkedResources) +18
   Navtran.Web.WebSite.Mailers.UserMailer.UserInvitation(UserInvitation userInvitation) in UserMailer.cs:46
   WebSite.Controllers.UserAccountController.SendInvitation(UserInvitation userInvitation) in UserAccountController.cs:71
   lambda_method(Closure , ControllerBase , Object[] ) +86
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +188
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +56
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +267
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +20
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +190
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +329
   System.Web.Mvc.Controller.ExecuteCore() +115
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +94
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +31
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +23
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +59
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8897857
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1 
smsohan commented 12 years ago

Looks like you hit an edge case. Didn't see this error before. One quick workaround: You can plugin an implementation of the LinkedResourceProvider that doesn't use this line. This line is used to set the content type for the linked resource.

On Tue, Sep 13, 2011 at 12:20 PM, m-tretyak < reply@reply.github.com>wrote:

I've got this error on godaddy hosting, only. It works correct on my internal server. I've created simple template with three images included as described on https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide

But it seems that Godaddy has strong security restriction and this call

Mvc.Mailer.LinkedResourceProvider.GetContentType(String fileName) +53

wan't work

Security Exception
Description: The application attempted to perform an operation not allowed
by the security policy.  To grant this application the required permission
please contact your system administrator or change the application's trust
level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the
permission of type 'System.Security.Permissions.RegistryPermission,
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
failed.

Source Error:

An unhandled exception was generated during the execution of the current
web request. Information regarding the origin and location of the exception
can be identified using the exception stack trace below.

Stack Trace:

[SecurityException: Request for the permission of type
'System.Security.Permissions.RegistryPermission, mscorlib, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
  System.Security.CodeAccessSecurityEngine.Check(Object demand,
StackCrawlMark& stackMark, Boolean isPermSet) +0
  System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap,
StackCrawlMark& stackMark) +31
  System.Security.CodeAccessPermission.Demand() +46
  Microsoft.Win32.RegistryKey.CheckPermission(RegistryInternalCheck check,
String item, Boolean subKeyWritable, RegistryKeyPermissionCheck subKeyCheck)
+9629642
  Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable) +70
  Microsoft.Win32.RegistryKey.OpenSubKey(String name) +6
  Mvc.Mailer.LinkedResourceProvider.GetContentType(String fileName) +53
  Mvc.Mailer.LinkedResourceProvider.Get(String contentId, String filePath)
+34
  Mvc.Mailer.LinkedResourceProvider.GetAll(Dictionary`2 resources) +168
  Mvc.Mailer.MailerBase.PopulateLinkedResources(AlternateView mailPart,
Dictionary`2 resources) +101
  Mvc.Mailer.MailerBase.PopulateHtmlPart(MailMessage mailMessage, String
viewName, String masterName, Dictionary`2 linkedResources) +48
  Mvc.Mailer.MailerBase.PopulateBody(MailMessage mailMessage, String
viewName, String masterName, Dictionary`2 linkedResources) +160
  Mvc.Mailer.MailerBase.PopulateBody(MailMessage mailMessage, String
viewName, Dictionary`2 linkedResources) +18
  Navtran.Web.WebSite.Mailers.UserMailer.UserInvitation(UserInvitation
userInvitation) in UserMailer.cs:46
  WebSite.Controllers.UserAccountController.SendInvitation(UserInvitation
userInvitation) in UserAccountController.cs:71
  lambda_method(Closure , ControllerBase , Object[] ) +86
  System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,
Object[] parameters) +17
  System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext
controllerContext, IDictionary`2 parameters) +188

System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext
controllerContext, ActionDescriptor actionDescriptor, IDictionary`2
parameters) +27
  System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
+56

System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter
filter, ActionExecutingContext preContext, Func`1 continuation) +267
  System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14()
+20

System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext
controllerContext, IList`1 filters, ActionDescriptor actionDescriptor,
IDictionary`2 parameters) +190
  System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext
controllerContext, String actionName) +329
  System.Web.Mvc.Controller.ExecuteCore() +115
  System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +94

System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext
requestContext) +10
  System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
  System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21

System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult
_) +12
  System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
  System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +31
  System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
  System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +23
  System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +59

System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult
result) +9

System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
+8897857
  System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&
completedSynchronously) +184

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NETVersion:4.0.30319.1

Reply to this email directly or view it on GitHub: https://github.com/smsohan/MvcMailer/issues/25

m-tretyak commented 12 years ago

Thank you, Sir! :) I've done something like this. If you would so kind to include this (like a patch?)

        public virtual ContentType GetContentType(string fileName)
        {
            var ext = (Path.GetExtension(fileName) ?? "*").ToLowerInvariant();
            var contentType = string.Empty;
            try
            {
                //possible security exception here
                var regKey = Registry.ClassesRoot.OpenSubKey(ext);
                if (regKey != null)
                {
                    var obj = regKey.GetValue("Content Type");
                    if (obj != null) contentType = obj.ToString();
                }
            }
            catch
            {
                contentType = string.Empty;
            }

            if (string.IsNullOrEmpty(contentType))
            {
                switch (ext)
                {
                    case ".png":
                        contentType = "image/png";
                        break;
                    case ".bmp":
                        contentType = "image/bmp";
                        break;
                    case ".exe":
                        contentType = "application/x-msdownload";
                        break;
                    case ".htm":
                    case ".html":
                        contentType = "text/html";
                        break;
                    case ".txt":
                        contentType = "text/plain";
                        break;
                    default:
                        contentType = "application/octet-stream";
                        break;
                }
            }

            return new ContentType(contentType);
        }