feat(settings): check sandbox launchers and configure protected paths

This commit is contained in:
Xubin Ren committed 2026-11-08 14:29:46 +08:00
1 parent f158779c60
commit 5b84b78cac
33 files changed
+1179 -20

No files matched your search

@@ -43,6 +43,7 @@ import {
import { ChannelsSettings } from "@/components/settings/system/ChannelsSettings";
import { RUNTIME_CONFIG_FIELDS, type RuntimeConfigPage } from "@/components/settings/system/runtime-config-fields";
import { RuntimeConfigSettings } from "@/components/settings/system/RuntimeConfigSettings";
import { useClient } from "@/providers/ClientProvider";
import { RuntimeSettings } from "@/components/settings/system/RuntimeSettings";
import type { SettingsController } from "@/components/settings/useSettingsController";
import type { SendAttachment, SendOptions } from "@/hooks/useNanobotStream";
@@ -92,6 +93,7 @@ export function SettingsPage({
isRestarting,
hostChromeInset,
}: SettingsPageProps) {
const { client, webuiCapabilities } = useClient();
const [dialogLayoutAnchor, setDialogLayoutAnchor] = useState<HTMLDivElement | null>(null);
const [mcpSetupName, setMcpSetupName] = useState<string | null>(null);
const presetDeletedCallback = useRef<(() => void) | null>(null);
@@ -193,7 +195,8 @@ export function SettingsPage({
: controllerPendingRestartSections;
const runtimeConfiguration = (page: RuntimeConfigPage) => settings && (
<RuntimeConfigSettings page={page} settings={settings} state={controller.runtimeConfigState}
<RuntimeConfigSettings page={page} settings={settings} state={controller.runtimeConfigState} client={client}
sandboxSupported={webuiCapabilities.includes("webui.sandbox-settings.v1")}
onRestart={showSidebar ? undefined : restartViaSettingsSurface} isRestarting={restartInProgress}
remoteBrowserAccess={remoteBrowserAccess}>
{page === "advanced" ? (
@@ -10,6 +10,7 @@ import { SharedProxyEditor } from "@/components/settings/shared/SharedProxyEdito
import { SettingsHint } from "@/components/settings/shared/SettingsHint";
import { ShellRuntimeControl } from "@/components/settings/system/ShellRuntimeControl";
import { IdleCompactionControl } from "@/components/settings/system/IdleCompactionControl";
import { SandboxSettings } from "@/components/settings/system/SandboxSettings";
import { Input } from "@/components/ui/input";
import { RUNTIME_CONFIG_FIELDS, RUNTIME_CONFIG_GROUPS, type RuntimeConfigField, type RuntimeConfigPage } from "@/components/settings/system/runtime-config-fields";
import { updateRuntimeConfigSettings } from "@/lib/api";
@@ -120,8 +121,10 @@ export function useRuntimeConfigSettings(
setSaved((prev) => ({ ...prev, [group]: true }));
} catch (error) {
const message = (error as Error).message;
setErrors((prev) => ({ ...prev, [group]: message }));
setInvalidField(changed.find((field) => message.startsWith(field.path))?.path ?? null);
const sandboxReason = message.match(/sandbox_launcher_([a-z_]+)/)?.[1];
setErrors((prev) => ({ ...prev, [group]: sandboxReason
? t(`settings.sandbox.reasons.${sandboxReason}`, { defaultValue: t("settings.sandbox.saveError") }) : message }));
setInvalidField(sandboxReason ? "tools.exec.sandbox_launcher" : changed.find((field) => message.startsWith(field.path))?.path ?? null);
} finally {
setSaving(null);
}
@@ -169,11 +172,13 @@ export function useRuntimeConfigSettings(
export type RuntimeConfigController = ReturnType<typeof useRuntimeConfigSettings>;
export function RuntimeConfigSettings({
page, settings, state, onRestart, isRestarting, remoteBrowserAccess, children,
page, settings, state, client, sandboxSupported = false, onRestart, isRestarting, remoteBrowserAccess, children,
}: {
page: RuntimeConfigPage;
settings: SettingsPayload;
state: RuntimeConfigController;
client?: NanobotClient;
sandboxSupported?: boolean;
onRestart?: () => void;
isRestarting: boolean;
remoteBrowserAccess: boolean;
@@ -193,18 +198,20 @@ export function RuntimeConfigSettings({
{restartPending && onRestart ? <RestartRequiredNotice message={t("settings.status.savedRestartApply")}
onRestart={onRestart} isRestarting={isRestarting} /> : null}
{groups.map((group) => {
if (group.enabledBy && state.value(group.enabledBy) === false) return null;
const sandbox = group.id === "shell" && sandboxSupported && settings.advanced.exec_sandbox_status && client;
if (group.enabledBy && state.value(group.enabledBy) === false && !sandbox) return null;
if (group.id === "chat") return <div key={group.id}>{children}</div>;
const fields = RUNTIME_CONFIG_FIELDS.filter((field) => field.group === group.id);
const fields = RUNTIME_CONFIG_FIELDS.filter((field) => field.group === group.id
&& field.path !== "tools.exec.sandbox_launcher" && (!sandbox || field.path !== "tools.exec.sandbox"));
const dirty = fields.filter(state.visible).some(state.dirty);
const title = tr(`groups.${group.id}.title`);
const errorId = `runtime-error-${group.id}`;
const inlineError = fields.some((field) => field.kind === "duration" && state.invalidField === field.path);
const section = (
<section key={group.id} aria-label={title}>
<SettingsSectionTitle>{group.id === "proxy"
{!sandbox ? <SettingsSectionTitle>{group.id === "proxy"
? <SettingsHint description={tr("fields.network_proxy.help")}>{title}</SettingsHint>
: title}</SettingsSectionTitle>
: title}</SettingsSectionTitle> : null}
<form noValidate onSubmit={(event) => { event.preventDefault(); void state.save(group.id); }}
onBlur={(event) => {
if (event.relatedTarget && event.currentTarget.contains(event.relatedTarget)) return;
@@ -298,11 +305,12 @@ export function RuntimeConfigSettings({
onRestart={onRestart} isRestarting={isRestarting} />
</SettingsGroup>
</fieldset>
{state.errors[group.id] && !inlineError ? <p id={errorId} role="alert" className="mt-2 break-words px-1 text-[12px] text-destructive">{state.errors[group.id]}</p> : null}
{state.errors[group.id] && !inlineError && !sandbox ? <p id={errorId} role="alert" className="mt-2 break-words px-1 text-[12px] text-destructive">{state.errors[group.id]}</p> : null}
</form>
</section>
);
return section;
return sandbox ? <SandboxSettings key={group.id} settings={settings} state={state}
client={client!} isRestarting={isRestarting}>{section}</SandboxSettings> : section;
})}
</div>
);
@@ -0,0 +1,123 @@
import { useEffect, useRef, useState, type ReactNode } from "react";
import { CircleAlert, Loader2, ShieldCheck } from "lucide-react";
import { useTranslation } from "react-i18next";
import { ProviderPicker } from "@/components/settings/shared/ModelControls";
import { SettingsGroup, SettingsRow, SettingsSectionTitle, StatusPill } from "@/components/settings/shared/SettingsControls";
import { SettingsAdvancedOptions } from "@/components/settings/shared/SettingsFeature";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { ToggleButton } from "@/components/ui/toggle-button";
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog";
import { checkSandboxSettings } from "@/lib/api";
import type { NanobotClient } from "@/lib/nanobot-client";
import type { SandboxCheck, SettingsPayload } from "@/lib/types";
import type { RuntimeConfigController } from "./RuntimeConfigSettings";
import { RUNTIME_CONFIG_FIELDS } from "./runtime-config-fields";
export function SandboxSettings({ settings, state, client, isRestarting, children }: {
settings: SettingsPayload;
state: RuntimeConfigController;
client: NanobotClient;
isRestarting: boolean;
children: ReactNode;
}) {
const { t } = useTranslation();
const tx = (key: string) => t(`settings.sandbox.${key}`);
const saved = settings.advanced.exec_sandbox_status!;
const [result, setResult] = useState<SandboxCheck | null>(null);
const [checking, setChecking] = useState(false);
const [checkError, setCheckError] = useState(false);
const [confirmDisable, setConfirmDisable] = useState(false);
const generation = useRef(0);
useEffect(() => {
generation.current += 1;
setResult(null);
setChecking(false);
setCheckError(false);
return () => { generation.current += 1; };
}, [client, saved.revision, saved.state, saved.reason, saved.launcher]);
const field = (path: string) => RUNTIME_CONFIG_FIELDS.find((item) => item.path === path)!;
const enabledField = field("tools.exec.enable");
const sandboxField = field("tools.exec.sandbox");
const launcherField = field("tools.exec.sandbox_launcher");
const enabled = state.value(enabledField.path) !== false;
const backend = String(state.value(sandboxField.path));
const dirty = RUNTIME_CONFIG_FIELDS.some((item) => (item.group === "shell" || item.path === enabledField.path) && state.dirty(item));
const busy = state.saving !== null || isRestarting || checking;
const status = dirty ? "unchecked" : checking ? "checking" : result?.revision === saved.revision ? result.state : saved.state;
const error = state.errors.shell;
const reason = dirty ? "" : result?.revision === saved.revision ? result.reason : saved.reason;
const launcher = result?.revision === saved.revision ? result.launcher : saved.launcher;
const options = [{ name: "none", label: tx("off") }];
if (saved.supported_backend) options.unshift({ name: saved.supported_backend, label: saved.supported_backend === "seatbelt" ? "Seatbelt" : "bwrap" });
if (backend && !options.some((item) => item.name === backend)) options.unshift({ name: backend, label: backend });
const check = async () => {
const id = ++generation.current;
setChecking(true);
setCheckError(false);
try {
const response = await checkSandboxSettings(client);
if (id !== generation.current) return;
if (response.sandbox.revision !== saved.revision) { setCheckError(true); return; }
setResult(response.sandbox);
} catch {
if (id === generation.current) setCheckError(true);
} finally {
if (id === generation.current) setChecking(false);
}
};
return <section aria-label={tx("title")}>
<SettingsSectionTitle>{tx("title")}</SettingsSectionTitle>
<SettingsGroup>
<SettingsRow title={t("settings.runtimeConfig.fields.tools_exec_enable.label")}>
<ToggleButton checked={enabled} disabled={busy} label={t("settings.runtimeConfig.fields.tools_exec_enable.label")}
onChange={(value) => state.commit(enabledField, value)} />
</SettingsRow>
<SettingsRow title={tx("method")}>
<ProviderPicker providers={options} value={backend || "none"} emptyLabel={tx("off")}
triggerProps={{ id: "runtime-tools.exec.sandbox", "aria-label": t("settings.runtimeConfig.fields.tools_exec_sandbox.label"), disabled: busy || !enabled }}
onChange={(value) => {
if (value === "none" && backend) setConfirmDisable(true);
else state.commit(sandboxField, value === "none" ? "" : value);
}} />
</SettingsRow>
<SettingsRow title={tx("status")}>
<StatusPill tone={status === "passed" ? "success" : status === "unavailable" || status === "unsupported" ? "warning" : "neutral"}>{tx(`states.${status}`)}</StatusPill>
</SettingsRow>
<div className="settings-list-inset pb-4 pt-1" aria-live="polite">
<div className="flex items-start gap-2 text-ui leading-6 text-muted-foreground">
{checking ? <Loader2 className="mt-1 h-4 w-4 shrink-0 animate-spin motion-reduce:animate-none" aria-hidden />
: status === "passed" ? <ShieldCheck className="mt-1 h-4 w-4 shrink-0" aria-hidden /> : <CircleAlert className="mt-1 h-4 w-4 shrink-0" aria-hidden />}
<p>{reason ? t(`settings.sandbox.reasons.${reason}`, { defaultValue: tx("reasons.unknown") }) : tx(`help.${dirty ? "unsaved" : status}`)}</p>
</div>
{launcher ? <p className="mt-2 break-all font-mono text-caption text-muted-foreground">{launcher}</p> : null}
<p className="mt-2 text-caption leading-5 text-muted-foreground">{tx("scope")}</p>
{saved.supported_backend && enabled && backend ? <Button variant="outline" size="sm" className="mt-3 rounded-full" disabled={busy || dirty} onClick={() => void check()}>{tx(checking ? "checking" : "check")}</Button> : null}
{checkError ? <p role="alert" className="mt-2 text-caption text-destructive">{tx("checkError")}</p> : null}
{error ? <p id="sandbox-settings-error" role="alert" className="mt-2 text-caption text-destructive">{error}</p> : null}
</div>
<SettingsAdvancedOptions>
{saved.supported_backend ? <div className="settings-list-inset pb-4 pt-2">
<label htmlFor="runtime-tools.exec.sandbox_launcher" className="text-control-label">{tx("path")}</label>
<p id="sandbox-launcher-help" className="mb-3 mt-1 text-caption leading-5 text-muted-foreground">{tx("pathHelp")}</p>
<div className="flex flex-wrap items-center gap-2">
<Input id="runtime-tools.exec.sandbox_launcher" className="min-w-0 flex-1 basis-56 text-ui" value={String(state.value(launcherField.path))}
aria-invalid={state.invalidField === launcherField.path || undefined}
aria-describedby={state.invalidField === launcherField.path ? "sandbox-launcher-help sandbox-settings-error" : "sandbox-launcher-help"}
placeholder={tx("automatic")} disabled={busy} onChange={(event) => state.change(launcherField, event.target.value)} />
<Button variant="outline" size="sm" disabled={busy || !state.dirty(launcherField)} onClick={() => void state.save("shell")}>{t("settings.actions.save")}</Button>
</div>
</div> : null}
{children}
</SettingsAdvancedOptions>
</SettingsGroup>
<AlertDialog open={confirmDisable} onOpenChange={setConfirmDisable}>
<AlertDialogContent><AlertDialogHeader><AlertDialogTitle>{tx("disableTitle")}</AlertDialogTitle>
<AlertDialogDescription>{tx("disableHelp")}</AlertDialogDescription></AlertDialogHeader>
<AlertDialogFooter><AlertDialogCancel>{tx("keep")}</AlertDialogCancel><AlertDialogAction onClick={() => state.commit(sandboxField, "")}>{tx("disable")}</AlertDialogAction></AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</section>;
}
@@ -50,6 +50,7 @@ export const RUNTIME_CONFIG_FIELDS: RuntimeConfigField[] = [
{ group: "shell", path: "tools.exec.path_prepend", kind: "text" },
{ group: "shell", path: "tools.exec.path_append", kind: "text" },
{ group: "shell", path: "tools.exec.sandbox", kind: "select", options: ["", "bwrap", "seatbelt"] },
{ group: "shell", path: "tools.exec.sandbox_launcher", kind: "text", manual: true },
{ group: "shell", path: "tools.exec.sandbox_ro_binds", kind: "list", when: { path: "tools.exec.sandbox", value: ["bwrap", "seatbelt"] } },
{ group: "shell", path: "tools.exec.sandbox_rw_binds", kind: "list", when: { path: "tools.exec.sandbox", value: ["bwrap", "seatbelt"] } },
{ group: "shell", path: "tools.exec.allowed_env_keys", kind: "list" },