@@ -345,19 +345,15 @@ public final class ExecService {
345345 }
346346
347347 func matchesCommandIdentity( _ candidate: URL ) -> Bool {
348- if !command0. contains ( " / " ) {
349- return candidate. resolvingSymlinksInPath ( ) . lastPathComponent == command0
350- }
351-
352- let declared : URL
353- if command0. hasPrefix ( " ./ " ) || command0. hasPrefix ( " ../ " ) {
354- let base = workingDirectory ?? URL ( fileURLWithPath: fm. currentDirectoryPath)
355- declared = base. appendingPathComponent ( command0)
356- } else {
357- declared = URL ( fileURLWithPath: ( command0 as NSString ) . expandingTildeInPath)
358- }
359- return candidate. standardizedFileURL. resolvingSymlinksInPath ( ) . path
360- == declared. standardizedFileURL. resolvingSymlinksInPath ( ) . path
348+ let trustedBareCommand = command0. contains ( " / " )
349+ ? nil
350+ : searchPATH ( for: command0, executableURL: executableURL)
351+ return executableMatchesCommandIdentity (
352+ command0: command0,
353+ candidate: candidate,
354+ workingDirectory: workingDirectory,
355+ trustedBareCommand: trustedBareCommand
356+ )
361357 }
362358
363359 func authorizedExecutableURL( atPath path: String ) -> URL ? {
@@ -411,6 +407,33 @@ public final class ExecService {
411407 return searchPATH ( for: command0, executableURL: executableURL)
412408 }
413409
410+ static func executableMatchesCommandIdentity(
411+ command0: String ,
412+ candidate: URL ,
413+ workingDirectory: URL ? ,
414+ trustedBareCommand: URL ?
415+ ) -> Bool {
416+ let canonicalCandidate = candidate. standardizedFileURL. resolvingSymlinksInPath ( )
417+ if !command0. contains ( " / " ) {
418+ guard candidate. lastPathComponent == command0 else { return false }
419+ if canonicalCandidate. lastPathComponent == command0 { return true }
420+ guard let trustedBareCommand else { return false }
421+ return canonicalCandidate. path
422+ == trustedBareCommand. standardizedFileURL. resolvingSymlinksInPath ( ) . path
423+ }
424+
425+ let declared : URL
426+ if command0. hasPrefix ( " ./ " ) || command0. hasPrefix ( " ../ " ) {
427+ let base = workingDirectory
428+ ?? URL ( fileURLWithPath: FileManager . default. currentDirectoryPath)
429+ declared = base. appendingPathComponent ( command0)
430+ } else {
431+ declared = URL ( fileURLWithPath: ( command0 as NSString ) . expandingTildeInPath)
432+ }
433+ return canonicalCandidate. path
434+ == declared. standardizedFileURL. resolvingSymlinksInPath ( ) . path
435+ }
436+
414437 private static func searchPATH(
415438 for name: String ,
416439 executableURL: ( String ) -> URL ?
0 commit comments