You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Microsoft.Extensions.Configuration.ConfigurationBinder.BindProperty(PropertyInfo,object, IConfiguration, BinderOptions) – harmful call to PropertyInfo.GetValue(object) #52905
privatestaticvoidBindProperty(PropertyInfoproperty,objectinstance,IConfigurationconfig,BinderOptionsoptions){// We don't support set only, non public, or indexer propertiesif(property.GetMethod==null||(!options.BindNonPublicProperties&&!property.GetMethod.IsPublic)||property.GetMethod.GetParameters().Length>0){return;}objectpropertyValue=property.GetValue(instance);boolhasSetter=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 graphreturn;}propertyValue=GetPropertyValue(property,instance,config,options);if(propertyValue!=null&&hasSetter){property.SetValue(instance,propertyValue);}}
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 graphreturn;}
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
Reacted by David Jarman and Daniel Petrov
Activity
ghost added
untriagedNew issue has not been triaged by the area owner
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
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:
publicclassBindingObject{publicstringBindThisString{get;set;}publicOtherObjectThisIsNotSetDuringBinding{get;set;}// This will throw an exception during binding, because the get is called for no good reasonpublicstringDerivedPropertyString=>$"DerivedProperty={ThisIsNotSetDuringBinding.Id}"}
Activity
[-]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)[/+]david-jarman commentedon Aug 31, 2021
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:
Remove extra call to PropertyInfo.GetValue(object)
Remove extra call to PropertyInfo.GetValue(object) (#60415)
ericstj commentedon Jan 10, 2022
Reopening since the fix introduced a regression and will be reverted.
ericstj commentedon Jan 10, 2022
Make sure that any fix for this in the future accounts for the scenario here: #63479
Change ConfigurationBinder to avoid reading properties unnecessarily.