Comments (13)
@lofcz YantraJS currently isn't designed to run in a browser like sandboxing. The purpose was to create Node like environment to create native apps and server apps where scripts are trusted.
- Limiting CLR interop on a whitelist basis is in development.
- Limiting the number of instructions to be executed - this one I have no idea how to do it as we have no control over CLR, however whielisting CLR interop will give you enough control on what to expose and what not to.
- Memory is again not in our control, unless some CLR API provides this, abstracting memory and providing memory control will slow down every execution and will defeat the purpose of making faster engine. Process isolation can provide memory control and it is better suited for browser like execution.
- Limting recursion might be an easy thing to implement. However it is not in current scope as we are working for debugger and other runtime features.
- Limiting execution time is an important feature, but .Net core has removed ability to abort the thread, thus checking timeout on every loop will slow the execution. We are searching some easy or faster ways to do it.
We hope more features will come into YantraJS as it gets more popular and we get some help from open source community.
from yantra.
Point 1 was implemented as decorating [JSExport]
on members of a class derived from JavaScriptObject
.
https://github.com/yantrajs/yantra/blob/main/YantraJS.Core.Tests/ClrObjects/CustomObject.cs
(4) I haven't thought about it yet, but yes, it is not that difficult to implement, I have created a task for it and will try to do it over weekend.
(5) There is no way to Abort thread in .NET core, so this is kind of deadend. Unless we find some workaround, we have no way to track this. However you can use cancellation token and Task.Any
to find out which one finished first, but there is no way to stop execution. We are still constantly looking for alternative.
from yantra.
@lofcz Hi, Can you give an example of how you want to sandbox the execution?
Currently, if you disable CLR, and if you create your own class derived from JSObject
and your own JSFunction
instances in global context, scripts will not be able to access anything. JSContext
has no access to CLR, JSModuleContext
has ability to turn off CLR.
Once you provide the example, I will provide you source code to achieve it, or else I will add improvement in JSContext
/JSModuleContext
to support it.
from yantra.
@lofcz I have released new version with support of stack overflow. However, compared to V8, stack size available is only 1/8th, that is due to lot of extra calls placed in .net code generation, which will improve in future.
from yantra.
@ackava would you please update this issue if any of the points get implemented? We are still interested in YantraJS but sandboxing is a showstopper for us.
from yantra.
Thanks for the fast and kind reply, currently the CLR integration is on an enable/disable basis, for our use case, we need to enable CLR for a subset of classes in the calling assembly, essentially point (1) in your previous answer. This is the bare minimum needed for us. I imagine I'd provide an ICollection
or a similar interface of classes to enable CLR interop onto the JSModuleContext
, that would be the optimal solution for the end-user.
We are also interested in whether there is any new know-how on points (4) and (5) above, but these are nice to haves that would be most welcome but are not strictly necessary for the first version of sandboxing.
from yantra.
Thanks! As for (1), I wonder whether it would be possible to also implement the proposed way of whitelisting in my previous comment as we need to whitelist different classes based on the authentication of the end user. That shouldn't be that hard I imagine?
from yantra.
@lofcz That is not possible as the IL is generated only first time, and it runs as native code. IL remains same throughout the lifetime of process. And there are some upcoming optimizations which will break any conditional IL generation such as tail call optimization, reuse same IL if the code is same without any closures, cache results of methods and loop optimizations.
I would recommend using source generator to create proxy on top of what you want to expose which can enable/disable access based on authentication.
from yantra.
Thanks, I have only limited experience with source generators as they are fairly new feature to the language. Is this something simple? If it is, could you write a minimal example, please?
from yantra.
@lofcz No, I don't think source generator will help. Sorry for misdirecting towards it, you will have to perform checks before start of method if user has access or not as shown below.
class JSFile : JavaScriptObject {
[JSExport]
public JSFile WriteText(string text) {
ThrowIfAccessDenied(... );
}
}
from yantra.
Thanks, got it. Seems like a lot of boilerplate, the checks themselves should be just a dictionary lookup in our case. One more idea, I'm not familiar with YantraJS CLR interop code but isn't it by chance possible to implement something like:
when the .NET method is resolved in a CLR interop layer before we invoke it, we call another method provided say to JSModuleContext
as a Func<MethodInfo, bool>
(given this delegate is not null, else we skip this new logic). The method would essentially wrap the resolved method and if we return false
from the wrapper, we skip the inner method invocation.
Now if this is possible it's a costly way to do things but we could improve this approach and hopefully reduce it to a dictionary lookup.
from yantra.
@lofcz No it is not possible, because IL generator will have to do dictionary look up for every methods.
But it is still not correct way, because same method will be used for some different user's context and if it works for first user, it may not be accessible to second user. Whether user has access to resource or not should be checked in beginning of method, not when the method is compiled.
Even when you pass new instance of method, JavaScript class remains same, the function called is same.
from yantra.
Alright, understood. I very much appreciate the time you've given me. I'll look into how the boilerplate could be avoided, probably with weaving but that's outside of the scope of this project. Once again, thanks ❤️
from yantra.
Related Issues (20)
- .Net 6 "Object reference not set to an instance of an object" on import statement HOT 9
- How to pass ClrType to module for create instance in script HOT 13
- Fix LinqJS parsing error HOT 1
- Xamarin Error HOT 10
- .NET MAUI Crash when using version > 1.2.59 HOT 6
- AOT (Trimming) Exception. HOT 8
- Ability to disable a Camel case at the engine level HOT 4
- await keyword HOT 6
- ClrProxy documentation HOT 2
- Add EnumeratedResult structure HOT 1
- Bug: Template literals HOT 2
- Occurred NullReferenceException or AccessViolationException while running n-body HOT 7
- Add `using` `System.dispose`
- Feature Request: Limit execution stack
- Disable unnecessary prototype
- Problems running benchmarks (crashing, too slow) HOT 2
- Asses every `.Value` property of `StringSpan` HOT 1
- JSString should not contain StringSpan
- unexpected console output while using JSModuleContext HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yantra.