Skip to content

Microsoft.Extensions.Configuration.ConfigurationBinder.BindProperty(PropertyInfo,object, IConfiguration, BinderOptions) – harmful call to PropertyInfo.GetValue(object) #52905

Closed

Description

@deep-outcome

Description

The ConfigurationBinder.BindProperty seems to be

private static void BindProperty(PropertyInfo property, object instance, IConfiguration config, BinderOptions options)
{
    // We don't support set only, non public, or indexer properties
    if (property.GetMethod == null ||
        (!options.BindNonPublicProperties && !property.GetMethod.IsPublic) ||
        property.GetMethod.GetParameters().Length > 0)
    {
        return;
    }

    object propertyValue = property.GetValue(instance);
    bool hasSetter = property.SetMethod != null && (property.SetMethod.IsPublic || options.BindNonPublicProperties);

    if (propertyValue == null && !hasSetter)
    {
        // Property doesn't have a value and we cannot set it so there is no
        // point in going further down the graph
        return;
    }

    propertyValue = GetPropertyValue(property, instance, config, options);

    if (propertyValue != null && hasSetter)
    {
        property.SetValue(instance, propertyValue);
    }
}

Lines object propertyValue = property.GetValue(instance); ,

if (propertyValue == null && !hasSetter)
{
  // Property doesn't have a value and we cannot set it so there is no
  // point in going further down the graph
  return;
}

are obviously some debris somebody forgotten to remove. The line object propertyValue = property.GetValue(instance); can call to properties with not setter – like static properties – forcing them to execute.

Moreover, why to obtain any property value before asking if it can be passed to existing setter. 👎🏻

Configuration

Microsoft.Extensions.Configuration.Binder 5.0.0 on .NET 5.0

Area

area-Extensions-Configuration

Activity

  1. ghost added
    untriaged
    on May 18, 2021
  2. changed the title [-]Microsoft.Extensions.Configuration.ConfigurationBinder.BindProperty(PropertyInfo,object, IConfiguration, BinderOptions) – useless call to PropertyInfo.GetValue(object)[/-] [+]Microsoft.Extensions.Configuration.ConfigurationBinder.BindProperty(PropertyInfo,object, IConfiguration, BinderOptions) – harmful call to PropertyInfo.GetValue(object)[/+] on May 18, 2021
  3. removed
    untriaged
    on Jun 5, 2021
  4. added this to the Future milestone on Jun 5, 2021
  5. david-jarman commented on Aug 31, 2021

    @david-jarman

    This is currently biting me, where I have a read-only property on one of my objects that can potentially throw an exception if called before some extra setup is executed. This forces me to modify the behavior of the property so it doesn't throw while the binding process is happening.

    example:

    public class BindingObject
    {
        public string BindThisString { get; set; }
    
        public OtherObject ThisIsNotSetDuringBinding { get; set; }
    
        // This will throw an exception during binding, because the get is called for no good reason
        public string DerivedPropertyString => $"DerivedProperty={ThisIsNotSetDuringBinding .Id}"
    }
  6. modified the milestones: Future, 7.0.0 on Oct 1, 2021
  7. added
    help wanted
    on Oct 1, 2021
  8. added a commit that references this issue on Oct 14, 2021
    d01bb92
  9. ghost added
    in-pr
    on Oct 14, 2021
  10. added a commit that references this issue on Oct 14, 2021
    97ef512
  11. ghost removed
    in-pr
    on Oct 14, 2021
  12. ghost locked as resolved and limited conversation to collaborators on Nov 14, 2021
  13. ericstj commented on Jan 10, 2022

    @ericstj
    Member

    Reopening since the fix introduced a regression and will be reverted.

  14. ericstj commented on Jan 10, 2022

    @ericstj
    Member

    Make sure that any fix for this in the future accounts for the scenario here: #63479

  15. added a commit that references this issue on Mar 16, 2022
    99f977b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Extensions-Configurationhelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions