private static object GetExpressionValue(ExpressionAst expressionAst, System.Management.Automation.ExecutionContext context, SessionStateInternal sessionStateInternal, IList usingValues, ref Func<FunctionContext, object> lambda, ref IScriptExtent[] sequencePoints, ref Type localsTupleType) { object obj2; object obj4; if (IsConstantValueVisitor.IsConstant(expressionAst, out obj2, false, false)) { return obj2; } VariableExpressionAst varAst = expressionAst as VariableExpressionAst; if (varAst != null) { return VariableOps.GetVariableValue(varAst.VariablePath, context, varAst); } if (lambda == null) { lambda = new Compiler().CompileSingleExpression(expressionAst, out sequencePoints, out localsTupleType); } SessionStateInternal engineSessionState = context.EngineSessionState; try { if ((sessionStateInternal != null) && (context.EngineSessionState != sessionStateInternal)) { context.EngineSessionState = sessionStateInternal; } ArrayList resultList = new ArrayList(); Pipe pipe = new Pipe(resultList); try { FunctionContext arg = new FunctionContext { _sequencePoints = sequencePoints, _executionContext = context, _outputPipe = pipe, _localsTuple = MutableTuple.MakeTuple(localsTupleType, DottedLocalsNameIndexMap) }; if (usingValues != null) { PSBoundParametersDictionary dictionary = new PSBoundParametersDictionary { ImplicitUsingParameters = usingValues }; arg._localsTuple.SetAutomaticVariable(AutomaticVariable.PSBoundParameters, dictionary, context); } object obj3 = lambda(arg); if (obj3 == AutomationNull.Value) { return ((resultList.Count == 0) ? null : PipelineOps.PipelineResult(resultList)); } obj4 = obj3; } catch (TargetInvocationException exception) { throw exception.InnerException; } } catch (TerminateException) { throw; } catch (FlowControlException) { obj4 = null; } finally { context.EngineSessionState = engineSessionState; } return obj4; }
private static object GetExpressionValue(ExpressionAst expressionAst, bool isTrustedInput, ExecutionContext context, SessionStateInternal sessionStateInternal, IDictionary usingValues, ref Func<FunctionContext, object> lambda, ref IScriptExtent[] sequencePoints, ref Type localsTupleType) { object constantValue; if (IsConstantValueVisitor.IsConstant(expressionAst, out constantValue)) { return constantValue; } // If this isn't trusted input, then just return. if (!isTrustedInput) { return null; } // Can't be exposed to untrusted input - exposing private variable names / etc. could be // information disclosure. var variableAst = expressionAst as VariableExpressionAst; if (variableAst != null) { // We can avoid creating a lambda for the common case of a simple variable expression. return VariableOps.GetVariableValue(variableAst.VariablePath, context, variableAst); } // Can't be exposed to untrusted input - invoking arbitrary code could result in remote code // execution. if (lambda == null) { lambda = (new Compiler()).CompileSingleExpression(expressionAst, out sequencePoints, out localsTupleType); } SessionStateInternal oldSessionState = context.EngineSessionState; try { if (sessionStateInternal != null && context.EngineSessionState != sessionStateInternal) { // If we're running a function from a module, we need to evaluate the initializers in the // module context, not the callers context... context.EngineSessionState = sessionStateInternal; } var resultList = new List<object>(); var pipe = new Pipe(resultList); try { var functionContext = new FunctionContext { _sequencePoints = sequencePoints, _executionContext = context, _file = expressionAst.Extent.File, _outputPipe = pipe, _localsTuple = MutableTuple.MakeTuple(localsTupleType, DottedLocalsNameIndexMap) }; if (usingValues != null) { var boundParameters = new PSBoundParametersDictionary { ImplicitUsingParameters = usingValues }; functionContext._localsTuple.SetAutomaticVariable(AutomaticVariable.PSBoundParameters, boundParameters, context); } var result = lambda(functionContext); if (result == AutomationNull.Value) { return resultList.Count == 0 ? null : PipelineOps.PipelineResult(resultList); } return result; } catch (TargetInvocationException tie) { throw tie.InnerException; } } catch (TerminateException) { // the debugger is terminating the execution; bubble up the exception throw; } catch (FlowControlException) { // ignore break, continue and return exceptions return null; } finally { context.EngineSessionState = oldSessionState; } }