-
Notifications
You must be signed in to change notification settings - Fork 7
[AUTOMATION] fix(localruntime): harden guard socket dir #292
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import ( | |
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "syscall" | ||
| ) | ||
|
|
||
| func DefaultSocketPath() string { | ||
|
|
@@ -14,5 +15,29 @@ func DefaultSocketPath() string { | |
| } | ||
|
|
||
| func EnsureSocketDir(socketPath string) error { | ||
| return os.MkdirAll(filepath.Dir(socketPath), 0o700) | ||
| dir := filepath.Dir(socketPath) | ||
| if err := os.MkdirAll(dir, 0o700); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| info, err := os.Lstat(dir) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if info.Mode()&os.ModeSymlink != 0 { | ||
| return fmt.Errorf("socket directory %q must not be a symlink", dir) | ||
| } | ||
| if !info.IsDir() { | ||
| return fmt.Errorf("socket directory %q is not a directory", dir) | ||
| } | ||
|
|
||
| stat, ok := info.Sys().(*syscall.Stat_t) | ||
| if !ok { | ||
| return fmt.Errorf("socket directory %q owner could not be verified", dir) | ||
| } | ||
| if int(stat.Uid) != os.Getuid() { | ||
| return fmt.Errorf("socket directory %q must be owned by uid %d", dir, os.Getuid()) | ||
| } | ||
|
|
||
| return os.Chmod(dir, 0o700) | ||
|
Comment on lines
+23
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The directory is checked with |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package localruntime | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "syscall" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestEnsureSocketDirCreatesPrivateDirectory(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| root := t.TempDir() | ||
| socketPath := filepath.Join(root, "guard", "kontext.sock") | ||
| if err := EnsureSocketDir(socketPath); err != nil { | ||
| t.Fatalf("EnsureSocketDir() error = %v", err) | ||
| } | ||
|
|
||
| info, err := os.Stat(filepath.Dir(socketPath)) | ||
| if err != nil { | ||
| t.Fatalf("Stat() error = %v", err) | ||
| } | ||
| if got := info.Mode().Perm(); got != 0o700 { | ||
| t.Fatalf("socket dir mode = %o, want 700", got) | ||
| } | ||
|
|
||
| stat, ok := info.Sys().(*syscall.Stat_t) | ||
| if !ok { | ||
| t.Fatal("socket dir stat missing uid") | ||
| } | ||
| if got := int(stat.Uid); got != os.Getuid() { | ||
| t.Fatalf("socket dir owner uid = %d, want %d", got, os.Getuid()) | ||
| } | ||
| } | ||
|
|
||
| func TestEnsureSocketDirTightensExistingDirectoryPermissions(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| root := t.TempDir() | ||
| dir := filepath.Join(root, "guard") | ||
| if err := os.MkdirAll(dir, 0o777); err != nil { | ||
| t.Fatalf("MkdirAll() error = %v", err) | ||
| } | ||
| if err := os.Chmod(dir, 0o777); err != nil { | ||
| t.Fatalf("Chmod() setup error = %v", err) | ||
| } | ||
|
|
||
| socketPath := filepath.Join(dir, "kontext.sock") | ||
| if err := EnsureSocketDir(socketPath); err != nil { | ||
| t.Fatalf("EnsureSocketDir() error = %v", err) | ||
| } | ||
|
|
||
| info, err := os.Stat(dir) | ||
| if err != nil { | ||
| t.Fatalf("Stat() error = %v", err) | ||
| } | ||
| if got := info.Mode().Perm(); got != 0o700 { | ||
| t.Fatalf("socket dir mode = %o, want 700", got) | ||
| } | ||
| } | ||
|
|
||
| func TestEnsureSocketDirRejectsSymlinkDirectory(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| root := t.TempDir() | ||
| target := filepath.Join(root, "target") | ||
| if err := os.MkdirAll(target, 0o700); err != nil { | ||
| t.Fatalf("MkdirAll() error = %v", err) | ||
| } | ||
|
|
||
| link := filepath.Join(root, "guard") | ||
| if err := os.Symlink(target, link); err != nil { | ||
| t.Fatalf("Symlink() error = %v", err) | ||
| } | ||
|
|
||
| err := EnsureSocketDir(filepath.Join(link, "kontext.sock")) | ||
| if err == nil { | ||
| t.Fatal("EnsureSocketDir() error = nil, want symlink rejection") | ||
| } | ||
| if !strings.Contains(err.Error(), "must not be a symlink") { | ||
| t.Fatalf("EnsureSocketDir() error = %v, want symlink rejection", err) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
socketPathis a basename such askontext.sock,filepath.Dir(socketPath)returns.. The new unconditionalos.Chmod(dir, 0o700)then changes the current working directory permissions, so a user settingKONTEXT_GUARD_SOCKET=kontext.sockfrom a shared project directory can unexpectedly remove group/world access from that directory. The previousMkdirAll(".", 0700)path did not tighten existing cwd permissions.