From 0159eaae926aa4c8d85490f5d846e44b1d88fb6e Mon Sep 17 00:00:00 2001 From: Brian Mello Date: Mon, 9 Nov 2020 15:11:37 -0500 Subject: [PATCH 01/10] Fixed server to allow nested git repos, by inputting folder and path information --- .../App_GlobalResources/Resources.Designer.cs | 20 ++++++++- .../App_GlobalResources/Resources.resx | 6 +++ .../DoesControllerExistConstraint.cs | 40 ++++++++++++++++++ Bonobo.Git.Server/App_Start/RouteConfig.cs | 35 ++++++---------- .../Attributes/FileNameAttribute.cs | 2 +- .../Attributes/GitAuthorizeAttribute.cs | 22 ++++++++-- Bonobo.Git.Server/Bonobo.Git.Server.csproj | 17 +++++--- .../Configuration/AuthenticationSettings.cs | 2 + .../Controllers/AccountController.cs | 13 ++++-- .../Controllers/GitController.cs | 42 +++++++++++++++++-- .../Controllers/RepositoryController.cs | 29 ++++++++++--- .../Data/Update/RepositorySynchronizer.cs | 8 ++-- Bonobo.Git.Server/Models/RepositoryModels.cs | 4 +- Bonobo.Git.Server/packages.config | 4 +- Bonobo.Git.Server/web.config | 9 +++- 15 files changed, 198 insertions(+), 55 deletions(-) create mode 100644 Bonobo.Git.Server/App_Start/DoesControllerExistConstraint.cs diff --git a/Bonobo.Git.Server/App_GlobalResources/Resources.Designer.cs b/Bonobo.Git.Server/App_GlobalResources/Resources.Designer.cs index eafec560a..98c1278c7 100644 --- a/Bonobo.Git.Server/App_GlobalResources/Resources.Designer.cs +++ b/Bonobo.Git.Server/App_GlobalResources/Resources.Designer.cs @@ -19,7 +19,7 @@ namespace Bonobo.Git.Server.App_GlobalResources { // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] public class Resources { @@ -1331,6 +1331,24 @@ public static string Repository_Create_Informations { } } + /// + /// Looks up a localized string similar to Invalid root repo folder. Choose a different folder name.. + /// + public static string Repository_Create_IsAControllerNameFailure { + get { + return ResourceManager.GetString("Repository_Create_IsAControllerNameFailure", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Must have an extension of .git. + /// + public static string Repository_Create_NameExtensionFailure { + get { + return ResourceManager.GetString("Repository_Create_NameExtensionFailure", resourceCulture); + } + } + /// /// Looks up a localized string similar to "Name" shouldn't contain only whitespace characters.. /// diff --git a/Bonobo.Git.Server/App_GlobalResources/Resources.resx b/Bonobo.Git.Server/App_GlobalResources/Resources.resx index 4fc18d6f0..d5c31f56b 100644 --- a/Bonobo.Git.Server/App_GlobalResources/Resources.resx +++ b/Bonobo.Git.Server/App_GlobalResources/Resources.resx @@ -957,4 +957,10 @@ The regular C# string formatting rules apply: To get {} in your link you need to URL to a CSS file to use for the site. Site-relative (~) paths are supported. + + Must have an extension of .git + + + Invalid root repo folder. Choose a different folder name. + \ No newline at end of file diff --git a/Bonobo.Git.Server/App_Start/DoesControllerExistConstraint.cs b/Bonobo.Git.Server/App_Start/DoesControllerExistConstraint.cs new file mode 100644 index 000000000..744a761fc --- /dev/null +++ b/Bonobo.Git.Server/App_Start/DoesControllerExistConstraint.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Web; +using System.Web.Routing; + +namespace Bonobo.Git.Server.App_Start +{ + /// + /// Checks if controller and action is defined + /// + public class DoesControllerExistConstraint : IRouteConstraint + { + public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) + { + if (routeDirection == RouteDirection.IncomingRequest) + { + var action = values["action"] as string; + var controller = values["controller"] as string; + + return DoesControllerExist(controller, action); + } + else + return true; + } + + public static bool DoesControllerExist(string controller, string action = null) + { + if (controller is null) + return false; + + var controllerFullName = string.Format("Bonobo.Git.Server.Controllers.{0}Controller", controller); + + var cont = Assembly.GetExecutingAssembly().GetType(controllerFullName); + + return cont != null && !string.IsNullOrEmpty(action) ? cont.GetMethod(action) != null : true; + } + } +} \ No newline at end of file diff --git a/Bonobo.Git.Server/App_Start/RouteConfig.cs b/Bonobo.Git.Server/App_Start/RouteConfig.cs index fafd1d6be..9febd6220 100644 --- a/Bonobo.Git.Server/App_Start/RouteConfig.cs +++ b/Bonobo.Git.Server/App_Start/RouteConfig.cs @@ -8,26 +8,6 @@ public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { - routes.MapRoute("SecureInfoRefs", - "{repositoryName}.git/info/refs", - new { controller = "Git", action = "SecureGetInfoRefs" }, - new { method = new HttpMethodConstraint("GET") }); - - routes.MapRoute("SecureUploadPack", - "{repositoryName}.git/git-upload-pack", - new { controller = "Git", action = "SecureUploadPack" }, - new { method = new HttpMethodConstraint("POST") }); - - routes.MapRoute("SecureReceivePack", - "{repositoryName}.git/git-receive-pack", - new { controller = "Git", action = "SecureReceivePack" }, - new { method = new HttpMethodConstraint("POST") }); - - routes.MapRoute("GitBaseUrl", - "{repositoryName}.git", - new { controller = "Git", action = "GitUrl" }, - new { method = new HttpMethodConstraint("GET") }); - routes.MapRoute("IndexRoute", "{controller}/Index/", new { action = "Index" }); @@ -90,11 +70,22 @@ public static void RegisterRoutes(RouteCollection routes) routes.MapRoute("RepoCommits", "Repository/Commits/{id}", - new { controller = "Repository", action = "Commits", id = string.Empty}); + new { controller = "Repository", action = "Commits", id = string.Empty }); routes.MapRoute("Default", "{controller}/{action}/{id}", - new { controller = "Home", action = "Index", id = String.Empty }); + new { controller = "Home", action = "Index", id = String.Empty }, + new { action = new DoesControllerExistConstraint() }); //Route constraints + + routes.MapRoute("GitBaseUrl", + "{repositoryName}", + new { controller = "Git", action = "GitUrl" }, + new { method = new HttpMethodConstraint("GET") }); + + routes.MapRoute( + "GitRepoRouting", + "{*url}", + new { controller = "Git", action = "Index" }); routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); diff --git a/Bonobo.Git.Server/Attributes/FileNameAttribute.cs b/Bonobo.Git.Server/Attributes/FileNameAttribute.cs index 731f4cdf1..eea8257fa 100644 --- a/Bonobo.Git.Server/Attributes/FileNameAttribute.cs +++ b/Bonobo.Git.Server/Attributes/FileNameAttribute.cs @@ -9,7 +9,7 @@ public override bool IsValid(object value) { if (value != null) { - return value.ToString().IndexOfAny(Path.GetInvalidFileNameChars()) == -1; + return value.ToString().IndexOfAny(Path.GetInvalidPathChars()) == -1; } return base.IsValid(null); diff --git a/Bonobo.Git.Server/Attributes/GitAuthorizeAttribute.cs b/Bonobo.Git.Server/Attributes/GitAuthorizeAttribute.cs index f0b547206..cdcf4abf5 100644 --- a/Bonobo.Git.Server/Attributes/GitAuthorizeAttribute.cs +++ b/Bonobo.Git.Server/Attributes/GitAuthorizeAttribute.cs @@ -9,6 +9,9 @@ using Microsoft.Practices.Unity; using Bonobo.Git.Server.Helpers; using Serilog; +using System.Linq; +using System.IO; +using Bonobo.Git.Server.Configuration; namespace Bonobo.Git.Server { @@ -26,10 +29,22 @@ public class GitAuthorizeAttribute : AuthorizeAttribute [Dependency] public IRepositoryRepository RepositoryRepository { get; set; } - public static string GetRepoPath(string path, string applicationPath) + public static string GetRepoPath(string url, string applicationPath) { - var repo = path.Replace(applicationPath, "").Replace("/",""); - return repo.Substring(0, repo.IndexOf(".git")); + var gitStartIndex = url.IndexOf(".git"); + if (gitStartIndex >= 0) + { + var repositoryPath = url.TrimStart(applicationPath.ToCharArray()).Substring(0, gitStartIndex + 4).Replace('/', '\\').TrimEnd('\\'); + + string path = Path.Combine(UserConfiguration.Current.Repositories, repositoryPath); + + if (Directory.Exists(path)) + { + return repositoryPath; + } + } + + return null; } public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext) @@ -42,6 +57,7 @@ public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterC HttpContextBase httpContext = filterContext.HttpContext; string incomingRepoName = GetRepoPath(httpContext.Request.Path, httpContext.Request.ApplicationPath); + string repoName = Repository.NormalizeRepositoryName(incomingRepoName, RepositoryRepository); // Add header to prevent redirection to login page even if we fail auth later (see IAuthenticationProvider.Configure) diff --git a/Bonobo.Git.Server/Bonobo.Git.Server.csproj b/Bonobo.Git.Server/Bonobo.Git.Server.csproj index 32725905f..840f3783a 100644 --- a/Bonobo.Git.Server/Bonobo.Git.Server.csproj +++ b/Bonobo.Git.Server/Bonobo.Git.Server.csproj @@ -1,8 +1,8 @@  + - @@ -47,6 +47,8 @@ prompt 4 false + true + true pdbonly @@ -82,9 +84,9 @@ ..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll True - - ..\packages\LibGit2Sharp.0.23.1\lib\net40\LibGit2Sharp.dll - True + + ..\packages\LibGit2Sharp.0.26.2\lib\net46\LibGit2Sharp.dll + False ..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.5\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll @@ -272,6 +274,7 @@ + @@ -703,7 +706,9 @@ - + + Designer + @@ -769,10 +774,10 @@ xcopy/s /y /d "$(SolutionDir)packages\LibGit2Sharp.0.21.0.176\lib\net40\NativeBi This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - + \ No newline at end of file diff --git a/Bonobo.Git.Server/Configuration/AuthenticationSettings.cs b/Bonobo.Git.Server/Configuration/AuthenticationSettings.cs index e20347367..8a9d4c090 100644 --- a/Bonobo.Git.Server/Configuration/AuthenticationSettings.cs +++ b/Bonobo.Git.Server/Configuration/AuthenticationSettings.cs @@ -8,6 +8,7 @@ public static class AuthenticationSettings public static string MembershipService { get; private set; } public static string AuthenticationProvider { get; private set; } public static bool ImportWindowsAuthUsersAsAdmin { get; private set; } + public static string EmailDomain { get; } public static bool DemoModeActive { get; private set; } static AuthenticationSettings() @@ -15,6 +16,7 @@ static AuthenticationSettings() MembershipService = ConfigurationManager.AppSettings["MembershipService"]; AuthenticationProvider = ConfigurationManager.AppSettings["AuthenticationProvider"]; ImportWindowsAuthUsersAsAdmin = Convert.ToBoolean(ConfigurationManager.AppSettings["ImportWindowsAuthUsersAsAdmin"]); + EmailDomain = ConfigurationManager.AppSettings["EmailDomain"]; DemoModeActive = Convert.ToBoolean(ConfigurationManager.AppSettings["demoModeActive"]); } } diff --git a/Bonobo.Git.Server/Controllers/AccountController.cs b/Bonobo.Git.Server/Controllers/AccountController.cs index 72b1d847d..b11dfbe49 100644 --- a/Bonobo.Git.Server/Controllers/AccountController.cs +++ b/Bonobo.Git.Server/Controllers/AccountController.cs @@ -187,10 +187,10 @@ public ActionResult Edit(UserEditModel model) public ActionResult CreateADUser() { var efms = MembershipService as EFMembershipService; - + if ((!Request.IsAuthenticated) || efms == null) { - Log.Warning("CreateADUser: can't run IsAuth: {IsAuth}, MemServ {MemServ}", + Log.Warning("CreateADUser: can't run IsAuth: {IsAuth}, MemServ {MemServ}", Request.IsAuthenticated, MembershipService.GetType()); return RedirectToAction("Unauthorized", "Home"); @@ -201,6 +201,13 @@ public ActionResult CreateADUser() if (adUser != null) { var userId = adUser.Guid.GetValueOrDefault(Guid.NewGuid()); + + if (string.IsNullOrEmpty(adUser.EmailAddress)) + { + var username = credentials.Split('\\')[1]; + adUser.EmailAddress = $"{username}@{AuthenticationSettings.EmailDomain}"; + } + if (MembershipService.CreateUser(credentials, Guid.NewGuid().ToString(), adUser.GivenName, adUser.Surname, adUser.EmailAddress, userId)) { // 2 because we just added the user and there is the default admin user. @@ -209,7 +216,7 @@ public ActionResult CreateADUser() Log.Information("Making AD user {User} into an admin", credentials); var id = MembershipService.GetUserModel(credentials).Id; - RoleProvider.AddUserToRoles(id, new[] {Definitions.Roles.Administrator}); + RoleProvider.AddUserToRoles(id, new[] { Definitions.Roles.Administrator }); // Add the administrator role to the Identity/cookie var Identity = (ClaimsIdentity)User.Identity; diff --git a/Bonobo.Git.Server/Controllers/GitController.cs b/Bonobo.Git.Server/Controllers/GitController.cs index 91bd95566..f68814126 100644 --- a/Bonobo.Git.Server/Controllers/GitController.cs +++ b/Bonobo.Git.Server/Controllers/GitController.cs @@ -31,7 +31,43 @@ public class GitController : Controller [Dependency] public IGitService GitService { get; set; } - public ActionResult SecureGetInfoRefs(String repositoryName, String service) + public ActionResult Index(string url) + { + if (url != null) + { + var gitStartIndex = url.IndexOf(".git"); + if (gitStartIndex >= 0) + { + var repositoryPath = url.Substring(0, gitStartIndex + 4).Replace('/', '\\'); + + string path = Path.Combine(UserConfiguration.Current.Repositories, repositoryPath); + + if (Directory.Exists(path)) + { + return RedirectGitQuery(url, repositoryPath); + } + } + } + + return RedirectToAction("Index", "Home"); + } + + private ActionResult RedirectGitQuery(string url, string repositoryPath) + { + var queryString = Request.QueryString; + + if (url.EndsWith("/info/refs")) + return SecureGetInfoRefs(repositoryPath, queryString["service"]); + else if (url.EndsWith("/git-upload-pack")) + return SecureUploadPack(repositoryPath); + else if (url.EndsWith("/git-receive-pack")) + return SecureReceivePack(repositoryPath); + else + return new HttpNotFoundResult(); + } + + [HttpGet] + private ActionResult SecureGetInfoRefs(String repositoryName, String service) { bool isPush = String.Equals("git-receive-pack", service, StringComparison.OrdinalIgnoreCase); @@ -72,7 +108,7 @@ public ActionResult SecureGetInfoRefs(String repositoryName, String service) } [HttpPost] - public ActionResult SecureUploadPack(String repositoryName) + private ActionResult SecureUploadPack(String repositoryName) { if (!RepositoryIsValid(repositoryName)) { @@ -90,7 +126,7 @@ public ActionResult SecureUploadPack(String repositoryName) } [HttpPost] - public ActionResult SecureReceivePack(String repositoryName) + private ActionResult SecureReceivePack(String repositoryName) { if (!RepositoryIsValid(repositoryName)) { diff --git a/Bonobo.Git.Server/Controllers/RepositoryController.cs b/Bonobo.Git.Server/Controllers/RepositoryController.cs index a4d52ee3b..8047b0dce 100644 --- a/Bonobo.Git.Server/Controllers/RepositoryController.cs +++ b/Bonobo.Git.Server/Controllers/RepositoryController.cs @@ -16,6 +16,7 @@ using Microsoft.Practices.Unity; using MimeTypes; using System.Security.Principal; +using Bonobo.Git.Server.App_Start; namespace Bonobo.Git.Server.Controllers { @@ -143,15 +144,27 @@ public ActionResult Create(RepositoryDetailModel model) return RedirectToAction("Unauthorized", "Home"); } - if (model != null && !String.IsNullOrEmpty(model.Name)) + bool isAControllerPath = false; + if (model != null && !string.IsNullOrEmpty(model.Name)) { model.Name = Regex.Replace(model.Name, @"\s", ""); + model.Name = model.Name.Replace('/', '\\'); + var rootDir = model.Name.Split('\\').FirstOrDefault(); + isAControllerPath = DoesControllerExistConstraint.DoesControllerExist(rootDir); } - if (model != null && String.IsNullOrEmpty(model.Name)) + if (model != null && string.IsNullOrEmpty(model.Name)) { ModelState.AddModelError("Name", Resources.Repository_Create_NameFailure); } + else if (model != null && !model.Name.Contains(".git")) + { + ModelState.AddModelError("Name", Resources.Repository_Create_NameExtensionFailure); + } + else if (model != null && isAControllerPath) + { + ModelState.AddModelError("Name", Resources.Repository_Create_IsAControllerNameFailure); + } else if (ModelState.IsValid) { @@ -242,11 +255,15 @@ void SetGitUrls(RepositoryDetailModel model) Request.ApplicationPath == "/" ? "" : Request.ApplicationPath ); - model.GitUrl = String.Concat(serverAddress, model.Name, ".git"); + var path = model.Name.Replace('\\', '/'); + + model.GitUrl = string.Concat(serverAddress, path); if (User.Identity.IsAuthenticated) { + var usermodel = MembershipService.GetUserModel(User.Username()); + model.PersonalGitUrl = - String.Concat(serverAddress.Replace("://", "://" + Uri.EscapeDataString(User.Username()) + "@"), model.Name, ".git"); + string.Concat(serverAddress.Replace("://", "://" + Uri.EscapeDataString(usermodel.Username) + "@"), path); } } @@ -549,12 +566,12 @@ public ActionResult Clone(Guid id, RepositoryDetailModel model) return RedirectToAction("Unauthorized", "Home"); } - if (model != null && !String.IsNullOrEmpty(model.Name)) + if (model != null && !string.IsNullOrEmpty(model.Name)) { model.Name = Regex.Replace(model.Name, @"\s", ""); } - if (model != null && String.IsNullOrEmpty(model.Name)) + if (model != null && string.IsNullOrEmpty(model.Name)) { ModelState.AddModelError("Name", Resources.Repository_Create_NameFailure); } diff --git a/Bonobo.Git.Server/Data/Update/RepositorySynchronizer.cs b/Bonobo.Git.Server/Data/Update/RepositorySynchronizer.cs index 08fec60f4..9c5e7f2ba 100644 --- a/Bonobo.Git.Server/Data/Update/RepositorySynchronizer.cs +++ b/Bonobo.Git.Server/Data/Update/RepositorySynchronizer.cs @@ -27,12 +27,12 @@ private void CheckForNewRepositories() // as this would make it impossible to start the server return; } - IEnumerable directories = Directory.EnumerateDirectories(UserConfiguration.Current.Repositories); + IEnumerable directories = Directory.EnumerateDirectories(UserConfiguration.Current.Repositories, "*.git", SearchOption.AllDirectories); foreach (string directory in directories) { - string name = Path.GetFileName(directory); + string path = directory.Remove(0, UserConfiguration.Current.Repositories.Length + "\\".Length); - RepositoryModel repository = _repositoryRepository.GetRepository(name); + RepositoryModel repository = _repositoryRepository.GetRepository(path); if (repository == null) { if (LibGit2Sharp.Repository.IsValid(directory)) @@ -40,7 +40,7 @@ private void CheckForNewRepositories() repository = new RepositoryModel(); repository.Id = Guid.NewGuid(); repository.Description = "Discovered in file system."; - repository.Name = name; + repository.Name = path; repository.AnonymousAccess = false; repository.Users = new UserModel[0]; repository.Teams = new TeamModel[0]; diff --git a/Bonobo.Git.Server/Models/RepositoryModels.cs b/Bonobo.Git.Server/Models/RepositoryModels.cs index ae815bedc..c814815c2 100644 --- a/Bonobo.Git.Server/Models/RepositoryModels.cs +++ b/Bonobo.Git.Server/Models/RepositoryModels.cs @@ -50,7 +50,7 @@ public bool NameIsValid { // Check for an exact match, not just a substring hit - based on RegularExpressionAttribute Match match = Regex.Match(Name, NameValidityRegex); - return match.Success && match.Index == 0 && match.Length == Name.Length; + return match.Success; } } @@ -80,7 +80,7 @@ public void EnsureCollectionsAreValid() } } - public const string NameValidityRegex = @"([\w\.-])*([\w])$"; + public const string NameValidityRegex = @".*([\w\.-])*([\w])$"; } public class RepositoryDetailModel diff --git a/Bonobo.Git.Server/packages.config b/Bonobo.Git.Server/packages.config index 3fb6f6e4b..fe20eaccb 100644 --- a/Bonobo.Git.Server/packages.config +++ b/Bonobo.Git.Server/packages.config @@ -6,8 +6,8 @@ - - + + diff --git a/Bonobo.Git.Server/web.config b/Bonobo.Git.Server/web.config index c41892d6a..e897b66b6 100644 --- a/Bonobo.Git.Server/web.config +++ b/Bonobo.Git.Server/web.config @@ -20,9 +20,14 @@ --> + + + --> + + + + From ae471238237dc70733c1187455cf4f9bc6b4eaec Mon Sep 17 00:00:00 2001 From: Brian Mello Date: Mon, 9 Nov 2020 15:19:23 -0500 Subject: [PATCH 02/10] Prevent finding of invalid repos --- Bonobo.Git.Server/Data/Update/RepositorySynchronizer.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Bonobo.Git.Server/Data/Update/RepositorySynchronizer.cs b/Bonobo.Git.Server/Data/Update/RepositorySynchronizer.cs index 9c5e7f2ba..8ae812b56 100644 --- a/Bonobo.Git.Server/Data/Update/RepositorySynchronizer.cs +++ b/Bonobo.Git.Server/Data/Update/RepositorySynchronizer.cs @@ -1,4 +1,5 @@ -using Bonobo.Git.Server.Configuration; +using Bonobo.Git.Server.App_Start; +using Bonobo.Git.Server.Configuration; using Bonobo.Git.Server.Models; using LibGit2Sharp; using System; @@ -30,6 +31,12 @@ private void CheckForNewRepositories() IEnumerable directories = Directory.EnumerateDirectories(UserConfiguration.Current.Repositories, "*.git", SearchOption.AllDirectories); foreach (string directory in directories) { + var repoPath = directory.Remove(0, UserConfiguration.Current.Repositories.Length); + var rootDir = repoPath.Split('\\').FirstOrDefault(); + + if (DoesControllerExistConstraint.DoesControllerExist(rootDir)) + continue; //Do not load as a valid repo + string path = directory.Remove(0, UserConfiguration.Current.Repositories.Length + "\\".Length); RepositoryModel repository = _repositoryRepository.GetRepository(path); From 554f7f31a31ff221a0d93924f025230ab5f99754 Mon Sep 17 00:00:00 2001 From: Brian Mello Date: Mon, 9 Nov 2020 15:38:04 -0500 Subject: [PATCH 03/10] Fixed unit test --- .../UnitTests/GitAuthorizeAttributeTest.cs | 8 ++++---- .../Attributes/GitAuthorizeAttribute.cs | 19 ++++++++++++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Bonobo.Git.Server.Test/UnitTests/GitAuthorizeAttributeTest.cs b/Bonobo.Git.Server.Test/UnitTests/GitAuthorizeAttributeTest.cs index e7081ef95..3a61af86e 100644 --- a/Bonobo.Git.Server.Test/UnitTests/GitAuthorizeAttributeTest.cs +++ b/Bonobo.Git.Server.Test/UnitTests/GitAuthorizeAttributeTest.cs @@ -10,10 +10,10 @@ public class GitAuthorizeAttributeTest [TestMethod] public void GetRepoPathTest() { - var repo = GitAuthorizeAttribute.GetRepoPath("/other/test.git/info/refs", "/other"); - Assert.AreEqual("test", repo); - repo = GitAuthorizeAttribute.GetRepoPath("/test.git/info/refs", "/"); - Assert.AreEqual("test", repo); + var repo = GitAuthorizeAttribute.GetRepoPath("/other/test.git/info/refs", "/other", false); + Assert.AreEqual(@"\test.git", repo); + repo = GitAuthorizeAttribute.GetRepoPath("/test.git/info/refs", "/", false); + Assert.AreEqual("test.git", repo); } } } diff --git a/Bonobo.Git.Server/Attributes/GitAuthorizeAttribute.cs b/Bonobo.Git.Server/Attributes/GitAuthorizeAttribute.cs index cdcf4abf5..b44d10e65 100644 --- a/Bonobo.Git.Server/Attributes/GitAuthorizeAttribute.cs +++ b/Bonobo.Git.Server/Attributes/GitAuthorizeAttribute.cs @@ -29,19 +29,28 @@ public class GitAuthorizeAttribute : AuthorizeAttribute [Dependency] public IRepositoryRepository RepositoryRepository { get; set; } - public static string GetRepoPath(string url, string applicationPath) + public static string GetRepoPath(string url, string applicationPath, bool checkDirectory=true) { var gitStartIndex = url.IndexOf(".git"); if (gitStartIndex >= 0) { - var repositoryPath = url.TrimStart(applicationPath.ToCharArray()).Substring(0, gitStartIndex + 4).Replace('/', '\\').TrimEnd('\\'); + var repositoryPath = url.Substring(0, gitStartIndex + 4); - string path = Path.Combine(UserConfiguration.Current.Repositories, repositoryPath); + repositoryPath = applicationPath.Length ==1 ? repositoryPath.TrimStart(applicationPath.ToCharArray()) : repositoryPath.Replace(applicationPath, ""); - if (Directory.Exists(path)) + repositoryPath = repositoryPath.Replace('/', '\\').TrimEnd('\\'); + + if (checkDirectory) { - return repositoryPath; + string path = Path.Combine(UserConfiguration.Current.Repositories, repositoryPath); + + if (Directory.Exists(path)) + { + return repositoryPath; + } } + else + return repositoryPath; } return null; From a951aaa75f341ae3f3c2cd9b075c1bedcadad137 Mon Sep 17 00:00:00 2001 From: Brian Mello Date: Mon, 9 Nov 2020 20:08:40 -0500 Subject: [PATCH 04/10] If there is an ambiguous match then true controller and action exist --- .../App_Start/DoesControllerExistConstraint.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Bonobo.Git.Server/App_Start/DoesControllerExistConstraint.cs b/Bonobo.Git.Server/App_Start/DoesControllerExistConstraint.cs index 744a761fc..04f2af14b 100644 --- a/Bonobo.Git.Server/App_Start/DoesControllerExistConstraint.cs +++ b/Bonobo.Git.Server/App_Start/DoesControllerExistConstraint.cs @@ -34,7 +34,14 @@ public static bool DoesControllerExist(string controller, string action = null) var cont = Assembly.GetExecutingAssembly().GetType(controllerFullName); - return cont != null && !string.IsNullOrEmpty(action) ? cont.GetMethod(action) != null : true; + try + { + return cont != null && !string.IsNullOrEmpty(action) ? cont.GetMethod(action) != null : true; + } + catch(AmbiguousMatchException) + { + return true; + } } } } \ No newline at end of file From a55383b2af7bf30474ddf6beb6026e8831ffa59a Mon Sep 17 00:00:00 2001 From: Brian Mello Date: Mon, 9 Nov 2020 20:20:24 -0500 Subject: [PATCH 05/10] remove xmlns from project --- Bonobo.Git.Server/Bonobo.Git.Server.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bonobo.Git.Server/Bonobo.Git.Server.csproj b/Bonobo.Git.Server/Bonobo.Git.Server.csproj index 840f3783a..af4ab0b81 100644 --- a/Bonobo.Git.Server/Bonobo.Git.Server.csproj +++ b/Bonobo.Git.Server/Bonobo.Git.Server.csproj @@ -1,5 +1,5 @@  - + From 6c2461453a53542e169e4820bdc43363c1a48492 Mon Sep 17 00:00:00 2001 From: Brian Mello Date: Mon, 9 Nov 2020 20:23:18 -0500 Subject: [PATCH 06/10] Revert "remove xmlns from project" This reverts commit a55383b2af7bf30474ddf6beb6026e8831ffa59a. --- Bonobo.Git.Server/Bonobo.Git.Server.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bonobo.Git.Server/Bonobo.Git.Server.csproj b/Bonobo.Git.Server/Bonobo.Git.Server.csproj index af4ab0b81..840f3783a 100644 --- a/Bonobo.Git.Server/Bonobo.Git.Server.csproj +++ b/Bonobo.Git.Server/Bonobo.Git.Server.csproj @@ -1,5 +1,5 @@  - + From 5464b92f681e7c78142e4815aa7b27d47c71a639 Mon Sep 17 00:00:00 2001 From: Brian Mello Date: Mon, 9 Nov 2020 20:25:17 -0500 Subject: [PATCH 07/10] rolled back lib git2sharp upgrade breaks appveyor build --- Bonobo.Git.Server/packages.config | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Bonobo.Git.Server/packages.config b/Bonobo.Git.Server/packages.config index fe20eaccb..3fb6f6e4b 100644 --- a/Bonobo.Git.Server/packages.config +++ b/Bonobo.Git.Server/packages.config @@ -6,8 +6,8 @@ - - + + From f16a62cc40e59ff88a2bb578652c0168147f440f Mon Sep 17 00:00:00 2001 From: Brian Mello Date: Mon, 9 Nov 2020 20:42:52 -0500 Subject: [PATCH 08/10] Fixed a bug in the constraint class where condition was not being assessed correctly. Removed possible back slash at beginning of repo sync --- Bonobo.Git.Server/App_Start/DoesControllerExistConstraint.cs | 2 +- Bonobo.Git.Server/Data/Update/RepositorySynchronizer.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Bonobo.Git.Server/App_Start/DoesControllerExistConstraint.cs b/Bonobo.Git.Server/App_Start/DoesControllerExistConstraint.cs index 04f2af14b..128bb6552 100644 --- a/Bonobo.Git.Server/App_Start/DoesControllerExistConstraint.cs +++ b/Bonobo.Git.Server/App_Start/DoesControllerExistConstraint.cs @@ -36,7 +36,7 @@ public static bool DoesControllerExist(string controller, string action = null) try { - return cont != null && !string.IsNullOrEmpty(action) ? cont.GetMethod(action) != null : true; + return cont != null && (!string.IsNullOrEmpty(action) ? cont.GetMethod(action) != null : true); } catch(AmbiguousMatchException) { diff --git a/Bonobo.Git.Server/Data/Update/RepositorySynchronizer.cs b/Bonobo.Git.Server/Data/Update/RepositorySynchronizer.cs index 8ae812b56..d08cbe05e 100644 --- a/Bonobo.Git.Server/Data/Update/RepositorySynchronizer.cs +++ b/Bonobo.Git.Server/Data/Update/RepositorySynchronizer.cs @@ -31,7 +31,7 @@ private void CheckForNewRepositories() IEnumerable directories = Directory.EnumerateDirectories(UserConfiguration.Current.Repositories, "*.git", SearchOption.AllDirectories); foreach (string directory in directories) { - var repoPath = directory.Remove(0, UserConfiguration.Current.Repositories.Length); + var repoPath = directory.Remove(0, UserConfiguration.Current.Repositories.Length).TrimStart('\\'); var rootDir = repoPath.Split('\\').FirstOrDefault(); if (DoesControllerExistConstraint.DoesControllerExist(rootDir)) From 0d3909573577e32a5ccc7fe327944e31ce1f7635 Mon Sep 17 00:00:00 2001 From: Brian Mello Date: Tue, 10 Nov 2020 11:08:26 -0500 Subject: [PATCH 09/10] Fix git url to use correct hostname address so it doesn't show up as localhost --- .../Controllers/RepositoryController.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Bonobo.Git.Server/Controllers/RepositoryController.cs b/Bonobo.Git.Server/Controllers/RepositoryController.cs index 8047b0dce..c690a154e 100644 --- a/Bonobo.Git.Server/Controllers/RepositoryController.cs +++ b/Bonobo.Git.Server/Controllers/RepositoryController.cs @@ -17,6 +17,7 @@ using MimeTypes; using System.Security.Principal; using Bonobo.Git.Server.App_Start; +using System.Net.NetworkInformation; namespace Bonobo.Git.Server.Controllers { @@ -247,10 +248,22 @@ public ActionResult Detail(Guid id) /// void SetGitUrls(RepositoryDetailModel model) { + var host = Request.Url.Host; + + if (host == "localhost" && Request.ServerVariables["remote_host"] != "::1") + { + var networkInfo = IPGlobalProperties.GetIPGlobalProperties(); + + if(!string.IsNullOrEmpty(networkInfo.DomainName)) + host = $"{networkInfo.HostName}.{networkInfo.DomainName}"; + else + host = $"{networkInfo.HostName}"; + } + string serverAddress = System.Configuration.ConfigurationManager.AppSettings["GitServerPath"] ?? string.Format("{0}://{1}{2}{3}/", Request.Url.Scheme, - Request.Url.Host, + host, (Request.Url.IsDefaultPort ? "" : (":" + Request.Url.Port)), Request.ApplicationPath == "/" ? "" : Request.ApplicationPath ); From 4bfd78f141dea921f15d3d360cfe8685cc9f8c35 Mon Sep 17 00:00:00 2001 From: Brian Mello Date: Thu, 19 Nov 2020 14:05:51 -0500 Subject: [PATCH 10/10] Fixed a bug where invalid file was being read in AD backend --- Bonobo.Git.Server/Controllers/SettingsController.cs | 2 ++ .../Data/Update/ADBackend/UpdateADBackend.cs | 2 +- .../Data/Update/RepositorySynchronizer.cs | 10 ++++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Bonobo.Git.Server/Controllers/SettingsController.cs b/Bonobo.Git.Server/Controllers/SettingsController.cs index 54a9b9616..e33267130 100644 --- a/Bonobo.Git.Server/Controllers/SettingsController.cs +++ b/Bonobo.Git.Server/Controllers/SettingsController.cs @@ -10,6 +10,7 @@ using Bonobo.Git.Server.App_GlobalResources; using Bonobo.Git.Server.Configuration; using Bonobo.Git.Server.Models; +using Serilog; namespace Bonobo.Git.Server.Controllers { @@ -76,6 +77,7 @@ public ActionResult Index(GlobalSettingsModel model) } else { + Log.Error($"Is Path Rooted: {Path.IsPathRooted(model.RepositoryPath)} Path: {model.RepositoryPath}"); ModelState.AddModelError("RepositoryPath", Resources.Settings_RepositoryPathNotExists); } } diff --git a/Bonobo.Git.Server/Data/Update/ADBackend/UpdateADBackend.cs b/Bonobo.Git.Server/Data/Update/ADBackend/UpdateADBackend.cs index ba8986aad..92313cb25 100644 --- a/Bonobo.Git.Server/Data/Update/ADBackend/UpdateADBackend.cs +++ b/Bonobo.Git.Server/Data/Update/ADBackend/UpdateADBackend.cs @@ -80,7 +80,7 @@ private static bool BackendSubDirectoryNeedsUpdating(string backendDirectory, try { var x = JsonConvert.DeserializeObject(File.ReadAllText(jsonfile.FullName)); - if (x.Id == Guid.Empty) + if (x == null || x.Id == Guid.Empty) { // No GUID - we need to convert return true; diff --git a/Bonobo.Git.Server/Data/Update/RepositorySynchronizer.cs b/Bonobo.Git.Server/Data/Update/RepositorySynchronizer.cs index d08cbe05e..df2c8879e 100644 --- a/Bonobo.Git.Server/Data/Update/RepositorySynchronizer.cs +++ b/Bonobo.Git.Server/Data/Update/RepositorySynchronizer.cs @@ -2,6 +2,7 @@ using Bonobo.Git.Server.Configuration; using Bonobo.Git.Server.Models; using LibGit2Sharp; +using Serilog; using System; using System.Collections.Generic; using System.IO; @@ -24,6 +25,7 @@ private void CheckForNewRepositories() { if (!Directory.Exists(UserConfiguration.Current.Repositories)) { + Log.Error($"Repo root doesn't exist: {UserConfiguration.Current.Repositories}"); // We don't want an exception if the repo dir no longer exists, // as this would make it impossible to start the server return; @@ -34,12 +36,12 @@ private void CheckForNewRepositories() var repoPath = directory.Remove(0, UserConfiguration.Current.Repositories.Length).TrimStart('\\'); var rootDir = repoPath.Split('\\').FirstOrDefault(); + Log.Debug($"Repo {repoPath}"); + if (DoesControllerExistConstraint.DoesControllerExist(rootDir)) continue; //Do not load as a valid repo - string path = directory.Remove(0, UserConfiguration.Current.Repositories.Length + "\\".Length); - - RepositoryModel repository = _repositoryRepository.GetRepository(path); + RepositoryModel repository = _repositoryRepository.GetRepository(repoPath); if (repository == null) { if (LibGit2Sharp.Repository.IsValid(directory)) @@ -47,7 +49,7 @@ private void CheckForNewRepositories() repository = new RepositoryModel(); repository.Id = Guid.NewGuid(); repository.Description = "Discovered in file system."; - repository.Name = path; + repository.Name = repoPath; repository.AnonymousAccess = false; repository.Users = new UserModel[0]; repository.Teams = new TeamModel[0];