fix: properly trim spaces so multi-line shebang executes (#10146)

This commit is contained in:
Kyle Carberry 2023-10-09 14:19:57 -05:00 committed by GitHub
parent 3bbfcc593e
commit 54648b90ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 2 deletions

View File

@ -519,13 +519,16 @@ func (s *Server) CreateCommand(ctx context.Context, script string, env []string)
name := shell
args := []string{caller, script}
// A preceding space is generally not idiomatic for a shebang,
// but in Terraform it's quite standard to use <<EOF for a multi-line
// string which would indent with spaces, so we accept it for user-ease.
if strings.HasPrefix(strings.TrimSpace(script), "#!") {
// If the script starts with a shebang, we should
// execute it directly. This is useful for running
// scripts that aren't executable.
shebang := strings.SplitN(script, "\n", 2)[0]
shebang = strings.TrimPrefix(shebang, "#!")
shebang := strings.SplitN(strings.TrimSpace(script), "\n", 2)[0]
shebang = strings.TrimSpace(shebang)
shebang = strings.TrimPrefix(shebang, "#!")
words, err := shellquote.Split(shebang)
if err != nil {
return nil, xerrors.Errorf("split shebang: %w", err)