Custom IExtensionFolders for dev purpose -> SecurityException during Directory.GetDirectories call

Topics: Writing modules
Apr 11, 2011 at 10:34 AM


I'm developing a suite of orchard modules; it will be really useful store my module projects in a different location than Orchard.Web\Modules project folder and take advantage from Orchard Dynamic Loading Modules feature also for external to virtual directory folders.

In order to satisfy my need, I wrote a custom IExtensionFolder class (named ExternalExtensionFolder) which accepts as constructor parameter a comma separated paths used to load ExtensionDescriptors of custom modules during AvailableExtensions methods call. In the web.config file of Orchard website I declared an autofact section and map ExternalExtensionFolders type to IExtensionFolders type with my custom path as parameter.

Orchard load correctly my custom IExtensionFolders class, but when method Directories.GetDirectory(custompath, "*.*", SearchOption.AllDirectories) was call from my AvailableExtensions implementation, a SecurityException was raised. I tried grant my custom folder with all read/write for Everyone but nothing changed. Obviously if my custom path reference an path inside virtual directory, metod call successed.

If call method Directories.GetDirectory(custompath, "*.*", SearchOption.AllDirectories)  is made in Application_Start method of a new simple MVC3 projects, no excepion was raised. 

There are some specific restrictions made by Orchard in order to avoid access to file system except virtual directories paths? If yes, how I can disable this restriction?

Thanks for your support.



Apr 11, 2011 at 11:15 AM

This would be useful to me as well for the media framework modules we're working on. I also saw another discussion on this topic.

I'm not sure why that SecurityException is happening but I'm wondering do you have any Web.config in your subdirectory under ~/Modules/?

However we probably need to look at all this a different way. Couple of factors to consider;

1. Since you need to modify the root Web.config to hook in your IExtensionFolders, this can't be easily distributed in a modular fashion (we don't want end users having to manually make changes in Web.config to support it)

2. Clearly an extension can't itself tell Orchard additional places to look for extensions. This would be some kind of paradox since the extension first has to be discovered before it can tell the system anything at all!

What I would propose is you look at making changes in Orchard.Core.Environment itself allowing support of "Suite" folders containing grouped modules in this fashion.

Perhaps such folders would contain a Suite.txt manifest similar to the Module.txt manifest; the suite manifest could contain additional details regarding the overall function of the suite, and its origin.

I'm pretty hopeful the dev team would appreciate the usefulness of such changes and (post-MIX of course) might well want to bring this functionality into the core framework if you're successful.

There are however some key implementation details you need to consider;

  • I note in ExtensionLoaderCoordinator, path monitoring is set up as follows:

            // Monitor add/remove of any module/theme

I'm not sure if this monitors all subdirectories and files or just the top-level module folders.

  • There are important factors to think about regarding Module Gallery download and updates. This could likely require some changes to gallery feeds themselves. But it would be a nice feature to be able to download and update a complete suite of packages all at once.

Obviously this has suddenly become quite a thorny issue to solve :)

In the mean time I can suggest a couple of fairly simple things that I'm currently doing to work around this issue;

1. Using NTFS soft links / directory junctions so you can work on your projects within a single source repository whilst exposing them correctly under the Modules folder of your Orchard enlistment. If you're not sure how to do this I can post a writeup and/or sample .cmd script, it was very easy once I'd worked it out and after nearly trashing a hard drive in the process :) ... No, really it's very safe, honestly ;)

2. Orchard 1.1 has a new feature called Recipes which allows you to batch download/install/enable any number of modules at once. So this gets around most user issues of having to install many different components, they can just download the Recipe (or perhaps several versions of the Recipe offering them a choice of typical installations) then run a command from the web interface or command line to do everything in one go. Hopefully this story will be made easier in the future (I'm hoping for something along the lines of Recipe execution directly from the Gallery).

Apr 11, 2011 at 2:30 PM

Thanks for your reply.

~/Modules/ folder and subfolder is same download from Orchard 1.0.20 source release; I think is not linked to SecurityException. 

I agree with you is not target solution modify web.config, but I need only during development cycle; for release environment I think is correct package modules and install it from admin section or download or from Gallery. An other scenario where you need modify web.config is adding inversion of control mapping: actually I'm working to integrate some functionality of NSK in new orchard modules, and I need to register some dependencies. One possibility is add dependencies via code, but I think should be exists some built-in functionality (a specific module config file?) to do this.

Module Suite could be a nice feature: after solved my security issue I try to work on your suggest.

Back to external virtual path reference topic, is it possible (optionally) add information in manifest module file, in order to inform Orchard where physically searching proj file or bin folder including extension? Manifest file should be always located in ~/Modules/ folder, but it's implementation (or binary files) could be reside in a different location: in this case any change to Orchard web.config is no longer need. I think in this scenario SecurityException could be raised on access Files of external folder linked by module.txt manifest.

Soft link is simple workaround: I will try as soon as possible.

Thanks for support.



Apr 11, 2011 at 3:10 PM

Ah yes I slightly misunderstood your situation. I think if you are trying to load projects outside of the web root that will always create a security exception unless you switch the application to Full Trust (in Web.config). So you could try that, but soft links will in any case solve the problem because I'm using them already without issue.

I'm not sure about IoC mapping; I take it you are referring to importing external dependencies that therefore cannot inherit from IDependency? One way might be to create your own thin wrappers around the NSK classes which you can then inject as normal with Autofac. Another approach could be to investigate the ICompositionStrategy interface in Orchard to see if you can supplement the composition; but it's something I just noticed and it's all a bit low-level so I'm not going to claim it'll do what you need :)

Modules have to reside in the same location as their Module.txt as far as I am aware.

Another option to mess with the file system is to use ICustomVirtualPathProvider to inject a VirtualPathProvider. But again this relies on actually loading your module before it can work.

So I think directory junctions are your answer since this is only for dev environment anyway.

Apr 13, 2011 at 1:20 PM

Thanks! I will take care your suggestion. 

Thanks for your support and have a nice MIX!


Apr 13, 2011 at 2:07 PM

Ah, I'm not at MIX, it's a bit far away :)

Anyway, glad to help...