From fe479a64d210be406e3d1e7ede2ed79eaba5a08e Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Thu, 6 Sep 2018 20:45:36 -0400 Subject: [PATCH] Prevent using CheckoutSha to checkout a reference and vice versa --- .../Controllers/RepositoryController.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Tgstation.Server.Host/Controllers/RepositoryController.cs b/src/Tgstation.Server.Host/Controllers/RepositoryController.cs index 034ba36ffb..721e8b6bd7 100644 --- a/src/Tgstation.Server.Host/Controllers/RepositoryController.cs +++ b/src/Tgstation.Server.Host/Controllers/RepositoryController.cs @@ -440,10 +440,16 @@ namespace Tgstation.Server.Host.Controllers //checkout/hard reset if (modelHasShaOrReference) { - if ((model.CheckoutSha != null && repo.Head.ToUpperInvariant() != model.CheckoutSha.ToUpperInvariant()) - || (model.Reference != null && repo.Reference != model.Reference)) + if ((model.CheckoutSha != null && repo.Head.ToUpperInvariant().StartsWith(model.CheckoutSha.ToUpperInvariant(), StringComparison.Ordinal)) + || (model.Reference != null && repo.Reference.ToUpperInvariant() != model.Reference.ToUpperInvariant())) { - await repo.CheckoutObject(model.CheckoutSha ?? model.Reference, ct).ConfigureAwait(false); + var committish = model.CheckoutSha ?? model.Reference; + var isSha = await repo.IsSha(committish, cancellationToken).ConfigureAwait(false); + + if ((isSha && model.Reference != null) || (!isSha && model.CheckoutSha != null)) + throw new JobException("Attempted to checkout a SHA or reference that was actually the opposite!"); + + await repo.CheckoutObject(committish, ct).ConfigureAwait(false); await LoadRevisionInformation(repo, databaseContext, attachedInstance, null, x => lastRevisionInfo = x, ct).ConfigureAwait(false); //we've either seen origin before or what we're checking out is on origin }