Skip to content

Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile Access Denied After a While #112896

Open
@cschuchardt88

Description

Description

When using NuGet.Protocol and NuGet.Packaging library. After a while of extracting files from nuget packages. The path becomes denied and exception is thrown. I believe this is a dotnet runtime issue and not a nuget issue. This assumption is based on the stack trace. However, It could be an issue with Microsoft Defender blocking the folder. If that is the case I did nothing that would cause Dender to block me, It's all default settings.

Note: I do feel it could be Defender issue, because we are downloading files and extracting them from nuget packages. If so, how does nuget.exe application get away with it.

Reproduction Steps

Running this function below, will cause the issue, after a while.

public void ExtractPackage(PackageArchiveReader packageReader, string directoryName, IEnumerable<string>? excludePackageIds = null)
{
    excludePackageIds ??= DefaultExcludedPackageIds;

    var files = packageReader.GetLibItems().SelectMany(static sm => sm.Items);
    var contents = packageReader.GetContentItems().SelectMany(static sm => sm.Items);
    var dependencies = packageReader.GetPackageDependencies()
        .SelectMany(static sm => sm.Packages)
        .Where(w => !excludePackageIds.Any(a => w.Id.Equals(a, StringComparison.InvariantCultureIgnoreCase)));

    var pluginPath = Path.Combine(AppContext.BaseDirectory, PluginRootPath, directoryName);

    foreach (var file in files)
    {
        var filename = Path.GetFileName(file);
        ConsoleHelper.Info("", $"Extracting {filename}...");
        _ = packageReader.ExtractFile(file, Path.Combine(pluginPath, filename), NullLogger.Instance);
    }

    foreach (var file in contents)
    {
        var filename = Path.GetFileName(file);
        ConsoleHelper.Info("", $"Extracting {filename}...");
        _ = packageReader.ExtractFile(file, Path.Combine(pluginPath, filename), NullLogger.Instance);
    }

    foreach (var file in dependencies)
    {
        var packageVersion = file.VersionRange.ToShortString();
        string[] excludeFiles = [.. DefaultExcludedPackageIds, file.Id];

        ConsoleHelper.Info("", $"Extracting {file.Id}...");

        if (TryDownloadPackage(file.Id, packageVersion, out var dependPackage))
            ExtractPackage(dependPackage, directoryName, excludeFiles);
        else
        {
            dependPackage = NuGetDownloadPackage(file.Id, packageVersion);
            ExtractPackage(dependPackage, directoryName, excludeFiles);
        }
    }
}

Expected behavior

Have a more descriptive error message along with detecting if its because of some kind of security issue, like Microsoft Defender

Actual behavior

System.UnauthorizedAccessException: Access to the path 'R:\github\cschuchardt88\neo\bin\Neo.CLI\net9.0\Plugins\oracleservice' is denied.
   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.File.Create(String path)
   at NuGet.Packaging.NuGetExtractionFileIO.CreateFile(String path)
   at NuGet.Packaging.StreamExtensions.Testable.FileStreamCopy(Stream inputStream, String fileFullPath)
   at NuGet.Packaging.StreamExtensions.Testable.CopyToFile(Stream inputStream, String fileFullPath)
   at NuGet.Packaging.StreamExtensions.CopyToFile(Stream inputStream, String fileFullPath)
   at NuGet.Packaging.ZipArchiveExtensions.SaveAsFile(ZipArchiveEntry entry, String fileFullPath, ILogger logger)
   at NuGet.Packaging.PackageArchiveReader.ExtractFile(String packageFile, String targetFilePath, ILogger logger)
   at Neo.CLI.Tools.NeoNuGetPackageDownloader.ExtractPackage(PackageArchiveReader packageReader, String directoryName, IEnumerable`1 excludePackageIds) in R:\github\cschuchardt88\neo\src\Neo.CLI\Tools\NeoNuGetPackageDownloader.cs:line 156
   at Neo.CLI.Tools.NeoNuGetPackageDownloader.ExtractPackage(PackageArchiveReader packageReader, String directoryName, IEnumerable`1 excludePackageIds) in R:\github\cschuchardt88\neo\src\Neo.CLI\Tools\NeoNuGetPackageDownloader.cs:line 178
   at Neo.CLI.Tools.NeoNuGetPackageDownloader.ExtractPackage(PackageArchiveReader packageReader, String directoryName, IEnumerable`1 excludePackageIds) in R:\github\cschuchardt88\neo\src\Neo.CLI\Tools\NeoNuGetPackageDownloader.cs:line 178
   at Neo.CLI.MainService.OnInstallCommand(String pluginName, String downloadUrl) in R:\github\cschuchardt88\neo\src\Neo.CLI\CLI\MainService.Plugins.cs:line 57

Regression?

Unknown

Known Workarounds

Unknown

Configuration

.NET SDK:
 Version:           9.0.200
 Commit:            90e8b202f2
 Workload version:  9.0.200-manifests.69179adf
 MSBuild version:   17.13.8+cbc39bea8

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.26100
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\9.0.200\

.NET workloads installed:
 [macos]
   Installation Source: VS 17.13.35818.85
   Manifest Version:    15.2.9180/9.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\9.0.100\microsoft.net.sdk.macos\15.2.9180\WorkloadManifest.json
   Install Type:              Msi

 [maui-windows]
   Installation Source: VS 17.13.35818.85
   Manifest Version:    9.0.14/9.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\9.0.100\microsoft.net.sdk.maui\9.0.14\WorkloadManifest.json
   Install Type:              Msi

 [android]
   Installation Source: VS 17.13.35818.85
   Manifest Version:    35.0.39/9.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\9.0.100\microsoft.net.sdk.android\35.0.39\WorkloadManifest.json
   Install Type:              Msi

 [maccatalyst]
   Installation Source: VS 17.13.35818.85
   Manifest Version:    18.2.9180/9.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\9.0.100\microsoft.net.sdk.maccatalyst\18.2.9180\WorkloadManifest.json
   Install Type:              Msi

 [ios]
   Installation Source: VS 17.13.35818.85
   Manifest Version:    18.2.9180/9.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\9.0.100\microsoft.net.sdk.ios\18.2.9180\WorkloadManifest.json
   Install Type:              Msi

Configured to use loose manifests when installing new manifests.

Host:
  Version:      9.0.2
  Architecture: x64
  Commit:       80aa709f5d

.NET SDKs installed:
  9.0.200 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 9.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 9.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 9.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

Other information

neo> install oracleservice
Extracting OracleService.dll...
Extracting OracleService.runtimeconfig.json...
Extracting OracleService.xml...
Extracting OracleService.json...
Extracting Neo.Plugins.RpcServer...
Extracting RpcServer.dll...
Extracting RpcServer.runtimeconfig.json...
Extracting RpcServer.xml...
Extracting RpcServer.json...
Extracting NeoFS.API...
Extracting Neo.FileStorage.API.dll...
Extracting BouncyCastle.NetCore...
Extracting BouncyCastle.Crypto.dll...
Extracting BouncyCastle.Crypto.xml...
Extracting BouncyCastle.Crypto.dll...
Extracting BouncyCastle.Crypto.xml...
Extracting Google.Protobuf...
Extracting ...
Error: Access to the path 'R:\github\cschuchardt88\neo\bin\Neo.CLI\net9.0\Plugins\oracleservice' is denied.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area ownersuntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions