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

NoScriptAccess for constructor #444

Closed
CrosRoad95 opened this issue Nov 13, 2022 · 4 comments
Closed

NoScriptAccess for constructor #444

CrosRoad95 opened this issue Nov 13, 2022 · 4 comments
Assignees

Comments

@CrosRoad95
Copy link

Hello again! i would like to disallow user to use class constrator and force to use method, how can i do this?
NoScriptAccess is not allowed for constructor

public class Foo{
  public Foo() {} // new Foo() should not be allowed
}

public class Bar{
  Foo CreateFoo(){ return new Foo(); }
}
new Foo() // throw exception
let bar = new Bar()
let foo = bar.CreateFoo() // ok
@ClearScriptLib
Copy link
Collaborator

Hi @CrosRoad95,

NoScriptAccess is not allowed for constructor

That's an oversight, and we'll fix it in the next release. Thanks for reporting it!

Regarding your code sample above, the JavaScript syntax new Foo() only works if you've exposed Foo via AddHostType or ToHostType. If you simply don't do that, everything will work as you expect. Script code will still have full access to objects returned by CreateFoo, but it won't be able to instantiate Foo via the new operator.

If you must expose Foo but wish to make its constructor inaccessible, you can apply NoScriptAccessAttribute via a custom attribute loader. For example:

public class MyAttributeLoader : CustomAttributeLoader {
    public override T[] LoadCustomAttributes<T>(ICustomAttributeProvider resource, bool inherit) {
        if (typeof(ScriptUsageAttribute).IsAssignableFrom(typeof(T)) && typeof(Foo).GetConstructors().Contains(resource)) {
            return new[] { new NoScriptAccessAttribute() } as T[];
        }
        return base.LoadCustomAttributes<T>(resource, inherit);
    }
}

Good luck!

@ClearScriptLib ClearScriptLib self-assigned this Nov 14, 2022
ClearScriptLib added a commit that referenced this issue Dec 20, 2022
…disabled (GitHub Issue #463); added DocumentFlags.AwaitDebuggerAndPause (GitHub Discussion #452); added AttributeTargets.Constructor to ScriptUsageAttribute and NoScriptAccessAttribute (GitHub Issue #444); updated API documentation. Tested with V8 10.8.168.24.
@ClearScriptLib
Copy link
Collaborator

Fixed in Version 7.3.6.

@handerss-spotfire
Copy link

handerss-spotfire commented Apr 24, 2023

Using CustomAttributeLoader to hide the constructor does not seem to work for all types. If I add Guid or DateTime for example and then try to disallow their constructors using LoadCustomAttributes in the same way as described in #444 (comment) I'm still able to use new Guid() in my script.

EDIT: This seems to only be true for the parameter-less constructor. Blocking for example Guid.ctor(System.String) works.

@ClearScriptLib
Copy link
Collaborator

Hi @handerss-tibco,

This seems to only be true for the parameter-less constructor. Blocking for example Guid.ctor(System.String) works.

.NET value types don't have default constructors. C# allows you to use new without arguments to create a struct, but that's just syntax, and ClearScript expressly enables the same syntax for JavaScript. The fact is, however, that there's no actual, reflection-accessible constructor there to which one could assign custom attributes.

UPDATE: It appears that default constructors for value types were added in .NET 6 and C# 10. ClearScript will support this scenario in the next release.

Thanks!

ClearScriptLib added a commit that referenced this issue Jun 1, 2023
…tors (mentioned in GitHub Issue #444); fixed COM-related memory leak on .NET Framework (GitHub Issue #510); enabled multidimensional array manipulation via VBScript indexing syntax (GitHub Issue #511); improved stability on Apple Silicon devices. Tested with V8 11.4.183.17.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants