Skip to content

Commit c167ca7

Browse files
authored
[wrappers] Always use system bash and sed in bin wrappers (#1571)
## Summary This feels like the safest solution to #1566 (comment) It also avoids creating bin wrappers for `bash` and `sed` which reduces risk and improves performance. ## How was it tested? Used setup from #1566 ``` devbox shell devbox add bash hash -r which bash # ensure I get profile bash instead of wrapped bash ```
1 parent d9b1635 commit c167ca7

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

internal/boxcli/root.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"go.jetpack.io/devbox/internal/boxcli/featureflag"
1818
"go.jetpack.io/devbox/internal/boxcli/midcobra"
1919
"go.jetpack.io/devbox/internal/cloud/openssh/sshshim"
20+
"go.jetpack.io/devbox/internal/cmdutil"
2021
"go.jetpack.io/devbox/internal/debug"
2122
"go.jetpack.io/devbox/internal/telemetry"
2223
"go.jetpack.io/devbox/internal/vercheck"
@@ -113,6 +114,7 @@ func Execute(ctx context.Context, args []string) int {
113114

114115
func Main() {
115116
timer := debug.Timer(strings.Join(os.Args, " "))
117+
setSystemBinaryPaths()
116118
ctx := context.Background()
117119
if strings.HasSuffix(os.Args[0], "ssh") ||
118120
strings.HasSuffix(os.Args[0], "scp") {
@@ -146,3 +148,12 @@ func listAllCommands(cmd *cobra.Command, indent string) {
146148
listAllCommands(childCmd, indent+"\t")
147149
}
148150
}
151+
152+
func setSystemBinaryPaths() {
153+
if os.Getenv("DEVBOX_SYSTEM_BASH") == "" {
154+
os.Setenv("DEVBOX_SYSTEM_BASH", cmdutil.GetPathOrDefault("bash", "/bin/bash"))
155+
}
156+
if os.Getenv("DEVBOX_SYSTEM_SED") == "" {
157+
os.Setenv("DEVBOX_SYSTEM_SED", cmdutil.GetPathOrDefault("sed", "/usr/bin/sed"))
158+
}
159+
}

internal/wrapnix/wrapper.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ import (
2121
"go.jetpack.io/devbox/internal/xdg"
2222
)
2323

24+
// Avoid wrapping bash and sed to prevent accidentally creating a recursive loop
25+
// We use DEVBOX_SYSTEM_BASH and DEVBOX_SYSTEM_SED so normally we won't use
26+
// user versions, but we want to be extra careful.
27+
// This also has minor performance benefits.
28+
var dontWrap = map[string]bool{
29+
"bash": true,
30+
"sed": true,
31+
}
32+
2433
type CreateWrappersArgs struct {
2534
NixBins []string
2635
ShellEnvHash string
@@ -47,14 +56,17 @@ func CreateWrappers(ctx context.Context, args CreateWrappersArgs) error {
4756
destPath := filepath.Join(wrapperBinPath(args.ProjectDir))
4857
_ = os.MkdirAll(destPath, 0o755)
4958

50-
bashPath := cmdutil.GetPathOrDefault("bash", "/bin/bash")
51-
sedPath := cmdutil.GetPathOrDefault("sed", "sed")
59+
bashPath := cmdutil.GetPathOrDefault(os.Getenv("DEVBOX_SYSTEM_BASH"), "/bin/bash")
60+
sedPath := cmdutil.GetPathOrDefault(os.Getenv("DEVBOX_SYSTEM_SED"), "/usr/bin/sed")
5261

5362
if err := CreateDevboxSymlinkIfPossible(); err != nil {
5463
return err
5564
}
5665

5766
for _, bin := range args.NixBins {
67+
if dontWrap[filepath.Base(bin)] {
68+
continue
69+
}
5870
if err := createWrapper(&createWrapperArgs{
5971
WrapperBinPath: destPath,
6072
CreateWrappersArgs: args,

0 commit comments

Comments
 (0)