-
Notifications
You must be signed in to change notification settings - Fork 1
fix(sandbox): keep agent workspace off gateway state files #79
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
Changes from all commits
48c03f5
dc14526
41fdaf8
e63a7ec
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 |
|---|---|---|
|
|
@@ -53,6 +53,9 @@ static const char *const BLOCK_SUBSTRINGS[] = { | |
| "~/.ssh/id_", | ||
| "id_rsa", | ||
| "id_ed25519", | ||
| "auth_tokens.json", | ||
| "shellclaw.pid", | ||
| "shellclaw.log", | ||
| /* Jetson Tegra GPU device nodes (audit 7.1 — not bind-mounted, block direct open) */ | ||
| "/dev/nvhost", | ||
| "/dev/nvgpu", | ||
|
|
@@ -119,6 +122,101 @@ int allowlist_path_is_under_workspace(const char *path, const char *workspace_ro | |
| return 0; | ||
| } | ||
|
|
||
| int allowlist_path_is_runtime_state_file(const char *path) | ||
| { | ||
| char resolved[PATH_MAX]; | ||
| const char *use = path; | ||
| const char *base; | ||
| const char *slash; | ||
| char parent[PATH_MAX]; | ||
| size_t parent_len; | ||
|
|
||
| if (!path || !path[0]) | ||
| return 0; | ||
| if (realpath(path, resolved) != NULL) | ||
| use = resolved; | ||
| base = strrchr(use, '/'); | ||
| base = base ? base + 1 : use; | ||
| if (strcmp(base, "auth_tokens.json") == 0 || | ||
| strcmp(base, "shellclaw.pid") == 0 || | ||
| strcmp(base, "shellclaw.log") == 0) | ||
| return 1; | ||
| if (strcmp(base, "config.toml") != 0 && strcmp(base, "memory.db") != 0 && | ||
| strncmp(base, "memory.db-", 10) != 0) | ||
| return 0; | ||
| slash = strrchr(use, '/'); | ||
| if (!slash || slash == use) | ||
| return 0; | ||
| parent_len = (size_t)(slash - use); | ||
| if (parent_len >= sizeof(parent)) | ||
| return 0; | ||
| memcpy(parent, use, parent_len); | ||
| parent[parent_len] = '\0'; | ||
| slash = strrchr(parent, '/'); | ||
| slash = slash ? slash + 1 : parent; | ||
| return strcmp(slash, ".shellclaw") == 0; | ||
| } | ||
|
|
||
| /** Copy @p rel under @p root, collapsing "." and "..". Absolute @p rel is copied as-is. */ | ||
| static int join_under_root(const char *root, const char *rel, char *out, size_t cap) | ||
| { | ||
| char tmp[PATH_MAX]; | ||
| char *dup; | ||
| char *save = NULL; | ||
| char *tok; | ||
| char *stack[48] = {0}; | ||
| int nstack = 0; | ||
| int i; | ||
| size_t used; | ||
|
|
||
| if (!rel || !rel[0] || !out || cap == 0) | ||
| return -1; | ||
| if (rel[0] == '/') { | ||
| if (strlen(rel) + 1 > cap) | ||
| return -1; | ||
| memcpy(out, rel, strlen(rel) + 1); | ||
| return 0; | ||
| } | ||
| if (!root || !root[0]) | ||
| return -1; | ||
| if (snprintf(tmp, sizeof(tmp), "%s/%s", root, rel) >= (int)sizeof(tmp)) | ||
| return -1; | ||
| dup = strdup(tmp); | ||
| if (!dup) | ||
| return -1; | ||
| for (tok = strtok_r(dup, "/", &save); tok; tok = strtok_r(NULL, "/", &save)) { | ||
| if (strcmp(tok, ".") == 0) | ||
| continue; | ||
| if (strcmp(tok, "..") == 0) { | ||
| if (nstack > 0) | ||
| nstack--; | ||
| continue; | ||
| } | ||
| if (nstack >= (int)(sizeof(stack) / sizeof(stack[0]))) { | ||
| free(dup); | ||
| return -1; | ||
| } | ||
| stack[nstack++] = tok; | ||
| } | ||
| used = 0; | ||
| out[0] = '\0'; | ||
| for (i = 0; i < nstack; i++) { | ||
| size_t part = strlen(stack[i]); | ||
| if (used + 1 + part + 1 > cap) { | ||
| free(dup); | ||
| return -1; | ||
| } | ||
| out[used++] = '/'; | ||
| memcpy(out + used, stack[i], part); | ||
| used += part; | ||
| out[used] = '\0'; | ||
| } | ||
| free(dup); | ||
| if (used == 0) | ||
| return -1; | ||
| return 0; | ||
| } | ||
|
|
||
| /* ------------------------------------------------------------------ */ | ||
| /* Public: combined check */ | ||
| /* ------------------------------------------------------------------ */ | ||
|
|
@@ -153,40 +251,59 @@ int allowlist_check_shell_command(const char *cmd, const allowlist_config_t *cfg | |
| return 1; | ||
| } | ||
| } | ||
| /* Phase 2: workspace path containment */ | ||
| if (!cfg || !cfg->workspace_only || !cfg->workspace_path || !cfg->workspace_path[0]) | ||
| return 0; | ||
| workspace_only = cfg->workspace_only; | ||
| (void)workspace_only; | ||
| /* Resolve workspace root once */ | ||
| if (!realpath(cfg->workspace_path, ws_resolved)) { | ||
| /* Workspace path does not exist; use as-is. */ | ||
| size_t n = strlen(cfg->workspace_path); | ||
| if (n >= PATH_MAX) n = PATH_MAX - 1; | ||
| memcpy(ws_resolved, cfg->workspace_path, n); | ||
| ws_resolved[n] = '\0'; | ||
| /* Phase 2: runtime-state paths, then optional workspace containment. | ||
| * State files are rejected even when workspace_only is off, so an | ||
| * unsandboxed `cat ~/.shellclaw/config.toml` cannot skip the check. */ | ||
| workspace_only = cfg && cfg->workspace_only && cfg->workspace_path && | ||
| cfg->workspace_path[0]; | ||
| if (cfg && cfg->workspace_path && cfg->workspace_path[0]) { | ||
| if (!realpath(cfg->workspace_path, ws_resolved)) { | ||
| size_t n = strlen(cfg->workspace_path); | ||
| if (n >= PATH_MAX) n = PATH_MAX - 1; | ||
| memcpy(ws_resolved, cfg->workspace_path, n); | ||
| ws_resolved[n] = '\0'; | ||
| } | ||
| workspace_root = ws_resolved; | ||
| } | ||
| workspace_root = ws_resolved; | ||
| /* Tokenize the command and check each path-like token. */ | ||
| cmd_copy = strdup(cmd); | ||
| if (!cmd_copy) return 0; /* fail-open on OOM */ | ||
| tok = strtok_r(cmd_copy, " \t\n;|&><", &saveptr); | ||
| while (tok) { | ||
| if (has_path_chars(tok)) { | ||
| /* Expand a leading tilde naively */ | ||
| char expanded[PATH_MAX]; | ||
| if (tok[0] == '~') { | ||
| const char *home = getenv("HOME"); | ||
| if (home) | ||
| snprintf(expanded, sizeof(expanded), "%s%s", home, tok + 1); | ||
| else | ||
| snprintf(expanded, sizeof(expanded), "%s", tok); | ||
| tok = expanded; | ||
| char expanded[PATH_MAX]; | ||
| const char *check = tok; | ||
| if (tok[0] == '~') { | ||
| const char *home = getenv("HOME"); | ||
| if (home) | ||
| snprintf(expanded, sizeof(expanded), "%s%s", home, tok + 1); | ||
| else | ||
| snprintf(expanded, sizeof(expanded), "%s", tok); | ||
| check = expanded; | ||
| } | ||
| if (allowlist_path_is_runtime_state_file(check)) { | ||
|
adriannoes marked this conversation as resolved.
Author
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. 🔒 Agentic Security Review The new runtime-state denylist inspects raw Impact: An inbound Discord/WebChat/cron prompt that causes Reviewed by Cursor Security Reviewer for commit e63a7ec. Configure here. |
||
| set_reason(reason_buf, reason_cap, | ||
| "command blocked: runtime state file: ", check); | ||
| fprintf(stderr, "allowlist: blocked runtime state file: %s\n", check); | ||
| free(cmd_copy); | ||
| return 1; | ||
| } | ||
| /* sandbox_exec chdirs into the workspace, so a bare name is that file. */ | ||
| if (workspace_root && check[0] != '/') { | ||
| char joined[PATH_MAX]; | ||
| if (join_under_root(workspace_root, check, joined, sizeof(joined)) == 0 && | ||
| allowlist_path_is_runtime_state_file(joined)) { | ||
| set_reason(reason_buf, reason_cap, | ||
| "command blocked: runtime state file: ", joined); | ||
| fprintf(stderr, "allowlist: blocked runtime state file: %s\n", joined); | ||
| free(cmd_copy); | ||
| return 1; | ||
| } | ||
| if (!allowlist_path_is_under_workspace(tok, workspace_root)) { | ||
| } | ||
| if (workspace_only && has_path_chars(tok)) { | ||
| if (!allowlist_path_is_under_workspace(check, workspace_root)) { | ||
| set_reason(reason_buf, reason_cap, | ||
| "command blocked: path escapes workspace: ", tok); | ||
| fprintf(stderr, "allowlist: blocked path outside workspace: %s\n", tok); | ||
| "command blocked: path escapes workspace: ", check); | ||
| fprintf(stderr, "allowlist: blocked path outside workspace: %s\n", check); | ||
| free(cmd_copy); | ||
| return 1; | ||
| } | ||
|
|
||


Uh oh!
There was an error while loading. Please reload this page.