Locking Down Custom SharePoint Assemblies

Regardless of its a WebPart, or a client application that is going to integrate with SharePoint, for the love of all that is great and holy, lock down your custom developed assemblies, and don’t blindly give permission to things that probably shouldn’t have elevated permission sets.

The best way to do this is to start from scratch, and pretty much work your way up. So, you want to begin with this:

  1. [assembly: PermissionSet(SecurityAction.RequestOptional, Name="Execution")]

Then, build up your assemblies with the relevant permission sets. You will have to use RequestOptional in this example as using RequestMinimal will not have the appropriate effect that we are looking to achieve.

Whatever you specify in the “Execution” Permmision set will obviously take effect here. Then you can just use RequestOptional following to include any permissions you need.

Let’s take a brief example of where this might be a good idea.

Let’s say you have a helper method that is meant to read a file, and using a string builder, build that file into a string (I know, this is pretty simple, but it is for an example). For example, I use this below method to work with my SharePoint template names in my current environment for reporting:

  1. public string pushFileIntoString(string filePath)
  2. {
  3. FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  4. StringBuilder builder = new StringBuilder(100);
  5. byte[] buffer = new byte[0x400];
  6. for (int i = stream.Read(buffer, 0, buffer.Length); i > 0; i = stream.Read(buffer, 0, buffer.Length))
  7. {
  8. builder.Append(Encoding.UTF8.GetString(buffer, 0, i));
  9. }
  10. stream.Close();
  11. return builder.ToString();
  12. }

This would of course be using some File I/O operations. Therefore, you would have to add:

  1. [assembly: FileIOPermission(SecurityAction.RequestOptional, Name="Execution")]

And then just work your way through. Writing secure code is important, more important than functionality, so take it into consideration while you are doing your development.

share save 171 16 Locking Down Custom SharePoint Assemblies

No Comments

Trackbacks/Pingbacks

  1. Links (7/12/2007) « Steve Pietrek’s SharePoint Stuff - [...] Locking Down Custom SharePoint Assemblies [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>