Skip to content

Commit

Permalink
fix failing on windows OS
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-erojaslizano committed Jul 29, 2024
1 parent abd9777 commit 65f15f9
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions Snowflake.Data/Configuration/EasyLoggingConfigParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using Microsoft.IdentityModel.Tokens;
Expand Down Expand Up @@ -52,19 +53,18 @@ private string TryToReadFile(string filePath)

try
{
FileAccessPermissions forbiddenPermissions = FileAccessPermissions.OtherWrite | FileAccessPermissions.GroupWrite;
var fileInfo = new UnixFileInfo(path: filePath);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
using var streamReader = new StreamReader(filePath, Encoding.Default);
return streamReader.ReadToEnd();
}
else
{
var handle = VerifyUnixPermissions(filePath);

using var handle = fileInfo.OpenRead();
if (handle.OwnerUser.UserId != Syscall.geteuid())
throw new SnowflakeDbException(SFError.INTERNAL_ERROR,"Attempting to read a file not owned by the effective user of the current process");
if (handle.OwnerGroup.GroupId != Syscall.getegid())
throw new SnowflakeDbException(SFError.INTERNAL_ERROR,"Attempting to read a file not owned by the effective group of the current process");
if ((handle.FileAccessPermissions & forbiddenPermissions) != 0)
throw new SnowflakeDbException( SFError.INTERNAL_ERROR,"Attempting to read a file with too broad permissions assigned");

using var streamReader = new StreamReader(handle, Encoding.Default);
return streamReader.ReadToEnd();
using var streamReader = new StreamReader(handle, Encoding.Default);
return streamReader.ReadToEnd();
}
}
catch (Exception e)
{
Expand All @@ -74,6 +74,21 @@ private string TryToReadFile(string filePath)
}
}

private static UnixStream VerifyUnixPermissions(string filePath)
{
FileAccessPermissions forbiddenPermissions = FileAccessPermissions.OtherWrite | FileAccessPermissions.GroupWrite;
var fileInfo = new UnixFileInfo(path: filePath);

using var handle = fileInfo.OpenRead();
if (handle.OwnerUser.UserId != Syscall.geteuid())
throw new SnowflakeDbException(SFError.INTERNAL_ERROR, "Attempting to read a file not owned by the effective user of the current process");
if (handle.OwnerGroup.GroupId != Syscall.getegid())
throw new SnowflakeDbException(SFError.INTERNAL_ERROR, "Attempting to read a file not owned by the effective group of the current process");
if ((handle.FileAccessPermissions & forbiddenPermissions) != 0)
throw new SnowflakeDbException(SFError.INTERNAL_ERROR, "Attempting to read a file with too broad permissions assigned");
return handle;
}

private ClientConfig TryToParseFile(string fileContent)
{
try
Expand Down

0 comments on commit 65f15f9

Please sign in to comment.