// Constructor
        /// <summary>
        /// Create a <see cref="ScriptType"/> from a <see cref="Type"/>.
        /// </summary>
        /// <param name="type">The <see cref="Type"/> to create the <see cref="ScriptType"/> from</param>
        public ScriptType(Type type)
        {
            this.assembly = null;
            this.rawType  = type;

            // Create member proxies
            fields    = new ScriptFieldProxy(true, this);
            properies = new ScriptPropertyProxy(true, this);
        }
        internal ScriptType(ScriptAssembly assembly, Type type)
        {
            this.assembly = assembly;
            this.rawType  = type;

            // Create member proxies
            fields    = new ScriptFieldProxy(true, this);
            properies = new ScriptPropertyProxy(true, this);
        }
        private ScriptAssembly RegisterAssembly(Assembly assembly, ScriptSecurityMode securityMode, CodeSecurityEngine securityEngine, bool isRuntimeCompiled, CompilationResult compileResult = null)
        {
            // Check for error
            if (assembly == null)
            {
                return(null);
            }

            // Reset report
            securityResult = null;

            // Create script assembly
            ScriptAssembly scriptAssembly = new ScriptAssembly(this, assembly, securityEngine, compileResult);

            // Check for ensure security mode
            bool performSecurityCheck = (securityMode == ScriptSecurityMode.EnsureSecurity);

            // Get value from settings
            if (securityMode == ScriptSecurityMode.UseSettings)
            {
                performSecurityCheck = RoslynCSharp.Settings.SecurityCheckCode;
            }

            // Check for security checks
            if (performSecurityCheck == true)
            {
                // Perform code validation
                if (scriptAssembly.SecurityCheckAssembly(RoslynCSharp.Settings.SecurityRestrictions, out securityResult) == false)
                {
                    // Log the error
                    RoslynCSharp.LogError(securityResult.GetSummaryText());
                    RoslynCSharp.LogError(securityResult.GetAllText(true));
                    // Dont load the assembly
                    return(null);
                }
                else
                {
                    RoslynCSharp.Log(securityResult.GetSummaryText());
                }
            }

            // Mark as runtime compiled
            if (isRuntimeCompiled == true)
            {
                scriptAssembly.MarkAsRuntimeCompiled();
            }

            lock (this)
            {
                // Register with domain
                this.loadedAssemblies.Add(scriptAssembly);
            }

            // Return result
            return(scriptAssembly);
        }
Пример #4
0
        /// <summary>
        /// Attempts to load a managed assembly from the specified raw bytes.
        /// Use <see cref="SecurityResult"/> to get the output from the code validation request.
        /// </summary>
        /// <param name="assemblyBytes">The raw data representing the file structure of the managed assembly, The result of <see cref="File.ReadAllBytes(string)"/> for example.</param>
        /// <returns>An instance of <see cref="ScriptAssembly"/> representing the loaded assembly or null if an error occurs</returns>
        /// <param name="securityMode">The security mode which determines whether code validation will run</param>
        public ScriptAssembly LoadAssembly(byte[] assemblyBytes, ScriptSecurityMode securityMode = ScriptSecurityMode.UseSettings)
        {
            // Check for disposed
            CheckDisposed();

            // Load the assembly
            Assembly assembly = sandbox.Load(assemblyBytes);

            // Create script assembly
            return(RegisterAssembly(ScriptAssembly.CreateAssembly(this, assembly, assemblyBytes), securityMode)); //RegisterAssembly(assembly, null, assemblyBytes, securityMode, false);
        }
Пример #5
0
        /// <summary>
        /// Attempts to load the specified managed assembly into the sandbox app domain.
        /// Use <see cref="SecurityResult"/> to get the output from the code validation request.
        /// </summary>
        /// <param name="name">The <see cref="AssemblyName"/> representing the assembly to load</param>
        /// <param name="securityMode">The security mode which determines whether code validation will run</param>
        /// <returns>An instance of <see cref="ScriptAssembly"/> representing the loaded assembly or null if an error occurs</returns>
        public ScriptAssembly LoadAssembly(AssemblyName name, ScriptSecurityMode securityMode = ScriptSecurityMode.UseSettings)
        {
            // Check for disposed
            CheckDisposed();

            // Load the assembly
            Assembly assembly = sandbox.Load(name);

            // Create script assembly
            return(RegisterAssembly(ScriptAssembly.CreateAssembly(this, assembly, assembly.Location), securityMode)); //RegisterAssembly(assembly, assembly.Location, null, securityMode, false);
        }
        // Methods
        /// <summary>
        /// Main entry point for async code.
        /// </summary>
        protected override void RunAsyncOperation()
        {
            ScriptAssembly result = null;

            lock (compileDomain)
            {
                switch (sourceCompileType)
                {
                case CompileType.CompileSource:
                {
                    if (sourceOrFiles.Length == 1)
                    {
                        // Compile the source
                        result = compileDomain.CompileAndLoadSource(sourceOrFiles[0], securityMode, additionalReferences);
                    }
                    else
                    {
                        // Compile the sources
                        result = compileDomain.CompileAndLoadSources(sourceOrFiles, securityMode, additionalReferences);
                    }
                    break;
                }

                case CompileType.CompileFile:
                {
                    if (sourceOrFiles.Length == 1)
                    {
                        // Compile the file
                        result = compileDomain.CompileAndLoadFile(sourceOrFiles[0], securityMode, additionalReferences);
                    }
                    else
                    {
                        // Compile the files
                        result = compileDomain.CompileAndLoadFiles(sourceOrFiles, securityMode, additionalReferences);
                    }
                    break;
                }
                }
                // Store result
                lock (assemblyAccessLock)
                {
                    compileResult = result;
                }

                // Set successful flag
                isSuccessful = compileDomain.CompileResult.Success;

                // Set verified flag
                isSecurityVerified = (compileDomain.SecurityResult != null)
                    ? compileDomain.SecurityResult.IsSecurityVerified
                    : false;
            }
        }
