Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Security? #29

Open
Apskaita5 opened this issue Jul 8, 2020 · 7 comments
Open

Security? #29

Apskaita5 opened this issue Jul 8, 2020 · 7 comments
Labels

Comments

@Apskaita5
Copy link

I can see from the examples that the expression can invoke native methods, like ToUpper. Is that safe to use when expressions are entered by a web application users?
Actually I'm looking for a simple math parser with conditional support ("if...else"), but all the solutions I found (yet) are using code generation in one way or another, which in my case is both overkill and security risk.

@VitaliyMF
Copy link
Contributor

In expression it is possible to call any public methods that are available in objects passed in the context. It is not possible to call static (or extension) methods in any way.

This means that you can control what is possible to call by providing evaluation context.

@Apskaita5
Copy link
Author

Apskaita5 commented Jul 8, 2020

User will not be able to declare a variable (e.g. File) within an expression?

@VitaliyMF
Copy link
Contributor

No, it is not possible to call smth like File.ReadAllText() directly.

@Apskaita5
Copy link
Author

I mean smth like "(new FileInfo("whatever")).DoWhatever()"?

@VitaliyMF
Copy link
Contributor

In expression user cannot create .NET object directly. "new" works for creation of an array or Dictionary.
If you need to give a possibility to create some objects this should be exposed explicitly with 'factory' method that is passed as a delegate to the evaluation context (like CreateMyObject() ).

@Apskaita5
Copy link
Author

Ok, last question :)
Is it possible to implement (custom?) variadic function, e.g. iff( cond-1, expr-1; ... ; cond-n, expr-n ) ?
I know I can use "?" but that's a bit hard/inconvenient for users non programmers.
Thx for your patience.

@VitaliyMF
Copy link
Contributor

Sure you can include any 'helper' functions into evaluation context. Smth like this:

varContext["IFF"] = (Func<bool, object, object,object>)((cnd, trueVal, falseVal) => {
  return cnd ? trueVal : falseVal;
});
Console.WriteLine(lambdaParser.Eval("IFF( 10 > 5,  \"Yes\", \"No\" )", varContext));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants