This Mathematica notebook, © Copyright 2009 Scientific Arts, LLC, all rights reserved.

Security Tools

Posted on: Sunday, March 2, 2008 at 08:01 PM UT  Notebook Version

When creating a general system in Mathematica like A WorkLife FrameWork (http://scientificarts.com/worklife) I often need to process multiple notebooks to gather data from those notebooks to distill and present to the user. Often I open these notebooks with the option Visible->False so that the user doesn't see them opening and closing as the processing takes place.  But if those notebooks have dynamic content in them then, each time they are opened (even if invisible), a dialog will open asking if the user gives permission for dynamic content to be evaluated.  This is a very good feature--it is key to security in a system that allows automatic evaluation of dynamic content that can access the operating system.  Mathematica allows you to mark a directory as "Trusted" and, if so marked, opening notebooks from a trusted directory will not prompt the permission dialog.

The interface in Mathematica for adding a new directory to the list of trusted directories is not very user friendly at the moment (it involves knowing how to edit FrontEnd`FileName expressions) and so I have created a function that does this in a way that is more intuitive in that it simply prompts you to select a directory from the standard file system Open dialog. 

In fact, in A WorkLife FrameWork, there are a very large number of features that make underlying functionality of Mathematica much more useful; hence the tagline for the product: "Extending Mathematica's Reach..."  You can download the fully functional trial version of A WorkLife FrameWork from http://scientificarts.com/worklife/trial.html

Here is the code that defines a function AddTrustedPath that presents the more useful interface to taking this action (note that, as it should, it still opens a dialog at the end that will ask for confirmation that you do indeed want to add the directory that you choose).  A set of functions like this (also ones for adding un-trusted paths, for example) are currently in the Development version of A WorkLife FrameWork:

AddTrustedPath::"nottrusted"="The directory `1` is currently contained in the list of Untrusted directories.  Please remove it from this list first before adding it to the list of Trusted directories.";

AddTrustedPath[]:=
Catch@Module[{securityOptions,dir,trustedPaths,unTrustedPaths,unTrustedPathsAsStrings,trustByDefault},

trustedPaths=CurrentValue[$FrontEnd,{"NotebookSecurityOptions","TrustedPath"}];

unTrustedPaths=CurrentValue[$FrontEnd,{"NotebookSecurityOptions","UntrustedPath"}];

unTrustedPathsAsStrings=ToFileName[{#}]&/@unTrustedPaths;

trustByDefault=CurrentValue[$FrontEnd,{"NotebookSecurityOptions","TrustByDefault"}];

dir=SystemDialogInput["Directory",$HomeDirectory,WindowTitle->"Select a Directory to Trust"];

If[dir===$Canceled,Throw[$Canceled]];

If[MemberQ[unTrustedPathsAsStrings,dir],Throw[Message[AddTrustedPath::nottrusted,dir];
(SetOptions[$FrontEnd,"PreferencesSettings"->{"Page"->"System"}];
FrontEndTokenExecute["PreferencesDialog"])]];

trustedPaths=Union@Flatten[{trustedPaths,FrontEnd`FileName[{#}]&[dir]}];

securityOptions={
"TrustedPath"->trustedPaths,
"UntrustedPath"->unTrustedPaths,"TrustByDefault"->trustByDefault};

SetOptions[$FrontEnd,"NotebookSecurityOptions"->securityOptions];

securityOptions
]

I hope that this is useful to folks!  Just remember that there are a huge number of things like this in A WorkLife FrameWork and more in development...  Give it a try: http://scientificarts.com/worklife/trial.html