Skip to content

Commit c7794ad

Browse files
committed
Restrict self config directory permissions
1 parent e0ac25c commit c7794ad

3 files changed

Lines changed: 19 additions & 16 deletions

File tree

internal/cmd/root_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"errors"
7+
"os"
78
"path/filepath"
89
"strings"
910
"testing"
@@ -219,7 +220,7 @@ func TestSelfCmd(t *testing.T) {
219220
t.Run("should use help func when run without args", func(t *testing.T) {
220221
rootCmd := CreateRootCommand("v0.0.0-test", "")
221222
rootCmd.SetArgs([]string{"self"})
222-
initSelfCmd(rootCmd, "v0.0.0-test", func(string) error { return nil })
223+
initSelfCmd(rootCmd, "v0.0.0-test", func(string) error { return nil }, func(string) error { return nil })
223224

224225
called := false
225226
rootCmd.SetHelpFunc(func(c *cobra.Command, args []string) {
@@ -246,7 +247,7 @@ func TestSelfCmd(t *testing.T) {
246247
rootCmd.SetArgs([]string{"self", "config", "path"})
247248
rootCmd.SetOut(bufOut)
248249
rootCmd.SetErr(bufOut)
249-
initSelfCmd(rootCmd, "v0.0.0-test", func(string) error { return nil })
250+
initSelfCmd(rootCmd, "v0.0.0-test", func(string) error { return nil }, func(string) error { return nil })
250251

251252
if err := rootCmd.Execute(); err != nil {
252253
t.Fatalf("unexpected error: %v", err)
@@ -269,7 +270,7 @@ func TestSelfCmd(t *testing.T) {
269270
rootCmd.SetArgs([]string{"self", "config", "edit"})
270271
rootCmd.SetOut(bufOut)
271272
rootCmd.SetErr(bufOut)
272-
initSelfCmdWithEditor(rootCmd, "v0.0.0-test", func(string) error { return nil }, func(path string) error {
273+
initSelfCmd(rootCmd, "v0.0.0-test", func(string) error { return nil }, func(path string) error {
273274
called = true
274275
gotPath = path
275276

@@ -288,6 +289,15 @@ func TestSelfCmd(t *testing.T) {
288289
if gotPath != expected {
289290
t.Fatalf("expected editor path %q, got %q", expected, gotPath)
290291
}
292+
293+
info, err := os.Stat(filepath.Dir(expected))
294+
if err != nil {
295+
t.Fatalf("expected config directory to exist: %v", err)
296+
}
297+
298+
if perm := info.Mode().Perm(); perm != 0o700 {
299+
t.Fatalf("expected config directory permissions 0700, got %o", perm)
300+
}
291301
})
292302

293303
t.Run("should open documentation in browser", func(t *testing.T) {
@@ -306,7 +316,7 @@ func TestSelfCmd(t *testing.T) {
306316
rootCmd.SetArgs([]string{"self", "doc"})
307317
rootCmd.SetOut(bufOut)
308318
rootCmd.SetErr(bufOut)
309-
initSelfCmd(rootCmd, "v0.0.0-test", openURL)
319+
initSelfCmd(rootCmd, "v0.0.0-test", openURL, func(string) error { return nil })
310320

311321
err := rootCmd.Execute()
312322
if err != nil {
@@ -333,7 +343,7 @@ func TestSelfCmd(t *testing.T) {
333343
rootCmd.SetArgs([]string{"self", "doc"})
334344
rootCmd.SetOut(bufOut)
335345
rootCmd.SetErr(bufOut)
336-
initSelfCmd(rootCmd, "v0.0.0-test", openURL)
346+
initSelfCmd(rootCmd, "v0.0.0-test", openURL, func(string) error { return nil })
337347

338348
err := rootCmd.Execute()
339349
if err == nil {

internal/cmd/self.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,11 @@ import (
77

88
// InitSelfCmd intializes root 'self' subcommand.
99
func InitSelfCmd(rootCmd *cobra.Command, version string) {
10-
initSelfCmd(rootCmd, version, util.OpenURL)
10+
initSelfCmd(rootCmd, version, util.OpenURL, util.OpenEditor)
1111
}
1212

13-
func initSelfCmd(rootCmd *cobra.Command, version string, openURL func(string) error) {
14-
initSelfCmdWithEditor(rootCmd, version, openURL, util.OpenEditor)
15-
}
16-
17-
func initSelfCmdWithEditor(
18-
rootCmd *cobra.Command,
19-
version string,
20-
openURL func(string) error,
21-
openEditor func(string) error,
13+
func initSelfCmd(
14+
rootCmd *cobra.Command, version string, openURL func(string) error, openEditor func(string) error,
2215
) {
2316
selfCmd := &cobra.Command{
2417
Use: "self",

internal/cmd/self_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func initConfigEditCommand(openEditor func(string) error) *cobra.Command {
5959
return err
6060
}
6161

62-
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
62+
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
6363
return fmt.Errorf("creating config directory: %w", err)
6464
}
6565

0 commit comments

Comments
 (0)