Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use powershell scripts instead of batch scripts for state exec. #3228

Merged
merged 9 commits into from
Apr 15, 2024

Conversation

mitchell-as
Copy link
Contributor

@mitchell-as mitchell-as commented Apr 8, 2024

TaskDX-2673 Get rid of batch files for running user commands

Passing arguments through batch is not reliable, especially arguments with special characters like '<' in them.

On Windows, Go -> cmd -> executor/exe argument passthrough via %* is problematic because the batch script doesn't know how to do parameter escaping if a parameter contains special characters. Instead, use Go -> pwsh -> executor/exe so that powershell correctly forwards arguments.

This PR adds the following new integration tests:

  • state exec uses powershell under the hood so that something like state exec perl testargs.pl "<3" works as expected (the original bug report) without the user needing to know anything about escaping.
  • state run foo "^<3" where foo is a batch script explicitly written by the user works, but requires the '^' escape (presumably the user knows this, as I mention in a stand-alone comment). Note that State Tool invokes the user-defined batch script; it doesn't create its own script to facilitate running this.
  • Within state shell, foo "<3" where foo is a powershell script explicitly written by the user works as expected. Note that State Tool invokes the user-defined script (whether it be powershell or batch); it doesn't create its own script to facilitate running this one.

Passing arguments through batch is not reliable, especially arguments with special characters like '<' in them.
Passing arguments with special characters is limited when it comes to batch scripting.
Comment on lines +345 to +348
// The '<' needs to be escaped with '^', and I don't know why. There is no way around it.
// The other exec and shell integration tests that test arg passing do not need this escape.
// Only this batch test does.
arg = "^<3"
Copy link
Contributor Author

@mitchell-as mitchell-as Apr 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this is okay because if you are explicitly writing a batch script, then you ought to know the ins and outs of how batch handles incoming arguments, so if you're doing state run <name> <args> with funky <args>, then you should to know what needs escaping and what does not.

@mitchell-as
Copy link
Contributor Author

The lone test failure is on macOS and is unrelated to this PR.

@mitchell-as mitchell-as marked this pull request as ready for review April 11, 2024 21:10
Copy link
Member

@MDrakos MDrakos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good but I haven't approved yet because of my concern in the comment below.

@@ -127,7 +127,7 @@ func runWithCmd(env []string, name string, args ...string) error {
case ".bat":
// No action required
case ".ps1":
args = append([]string{"-file", name}, args...)
args = append([]string{"-executionpolicy", "bypass", "-file", name}, args...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we've worked through the potential implications of this... some customers might have locked down environments and passing this flag could cause execution to fail. I remember we had a discussion about this during stand but I can't recall all of the details.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We spoke during stand that we'd like to have Mark test this in a brand-new, unprivileged environment. If that fails, we can undo all of this. In my testing, it seems to work, but I haven't tried all configurations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do wonder if that will catch potential issues with more complex locked-down environment, but I guess we will see if people complain.

@mitchell-as mitchell-as reopened this Apr 15, 2024
@mitchell-as mitchell-as merged commit 14a7c67 into version/0-44-0-RC1 Apr 15, 2024
22 of 25 checks passed
@mitchell-as mitchell-as deleted the mitchell/dx-2673 branch April 15, 2024 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants