From 51506f3a6cc48e1abef92bfc7a1a7745c67073d8 Mon Sep 17 00:00:00 2001 From: David Linares <28559777+dlinares-linux@users.noreply.github.com> Date: Fri, 3 May 2024 16:57:40 +0900 Subject: [PATCH] Reverse logic for secret scanning alert location matching --- .../Services/SecretScanningAlertService.cs | 58 ++++++++++--------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/src/Octoshift/Services/SecretScanningAlertService.cs b/src/Octoshift/Services/SecretScanningAlertService.cs index f10b5545f..0b33440cd 100644 --- a/src/Octoshift/Services/SecretScanningAlertService.cs +++ b/src/Octoshift/Services/SecretScanningAlertService.cs @@ -83,29 +83,16 @@ private AlertWithLocations MatchTargetSecret(AlertWithLocations source, List + /// Determine whether or not the locations for a source and target secret scanning alerts match + /// + /// List of locations from a source secret scanning alert + /// List of locations from a target secret scanning alert + /// Boolean indicating if locations match + private bool AreSecretAlertLocationsMatching(GithubSecretScanningAlertLocation[] sourceLocations, GithubSecretScanningAlertLocation[] targetLocations) { - // We cannot guarantee the ordering of things with the locations and the APIs, typically they would match, but cannot be sure - // so we need to iterate over all the targets to ensure a match - return targetLocations.Any( - target => sourceLocation.Path == target.Path - && sourceLocation.StartLine == target.StartLine - && sourceLocation.EndLine == target.EndLine - && sourceLocation.StartColumn == target.StartColumn - && sourceLocation.EndColumn == target.EndColumn - && sourceLocation.BlobSha == target.BlobSha - // Technically this wil hold, but only if there is not commit rewriting going on, so we need to make this last one optional for now - // && sourceDetails.CommitSha == target.Details.CommitSha) + var locationMatch = true; + // Right after a code migration, as not all content gets migrated, the number of locations + // in the source alert will always be greater or equal to the number of locations in the + // target alert, hence looping through the target alert locations. + foreach (var targetLocation in targetLocations) + { + locationMatch = sourceLocations.Any( + sourceLocation => sourceLocation.Path == targetLocation.Path + && sourceLocation.StartLine == targetLocation.StartLine + && sourceLocation.EndLine == targetLocation.EndLine + && sourceLocation.StartColumn == targetLocation.StartColumn + && sourceLocation.EndColumn == targetLocation.EndColumn + && sourceLocation.BlobSha == targetLocation.BlobSha + // Technically this will hold, but only if there is not commit rewriting going on, so we need to make this last one optional for now + // && sourceLocation.CommitSha == targetLocation.CommitSha) ); + if (!locationMatch) + { + break; + } + } + + return locationMatch; } private async Task> GetAlertsWithLocations(GithubApi api, string org, string repo)