Skip to content

Analyzer: warn on use of module initializer #43328

Closed

Description

@stephentoub

See discussion in #43268.

The analyzer would flag any application of [ModuleInitializer]. We would turn this on in dotnet/runtime, and prohibit any use of the attribute. It would likely be an info-level analyzer by default, suggesting not to use initializers in library-level code.

Activity

  1. added
    api-suggestion
    code-analyzer
    api-ready-for-review
    on Oct 12, 2020
  2. added this to the 6.0.0 milestone on Oct 12, 2020
  3. added
    untriaged
    on Oct 12, 2020
  4. removed
    untriaged
    on Oct 12, 2020
  5. added
    api-approved
    and removed
    api-ready-for-review
    api-suggestion
    on Oct 27, 2020
  6. terrajobst commented on Oct 27, 2020

    @terrajobst
    Contributor

    Video

    We seem to believe this can even be a warning that is on by default (but it shouldn't apply to generated code). Advanced users (who this feature is for) know how to turn it off.

  7. sharwell commented on Oct 27, 2020

    @sharwell
    Contributor

    📝 I would not be surprised to see test libraries inject these to ensure things like trace listeners are properly initialized. Roslyn is using this approach today.

  8. sharwell commented on Oct 27, 2020

    @sharwell
    Contributor

    💭 It almost seems like this is better suited to Public API analyzer

  9. terrajobst commented on Oct 28, 2020

    @terrajobst
    Contributor

    @sharwell

    📝 I would not be surprised to see test libraries inject these to ensure things like trace listeners are properly initialized. Roslyn is using this approach today.

    That seems like a reasonable scenario. So long people are aware what the limitations are, that's fine. However, we know from static constructors (type initializers) that people think of these things as constructors and don't consider interdependencies. It's not a feature for the masses.

    💭 It almost seems like this is better suited to Public API analyzer

    What does this mean? Isn't the Roslyn specific analyzer?

  10. carlossanlop commented on Nov 11, 2020

    @carlossanlop
    Contributor

    @sharwell ping... can you please clarify your last comment?

  11. sharwell commented on Nov 12, 2020

    @sharwell
    Contributor

    We could treat module initializers like a public API, where the PublicAPI.Shipped.md and PublicAPI.Unshipped.md files list the initializers when present. This would prevent them from accidentally getting included in assemblies that track the public API surface.

  12. added
    help wanted
    on Feb 4, 2021
  13. khalidabuhakmeh commented on May 6, 2021

    @khalidabuhakmeh
    Contributor

    Posting this blog post by Maarten Balliauw to show how malicious parties can use source generators to theoretically do a supply chain attack using ModuleInitializer.

    https://blog.maartenballiauw.be/post/2021/05/05/building-a-supply-chain-attack-with-dotnet-nuget-dns-source-generators-and-more.html

  14. terrajobst commented on May 6, 2021

    @terrajobst
    Contributor

    I don't see how module initializers are different from any of the other opportunities to hide code. In the end, if you take a dependency on a package, that package will contain code that can run in your process or on your machine during design time. There is an infinite amount of ways that code can execute in your process. It can start threads, it can put files on disk and schedule them using task manager etc. If you depend on code, you better either know what it does, trust the provider of the code to not be malicious, and have decent security to not be compromised themselves. Once 3rd party code runs in your process, all bets are off IMHO so I don't see a reason to single out module initializers. What about process wide events? What about access to disk? What about reflection emit? What about private reflection?

    To be clear, I'm not trying to downplay the risk of supply chain attacks. Those are very real and a huge problem space. But I'm objecting to removing useful features if they are don't increase the risk envelope. The way to deal with supply chain attacks is by making it harder to accidently depend on non-trust worthy parties or be vulnerable to package swaps.

    /cc @clairernovotny @jaredpar

  15. clairernovotny commented on May 6, 2021

    @clairernovotny

    Quite frankly, given that a package can contain targets that run by default, you don't need a ModuleInitializer to do damage -- just the PackageReference. Put something that runs on a design time build, add the package reference, and then boom, your code runs, game over.

    I agree with @terrajobst; I'm not sure that Module Initializers should be singled out.

  16. sharwell commented on May 6, 2021

    @sharwell
    Contributor

    To put @terrajobst comments in concrete form: the issue described in #43328 (comment) suggests that a supply chain attack can be performed, but the argument is based on a prerequisite that a supply chain attack already occurred (introduction of a malicious source generator is considered a supply chain attack, regardless of whether the malicious output is code executed on the host or injected into the output).

  17. self-assigned this
    on Aug 4, 2021
  18. removed
    help wanted
    on Aug 6, 2021
  19. jeffhandley commented on Aug 7, 2021

    @jeffhandley
    Member

    Closed via dotnet/roslyn-analyzers#5347 - CA2255 - ModuleInitializerAttributeShouldNotBeUsedInLibraries

  20. ghost locked as resolved and limited conversation to collaborators on Sep 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Runtime.CompilerServicescode-analyzer

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions