Skip to content

Circular dependency at runtime between System.Management.Automation and Microsoft.PowerShell.Security #14095

Closed

Description

While using only System.Management.Automation as a dependency for an application of mine (without the entire SDK because I didn't need it), I was getting sometimes a NullReferenceException that was thrown in this line of code:

return type.GetMethod(method, BindingFlags.Static | BindingFlags.Public | BindingFlags.IgnoreCase);

because type was null.

Digging in the stack trace, I found out that the null reference was originated by the following instruction:

Type securityDescriptorCommandsBaseType = TypeResolver.ResolveType("Microsoft.PowerShell.Commands.SecurityDescriptorCommandsBase", exception: out _);

The securityDescriptorCommandsBaseType variable gets passed to the method GetMethodInfo, which contains the return statement with the variable type shown above.

Once you know that the referenced type (Microsoft.PowerShell.Commands.SecurityDescriptorCommandsBase) belongs to the Microsoft.PowerShell.Security package, and that the latter depends on System.Management.Automation, it becomes apparent that TypeResolver.ResolveType was being used in place of typeof to avoid a circular dependency at compile time between the two assemblies.
The problem is that the circular dependency still exists and causes the NRE mentioned above when System.Management.Automation is used alone.

Therefore, since I think that probably that dependency can't be broken, I would propose to "loosen" it by adding a null-check that prevents all the code related to the securityDescriptorCommandsBaseType from being executed when Microsoft.PowerShell.Security assembly is not available (which should be lines 4074-4128 or even 4074-4210, I'm not sure).

I'm willing to open a PR to mitigate this problem after receiving your feedback on it, since - unlike me - you know the context and the motivation of the code I mentioned throughout this issue.

Activity

  1. added
    Issue-Question
    on Nov 15, 2020
  2. iSazonov commented on Nov 15, 2020

    @iSazonov
    Collaborator

    Dongbo Wang (@daxian-dbw) Could you please comment?

  3. added
    WG-DevEx-SDK
    on Nov 15, 2020
  4. Fs00 commented on Feb 10, 2021

    @Fs00
    ContributorAuthor

    Hello Dongbo Wang (@daxian-dbw), could you please take a look at this and see if my proposed solution could be enough to solve it?
    I don't want to bother you, but this issue is getting stale and I would like to see it fixed (if it's possible to).

  5. added
    WG-DevEx-SDK
    WG-Security
    Needs-Triage
    and removed
    WG-DevEx-SDK
    on Feb 25, 2021
  6. added
    Issue-Enhancement
    and removed
    Issue-Question
    Needs-Triage
    on Mar 8, 2021
  7. TravisEz13 commented on Mar 8, 2021

    @TravisEz13
    Member

    Currently, the security assembly is required. We are looking at this type of feature in the future.

  8. Fs00 commented on Mar 8, 2021

    @Fs00
    ContributorAuthor

    Ok, so - if I understood correctly - the proper fix would consist in fully decoupling those two assemblies, right?

  9. TravisEz13 commented on Mar 9, 2021

    @TravisEz13
    Member

    I'll let Dongbo Wang (@daxian-dbw) talk to what the correct fix is.

  10. added
    WG-Engine
    and removed
    WG-DevEx-SDK
    on May 19, 2021
  11. added
    Needs-Triage
    and removed
    WG-Security
    on Jun 7, 2021
  12. added
    Up-for-Grabs
    and removed
    Needs-Triage
    on Feb 4, 2022
  13. ghost added
    In-PR
    on Jul 26, 2022
  14. ghost removed
    In-PR
    on Jul 26, 2022
  15. ghost added
    Resolution-Fixed
    on Jul 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-EnhancementResolution-FixedUp-for-GrabsWG-Engine

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions