Skip to content

Commit

Permalink
Add safe handle for net8
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-erojaslizano committed Apr 23, 2024
1 parent cc5e12c commit f5cacd1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
6 changes: 3 additions & 3 deletions Snowflake.Data.Tests/Snowflake.Data.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net471;net472</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">net6.0</TargetFrameworks>
<TargetFrameworks>net8.0;net6.0;net471;net472</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">net6.0;net8.0</TargetFrameworks>
<RuntimeFrameworkVersion>6.0.0</RuntimeFrameworkVersion>
<Title>Snowflake.Data.Tests</Title>
<Description>Snowflake Connector for .NET</Description>
Expand Down Expand Up @@ -37,7 +37,7 @@
<Reference Include="System.Web" />
<Reference Include="System.Net.Http.WebRequest" />
</ItemGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>full</DebugType>
<DebugSymbols>True</DebugSymbols>
Expand Down
26 changes: 18 additions & 8 deletions Snowflake.Data/Configuration/EasyLoggingConfigParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ private string TryToReadFile(string filePath)
{
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
CheckIfValidPermissions(fileStream, filePath);

using (StreamReader reader = new StreamReader(fileStream))
{
CheckIfValidPermissions(filePath);

string fileContent = reader.ReadToEnd();

return fileContent;
Expand Down Expand Up @@ -99,18 +99,28 @@ private void CheckForUnknownFields(string fileContent)
.ForEach(unknownKey => s_logger.Warn($"Unknown field from config: {unknownKey.Name}"));
}

private void CheckIfValidPermissions(string filePath)
private void CheckIfValidPermissions(FileStream fileStream, string filePath)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return;
}

// Check if others have permissions to modify the file and fail if so
if (_unixOperations.CheckFileHasAnyOfPermissions(filePath, FileAccessPermissions.GroupWrite | FileAccessPermissions.OtherWrite))
#if NET8_0_OR_GREATER
var unixFileMode = File.GetUnixFileMode(fileStream.SafeFileHandle);
var hasPermissions = ((UnixFileMode.OtherRead | UnixFileMode.GroupWrite) & unixFileMode) == 0;
#else
var entitlements = FileAccessPermissions.GroupWrite | FileAccessPermissions.OtherWrite;
var hasPermissions = _unixOperations.CheckFileHasAnyOfPermissions(filePath, entitlements);
#endif
if (hasPermissions)
{
var errorMessage = $"Error due to other users having permission to modify the config file: {filePath}";
s_logger.Error(errorMessage);
throw new Exception(errorMessage);
return;
}

var errorMessage = $"Error due to other users having permission to modify the config file: {filePath}";
s_logger.Error(errorMessage);
throw new Exception(errorMessage);
}
}
}
4 changes: 2 additions & 2 deletions Snowflake.Data/Snowflake.Data.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net471;net472</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">net6.0</TargetFrameworks>
<TargetFrameworks>net8.0;net6.0;net471;net472</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">net6.0;net8.0</TargetFrameworks>
<Title>Snowflake.Data</Title>
<PackageId>Snowflake.Data</PackageId>
<PackageLicenseUrl>https://github.com/snowflakedb/snowflake-connector-net/blob/master/LICENSE</PackageLicenseUrl>
Expand Down

0 comments on commit f5cacd1

Please sign in to comment.