|
1 | 1 | package shell |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "crypto/sha256" |
| 5 | + "encoding/hex" |
4 | 6 | "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
5 | 9 | "os" |
6 | 10 | "os/exec" |
7 | 11 | "path/filepath" |
8 | 12 | "regexp" |
9 | 13 | "strings" |
10 | 14 |
|
| 15 | + "github.com/openbootdotdev/openboot/internal/httputil" |
11 | 16 | "github.com/openbootdotdev/openboot/internal/system" |
12 | 17 | ) |
13 | 18 |
|
| 19 | +// knownOMZInstallHash is the SHA256 of the Oh-My-Zsh install script pinned on |
| 20 | +// 2026-04-19 (ohmyzsh/ohmyzsh master, commit circa that date). Update this |
| 21 | +// constant whenever the installer script changes upstream. |
| 22 | +const knownOMZInstallHash = "21043aec5b791ce4835479dc33ba2f92155946aeafd54604a8c83522627cc803" |
| 23 | + |
| 24 | +// omzInstallURL is a var so tests can redirect it to a local httptest server. |
| 25 | +var omzInstallURL = "https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh" |
| 26 | + |
14 | 27 | var shellIdentifierRe = regexp.MustCompile(`^[a-zA-Z0-9_.-]+$`) |
15 | 28 |
|
16 | 29 | func validateShellIdentifier(value, label string) error { |
@@ -42,12 +55,53 @@ func InstallOhMyZsh(dryRun bool) error { |
42 | 55 | return nil |
43 | 56 | } |
44 | 57 |
|
45 | | - script := `sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended` |
46 | | - cmd := exec.Command("bash", "-c", script) |
47 | | - cmd.Stdout = os.Stdout |
48 | | - cmd.Stderr = os.Stderr |
49 | | - cmd.Stdin = os.Stdin |
50 | | - return cmd.Run() |
| 58 | + // Download the installer via httputil.Do so rate-limit handling is applied. |
| 59 | + req, err := http.NewRequest(http.MethodGet, omzInstallURL, nil) |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("create omz install request: %w", err) |
| 62 | + } |
| 63 | + resp, err := httputil.Do(http.DefaultClient, req) |
| 64 | + if err != nil { |
| 65 | + return fmt.Errorf("download omz install script: %w", err) |
| 66 | + } |
| 67 | + defer resp.Body.Close() |
| 68 | + |
| 69 | + if resp.StatusCode != http.StatusOK { |
| 70 | + return fmt.Errorf("download omz install script: unexpected status %d", resp.StatusCode) |
| 71 | + } |
| 72 | + |
| 73 | + scriptBytes, err := io.ReadAll(resp.Body) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("read omz install script: %w", err) |
| 76 | + } |
| 77 | + |
| 78 | + // Verify SHA256 before executing anything. |
| 79 | + sum := sha256.Sum256(scriptBytes) |
| 80 | + got := hex.EncodeToString(sum[:]) |
| 81 | + if got != knownOMZInstallHash { |
| 82 | + return fmt.Errorf("Oh-My-Zsh install script hash mismatch: download may be compromised (got %s, want %s)", got, knownOMZInstallHash) |
| 83 | + } |
| 84 | + |
| 85 | + // Write verified script to a temp file, execute, then clean up. |
| 86 | + tmpFile, err := os.CreateTemp("", "omz-install-*.sh") |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("create temp file for omz install: %w", err) |
| 89 | + } |
| 90 | + defer os.Remove(tmpFile.Name()) |
| 91 | + |
| 92 | + if _, err := tmpFile.Write(scriptBytes); err != nil { |
| 93 | + tmpFile.Close() |
| 94 | + return fmt.Errorf("write omz install script: %w", err) |
| 95 | + } |
| 96 | + if err := tmpFile.Close(); err != nil { |
| 97 | + return fmt.Errorf("close omz install script: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + if err := os.Chmod(tmpFile.Name(), 0700); err != nil { |
| 101 | + return fmt.Errorf("chmod omz install script: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + return system.RunCommand(tmpFile.Name(), "--unattended") |
51 | 105 | } |
52 | 106 |
|
53 | 107 | const brewShellenvLine = `eval "$(/opt/homebrew/bin/brew shellenv)"` |
|
0 commit comments