Skip to content

Use the MetadataLoadContext to restore reflection-only type discovery for mechanisms like using assembly #6653

Closed

Description

@rjmholt

See #3641. Types needed in PowerShell classes must be available at parse-time (i.e. not loaded with Import-Module or Add-Type) to be able to compile classes to IL.

To be able to resolve these types, we want to support the using assembly statement. However, the most available way to do this involves loading a DLL and possibly executing some of its code, presenting a vector for arbitrary code execution.

Instead, we should analyse a DLL's metadata to discover the types it exports, using a System.Reflection.Metadata.MetadataReader. Example code for this is in PR #3169. The entry point for this analysis would be the PowerShellModuleAssemblyAnalyzer class.

The right way to do this is now to use a MetadataLoadContext

Activity

  1. added
    Issue-Enhancementthe issue is more of a feature request than a bug
    WG-Enginecore PowerShell engine, interpreter, and runtime
    WG-Languageparser, language semantics
    on Apr 15, 2018
  2. iSazonov commented on Apr 15, 2018

    @iSazonov
    Collaborator

    Should we thing about PowerShell classes too? I mean getting PowerShell classes at parse time from PowerShell modules.

  3. PetSerAl commented on Apr 15, 2018

    @PetSerAl
    Contributor

    Ilya (@iSazonov)

    Should we thing about PowerShell classes too? I mean getting PowerShell classes at parse time from PowerShell modules.

    Is not using module already do that?

  4. iSazonov commented on Apr 15, 2018

    @iSazonov
    Collaborator

    I don't know.

  5. rjmholt commented on May 2, 2018

    @rjmholt
    CollaboratorAuthor

    Ilya (@iSazonov) when you say "getting PowerShell classes at parse time" do you mean loading?

    That's basically what happens with using module. Since we have a nice PowerShell parser that we know doesn't run any code, we read in the class definitions with it and then compile them to .NET IL. So the parser is already able to resolve PowerShell-class-defined types at parse time.

    The problem is that we don't have a nice .NET IL parser that we know doesn't run any code (and we only need the IL metadata anyway, to discover what types are available). corefx ships one, but it's pretty low level and documentation on it is sparse. Last time I wrote something using it, I used their tests for guidance. But it should be relatively straightforward.

  6. iSazonov commented on May 3, 2018

    @iSazonov
    Collaborator

    Rob Holt (@rjmholt) You could look our new Add-Type cmdlet - it has a code to retrieve types from assemblies (to check duplications).

  7. iSazonov commented on Jun 28, 2019

    @iSazonov
    Collaborator

    It makes sense to use MetadataReader in our TypeResolver too - it seems we load dozens of assemblies unnecessarily.

  8. rjmholt commented on Jul 28, 2020

    @rjmholt
    CollaboratorAuthor

    The metadata reader implementation from us is no longer required, since dotnet/corefx#33201. We should now just use that

  9. removed their assignment
    on Jul 28, 2020
  10. changed the title [-]Implement a `MetadataReader` from `System.Reflection.Metadata` to support `using assembly`[/-] [+]Use the MetadataLoadContext to restore reflection-only type discovery for mechanisms like using assembly[/+] on Jul 28, 2020
  11. daxian-dbw commented on Jun 10, 2021

    @daxian-dbw
  12. rjmholt commented on Jun 10, 2021

    @rjmholt
    CollaboratorAuthor

    This might not be that useful to using assembly since using assembly needs to make the assembly actually loaded for its types to be used in IL emitting

    I think the scenario here was to make class definition that depends on using assembly and using module still safe, so that script files can be parsed and type check successfully without the risk of running an arbitrary DLL on the filesystem.

    Much of this isn't actually required, but the discussion in that issue seems to indicate a preference for going down that path.

    I suspect a lot of these implementation discussions should really be had secondary to a design review of classes in some sense, but the available tooling in .NET has moved to a point that makes more things possible.

  13. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  14. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  15. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  16. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

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

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtimeWG-Languageparser, language semantics

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions