22// See License in the project root for license information.
33
44using System ;
5+ using System . Linq ;
56using Simpleflow . CodeGenerator ;
7+ using Simpleflow . Exceptions ;
68
79namespace Simpleflow . Services
810{
@@ -34,7 +36,10 @@ So here we don't need to run again */
3436
3537 if ( context . Internals . CompiledScript == null )
3638 {
37- context . Internals . CompiledScript = SimpleflowCompiler . Compile < TArg > ( context . Script , _activityRegister ) ;
39+ var eventPublisher = new ParserEventPublisher ( ) ;
40+ CheckFunctionExecutionPermissions ( context , eventPublisher ) ;
41+
42+ context . Internals . CompiledScript = SimpleflowCompiler . Compile < TArg > ( context . Script , _activityRegister , eventPublisher ) ;
3843
3944 context . Trace . Write ( "Compiled" ) ;
4045 }
@@ -45,5 +50,33 @@ So here we don't need to run again */
4550
4651 next ? . Invoke ( context ) ;
4752 }
53+
54+ private static void CheckFunctionExecutionPermissions < TArg > ( FlowContext < TArg > context , ParserEventPublisher eventPublisher )
55+ {
56+ eventPublisher . OnVisit = ( type , data ) =>
57+ {
58+ if ( type == EventType . VisitFunctionOnAvail
59+ && context . Options ? . DenyFunctions != null )
60+ {
61+ var functionName = data . ToString ( ) ;
62+ if ( context . Options . DenyFunctions . Contains ( functionName , StringComparer . OrdinalIgnoreCase ) )
63+ {
64+ throw new AccessDeniedException ( $ "Function '{ functionName } ' cannot be allowed to run in this context.") ;
65+ }
66+ }
67+
68+ if ( type == EventType . VisitFunctionOnAvail
69+ && context . Options ? . AllowFunctions != null )
70+ {
71+ var functionName = data . ToString ( ) ;
72+ if ( ! context . Options . AllowFunctions . Contains ( functionName , StringComparer . OrdinalIgnoreCase ) )
73+ {
74+ throw new AccessDeniedException ( $ "Function '{ functionName } ' cannot be allowed to run in this context.") ;
75+ }
76+ }
77+ } ;
78+ }
79+
80+
4881 }
4982}
0 commit comments