Skip to content

JS 'in' operator always return true for objects whose types have string indexers #104

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

Closed
5 tasks done
arekdygas opened this issue Mar 26, 2025 · 0 comments · Fixed by #105
Closed
5 tasks done

JS 'in' operator always return true for objects whose types have string indexers #104

arekdygas opened this issue Mar 26, 2025 · 0 comments · Fixed by #105
Labels

Comments

@arekdygas
Copy link
Contributor

Bug Report

Prerequisites

  • Can you reproduce the problem in a MWE?
  • Are you running the latest version of AngleSharp?
  • Did you check the FAQs to see if that helps you?
  • Are you reporting to the correct repository? (there are multiple AngleSharp libraries, e.g., AngleSharp.Css for CSS support)
  • Did you perform a search in the issues?

For more information, see the CONTRIBUTING guide.

Description

JS in operator always return true for objects whose types have string indexers.

Steps to Reproduce

var config = Configuration.Default.WithJs();
var context = BrowsingContext.New(config);
var document = await context.OpenAsync(req => req.Content("")).ConfigureAwait(false);
Console.WriteLine(document.ExecuteScript("'somethingThatDoesntExist' in document.createElement('form')"));

Expected behavior:

Console output: False

Actual behavior:

Console output: True

Environment details:

.NET Framework 4.8, Win 10

Possible Solution

In the DomPrototypeInstance.TryGetFromIndex method, there code responsible for returning value is as follows:

if (_stringIndexer != null && !HasProperty(index))
{
    var args = new Object[] { index };
    var prop = _stringIndexer.GetMethod.Invoke(value, args).ToJsValue(_instance);
    result = new PropertyDescriptor(prop, false, false, false);
    return true;
}

This always returns true, no matter if the item is present or not. I think that in case when Invoke call returns null, false should be returned and result should be set to PropertyDescriptor.Undefined.

I checked all string based indexers in code (AngleSharp & AngleSharp.Js repositories only), and apart from the one in JsonObject (which I think is unrelated), none of them could return null if a matching value exists.

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