Пример #7
0
        public ScriptAssembly RegisterAssembly(ScriptAssembly scriptAssembly, ScriptSecurityMode securityMode)
        {
            // Check for error
            if (scriptAssembly == null)
            {
                return(null);
            }

            // Reset report
            securityResult = null;

            // Check for ensure security mode
            bool performSecurityCheck = (securityMode == ScriptSecurityMode.EnsureSecurity);

            // Get value from settings
            if (securityMode == ScriptSecurityMode.UseSettings)
            {
                performSecurityCheck = RoslynCSharp.Settings.SecurityCheckCode;
            }

            // Check for security checks
            if (performSecurityCheck == true)
            {
                CodeSecurityRestrictions restrictions = RoslynCSharp.Settings.SecurityRestrictions;

                // Use pinvoke option
                restrictions.AllowPInvoke = RoslynCSharp.Settings.AllowPInvoke;

                // Perform code validation
                if (scriptAssembly.SecurityCheckAssembly(restrictions, out securityResult) == false)
                {
                    // Log the error
                    RoslynCSharp.LogError(securityResult.GetSummaryText());
                    RoslynCSharp.LogError(securityResult.GetAllText(true));
                    // Dont load the assembly
                    return(null);
                }
                else
                {
                    RoslynCSharp.Log(securityResult.GetSummaryText());
                }
            }

            lock (this)
            {
                // Register with domain
                this.loadedAssemblies.Add(scriptAssembly);
            }

            // Return result
            return(scriptAssembly);
        }
        /// <summary>
        /// Compile and load the specified C# source file.
        /// Use <see cref="CompileResult"/> to get the output from the compile request.
        /// Use <see cref="SecurityResult"/> to get the output from the code validation request.
        /// This does the same as <see cref="CompileAndLoadFileAsync(string, ScriptSecurityMode)"/> but returns the main type of the <see cref="ScriptAssembly"/> for convenience.
        /// </summary>
        /// <param name="cSharpFile">The filepath to a file containing C# code</param>
        /// <param name="securityMode">The code validation used to verify the code</param>
        /// <returns>The main type of the compiled assembly or null if the compile failed, security validation failed or there was no main type</returns>
        public ScriptType CompileAndLoadMainFile(string cSharpFile, ScriptSecurityMode securityMode = ScriptSecurityMode.UseSettings, IMetadataReferenceProvider[] additionalReferenceAssemblies = null)
        {
            // Send compile request
            ScriptAssembly assembly = CompileAndLoadFile(cSharpFile, securityMode, additionalReferenceAssemblies);

            // Try to get main type
            if (assembly != null && assembly.MainType != null)
            {
                return(assembly.MainType);
            }

            return(null);
        }
