From f5cacd19d5d5e708146d160a8f0a5f4af34d7993 Mon Sep 17 00:00:00 2001 From: Steven Lizano Date: Tue, 23 Apr 2024 15:45:18 -0600 Subject: [PATCH] Add safe handle for net8 --- .../Snowflake.Data.Tests.csproj | 6 ++--- .../Configuration/EasyLoggingConfigParser.cs | 26 +++++++++++++------ Snowflake.Data/Snowflake.Data.csproj | 4 +-- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/Snowflake.Data.Tests/Snowflake.Data.Tests.csproj b/Snowflake.Data.Tests/Snowflake.Data.Tests.csproj index 86decd67a..da9a90f46 100644 --- a/Snowflake.Data.Tests/Snowflake.Data.Tests.csproj +++ b/Snowflake.Data.Tests/Snowflake.Data.Tests.csproj @@ -1,7 +1,7 @@  - net6.0;net471;net472 - net6.0 + net8.0;net6.0;net471;net472 + net6.0;net8.0 6.0.0 Snowflake.Data.Tests Snowflake Connector for .NET @@ -37,7 +37,7 @@ - + full True diff --git a/Snowflake.Data/Configuration/EasyLoggingConfigParser.cs b/Snowflake.Data/Configuration/EasyLoggingConfigParser.cs index 466e7b2bd..4fbb1c729 100644 --- a/Snowflake.Data/Configuration/EasyLoggingConfigParser.cs +++ b/Snowflake.Data/Configuration/EasyLoggingConfigParser.cs @@ -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; @@ -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); } } } diff --git a/Snowflake.Data/Snowflake.Data.csproj b/Snowflake.Data/Snowflake.Data.csproj index 0621c5fb0..1fd4ca374 100644 --- a/Snowflake.Data/Snowflake.Data.csproj +++ b/Snowflake.Data/Snowflake.Data.csproj @@ -1,7 +1,7 @@  - net6.0;net471;net472 - net6.0 + net8.0;net6.0;net471;net472 + net6.0;net8.0 Snowflake.Data Snowflake.Data https://github.com/snowflakedb/snowflake-connector-net/blob/master/LICENSE