execabs.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2020 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package execabs is a drop-in replacement for os/exec
  5. // that requires PATH lookups to find absolute paths.
  6. // That is, execabs.Command("cmd") runs the same PATH lookup
  7. // as exec.Command("cmd"), but if the result is a path
  8. // which is relative, the Run and Start methods will report
  9. // an error instead of running the executable.
  10. //
  11. // See https://blog.golang.org/path-security for more information
  12. // about when it may be necessary or appropriate to use this package.
  13. package execabs
  14. import (
  15. "context"
  16. "fmt"
  17. "os/exec"
  18. "path/filepath"
  19. "reflect"
  20. "unsafe"
  21. )
  22. // ErrNotFound is the error resulting if a path search failed to find an executable file.
  23. // It is an alias for exec.ErrNotFound.
  24. var ErrNotFound = exec.ErrNotFound
  25. // Cmd represents an external command being prepared or run.
  26. // It is an alias for exec.Cmd.
  27. type Cmd = exec.Cmd
  28. // Error is returned by LookPath when it fails to classify a file as an executable.
  29. // It is an alias for exec.Error.
  30. type Error = exec.Error
  31. // An ExitError reports an unsuccessful exit by a command.
  32. // It is an alias for exec.ExitError.
  33. type ExitError = exec.ExitError
  34. func relError(file, path string) error {
  35. return fmt.Errorf("%s resolves to executable in current directory (.%c%s)", file, filepath.Separator, path)
  36. }
  37. // LookPath searches for an executable named file in the directories
  38. // named by the PATH environment variable. If file contains a slash,
  39. // it is tried directly and the PATH is not consulted. The result will be
  40. // an absolute path.
  41. //
  42. // LookPath differs from exec.LookPath in its handling of PATH lookups,
  43. // which are used for file names without slashes. If exec.LookPath's
  44. // PATH lookup would have returned an executable from the current directory,
  45. // LookPath instead returns an error.
  46. func LookPath(file string) (string, error) {
  47. path, err := exec.LookPath(file)
  48. if err != nil && !isGo119ErrDot(err) {
  49. return "", err
  50. }
  51. if filepath.Base(file) == file && !filepath.IsAbs(path) {
  52. return "", relError(file, path)
  53. }
  54. return path, nil
  55. }
  56. func fixCmd(name string, cmd *exec.Cmd) {
  57. if filepath.Base(name) == name && !filepath.IsAbs(cmd.Path) && !isGo119ErrFieldSet(cmd) {
  58. // exec.Command was called with a bare binary name and
  59. // exec.LookPath returned a path which is not absolute.
  60. // Set cmd.lookPathErr and clear cmd.Path so that it
  61. // cannot be run.
  62. lookPathErr := (*error)(unsafe.Pointer(reflect.ValueOf(cmd).Elem().FieldByName("lookPathErr").Addr().Pointer()))
  63. if *lookPathErr == nil {
  64. *lookPathErr = relError(name, cmd.Path)
  65. }
  66. cmd.Path = ""
  67. }
  68. }
  69. // CommandContext is like Command but includes a context.
  70. //
  71. // The provided context is used to kill the process (by calling os.Process.Kill)
  72. // if the context becomes done before the command completes on its own.
  73. func CommandContext(ctx context.Context, name string, arg ...string) *exec.Cmd {
  74. cmd := exec.CommandContext(ctx, name, arg...)
  75. fixCmd(name, cmd)
  76. return cmd
  77. }
  78. // Command returns the Cmd struct to execute the named program with the given arguments.
  79. // See exec.Command for most details.
  80. //
  81. // Command differs from exec.Command in its handling of PATH lookups,
  82. // which are used when the program name contains no slashes.
  83. // If exec.Command would have returned an exec.Cmd configured to run an
  84. // executable from the current directory, Command instead
  85. // returns an exec.Cmd that will return an error from Start or Run.
  86. func Command(name string, arg ...string) *exec.Cmd {
  87. cmd := exec.Command(name, arg...)
  88. fixCmd(name, cmd)
  89. return cmd
  90. }