Skip to content

Commit

Permalink
Bash executor should not tolerate non-zero exit codes by default (#406)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #406

From github issue #210
#210

> All inline TTPs will have set -e by default. If a user wants to not run a step with that, they can add set +e to their TTP.

Reviewed By: nicolagiacchetta, d3sch41n

Differential Revision: D50892103

fbshipit-source-id: fbaee2bbc51fbdeb1c9bf474f53aecf02c62640d
  • Loading branch information
inesusvet authored and facebook-github-bot committed Nov 6, 2023
1 parent 0457b9a commit 486ec6a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pkg/blocks/basicstep.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (b *BasicStep) Validate(execCtx TTPExecutionContext) error {
// Set Executor to "bash" if it is not provided
if b.Executor == "" && b.Inline != "" {
logging.L().Debug("defaulting to bash since executor was not provided")
b.Executor = "bash"
b.Executor = ExecutorBash
}

// Return if Executor is ExecutorBinary
Expand Down Expand Up @@ -153,8 +153,15 @@ func (b *BasicStep) executeBashStdin(ptx context.Context, execCtx TTPExecutionCo
return result, nil
}

func (b *BasicStep) buildCommand(ctx context.Context, executor string) *exec.Cmd {
if executor == ExecutorBash {
return exec.CommandContext(ctx, executor, "-o", "errexit")
}
return exec.CommandContext(ctx, executor)
}

func (b *BasicStep) prepareCommand(ctx context.Context, execCtx TTPExecutionContext, envAsList []string, inline string) *exec.Cmd {
cmd := exec.CommandContext(ctx, b.Executor)
cmd := b.buildCommand(ctx, b.Executor)
cmd.Env = envAsList
cmd.Dir = execCtx.WorkDir
cmd.Stdin = strings.NewReader(inline)
Expand Down
28 changes: 28 additions & 0 deletions pkg/blocks/step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,34 @@ inline: this will error`,
content: `inline: echo should_error_before_execution`,
wantUnmarshalError: true,
},
{
name: "Basic bash executor doesn't tolerate non-zero exit codes in inline scripts",
content: `name: inline_step
description: this is a test
inline: |
false
echo executing
cleanup:
inline: echo cleanup
`,
wantExecuteError: true,
expectedExecuteStdout: "",
expectedCleanupStdout: "cleanup\n",
},
{
name: "Basic bash supports setting error processing option to ignore errors",
content: `name: inline_step
inline: |
set +e
false
echo executing
cleanup:
inline: echo cleanup
`,
wantExecuteError: false,
expectedExecuteStdout: "executing\n",
expectedCleanupStdout: "cleanup\n",
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 486ec6a

Please sign in to comment.