Skip to content

Commit

Permalink
Make owner validation configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski authored and bording committed Nov 26, 2024
1 parent 5fd810d commit 9014820
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Thumbs.db
*.ncb
*.suo
.vs/
.idea/
*.sln.ide/
*.tlb
*.tlh
Expand Down
15 changes: 15 additions & 0 deletions LibGit2Sharp.Tests/GlobalSettingsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,20 @@ public void SetExtensions()
extensions = GlobalSettings.GetExtensions();
Assert.Equal(new[] { "newext", "noop", "objectformat", "partialclone", "worktreeconfig" }, extensions);
}

[Fact]
public void OwnerValidation()
{
// Assert that owner validation is enabled by default
Assert.True(GlobalSettings.OwnerValidation);

// Disable owner validation
GlobalSettings.OwnerValidation = false;
Assert.False(GlobalSettings.OwnerValidation);

// Enable it again
GlobalSettings.OwnerValidation = true;
Assert.True(GlobalSettings.OwnerValidation);
}
}
}
8 changes: 8 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,10 @@ internal static extern int git_libgit2_opts(int option, uint level,
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
internal static extern int git_libgit2_opts(int option, int enabled);

// git_libgit2_opts(GIT_OPT_GET_*, int *enabled)
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe int git_libgit2_opts(int option, int* enabled);

// git_libgit2_opts(GIT_OPT_SET_USER_AGENT, const char *path)
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
internal static extern int git_libgit2_opts(int option,
Expand Down Expand Up @@ -782,6 +786,10 @@ internal static extern int git_libgit2_opts_osxarm64(int option, IntPtr nop2, In
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl, EntryPoint = "git_libgit2_opts")]
internal static extern int git_libgit2_opts_osxarm64(int option, IntPtr nop2, IntPtr nop3, IntPtr nop4, IntPtr nop5, IntPtr nop6, IntPtr nop7, IntPtr nop8, int enabled);

// git_libgit2_opts(GIT_OPT_GET_*, int enabled)
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl, EntryPoint = "git_libgit2_opts")]
internal static extern unsafe int git_libgit2_opts_osxarm64(int option, IntPtr nop2, IntPtr nop3, IntPtr nop4, IntPtr nop5, IntPtr nop6, IntPtr nop7, IntPtr nop8, int* enabled);

// git_libgit2_opts(GIT_OPT_SET_USER_AGENT, const char *path)
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl, EntryPoint = "git_libgit2_opts")]
internal static extern int git_libgit2_opts_osxarm64(int option, IntPtr nop2, IntPtr nop3, IntPtr nop4, IntPtr nop5, IntPtr nop6, IntPtr nop7, IntPtr nop8,
Expand Down
32 changes: 32 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3397,6 +3397,8 @@ private enum LibGit2Option
SetOdbLoosePriority, // GIT_OPT_SET_ODB_LOOSE_PRIORITY,
GetExtensions, // GIT_OPT_GET_EXTENSIONS,
SetExtensions, // GIT_OPT_SET_EXTENSIONS
GetOwnerValidation, // GIT_OPT_GET_OWNER_VALIDATION
SetOwnerValidation, // GIT_OPT_SET_OWNER_VALIDATION
}

/// <summary>
Expand Down Expand Up @@ -3570,6 +3572,36 @@ public static string[] git_libgit2_opts_get_extensions()
}
}

/// <summary>
/// Gets the value of owner validation
/// </summary>
public static unsafe bool git_libgit2_opts_get_owner_validation()
{
// libgit2 expects non-zero value for true
int res, enabled;
if (isOSXArm64)
res = NativeMethods.git_libgit2_opts_osxarm64((int)LibGit2Option.GetOwnerValidation, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, &enabled);
else
res = NativeMethods.git_libgit2_opts((int)LibGit2Option.GetOwnerValidation, &enabled);
Ensure.ZeroResult(res);
return enabled != 0;
}

/// <summary>
/// Enable or disable owner validation
/// </summary>
/// <param name="enabled">true to enable owner validation, false otherwise</param>
public static void git_libgit2_opts_set_owner_validation(bool enabled)
{
// libgit2 expects non-zero value for true
int res;
if (isOSXArm64)
res = NativeMethods.git_libgit2_opts_osxarm64((int)LibGit2Option.SetOwnerValidation, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, enabled ? 1 : 0);
else
res = NativeMethods.git_libgit2_opts((int)LibGit2Option.SetOwnerValidation, enabled ? 1 : 0);
Ensure.ZeroResult(res);
}

#endregion

#region git_worktree_
Expand Down
13 changes: 13 additions & 0 deletions LibGit2Sharp/GlobalSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,19 @@ public static string NativeLibraryPath
}
}

/// <summary>
/// Controls the status of repository directory owner validation.
/// </summary>
/// <remarks>
/// By default, repository directories must be owned by the current user to be opened. This can be disabled by setting this property to false.
/// Note that disabling this can lead to security vulnerabilities (see CVE-2022-24765).
/// </remarks>
public static bool OwnerValidation
{
get => Proxy.git_libgit2_opts_get_owner_validation();
set => Proxy.git_libgit2_opts_set_owner_validation(value);
}

internal static string GetAndLockNativeLibraryPath()
{
nativeLibraryPathLocked = true;
Expand Down

0 comments on commit 9014820

Please sign in to comment.