Skip to content

Commit

Permalink
Changed toml file permissions to check 400 and 600 permissions only.
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jmartinezramirez committed Nov 6, 2024
1 parent ca94da5 commit 08d40ba
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
43 changes: 27 additions & 16 deletions Snowflake.Data.Tests/UnitTests/Tools/FileOperationsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@
* Copyright (c) 2024 Snowflake Computing Inc. All rights reserved.
*/


using System;
using System.Collections.Generic;
using Snowflake.Data.Core;
using System.IO;
using System.Runtime.InteropServices;
using Mono.Unix;
using Mono.Unix.Native;
using NUnit.Framework;
using Snowflake.Data.Core.Tools;
using static Snowflake.Data.Tests.UnitTests.Configuration.EasyLoggingConfigGenerator;
using System.Security;

namespace Snowflake.Data.Tests.Tools
{
using System.IO;
using System.Runtime.InteropServices;
using Mono.Unix;
using Mono.Unix.Native;
using NUnit.Framework;
using Snowflake.Data.Core.Tools;
using static Snowflake.Data.Tests.UnitTests.Configuration.EasyLoggingConfigGenerator;
using System.Security;

[TestFixture, NonParallelizable]
public class FileOperationsTest
{
Expand Down Expand Up @@ -59,7 +57,9 @@ public void TestReadAllTextOnWindows()
}

[Test]
public void TestReadAllTextCheckingPermissionsUsingTomlConfigurationFileValidations()
public void TestReadAllTextCheckingPermissionsUsingTomlConfigurationFileValidations(
[ValueSource(nameof(UserAllowedFilePermissions))]
FileAccessPermissions userAllowedFilePermissions)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Expand All @@ -68,8 +68,9 @@ public void TestReadAllTextCheckingPermissionsUsingTomlConfigurationFileValidati

var content = "random text";
var filePath = CreateConfigTempFile(s_workingDirectory, content);
var filePermissions = FileAccessPermissions.UserReadWriteExecute;
Syscall.chmod(filePath, (FilePermissions)filePermissions);
var filePermissions = userAllowedFilePermissions;

Syscall.chmod(filePath, (FilePermissions)filePermissions);

// act
var result = s_fileOperations.ReadAllText(filePath, TomlConnectionBuilder.ValidateFilePermissions);
Expand All @@ -79,7 +80,9 @@ public void TestReadAllTextCheckingPermissionsUsingTomlConfigurationFileValidati
}

[Test]
public void TestShouldThrowExceptionIfOtherPermissionsIsSetWhenReadConfigurationFile()
public void TestShouldThrowExceptionIfOtherPermissionsIsSetWhenReadConfigurationFile(
[ValueSource(nameof(UserAllowedFilePermissions))]
FileAccessPermissions userAllowedFilePermissions)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Expand All @@ -88,12 +91,20 @@ public void TestShouldThrowExceptionIfOtherPermissionsIsSetWhenReadConfiguration

var content = "random text";
var filePath = CreateConfigTempFile(s_workingDirectory, content);
var filePermissions = FileAccessPermissions.UserReadWriteExecute | FileAccessPermissions.OtherReadWriteExecute;
var filePermissions = userAllowedFilePermissions | FileAccessPermissions.OtherReadWriteExecute;

Syscall.chmod(filePath, (FilePermissions)filePermissions);

// act and assert
Assert.Throws<SecurityException>(() => s_fileOperations.ReadAllText(filePath, TomlConnectionBuilder.ValidateFilePermissions),
"Attempting to read a file with too broad permissions assigned");
}


public static IEnumerable<FileAccessPermissions> UserAllowedFilePermissions()
{
yield return FileAccessPermissions.UserRead;
yield return FileAccessPermissions.UserRead | FileAccessPermissions.UserWrite;
}
}
}
14 changes: 10 additions & 4 deletions Snowflake.Data.Tests/UnitTests/Tools/UnixOperationsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,16 @@ public void TestDetectGroupOrOthersNotWritablePermissions(
}

[Test]
public void TestReadAllTextCheckingPermissionsUsingTomlConfigurationFileValidations()
public void TestReadAllTextCheckingPermissionsUsingTomlConfigurationFileValidations(
[ValueSource(nameof(UserAllowedPermissions))] FilePermissions userAllowedPermissions)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Assert.Ignore("skip test on Windows");
}
var content = "random text";
var filePath = CreateConfigTempFile(s_workingDirectory, content);
var filePermissions = FileAccessPermissions.UserReadWriteExecute;
Syscall.chmod(filePath, (FilePermissions)filePermissions);
Syscall.chmod(filePath, userAllowedPermissions);

// act
var result = s_unixOperations.ReadAllText(filePath, TomlConnectionBuilder.ValidateFilePermissions);
Expand Down Expand Up @@ -177,7 +177,13 @@ public static IEnumerable<FilePermissions> OtherNotWritablePermissions()

public static IEnumerable<FilePermissions> UserReadWritePermissions()
{
yield return FilePermissions.S_IRUSR | FilePermissions.S_IWUSR | FilePermissions.S_IXUSR;
yield return FilePermissions.S_IRUSR | FilePermissions.S_IWUSR;
}

public static IEnumerable<FilePermissions> UserAllowedPermissions()
{
yield return FilePermissions.S_IRUSR;
yield return FilePermissions.S_IRUSR | FilePermissions.S_IWUSR;
}

public static IEnumerable<FilePermissions> GroupOrOthersReadablePermissions()
Expand Down
9 changes: 7 additions & 2 deletions Snowflake.Data/Core/TomlConnectionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using System.Text;
using Mono.Unix;
Expand Down Expand Up @@ -154,12 +155,16 @@ private string ResolveConnectionTomlFile()

internal static void ValidateFilePermissions(UnixStream stream)
{
const FileAccessPermissions forbiddenPermissions = FileAccessPermissions.OtherReadWriteExecute | FileAccessPermissions.GroupReadWriteExecute;
var allowedPermissions = new FileAccessPermissions[]
{
FileAccessPermissions.UserRead | FileAccessPermissions.UserWrite,
FileAccessPermissions.UserRead
};
if (stream.OwnerUser.UserId != Syscall.geteuid())
throw new SecurityException("Attempting to read a file not owned by the effective user of the current process");
if (stream.OwnerGroup.GroupId != Syscall.getegid())
throw new SecurityException("Attempting to read a file not owned by the effective group of the current process");
if ((stream.FileAccessPermissions & forbiddenPermissions) != 0)
if (!(allowedPermissions.Any(a => stream.FileAccessPermissions == a)))
throw new SecurityException("Attempting to read a file with too broad permissions assigned");
}
}
Expand Down

0 comments on commit 08d40ba

Please sign in to comment.