sandboxTypes.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * Sandbox types for the Claude Code Agent SDK
  3. *
  4. * This file is the single source of truth for sandbox configuration types.
  5. * Both the SDK and the settings validation import from here.
  6. */
  7. import { z } from 'zod/v4'
  8. import { lazySchema } from '../utils/lazySchema.js'
  9. /**
  10. * Network configuration schema for sandbox.
  11. */
  12. export const SandboxNetworkConfigSchema = lazySchema(() =>
  13. z
  14. .object({
  15. allowedDomains: z.array(z.string()).optional(),
  16. allowManagedDomainsOnly: z
  17. .boolean()
  18. .optional()
  19. .describe(
  20. 'When true (and set in managed settings), only allowedDomains and WebFetch(domain:...) allow rules from managed settings are respected. ' +
  21. 'User, project, local, and flag settings domains are ignored. Denied domains are still respected from all sources.',
  22. ),
  23. allowUnixSockets: z
  24. .array(z.string())
  25. .optional()
  26. .describe(
  27. 'macOS only: Unix socket paths to allow. Ignored on Linux (seccomp cannot filter by path).',
  28. ),
  29. allowAllUnixSockets: z
  30. .boolean()
  31. .optional()
  32. .describe(
  33. 'If true, allow all Unix sockets (disables blocking on both platforms).',
  34. ),
  35. allowLocalBinding: z.boolean().optional(),
  36. httpProxyPort: z.number().optional(),
  37. socksProxyPort: z.number().optional(),
  38. })
  39. .optional(),
  40. )
  41. /**
  42. * Filesystem configuration schema for sandbox.
  43. */
  44. export const SandboxFilesystemConfigSchema = lazySchema(() =>
  45. z
  46. .object({
  47. allowWrite: z
  48. .array(z.string())
  49. .optional()
  50. .describe(
  51. 'Additional paths to allow writing within the sandbox. ' +
  52. 'Merged with paths from Edit(...) allow permission rules.',
  53. ),
  54. denyWrite: z
  55. .array(z.string())
  56. .optional()
  57. .describe(
  58. 'Additional paths to deny writing within the sandbox. ' +
  59. 'Merged with paths from Edit(...) deny permission rules.',
  60. ),
  61. denyRead: z
  62. .array(z.string())
  63. .optional()
  64. .describe(
  65. 'Additional paths to deny reading within the sandbox. ' +
  66. 'Merged with paths from Read(...) deny permission rules.',
  67. ),
  68. allowRead: z
  69. .array(z.string())
  70. .optional()
  71. .describe(
  72. 'Paths to re-allow reading within denyRead regions. ' +
  73. 'Takes precedence over denyRead for matching paths.',
  74. ),
  75. allowManagedReadPathsOnly: z
  76. .boolean()
  77. .optional()
  78. .describe(
  79. 'When true (set in managed settings), only allowRead paths from policySettings are used.',
  80. ),
  81. })
  82. .optional(),
  83. )
  84. /**
  85. * Sandbox settings schema.
  86. */
  87. export const SandboxSettingsSchema = lazySchema(() =>
  88. z
  89. .object({
  90. enabled: z.boolean().optional(),
  91. failIfUnavailable: z
  92. .boolean()
  93. .optional()
  94. .describe(
  95. 'Exit with an error at startup if sandbox.enabled is true but the sandbox cannot start ' +
  96. '(missing dependencies, unsupported platform, or platform not in enabledPlatforms). ' +
  97. 'When false (default), a warning is shown and commands run unsandboxed. ' +
  98. 'Intended for managed-settings deployments that require sandboxing as a hard gate.',
  99. ),
  100. // Note: enabledPlatforms is an undocumented setting read via .passthrough()
  101. // It restricts sandboxing to specific platforms (e.g., ["macos"]).
  102. //
  103. // Added to unblock NVIDIA enterprise rollout: they want to enable
  104. // autoAllowBashIfSandboxed but only on macOS initially, since Linux/WSL
  105. // sandbox support is newer and less battle-tested. This allows them to
  106. // set enabledPlatforms: ["macos"] to disable sandbox (and auto-allow)
  107. // on other platforms until they're ready to expand.
  108. autoAllowBashIfSandboxed: z.boolean().optional(),
  109. allowUnsandboxedCommands: z
  110. .boolean()
  111. .optional()
  112. .describe(
  113. 'Allow commands to run outside the sandbox via the dangerouslyDisableSandbox parameter. ' +
  114. 'When false, the dangerouslyDisableSandbox parameter is completely ignored and all commands must run sandboxed. ' +
  115. 'Default: true.',
  116. ),
  117. network: SandboxNetworkConfigSchema(),
  118. filesystem: SandboxFilesystemConfigSchema(),
  119. ignoreViolations: z.record(z.string(), z.array(z.string())).optional(),
  120. enableWeakerNestedSandbox: z.boolean().optional(),
  121. enableWeakerNetworkIsolation: z
  122. .boolean()
  123. .optional()
  124. .describe(
  125. 'macOS only: Allow access to com.apple.trustd.agent in the sandbox. ' +
  126. 'Needed for Go-based CLI tools (gh, gcloud, terraform, etc.) to verify TLS certificates ' +
  127. 'when using httpProxyPort with a MITM proxy and custom CA. ' +
  128. '**Reduces security** — opens a potential data exfiltration vector through the trustd service. Default: false',
  129. ),
  130. excludedCommands: z.array(z.string()).optional(),
  131. ripgrep: z
  132. .object({
  133. command: z.string(),
  134. args: z.array(z.string()).optional(),
  135. })
  136. .optional()
  137. .describe('Custom ripgrep configuration for bundled ripgrep support'),
  138. })
  139. .passthrough(),
  140. )
  141. // Inferred types from schemas
  142. export type SandboxSettings = z.infer<ReturnType<typeof SandboxSettingsSchema>>
  143. export type SandboxNetworkConfig = NonNullable<
  144. z.infer<ReturnType<typeof SandboxNetworkConfigSchema>>
  145. >
  146. export type SandboxFilesystemConfig = NonNullable<
  147. z.infer<ReturnType<typeof SandboxFilesystemConfigSchema>>
  148. >
  149. export type SandboxIgnoreViolations = NonNullable<
  150. SandboxSettings['ignoreViolations']
  151. >