Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion daemon/internal/provider/enclave/drift.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,19 @@ func (p *Provider) outdatedSettings(ctx context.Context, container Container, to
// changedOptions names the options whose running value differs from current,
// sorted. An option either side does not report is unknown rather than changed,
// and so is one Enclave reports as null: it could not resolve it, as `features`
// when the features cannot be listed.
// when the features cannot be listed. `allow_domains` is not compared when both
// sides allow all network: unrestricted mode has no allowlist for it to change.
func changedOptions(running map[string]json.RawMessage, current sandboxschema.EffectiveConfig) []string {
unrestricted := sameJSON(running["allow_all_network"], []byte("true")) && current.Bool("allow_all_network")
var changed []string
for name, value := range running {
option, ok := current[name]
if !ok || isNull(value) || isNull(option.Value) {
continue
}
if name == "allow_domains" && unrestricted {
continue
}
if !sameJSON(value, option.Value) {
changed = append(changed, name)
}
Expand Down
37 changes: 37 additions & 0 deletions daemon/internal/provider/enclave/drift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,43 @@ func TestChangedOptions(t *testing.T) {
}
}

// A session started with extra domains under all network was passed them, but
// the domains change nothing while both sides allow all network.
func TestChangedOptionsAllowDomainsUnderAllNetwork(t *testing.T) {
tests := []struct {
name string
running, config string
want []string
}{
{name: "both allow all network", running: `true`, config: `true`, want: nil},
{name: "only the session allows all network", running: `true`, config: `false`,
want: []string{"allow_all_network", "allow_domains"}},
{name: "only the config allows all network", running: `false`, config: `true`,
want: []string{"allow_all_network", "allow_domains"}},
{name: "neither allows all network", running: `false`, config: `false`, want: []string{"allow_domains"}},
// Unknown on the session's side is not taken as allowed, so the domains are compared.
{name: "the session does not report it", running: "", config: `true`, want: []string{"allow_domains"}},
{name: "the session reports null", running: `null`, config: `true`, want: []string{"allow_domains"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
current := sandboxschema.EffectiveConfig{
"allow_all_network": option(tt.config, "project"),
"allow_domains": option(`[]`, "default"),
}
running := map[string]json.RawMessage{
"allow_domains": json.RawMessage(`["api.example.com"]`),
}
if tt.running != "" {
running["allow_all_network"] = json.RawMessage(tt.running)
}
if got := changedOptions(running, current); !slices.Equal(got, tt.want) {
t.Errorf("changedOptions = %q; want %q", got, tt.want)
}
})
}
}

// driftProvider is sandboxProvider listing one container of its workstream,
// running with options, whose launch HomeShell recorded when launch is set.
func driftProvider(t *testing.T, options map[string]json.RawMessage, launch *domain.LaunchConfig) (*Provider, *fakeCLI) {
Expand Down
37 changes: 29 additions & 8 deletions daemon/internal/provider/enclave/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,13 @@ func (p *Provider) sandboxBase(ctx context.Context, dir, tool string) (sandboxsc
// sandboxChoice is the sandbox group as a create form submits it. A field left
// out means "Enclave's default".
type sandboxChoice struct {
Features *[]string `json:"features,omitempty"`
Dirs []sandboxschema.Dir `json:"dirs,omitempty"`
Ports []string `json:"ports,omitempty"`
BridgePorts []int `json:"bridgePorts,omitempty"`
AllowDomains []string `json:"allowDomains,omitempty"`
HostConfig *bool `json:"hostConfig,omitempty"`
Features *[]string `json:"features,omitempty"`
Dirs []sandboxschema.Dir `json:"dirs,omitempty"`
Ports []string `json:"ports,omitempty"`
BridgePorts []int `json:"bridgePorts,omitempty"`
AllowAllNetwork *bool `json:"allowAllNetwork,omitempty"`
AllowDomains []string `json:"allowDomains,omitempty"`
HostConfig *bool `json:"hostConfig,omitempty"`
}

// unavailableSandboxSchema stands in for the sandbox group when Enclave could not
Expand All @@ -146,7 +147,8 @@ var unavailableSandboxSchema = json.RawMessage(`{"type":"object","title":"Contai
// Enclave.
func (choice sandboxChoice) isEmpty() bool {
return choice.Features == nil && len(choice.Dirs) == 0 && len(choice.Ports) == 0 &&
len(choice.BridgePorts) == 0 && len(choice.AllowDomains) == 0 && choice.HostConfig == nil
len(choice.BridgePorts) == 0 && choice.AllowAllNetwork == nil && len(choice.AllowDomains) == 0 &&
choice.HostConfig == nil
}

// sandboxOf reads the sandbox group out of a session config.
Expand Down Expand Up @@ -208,7 +210,21 @@ func sandboxFlags(choice sandboxChoice, base sandboxschema.Base) []string {
bridge[i] = strconv.Itoa(port)
}
flags = append(flags, extendList("--bridge-port", config.List("bridge_ports"), bridge)...)
flags = append(flags, extendList("--allow-domain", config.List("allow_domains"), choice.AllowDomains)...)
// Boolean flags take an explicit value, and the command line beats Enclave's
// config, so a session can also restrict what the config allows. With all
// network allowed there is no allowlist for the domains to extend.
allowAllNetwork := config.Bool("allow_all_network")
if choice.AllowAllNetwork != nil && *choice.AllowAllNetwork != allowAllNetwork {
allowAllNetwork = *choice.AllowAllNetwork
Comment thread
fismif marked this conversation as resolved.
Resolved
if allowAllNetwork {
flags = append(flags, "--allow-all-network")
} else {
flags = append(flags, "--allow-all-network=false")
}
}
if !allowAllNetwork {
Comment thread
fismif marked this conversation as resolved.
Resolved
flags = append(flags, extendList("--allow-domain", config.List("allow_domains"), choice.AllowDomains)...)
}

if choice.HostConfig != nil && *choice.HostConfig != (config.String("host_config") == "passthrough") {
value := "none"
Expand Down Expand Up @@ -278,6 +294,11 @@ func sandboxChoiceFromOptions(running map[string]json.RawMessage, base sandboxsc
choice.BridgePorts = append(choice.BridgePorts, number)
}
}
if allowAllNetwork, ok := decode[bool](running, "allow_all_network"); ok {
if allowAllNetwork != base.Bool("allow_all_network") {
choice.AllowAllNetwork = &allowAllNetwork
}
}
choice.AllowDomains = listed("allow_domains")
if hostConfig, ok := decode[string](running, "host_config"); ok {
if passthrough := hostConfig == "passthrough"; passthrough != (base.String("host_config") == "passthrough") {
Expand Down
49 changes: 47 additions & 2 deletions daemon/internal/provider/enclave/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,44 @@ func TestSandboxFlagsAgainstAConfigThatTurnsThingsOn(t *testing.T) {
}
}

// A session can allow all network, or turn off what Enclave's config allows, and
// while all network is allowed the domains, which nothing would enforce, are not
// passed.
func TestSandboxFlagsForAllNetwork(t *testing.T) {
tests := []struct {
name string
configured bool
choice sandboxChoice
want []string
}{
{name: "allowed for one session",
choice: sandboxChoice{AllowAllNetwork: ptr(true)}, want: []string{"--allow-all-network"}},
{name: "restricted as configured", choice: sandboxChoice{AllowAllNetwork: ptr(false)}},
{name: "allowed as configured", configured: true, choice: sandboxChoice{AllowAllNetwork: ptr(true)}},
{name: "restricted for one session against the config", configured: true,
choice: sandboxChoice{AllowAllNetwork: ptr(false)}, want: []string{"--allow-all-network=false"}},
{name: "restricted for one session keeps the extra domains", configured: true,
choice: sandboxChoice{AllowAllNetwork: ptr(false), AllowDomains: []string{"cdn.example.com"}},
want: []string{"--allow-all-network=false", "--allow-domain", "api.example.com", "--allow-domain", "cdn.example.com"}},
{name: "extra domains are not passed while allowed",
choice: sandboxChoice{AllowAllNetwork: ptr(true), AllowDomains: []string{"cdn.example.com"}},
want: []string{"--allow-all-network"}},
{name: "nor while the config allows it", configured: true,
choice: sandboxChoice{AllowDomains: []string{"cdn.example.com"}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
base := testBase()
if tt.configured {
base.Config["allow_all_network"] = option(`true`, "project")
}
if got := sandboxFlags(tt.choice, base); !slices.Equal(got, tt.want) {
t.Errorf("sandboxFlags = %q; want %q", got, tt.want)
}
})
}
}

// The features Enclave resolves for the tool produce no flag, in any order.
func TestSandboxFlagsAgainstTheToolsFeatureSelection(t *testing.T) {
base := testBase()
Expand All @@ -145,9 +183,10 @@ func TestSandboxOf(t *testing.T) {
if _, ok := sandboxOf(json.RawMessage(`{"model":"opus"}`)); ok {
t.Error("a config without the group: want no sandbox")
}
choice, ok := sandboxOf(json.RawMessage(`{"enclave":{"features":["devtools"],"bridgePorts":[5432],"hostConfig":true}}`))
choice, ok := sandboxOf(json.RawMessage(`{"enclave":{"features":["devtools"],"bridgePorts":[5432],"allowAllNetwork":true,"hostConfig":true}}`))
if !ok || choice.Features == nil || !slices.Equal(*choice.Features, []string{"devtools"}) ||
!slices.Equal(choice.BridgePorts, []int{5432}) || choice.HostConfig == nil || !*choice.HostConfig {
!slices.Equal(choice.BridgePorts, []int{5432}) || choice.HostConfig == nil || !*choice.HostConfig ||
choice.AllowAllNetwork == nil || !*choice.AllowAllNetwork {
t.Errorf("sandboxOf = %+v, %v", choice, ok)
}
}
Expand Down Expand Up @@ -469,6 +508,9 @@ func TestSandboxChoiceFromOptions(t *testing.T) {
{name: "host config passed through", running: map[string]string{"host_config": `"passthrough"`},
want: sandboxChoice{HostConfig: ptr(true)}},
{name: "host config as configured", running: map[string]string{"host_config": `"none"`}},
{name: "all network allowed", running: map[string]string{"allow_all_network": `true`},
want: sandboxChoice{AllowAllNetwork: ptr(true)}},
{name: "network restricted as configured", running: map[string]string{"allow_all_network": `false`}},
{name: "options the form does not model", running: map[string]string{"yolo": `true`, "backend": `"auto"`}},
}
for _, tt := range tests {
Expand All @@ -489,6 +531,9 @@ func TestSandboxChoiceConfigHasNoNulls(t *testing.T) {
if got := (sandboxChoice{AllowDomains: []string{"y.org"}}).config(); string(got) != `{"enclave":{"allowDomains":["y.org"]}}` {
t.Errorf("config = %s", got)
}
if got := (sandboxChoice{AllowAllNetwork: ptr(false)}).config(); string(got) != `{"enclave":{"allowAllNetwork":false}}` {
t.Errorf("config = %s; want a restricted network kept, not dropped as unset", got)
}
if got := (sandboxChoice{}).config(); string(got) != `{}` {
t.Errorf("empty config = %s; want {}", got)
}
Expand Down
45 changes: 32 additions & 13 deletions daemon/internal/provider/enclave/sandboxschema/schema.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Package sandboxschema builds the create-form group for an Enclave container's
// sandbox settings — features, mounts, ports, domains, host config — from what
// Enclave resolves for a tool, with Enclave's values as the defaults. It is free
// of the Enclave provider so the fake provider's demo offers the same form.
// sandbox settings — features, mounts, ports, network, domains, host config —
// from what Enclave resolves for a tool, with Enclave's values as the defaults.
// It is free of the Enclave provider so the fake provider's demo offers the same
// form.
//
// Two properties of Enclave's flags shape the form: a list flag replaces the
// configured list rather than adding to it (enclave#99), and no flag empties one.
Expand Down Expand Up @@ -214,17 +215,18 @@ func Schema(base Base) json.RawMessage {
"default": []int{},
}, inheritedBridge, listSource(config, "bridge_ports")))

domainsDescription := "Domains the container may reach, in addition to Enclave's allowlist for the agent and its features."
if config.Bool("allow_all_network") {
domainsDescription = "Enclave's config allows all network access for this project, so extra domains change nothing."
}
props = props.add("allowAllNetwork", allowAllNetworkProp(config))

// Unrestricted mode has no allowlist to extend, so the domains are
// greyed out while all network is allowed, and kept for when it no longer is.
props = props.add("allowDomains", withInherited(map[string]any{
"type": "array",
"title": "Extra allowed domains",
"description": domainsDescription,
"uniqueItems": true,
"items": map[string]any{"type": "string", "pattern": domainNamePattern, "maxLength": 253},
"default": []string{},
"type": "array",
"title": "Extra allowed domains",
"description": "Domains the container may reach, in addition to Enclave's allowlist for the agent and its features.",
"uniqueItems": true,
"items": map[string]any{"type": "string", "pattern": domainNamePattern, "maxLength": 253},
"default": []string{},
"x-disabled-when": map[string]any{"allowAllNetwork": true},
}, config.List("allow_domains"), listSource(config, "allow_domains")))

props = props.add("hostConfig", map[string]any{
Expand All @@ -250,6 +252,23 @@ func Schema(base Base) json.RawMessage {
return out
}

// allowAllNetworkProp is the checkbox that turns Enclave's network restrictions
// off for a session (`--allow-all-network`), defaulting to what Enclave's config
// says. A config that allows all network can still be overridden for one session
// (`--allow-all-network=false`). Enclave's network policy mode, global or
// project, is not asked for yet (`enclave network print --json` would report
// it), and the flag cannot override an "unrestricted" mode.
func allowAllNetworkProp(config EffectiveConfig) map[string]any {
return map[string]any{
"type": "boolean",
"title": "Allow all network access",
"description": "The container can reach any host. Enclave also stops shielding your agent tokens, " +
"so the agent sees the real ones.",
"default": config.Bool("allow_all_network"),
"x-source": sourceLabel(config["allow_all_network"].Source),
}
}

// retiredSettings are settings the group no longer offers. They are accepted on
// create and restart, kept in the record but not passed to Enclave, so a session
// recorded with one still restarts, without the setting; the form shows only
Expand Down
47 changes: 41 additions & 6 deletions daemon/internal/provider/enclave/sandboxschema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestSchema(t *testing.T) {
group := Schema(testBase())
names, props := schemaProps(t, group)

wantOrder := []string{"features", "dirs", "ports", "bridgePorts", "allowDomains", "hostConfig"}
wantOrder := []string{"features", "dirs", "ports", "bridgePorts", "allowAllNetwork", "allowDomains", "hostConfig"}
if !slices.Equal(names, wantOrder) {
t.Errorf("field order = %q; want %q", names, wantOrder)
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestSchema(t *testing.T) {

// The group must be a schema the daemon can validate a submitted config with.
schema := with(t, nil, group)
if err := provider.ValidateConfig(schema, json.RawMessage(`{"enclave":{"features":["playwright"],"dirs":[{"path":"/x","readonly":true}],"ports":["3000","0:3000","8080:80","127.0.0.1:8080:80","[::1]:8080:80","*:8080:80","65535"],"bridgePorts":[6379],"allowDomains":["cdn.example.com","API.Example.COM"],"hostConfig":true}}`)); err != nil {
if err := provider.ValidateConfig(schema, json.RawMessage(`{"enclave":{"features":["playwright"],"dirs":[{"path":"/x","readonly":true}],"ports":["3000","0:3000","8080:80","127.0.0.1:8080:80","[::1]:8080:80","*:8080:80","65535"],"bridgePorts":[6379],"allowAllNetwork":true,"allowDomains":["cdn.example.com","API.Example.COM"],"hostConfig":true}}`)); err != nil {
t.Errorf("a valid sandbox config was refused: %v", err)
}
for name, config := range map[string]string{
Expand All @@ -187,23 +187,58 @@ func TestSchema(t *testing.T) {
"a domain with a port": `{"enclave":{"allowDomains":["example.com:443"]}}`,
"a dir with no path": `{"enclave":{"dirs":[{"readonly":true}]}}`,
"a setting the group lacks": `{"enclave":{"yolo":true}}`,
"all network as a string": `{"enclave":{"allowAllNetwork":"yes"}}`,
} {
if err := provider.ValidateConfig(schema, json.RawMessage(config)); err == nil {
t.Errorf("%s was accepted", name)
}
}
}

func TestSchemaWithoutFeaturesAndWithAllNetwork(t *testing.T) {
func TestSchemaWithoutFeatures(t *testing.T) {
base := testBase()
base.Features = nil
base.Config["allow_all_network"] = option(`true`, "global")
_, props := schemaProps(t, Schema(base))
if _, ok := props["features"]; ok {
t.Error("an Enclave listing no features still gets a features field")
}
if !strings.Contains(props["allowDomains"]["description"].(string), "allows all network access") {
t.Errorf("allowDomains description = %q; want it to say extra domains change nothing", props["allowDomains"]["description"])
}

func TestSchemaAllowAllNetwork(t *testing.T) {
tests := []struct {
name string
config map[string]ConfigOption
wantDefault bool
wantSource string
}{
{name: "restricted by default", wantDefault: false, wantSource: "Enclave defaults"},
{name: "allowed by Enclave's project config",
config: map[string]ConfigOption{"allow_all_network": option(`true`, "project")},
wantDefault: true, wantSource: "Enclave project config"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
base := testBase()
for name, value := range tt.config {
base.Config[name] = value
}
_, props := schemaProps(t, Schema(base))
field := props["allowAllNetwork"]
if field["type"] != "boolean" || field["title"] != "Allow all network access" {
t.Errorf("allowAllNetwork = %v; want a checkbox titled for what it does", field)
}
if field["default"] != tt.wantDefault || field["x-source"] != tt.wantSource {
t.Errorf("allowAllNetwork default = %v from %v; want %v from %v",
field["default"], field["x-source"], tt.wantDefault, tt.wantSource)
}
description, _ := field["description"].(string)
if !strings.Contains(description, "real ones") {
t.Errorf("allowAllNetwork description = %q; want it to warn that the agent sees real tokens", description)
}
if got := props["allowDomains"]["x-disabled-when"]; !equalJSON(got, map[string]any{"allowAllNetwork": true}) {
t.Errorf("allowDomains x-disabled-when = %v; want it disabled while all network is allowed", got)
}
})
}
}

Expand Down
6 changes: 6 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@ Paths are under `/api/projects/{projectId}`; `{ws}` stands for `/workstreams/{wo
- **Schemas** list the session types each session provider offers in this workstream; `schema`
is a JSON Schema the UI renders as the create form. Read per request, so catalog edits apply
at once.
- **Form keywords** beyond JSON Schema, which the UI reads but never works out itself:
- `x-source`: where a field's default comes from, shown while the field holds it.
- `x-inherited`: `{values, source}` a list already gets from elsewhere, shown locked and not part
of its value.
- `x-disabled-when`: on a field in a group, `{sibling: value, …}`. The field is greyed out while
every named sibling holds its value, and keeps what it holds for when it no longer does.
- **Placeholders** carry `placeholder: true`: the daemon's own row for a session create in
flight, or one that failed. Deleting one cancels its create. A session restarting is `starting`
too, but carries no flag.
Expand Down
3 changes: 2 additions & 1 deletion docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,8 @@ the agent's PTY and serves screen snapshots on a unix socket; the wire format is
(`provider/enclave/restart.go` `completeReplacements`). Cancelled, as by a shutdown, the restart
stops the new container, so the session is left to end and be continued.
- **Outdated settings.** Every 30 s (`provider/enclave/drift.go`) the provider compares each
session's options with what Enclave's config now resolves.
session's options with what Enclave's config now resolves. `allow_domains` is skipped while both
allow all network: unrestricted mode has no allowlist for it to change.
- **Sandbox settings** come from Enclave's config and features, cached briefly
(`provider/enclave/sandbox.go`); HomeShell passes them through.
- **CLI sessions.** The same check adopts sessions started with the `enclave` CLI
Expand Down
Loading
Loading