dotnet / dotnet/fsharp

AutoOpen in the empty namespace as malicious code vector

Open
#17,137 1 comment 3 reactions 0 assignees View on GitHub
Analyzers Area-Compiler-Checking-NameResolution Feature Improvement
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

I noticed this while trying to excise unexpected uses of YoLo.fs in projects that depend (even if only transitively) on Http.Fs: https://github.com/haf/Http.fs/issues/176 . While this particular case does not appear to be malicious, and merely makes it harder to remove the dependency, it is easy to see how it could be exploited in a malicious way.

Typically an `AutoOpen` attribute is declared on a module in a nonempty namespace, so that `open`-ing the namespace will implicitly open the module. Of course, consumers of external packages should always take care when using `open` declarations of external packages' namespaces and modules. However, in the empty namespace, this will happen unconditionally with no `open` usage at all, and, since there is currently no good standard way to disable transitive NuGet package references ( https://github.com/dotnet/sdk/issues/11803 ), the `AutoOpen` will *transitively infect all packages and projects that depend on the offending package*.

While the F# style guide advises programmers to take care in using `AutoOpen` and `RequireQualifiedAccess` in their own code, there is nothing stopping a library author from abusing `AutoOpen` in this way to e.g. typo-squat a malicious definition onto a standard type or module and infecting even codebases that are very careful about managing their dependencies.

I propose that the compiler should not honor `AutoOpen` declarations on modules declared in the empty namespace.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.