Пример #9
0
        /// <summary>
        /// Attempts to load the specified managed assembly into the sandbox app domain.
        /// Use <see cref="SecurityResult"/> to get the output from the code validation request.
        /// </summary>
        /// <param name="fullPath">The full path the the .dll file</param>
        /// <param name="securityMode">The security mode which determines whether code validation will run</param>
        /// <returns>An instance of <see cref="ScriptAssembly"/> representing the loaded assembly or null if an error occurs</returns>
        public ScriptAssembly LoadAssembly(string fullPath, ScriptSecurityMode securityMode = ScriptSecurityMode.UseSettings)
        {
            // Check for disposed
            CheckDisposed();

            // Create an assembly name object
            AssemblyName name = AssemblyName.GetAssemblyName(fullPath);// new AssemblyName();
            //name.CodeBase = fullPath;

            // Load the assembly
            Assembly assembly = sandbox.Load(name);

            // Create script assembly
            return(RegisterAssembly(ScriptAssembly.CreateAssembly(this, assembly, fullPath), securityMode)); //RegisterAssembly<ScriptAssembly>(assembly, fullPath, null, securityMode, false);
        }
        /// <summary>
        /// Attempts to load a managed assembly from the raw assembly data.
        /// Any exceptions thrown while loading will be caught.
        /// </summary>
        /// <param name="data">The raw data representing the file structure of the managed assembly, The result of <see cref="File.ReadAllBytes(string)"/> for example.</param>
        /// <param name="result">An instance of <see cref="ScriptAssembly"/> representing the loaded assembly or null if the load failed</param>
        /// <param name="securityMode">The security mode which determines whether code validation will run</param>
        /// <returns>True if the assembly was loaded successfully or false if an error occured</returns>
        public bool TryLoadAssembly(byte[] data, out ScriptAssembly result, ScriptSecurityMode securityMode = ScriptSecurityMode.UseSettings)
        {
            // Even though this method is safe we cannot allow access to a disposed domain
            CheckDisposed();

            try
            {
                // Call through
                result = LoadAssembly(data, securityMode);
                return(true);
            }
            catch (Exception)
            {
                result = null;
                return(false);
            }
        }
Пример #11
0
        /// <summary>
        /// Compile and load the specified C# source files.
        /// Use <see cref="CompileResult"/> to get the output from the compile request.
        /// Use <see cref="SecurityResult"/> to get the output from the code validation request.
        /// </summary>
        /// <param name="cSharpFiles">An array of filepaths to C# source files</param>
        /// <param name="securityMode">The code validation used to verify the code</param>
        /// <returns>The compiled and loaded assembly or null if the compil or security verification failed</returns>
        public ScriptAssembly CompileAndLoadFiles(string[] cSharpFiles, ScriptSecurityMode securityMode = ScriptSecurityMode.UseSettings, IMetadataReferenceProvider[] additionalReferenceAssemblies = null)
        {
            // Make sure the compiler is initialized and the domain is valid
            CheckDisposed();
            CheckCompiler();

            lock (this)
            {
                // Compile from file
                compileResult = sharedCompiler.CompileFromFiles(cSharpFiles, additionalReferenceAssemblies);

                // Log to console
                LogCompilerOutputToConsole();

                // Security check
                return(RegisterAssembly(ScriptAssembly.CreateAssembly(this, CompileResult), securityMode)); //RegisterAssembly(compileResult.OutputAssembly, compileResult.OutputFile, compileResult.OutputAssemblyImage, securityMode, true, compileResult);
            }
        }
Пример #12
0
        public static ScriptAssembly CreateAssembly(ScriptDomain domain, Assembly rawAssembly, byte[] assemblyImage)
        {
            if (rawAssembly == null)
            {
                return(null);
            }

            ScriptAssembly asm = new ScriptAssembly()
            {
                domain           = domain,
                rawAssembly      = rawAssembly,
                rawAssemblyImage = assemblyImage,
            };

            // Build the assembly
            asm.BuildAssembly();

            return(asm);
        }
Пример #13
0
        public static ScriptAssembly CreateAssembly(ScriptDomain domain, Assembly rawAssembly, string assemblyPath)
        {
            if (rawAssembly == null)
            {
                return(null);
            }

            ScriptAssembly asm = new ScriptAssembly()
            {
                domain       = domain,
                rawAssembly  = rawAssembly,
                assemblyPath = assemblyPath,
            };

            // Build the assembly
            asm.BuildAssembly();

            return(asm);
        }
        // Methods
        /// <summary>
        /// Main entry point for async code.
        /// </summary>
        protected override void RunAsyncOperation()
        {
            ScriptAssembly result = null;

            lock (loadDomain)
            {
                switch (loadType)
                {
                case AssemblyLoadType.LoadByName:
                {
                    result = loadDomain.LoadAssembly(asmName, securityMode);
                    break;
                }

                case AssemblyLoadType.LoadByPath:
                {
                    result = loadDomain.LoadAssembly(asmPath, securityMode);
                    break;
                }

                case AssemblyLoadType.LoadFromBytes:
                {
                    result = loadDomain.LoadAssembly(asmBytes, securityMode);
                    break;
                }
                }
                // Store result
                lock (assemblyAccessLock)
                {
                    loadResult = result;
                }
            }

            // Set successful flag
            isSuccessful = result != null;

            // Set verified flag
            isSecurityVerified = (loadDomain.SecurityResult != null)
                ? loadDomain.SecurityResult.IsSecurityVerified
                : false;
        }
Пример #15
0
 protected internal virtual ScriptType BuildScriptType(ScriptAssembly owner, ScriptType parent, Type type)
 {
     return(new ScriptType(owner, parent, type));
 }