Skip to content

Commit b8f974f

Browse files
authored
Merge pull request #2093 from ltrzesniewski/owner-validation
Make owner validation configurable
2 parents 5fd810d + d623e3e commit b8f974f

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

LibGit2Sharp.Tests/GlobalSettingsFixture.cs

+15
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,20 @@ public void SetExtensions()
102102
extensions = GlobalSettings.GetExtensions();
103103
Assert.Equal(new[] { "newext", "noop", "objectformat", "partialclone", "worktreeconfig" }, extensions);
104104
}
105+
106+
[Fact]
107+
public void OwnerValidation()
108+
{
109+
// Assert that owner validation is enabled by default
110+
Assert.True(GlobalSettings.GetOwnerValidation());
111+
112+
// Disable owner validation
113+
GlobalSettings.SetOwnerValidation(false);
114+
Assert.False(GlobalSettings.GetOwnerValidation());
115+
116+
// Enable it again
117+
GlobalSettings.SetOwnerValidation(true);
118+
Assert.True(GlobalSettings.GetOwnerValidation());
119+
}
105120
}
106121
}

LibGit2Sharp/Core/NativeMethods.cs

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.IO;
3+
#if NET
34
using System.Reflection;
5+
#endif
46
using System.Runtime.CompilerServices;
57
using System.Runtime.ConstrainedExecution;
68
using System.Runtime.InteropServices;
@@ -743,6 +745,7 @@ internal static extern int git_libgit2_opts(int option, uint level,
743745
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string path);
744746

745747
// git_libgit2_opts(GIT_OPT_ENABLE_*, int enabled)
748+
// git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, int enabled)
746749
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
747750
internal static extern int git_libgit2_opts(int option, int enabled);
748751

@@ -762,6 +765,10 @@ internal static extern int git_libgit2_opts(int option,
762765
// git_libgit2_opts(GIT_OPT_GET_EXTENSIONS, git_strarray *out)
763766
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
764767
internal static extern int git_libgit2_opts(int option, out GitStrArray extensions);
768+
769+
// git_libgit2_opts(GIT_OPT_GET_OWNER_VALIDATION, int *enabled)
770+
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
771+
internal static extern unsafe int git_libgit2_opts(int option, int* enabled);
765772
#endregion
766773

767774
#region git_libgit2_opts_osxarm64
@@ -779,6 +786,7 @@ internal static extern int git_libgit2_opts_osxarm64(int option, IntPtr nop2, In
779786
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string path);
780787

781788
// git_libgit2_opts(GIT_OPT_ENABLE_*, int enabled)
789+
// git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, int enabled)
782790
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl, EntryPoint = "git_libgit2_opts")]
783791
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);
784792

@@ -798,6 +806,10 @@ internal static extern int git_libgit2_opts_osxarm64(int option, IntPtr nop2, In
798806
// git_libgit2_opts(GIT_OPT_GET_EXTENSIONS, git_strarray *out)
799807
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl, EntryPoint = "git_libgit2_opts")]
800808
internal static extern int git_libgit2_opts_osxarm64(int option, IntPtr nop2, IntPtr nop3, IntPtr nop4, IntPtr nop5, IntPtr nop6, IntPtr nop7, IntPtr nop8, out GitStrArray extensions);
809+
810+
// git_libgit2_opts(GIT_OPT_GET_OWNER_VALIDATION, int *enabled)
811+
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl, EntryPoint = "git_libgit2_opts")]
812+
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);
801813
#endregion
802814

803815
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]

LibGit2Sharp/Core/Proxy.cs

+43
Original file line numberDiff line numberDiff line change
@@ -3397,6 +3397,8 @@ private enum LibGit2Option
33973397
SetOdbLoosePriority, // GIT_OPT_SET_ODB_LOOSE_PRIORITY,
33983398
GetExtensions, // GIT_OPT_GET_EXTENSIONS,
33993399
SetExtensions, // GIT_OPT_SET_EXTENSIONS
3400+
GetOwnerValidation, // GIT_OPT_GET_OWNER_VALIDATION
3401+
SetOwnerValidation, // GIT_OPT_SET_OWNER_VALIDATION
34003402
}
34013403

34023404
/// <summary>
@@ -3570,6 +3572,47 @@ public static string[] git_libgit2_opts_get_extensions()
35703572
}
35713573
}
35723574

3575+
/// <summary>
3576+
/// Gets the value of owner validation
3577+
/// </summary>
3578+
public static unsafe bool git_libgit2_opts_get_owner_validation()
3579+
{
3580+
int res;
3581+
int enabled;
3582+
3583+
if (isOSXArm64)
3584+
{
3585+
res = NativeMethods.git_libgit2_opts_osxarm64((int)LibGit2Option.GetOwnerValidation, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, &enabled);
3586+
}
3587+
else
3588+
{
3589+
res = NativeMethods.git_libgit2_opts((int)LibGit2Option.GetOwnerValidation, &enabled);
3590+
}
3591+
3592+
Ensure.ZeroResult(res);
3593+
3594+
return enabled != 0;
3595+
}
3596+
3597+
/// <summary>
3598+
/// Enable or disable owner validation
3599+
/// </summary>
3600+
/// <param name="enabled">true to enable owner validation, false otherwise</param>
3601+
public static void git_libgit2_opts_set_owner_validation(bool enabled)
3602+
{
3603+
int res;
3604+
3605+
if (isOSXArm64)
3606+
{
3607+
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);
3608+
}
3609+
else
3610+
{
3611+
res = NativeMethods.git_libgit2_opts((int)LibGit2Option.SetOwnerValidation, enabled ? 1 : 0);
3612+
}
3613+
3614+
Ensure.ZeroResult(res);
3615+
}
35733616
#endregion
35743617

35753618
#region git_worktree_

LibGit2Sharp/GlobalSettings.cs

+21
Original file line numberDiff line numberDiff line change
@@ -417,5 +417,26 @@ public static string GetUserAgent()
417417
{
418418
return Proxy.git_libgit2_opts_get_user_agent();
419419
}
420+
421+
/// <summary>
422+
/// Gets the owner validation setting for repository directories.
423+
/// </summary>
424+
/// <returns></returns>
425+
public static bool GetOwnerValidation()
426+
{
427+
return Proxy.git_libgit2_opts_get_owner_validation();
428+
}
429+
430+
/// <summary>
431+
/// Sets whether repository directories should be owned by the current user. The default is to validate ownership.
432+
/// </summary>
433+
/// <remarks>
434+
/// Disabling owner validation can lead to security vulnerabilities (see CVE-2022-24765).
435+
/// </remarks>
436+
/// <param name="enabled">true to enable owner validation; otherwise, false.</param>
437+
public static void SetOwnerValidation(bool enabled)
438+
{
439+
Proxy.git_libgit2_opts_set_owner_validation(enabled);
440+
}
420441
}
421442
}

0 commit comments

Comments
 (0)