mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
* feat(sandbox): Seatbelt lockdown translation confines outbound to the PMG proxy Under network_via_proxy_only the Seatbelt profile now denies all network-outbound (with a target=direct violation marker) and allows only the loopback proxy port. SBPL is last-match-wins, so the broad deny is emitted first, specific allows after, and the allow_network_bind rules last — keeping loopback-to-loopback dev traffic working under lockdown. allow_direct_dns re-opens the /var/run/mDNSResponder unix socket that the deny otherwise covers. Replaces the temporary fail-closed rejection in the Seatbelt driver with the real translation; non-lockdown profiles translate byte-identically to before. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PqMU5GNBbQvQct9nxek1VS * test(sandbox): assert deny marker presence before ordering comparison Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PqMU5GNBbQvQct9nxek1VS * feat(sandbox): render lockdown profiles without a proxy as deny-only with runtime note pmg sandbox profile show renders profiles for debugging and must not fail on lockdown profiles. Without a running proxy the translator keeps the broad deny (rendered profile stays fail-closed, never looser than runtime) and documents the runtime-only proxy-port allow in an SBPL comment instead of fabricating a port. Execution is unaffected: the driver validates the proxy address before translating. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PqMU5GNBbQvQct9nxek1VS --------- Co-authored-by: Claude <noreply@anthropic.com>
121 lines
3.3 KiB
Go
121 lines
3.3 KiB
Go
//go:build darwin
|
|
// +build darwin
|
|
|
|
package platform
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"testing"
|
|
|
|
"github.com/safedep/dry/utils"
|
|
"github.com/safedep/pmg/sandbox"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestSeatbeltDarwin(t *testing.T) {
|
|
policy := &sandbox.SandboxPolicy{
|
|
Name: "test",
|
|
Description: "test",
|
|
PackageManagers: []string{"npm"},
|
|
Filesystem: sandbox.FilesystemPolicy{
|
|
AllowRead: []string{"/tmp"},
|
|
AllowWrite: []string{"/tmp"},
|
|
DenyRead: []string{"/private/var"},
|
|
DenyWrite: []string{"/private/var"},
|
|
},
|
|
Network: sandbox.NetworkPolicy{
|
|
AllowOutbound: []string{"*:*"},
|
|
},
|
|
Process: sandbox.ProcessPolicy{
|
|
AllowExec: []string{"/bin/sh"},
|
|
DenyExec: []string{"/bin/bash"},
|
|
},
|
|
}
|
|
|
|
sb, err := newSeatbeltSandbox()
|
|
assert.NoError(t, err)
|
|
|
|
cmd := exec.Command("npm", "install", "lodash")
|
|
npmResolvedPath := cmd.Path
|
|
|
|
result, err := sb.Execute(context.Background(), cmd, policy, nil)
|
|
assert.NoError(t, err)
|
|
assert.True(t, result.ShouldRun(), "command should be runnable because seatbelt only patches the command")
|
|
|
|
assert.Equal(t, cmd.Path, "/usr/bin/sandbox-exec")
|
|
assert.Equal(t, cmd.Args, []string{"sandbox-exec", "-f", sb.tempProfilePath, npmResolvedPath, "install", "lodash"})
|
|
|
|
err = result.Close()
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoFileExists(t, sb.tempProfilePath)
|
|
}
|
|
|
|
func TestSeatbeltExecuteLockdownValidation(t *testing.T) {
|
|
lockdownPolicy := &sandbox.SandboxPolicy{
|
|
Name: "lockdown",
|
|
PackageManagers: []string{"npm"},
|
|
Filesystem: sandbox.FilesystemPolicy{
|
|
AllowRead: []string{"/tmp"},
|
|
AllowWrite: []string{"/tmp"},
|
|
},
|
|
NetworkViaProxyOnly: utils.PtrTo(true),
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
rt *sandbox.ExecutionContext
|
|
wantErr string
|
|
}{
|
|
{"nil execution context", nil, "requires the PMG proxy"},
|
|
{"empty proxy address", &sandbox.ExecutionContext{}, "requires the PMG proxy"},
|
|
{"non-loopback proxy address", &sandbox.ExecutionContext{ProxyAddr: "192.168.1.5:9999"}, "loopback"},
|
|
{"unparseable proxy address", &sandbox.ExecutionContext{ProxyAddr: "not-an-address"}, "loopback"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
sb, err := newSeatbeltSandbox()
|
|
require.NoError(t, err)
|
|
|
|
cmd := exec.Command("/usr/bin/true")
|
|
result, err := sb.Execute(context.Background(), cmd, lockdownPolicy, tt.rt)
|
|
|
|
require.Error(t, err)
|
|
assert.Nil(t, result)
|
|
assert.Contains(t, err.Error(), tt.wantErr)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSeatbeltExecuteLockdownWrapsCommand(t *testing.T) {
|
|
policy := &sandbox.SandboxPolicy{
|
|
Name: "lockdown",
|
|
PackageManagers: []string{"npm"},
|
|
Filesystem: sandbox.FilesystemPolicy{
|
|
AllowRead: []string{"/tmp"},
|
|
AllowWrite: []string{"/tmp"},
|
|
},
|
|
NetworkViaProxyOnly: utils.PtrTo(true),
|
|
}
|
|
|
|
sb, err := newSeatbeltSandbox()
|
|
require.NoError(t, err)
|
|
|
|
cmd := exec.Command("/usr/bin/true")
|
|
result, err := sb.Execute(context.Background(), cmd, policy,
|
|
&sandbox.ExecutionContext{ProxyAddr: "127.0.0.1:54321"})
|
|
require.NoError(t, err)
|
|
assert.True(t, result.ShouldRun())
|
|
assert.Equal(t, "/usr/bin/sandbox-exec", cmd.Path)
|
|
|
|
profile, err := os.ReadFile(sb.tempProfilePath)
|
|
require.NoError(t, err)
|
|
assert.Contains(t, string(profile), `(allow network-outbound (remote ip "localhost:54321"))`)
|
|
|
|
require.NoError(t, result.Close())
|
|
}
|