Skip to content

Instantly share code, notes, and snippets.

@hiredman
Created December 16, 2008 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiredman/36217 to your computer and use it in GitHub Desktop.
Save hiredman/36217 to your computer and use it in GitHub Desktop.
;; need a java policy file that allows all or enabling the security manager will
;; lock you out
;;
;; grant {
;; permission java.security.AllPermission;
;; };
(System/setSecurityManager (SecurityManager.))
(defn sandbox [func]
(let [perms (java.security.Permissions.)
domain (java.security.ProtectionDomain.
(java.security.CodeSource. nil
(cast java.security.cert.Certificate nil))
perms)
context (java.security.AccessControlContext. (into-array [domain]))
pA (proxy [java.security.PrivilegedAction] [] (run [] (func)))]
(java.security.AccessController/doPrivileged
pA context)))
;; this fails
(sandbox
#(doto (-> "foo.bar" java.io.File. java.io.FileWriter.) (.write "foo") .close))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment