This is an automated email from the git hooks/post-receive script.
git pushed a commit to branch master
in repository edi.
View the commit online.
commit 41f2af4f54aafb63d5dda7936497fb25cc1ad368
Author: Alastair Poole <[email protected]>
AuthorDate: Thu Apr 23 13:22:11 2026 +0100
feat(aipanel): add AI command policy enforcement with allow/deny prompt lists and tests
---
data/extra/prompts/agent_commands_allowed.txt | 29 ++
data/extra/prompts/agent_commands_denied.txt | 5 +
data/extra/prompts/agent_prompt_followup.txt | 4 +-
data/extra/prompts/agent_prompt_initial.txt | 5 +-
data/extra/prompts/agent_prompt_resume.txt | 4 +-
data/extra/prompts/meson.build | 2 +
src/bin/edi_ai_command_policy.c | 456 ++++++++++++++++++++++++++
src/bin/edi_ai_command_policy.h | 12 +
src/bin/edi_aipanel.c | 281 ++++++++++++----
src/bin/meson.build | 2 +
src/tests/edi_suite.c | 1 +
src/tests/edi_suite.h | 1 +
src/tests/edi_test_ai_command_policy.c | 185 +++++++++++
src/tests/meson.build | 2 +
14 files changed, 916 insertions(+), 73 deletions(-)
diff --git a/data/extra/prompts/agent_commands_allowed.txt b/data/extra/prompts/agent_commands_allowed.txt
new file mode 100644
index 0000000..dac2a70
--- /dev/null
+++ b/data/extra/prompts/agent_commands_allowed.txt
@@ -0,0 +1,29 @@
+pwd
+ls
+find
+grep
+sed
+awk
+cat
+head
+tail
+wc
+cut
+sort
+uniq
+tr
+mkdir
+touch
+cp
+mv
+git
+printf
+echo
+meson
+ninja
+make
+cmake
+cd
+tee
+chmod
+test
diff --git a/data/extra/prompts/agent_commands_denied.txt b/data/extra/prompts/agent_commands_denied.txt
new file mode 100644
index 0000000..8bb89d7
--- /dev/null
+++ b/data/extra/prompts/agent_commands_denied.txt
@@ -0,0 +1,5 @@
+rm -rf
+sudo
+doas
+git reset --hard
+git checkout --
diff --git a/data/extra/prompts/agent_prompt_followup.txt b/data/extra/prompts/agent_prompt_followup.txt
index 579565d..4417b8e 100644
--- a/data/extra/prompts/agent_prompt_followup.txt
+++ b/data/extra/prompts/agent_prompt_followup.txt
@@ -3,8 +3,10 @@ If another command is needed, reply only with {{RUN_OPEN}}COMMAND{{RUN_CLOSE}}.
Otherwise provide the final answer.
If the previous command failed (non-zero exit code), do not claim success.
+{{COMMAND_POLICY}}
Commands execute via /bin/sh -lc; use POSIX sh syntax and quote paths/arguments safely.
+Use only single-line commands. Do not use shell control operators (; | & ( ) ` < or $()).
If using > or >>, use repo-relative file paths only (no absolute paths, no '..', no descriptor targets).
Do not use multiple redirections in one command.
Safety policy (mandatory):
@@ -19,7 +21,7 @@ Task breakdown policy (mandatory):
- Avoid combining multiple write targets or risky shell chaining in one command.
- Prefer inspect/read commands before mutate/write commands.
- If verification fails, stop and repair before next actions.
-MANDATORY file-creation sequence: mkdir -p parent dir, then cat <<'EOF' > path, then test -f path. Do not continue until test -f passes. Never use touch/tee/echo/printf for initial creation.
+For file creation, use a single safe write step to a repo-relative path, then test -f path before continuing.
Original user request:
{{ORIGINAL_USER_REQUEST}}
diff --git a/data/extra/prompts/agent_prompt_initial.txt b/data/extra/prompts/agent_prompt_initial.txt
index d47f37f..a4ca5a8 100644
--- a/data/extra/prompts/agent_prompt_initial.txt
+++ b/data/extra/prompts/agent_prompt_initial.txt
@@ -4,10 +4,11 @@ If you need a command run, reply with exactly {{RUN_OPEN}}COMMAND{{RUN_CLOSE}} a
Run one command at a time.
Avoid destructive actions.
-Allowed command starters: pwd ls find grep sed awk cat head tail wc cut sort uniq tr xargs mkdir touch cp mv git printf echo meson ninja make cmake sh set cd tee chmod test.
+{{COMMAND_POLICY}}
Before creating or modifying code, first inspect repo root and all files to learn naming conventions.
Required before edits: run root inspection (pwd and/or ls) and run a full file listing command (find . -type f).
Commands execute via /bin/sh -lc; use POSIX sh syntax and shell-quote paths/arguments safely.
+Use only single-line commands. Do not use shell control operators (; | & ( ) ` < or $()).
If using > or >>, use repo-relative file paths only (no absolute paths, no '..', no descriptor targets).
Do not use multiple redirections in one command.
Safety policy (mandatory):
@@ -23,7 +24,7 @@ Task breakdown policy (mandatory):
- Do not combine multiple write targets or risky shell chaining in one command.
- Prefer inspect/read commands before mutate/write commands.
- If a verification step fails, stop and fix it before continuing.
-MANDATORY file-creation sequence: mkdir -p parent dir, then create file with cat heredoc redirection (cat <<'EOF' > path/to/file), then confirm with test -f path/to/file before continuing. Never use touch/tee/echo >/printf > for initial creation.
+For file creation, use a single safe write step to a repo-relative path, then confirm with test -f before continuing.
Do not claim success unless command output confirms it.
Mandatory repo inspection output:
diff --git a/data/extra/prompts/agent_prompt_resume.txt b/data/extra/prompts/agent_prompt_resume.txt
index df0aceb..f87cbf0 100644
--- a/data/extra/prompts/agent_prompt_resume.txt
+++ b/data/extra/prompts/agent_prompt_resume.txt
@@ -1,8 +1,10 @@
Resume the previous staged task.
If another command is needed, reply only with {{RUN_OPEN}}COMMAND{{RUN_CLOSE}}.
Do not restart from scratch.
+{{COMMAND_POLICY}}
Commands execute via /bin/sh -lc; use POSIX sh syntax and quote paths/arguments safely.
+Use only single-line commands. Do not use shell control operators (; | & ( ) ` < or $()).
If using > or >>, use repo-relative file paths only (no absolute paths, no '..', no descriptor targets).
Do not use multiple redirections in one command.
Safety policy (mandatory):
@@ -17,7 +19,7 @@ Task breakdown policy (mandatory):
- Avoid combining multiple write targets or risky shell chaining in one command.
- Prefer inspect/read commands before mutate/write commands.
- If verification fails, stop and fix before continuing.
-MANDATORY file-creation sequence: mkdir -p parent dir, then cat <<'EOF' > path, then test -f path. Do not continue until test -f passes. Never use touch/tee/echo/printf for initial creation.
+For file creation, use a single safe write step to a repo-relative path, then test -f path before continuing.
Original user request:
{{ORIGINAL_USER_REQUEST}}
diff --git a/data/extra/prompts/meson.build b/data/extra/prompts/meson.build
index bda76d7..f1675da 100644
--- a/data/extra/prompts/meson.build
+++ b/data/extra/prompts/meson.build
@@ -4,6 +4,8 @@ install_data(
'agent_prompt_followup.txt',
'agent_prompt_resume.txt',
'agent_prompt_suffix.txt',
+ 'agent_commands_allowed.txt',
+ 'agent_commands_denied.txt',
],
install_dir: join_paths(get_option('prefix'), get_option('datadir'), 'edi', 'prompts')
)
diff --git a/src/bin/edi_ai_command_policy.c b/src/bin/edi_ai_command_policy.c
new file mode 100644
index 0000000..c9cfac3
--- /dev/null
+++ b/src/bin/edi_ai_command_policy.c
@@ -0,0 +1,456 @@
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "edi_ai_command_policy.h"
+
+static Eina_Bool
+_edi_ai_command_token_get(const char *cmd, char *token, size_t token_len)
+{
+ const char *p;
+ size_t i = 0;
+
+ if (!cmd || !token || token_len < 2)
+ return EINA_FALSE;
+
+ p = cmd;
+ while (*p && isspace((unsigned char)*p))
+ p++;
+
+ while (*p && !isspace((unsigned char)*p) && i < token_len - 1)
+ token[i++] = *p++;
+ token[i] = '\0';
+
+ return token[0] != '\0';
+}
+
+static char *
+_edi_ai_command_primary_line_get(const char *cmd)
+{
+ const char *start;
+ const char *end;
+
+ if (!cmd)
+ return strdup("");
+
+ start = cmd;
+ while (*start == '\r' || *start == '\n')
+ start++;
+ end = strchr(start, '\n');
+ if (!end)
+ return strdup(start);
+
+ return strndup(start, end - start);
+}
+
+static char *
+_edi_ai_token_unquote(const char *token)
+{
+ size_t len;
+
+ if (!token)
+ return NULL;
+
+ len = strlen(token);
+ if (len >= 2 &&
+ ((token[0] == '\'' && token[len - 1] == '\'') ||
+ (token[0] == '"' && token[len - 1] == '"')))
+ return strndup(token + 1, len - 2);
+
+ return strdup(token);
+}
+
+static Eina_Bool
+_edi_ai_path_is_safe_repo_target(const char *path)
+{
+ const char *p;
+ const char *seg;
+
+ if (!path || !path[0])
+ return EINA_FALSE;
+
+ if (path[0] == '/' || path[0] == '~')
+ return EINA_FALSE;
+
+ if (!strcmp(path, ".") || !strcmp(path, ".."))
+ return EINA_FALSE;
+
+ p = path;
+ while (*p)
+ {
+ if (!isalnum((unsigned char)*p) &&
+ *p != '/' && *p != '.' && *p != '_' && *p != '-')
+ return EINA_FALSE;
+ p++;
+ }
+
+ if (strstr(path, "//"))
+ return EINA_FALSE;
+ if (!strncmp(path, "../", 3) || strstr(path, "/../") ||
+ !strcmp(path, "..") || !strcmp(path, "."))
+ return EINA_FALSE;
+ if (!strncmp(path, "./../", 5))
+ return EINA_FALSE;
+ if (path[strlen(path) - 1] == '/')
+ return EINA_FALSE;
+
+ seg = path;
+ for (p = path; ; p++)
+ {
+ if (*p == '/' || *p == '\0')
+ {
+ size_t len = p - seg;
+
+ if (len == 0)
+ return EINA_FALSE;
+ if ((len == 1 && seg[0] == '.') ||
+ (len == 2 && seg[0] == '.' && seg[1] == '.'))
+ return EINA_FALSE;
+
+ if (*p == '\0')
+ break;
+ seg = p + 1;
+ }
+ }
+
+ return EINA_TRUE;
+}
+
+static Eina_Bool
+_edi_ai_redirections_validate(const char *cmd, Eina_Bool *has_redirection,
+ char **target_out)
+{
+ char *line;
+ const char *p;
+ Eina_Bool in_single = EINA_FALSE;
+ Eina_Bool in_double = EINA_FALSE;
+ Eina_Bool escaped = EINA_FALSE;
+ char *selected_target = NULL;
+
+ if (has_redirection)
+ *has_redirection = EINA_FALSE;
+ if (target_out)
+ *target_out = NULL;
+ if (!cmd || !cmd[0])
+ return EINA_TRUE;
+
+ line = _edi_ai_command_primary_line_get(cmd);
+ if (!line)
+ return EINA_FALSE;
+
+ for (p = line; *p; p++)
+ {
+ const char *t;
+ const char *end;
+ const char *prev;
+ char *target;
+
+ if (escaped)
+ {
+ escaped = EINA_FALSE;
+ continue;
+ }
+
+ if (*p == '\\' && !in_single)
+ {
+ escaped = EINA_TRUE;
+ continue;
+ }
+
+ if (*p == '\'' && !in_double)
+ {
+ in_single = !in_single;
+ continue;
+ }
+ if (*p == '"' && !in_single)
+ {
+ in_double = !in_double;
+ continue;
+ }
+
+ if (in_single || in_double || *p != '>')
+ continue;
+
+ if (has_redirection)
+ *has_redirection = EINA_TRUE;
+
+ prev = p;
+ while (prev > line && isspace((unsigned char)*(prev - 1)))
+ prev--;
+ if (prev > line &&
+ (isdigit((unsigned char)*(prev - 1)) || *(prev - 1) == '&'))
+ goto invalid;
+
+ t = p + 1;
+ if (*t == '>')
+ t++;
+ while (*t && isspace((unsigned char)*t))
+ t++;
+ if (!*t)
+ goto invalid;
+
+ if (*t == '&' || *t == '<' || *t == '>' || *t == '|' || *t == ';' || *t == '(')
+ goto invalid;
+
+ end = t;
+ while (*end && !isspace((unsigned char)*end) &&
+ *end != ';' && *end != '|' && *end != '&' &&
+ *end != '<' && *end != '>')
+ end++;
+ if (end == t)
+ goto invalid;
+
+ target = strndup(t, end - t);
+ if (!target)
+ goto invalid;
+ if (!_edi_ai_path_is_safe_repo_target(target))
+ {
+ free(target);
+ goto invalid;
+ }
+
+ free(selected_target);
+ selected_target = target;
+ }
+
+ if (target_out)
+ *target_out = selected_target;
+ else
+ free(selected_target);
+ free(line);
+ return EINA_TRUE;
+
+invalid:
+ free(selected_target);
+ free(line);
+ return EINA_FALSE;
+}
+
+static Eina_Bool
+_edi_ai_command_single_line_only(const char *cmd)
+{
+ return (!strchr(cmd, '\n') && !strchr(cmd, '\r'));
+}
+
+static Eina_Bool
+_edi_ai_command_has_forbidden_operators(const char *cmd)
+{
+ const char *p;
+ Eina_Bool in_single = EINA_FALSE;
+ Eina_Bool in_double = EINA_FALSE;
+ Eina_Bool escaped = EINA_FALSE;
+
+ if (!cmd)
+ return EINA_TRUE;
+
+ for (p = cmd; *p; p++)
+ {
+ if (escaped)
+ {
+ escaped = EINA_FALSE;
+ continue;
+ }
+
+ if (*p == '\\' && !in_single)
+ {
+ escaped = EINA_TRUE;
+ continue;
+ }
+
+ if (*p == '\'' && !in_double)
+ {
+ in_single = !in_single;
+ continue;
+ }
+ if (*p == '"' && !in_single)
+ {
+ in_double = !in_double;
+ continue;
+ }
+
+ if (in_single || in_double)
+ continue;
+
+ if (*p == ';' || *p == '|' || *p == '&' || *p == '(' || *p == ')' ||
+ *p == '`' || *p == '<')
+ return EINA_TRUE;
+ if (*p == '$' && *(p + 1) == '(')
+ return EINA_TRUE;
+ }
+
+ return EINA_FALSE;
+}
+
+static Eina_Bool
+_edi_ai_git_command_allowed(const char *cmd)
+{
+ char *line;
+ char *saveptr = NULL;
+ char *subcmd;
+ Eina_Bool ok = EINA_FALSE;
+
+ line = _edi_ai_command_primary_line_get(cmd);
+ if (!line)
+ return EINA_FALSE;
+
+ strtok_r(line, " \t", &saveptr); /* git */
+ subcmd = strtok_r(NULL, " \t", &saveptr);
+ if (!subcmd || !subcmd[0])
+ goto end;
+
+ if (!strcmp(subcmd, "status") || !strcmp(subcmd, "diff") ||
+ !strcmp(subcmd, "show") || !strcmp(subcmd, "log") ||
+ !strcmp(subcmd, "add"))
+ ok = EINA_TRUE;
+
+end:
+ free(line);
+ return ok;
+}
+
+static Eina_Bool
+_edi_ai_mutating_paths_validate(const char *cmd, const char *token)
+{
+ char *line;
+ char *saveptr = NULL;
+ char *arg;
+ unsigned int path_args = 0;
+ Eina_Bool ok = EINA_TRUE;
+
+ if (!token || !token[0])
+ return EINA_FALSE;
+
+ if (!strcmp(token, "git"))
+ return _edi_ai_git_command_allowed(cmd);
+
+ if (strcmp(token, "mkdir") && strcmp(token, "touch") && strcmp(token, "cp") &&
+ strcmp(token, "mv") && strcmp(token, "chmod") && strcmp(token, "tee"))
+ return EINA_TRUE;
+
+ line = _edi_ai_command_primary_line_get(cmd);
+ if (!line)
+ return EINA_FALSE;
+
+ strtok_r(line, " \t", &saveptr); /* command token */
+ while ((arg = strtok_r(NULL, " \t", &saveptr)))
+ {
+ char *path = _edi_ai_token_unquote(arg);
+ Eina_Bool treat_as_path = EINA_TRUE;
+
+ if (!path || !path[0])
+ {
+ free(path);
+ continue;
+ }
+
+ if (path[0] == '-')
+ treat_as_path = EINA_FALSE;
+
+ if (treat_as_path)
+ {
+ if (!_edi_ai_path_is_safe_repo_target(path))
+ {
+ ok = EINA_FALSE;
+ free(path);
+ break;
+ }
+ path_args++;
+ }
+ free(path);
+ }
+
+ if (ok && (!strcmp(token, "cp") || !strcmp(token, "mv")) && path_args < 2)
+ ok = EINA_FALSE;
+
+ free(line);
+ return ok;
+}
+
+Eina_Bool
+edi_ai_command_policy_check(const Eina_List *allowed_commands,
+ const Eina_List *denied_patterns,
+ const char *cmd,
+ const char **reason)
+{
+ char token[64];
+ char *redir_target = NULL;
+ Eina_Bool has_redirection = EINA_FALSE;
+ Eina_List *l;
+ char *entry;
+ Eina_Bool token_allowed = EINA_FALSE;
+
+ if (!cmd || !cmd[0])
+ {
+ *reason = "Empty command.";
+ return EINA_FALSE;
+ }
+
+ if (!allowed_commands)
+ {
+ *reason = "Command policy unavailable.";
+ return EINA_FALSE;
+ }
+
+ if (!_edi_ai_command_single_line_only(cmd))
+ {
+ *reason = "Only single-line commands are allowed.";
+ return EINA_FALSE;
+ }
+
+ if (_edi_ai_command_has_forbidden_operators(cmd))
+ {
+ *reason = "Shell control operators are not allowed.";
+ return EINA_FALSE;
+ }
+
+ EINA_LIST_FOREACH((Eina_List *)denied_patterns, l, entry)
+ if (strstr(cmd, entry))
+ {
+ *reason = "Blocked dangerous command.";
+ return EINA_FALSE;
+ }
+
+ if (!_edi_ai_command_token_get(cmd, token, sizeof(token)))
+ {
+ *reason = "Missing command token.";
+ return EINA_FALSE;
+ }
+
+ if (!_edi_ai_redirections_validate(cmd, &has_redirection, &redir_target))
+ {
+ *reason = "Unsafe redirection target.";
+ return EINA_FALSE;
+ }
+ if (has_redirection && !redir_target)
+ {
+ *reason = "Missing redirection target.";
+ return EINA_FALSE;
+ }
+ free(redir_target);
+
+ EINA_LIST_FOREACH((Eina_List *)allowed_commands, l, entry)
+ {
+ if (!strcmp(token, entry))
+ {
+ token_allowed = EINA_TRUE;
+ break;
+ }
+ }
+
+ if (token_allowed)
+ {
+ if (!_edi_ai_mutating_paths_validate(cmd, token))
+ {
+ *reason = "Unsafe path argument or command mode.";
+ return EINA_FALSE;
+ }
+ return EINA_TRUE;
+ }
+
+ *reason = "Command not in allow list.";
+ return EINA_FALSE;
+}
diff --git a/src/bin/edi_ai_command_policy.h b/src/bin/edi_ai_command_policy.h
new file mode 100644
index 0000000..a02f098
--- /dev/null
+++ b/src/bin/edi_ai_command_policy.h
@@ -0,0 +1,12 @@
+#ifndef EDI_AI_COMMAND_POLICY_H
+#define EDI_AI_COMMAND_POLICY_H
+
+#include <Eina.h>
+
+Eina_Bool
+edi_ai_command_policy_check(const Eina_List *allowed_commands,
+ const Eina_List *denied_patterns,
+ const char *cmd,
+ const char **reason);
+
+#endif
diff --git a/src/bin/edi_aipanel.c b/src/bin/edi_aipanel.c
index c88ca9a..fddee0a 100644
--- a/src/bin/edi_aipanel.c
+++ b/src/bin/edi_aipanel.c
@@ -9,6 +9,7 @@
#include "Edi.h"
#include "edi_aipanel.h"
#include "edi_agent.h"
+#include "edi_ai_command_policy.h"
#include "edi_theme.h"
#include "edi_config.h"
@@ -39,7 +40,13 @@ static Eina_Bool _edi_ai_agent_inspected_all_files = EINA_FALSE;
static char *_edi_ai_prompt_tmpl_initial = NULL;
static char *_edi_ai_prompt_tmpl_followup = NULL;
static char *_edi_ai_prompt_tmpl_resume = NULL;
+static Eina_List *_edi_ai_allowed_commands = NULL;
+static Eina_List *_edi_ai_denied_patterns = NULL;
+static char *_edi_ai_command_policy_text = NULL;
static Eina_Bool _edi_ai_prompt_templates_loaded = EINA_FALSE;
+static Eina_Bool _edi_ai_command_policy_loaded = EINA_FALSE;
+static Eina_Bool _edi_ai_edits_notice_known = EINA_FALSE;
+static Eina_Bool _edi_ai_edits_notice_enabled = EINA_FALSE;
#define EDI_AI_TAG_USER "[user]"
#define EDI_AI_TAG_AGENT "[agent]"
@@ -142,6 +149,24 @@ _edi_aipanel_steps_max_get(void)
return (unsigned int) configured;
}
+static void
+_edi_aipanel_edits_mode_notice_maybe_append(void)
+{
+ Eina_Bool enabled;
+
+ enabled = _edi_aipanel_edits_enabled_get();
+ if (_edi_ai_edits_notice_known && (_edi_ai_edits_notice_enabled == enabled))
+ return;
+
+ _edi_ai_edits_notice_known = EINA_TRUE;
+ _edi_ai_edits_notice_enabled = enabled;
+
+ if (enabled)
+ _edi_aipanel_append_line("AI command execution is enabled in Settings -> AI.");
+ else
+ _edi_aipanel_append_line("AI command execution is disabled in Settings -> AI.");
+}
+
static char *
_edi_aipanel_shell_quote(const char *src)
{
@@ -300,6 +325,150 @@ _edi_aipanel_file_read_all(const char *path)
return buf;
}
+static void
+_edi_aipanel_string_list_free(Eina_List **list)
+{
+ char *entry;
+
+ if (!list)
+ return;
+
+ EINA_LIST_FREE(*list, entry)
+ free(entry);
+}
+
+static char *
+_edi_aipanel_string_strip_dup(const char *src, size_t len)
+{
+ const char *start;
+ const char *end;
+
+ start = src;
+ end = src + len;
+
+ while (start < end && isspace((unsigned char)*start))
+ start++;
+ while (end > start && isspace((unsigned char)*(end - 1)))
+ end--;
+
+ return strndup(start, end - start);
+}
+
+static Eina_List *
+_edi_aipanel_string_list_parse(const char *text)
+{
+ Eina_List *list = NULL;
+ const char *line_start;
+ const char *p;
+
+ if (!text)
+ return NULL;
+
+ line_start = text;
+ for (p = text; ; p++)
+ {
+ if (*p == '\n' || *p == '\0')
+ {
+ char *entry;
+ size_t len = p - line_start;
+
+ if (len && line_start[len - 1] == '\r')
+ len--;
+
+ entry = _edi_aipanel_string_strip_dup(line_start, len);
+ if (!entry)
+ {
+ _edi_aipanel_string_list_free(&list);
+ return NULL;
+ }
+
+ if (entry[0] && entry[0] != '#')
+ list = eina_list_append(list, entry);
+ else
+ free(entry);
+
+ if (*p == '\0')
+ break;
+
+ line_start = p + 1;
+ }
+ }
+
+ return list;
+}
+
+static char *
+_edi_aipanel_command_policy_text_build(void)
+{
+ Eina_Strbuf *buf;
+ Eina_List *l;
+ char *item;
+ Eina_Bool first = EINA_TRUE;
+
+ buf = eina_strbuf_new();
+ if (!buf)
+ return strdup("");
+
+ eina_strbuf_append(buf, "Allowed command starters: ");
+ EINA_LIST_FOREACH(_edi_ai_allowed_commands, l, item)
+ {
+ if (!first)
+ eina_strbuf_append_char(buf, ' ');
+ eina_strbuf_append(buf, item);
+ first = EINA_FALSE;
+ }
+ if (first)
+ eina_strbuf_append(buf, "(none)");
+
+ eina_strbuf_append(buf, ".\nDenied command patterns: ");
+ first = EINA_TRUE;
+ EINA_LIST_FOREACH(_edi_ai_denied_patterns, l, item)
+ {
+ if (!first)
+ eina_strbuf_append(buf, " | ");
+ eina_strbuf_append(buf, item);
+ first = EINA_FALSE;
+ }
+ if (first)
+ eina_strbuf_append(buf, "(none)");
+ eina_strbuf_append(buf, ".");
+
+ item = eina_strbuf_string_steal(buf);
+ eina_strbuf_free(buf);
+ return item;
+}
+
+static void
+_edi_aipanel_command_policy_ensure(void)
+{
+ const char *path;
+ char *allowed;
+ char *denied;
+
+ if (_edi_ai_command_policy_loaded)
+ return;
+ _edi_ai_command_policy_loaded = EINA_TRUE;
+
+ path = eina_slstr_printf(PACKAGE_DATA_DIR "/prompts/agent_commands_allowed.txt");
+ allowed = _edi_aipanel_file_read_all(path);
+ path = eina_slstr_printf(PACKAGE_DATA_DIR "/prompts/agent_commands_denied.txt");
+ denied = _edi_aipanel_file_read_all(path);
+
+ _edi_ai_allowed_commands = _edi_aipanel_string_list_parse(allowed);
+ _edi_ai_denied_patterns = _edi_aipanel_string_list_parse(denied);
+ _edi_ai_command_policy_text = _edi_aipanel_command_policy_text_build();
+
+ free(allowed);
+ free(denied);
+}
+
+static const char *
+_edi_aipanel_command_policy_text_get(void)
+{
+ _edi_aipanel_command_policy_ensure();
+ return _edi_ai_command_policy_text ?: "";
+}
+
static void
_edi_aipanel_prompt_templates_ensure(void)
{
@@ -380,11 +549,12 @@ _edi_aipanel_prompt_build_initial(const char *user_prompt)
{
char *state;
char *out;
- const char *keys[5];
- const char *vals[5];
+ const char *keys[6];
+ const char *vals[6];
const char *inspection;
_edi_aipanel_prompt_templates_ensure();
+ _edi_aipanel_command_policy_ensure();
state = _edi_aipanel_repo_state_get();
inspection = (_edi_ai_repo_inspection && _edi_ai_repo_inspection[0])
@@ -394,7 +564,8 @@ _edi_aipanel_prompt_build_initial(const char *user_prompt)
keys[2] = "REPO_INSPECTION"; vals[2] = inspection;
keys[3] = "STATE"; vals[3] = state ?: "";
keys[4] = "USER_REQUEST"; vals[4] = user_prompt ?: "";
- out = _edi_aipanel_prompt_template_render(_edi_ai_prompt_tmpl_initial, keys, vals, 5);
+ keys[5] = "COMMAND_POLICY"; vals[5] = _edi_aipanel_command_policy_text_get();
+ out = _edi_aipanel_prompt_template_render(_edi_ai_prompt_tmpl_initial, keys, vals, 6);
free(state);
if (out)
@@ -411,10 +582,11 @@ _edi_aipanel_prompt_build_followup(const char *executed_cmd, int exit_code,
char *output_clip;
char *out;
char exit_code_buf[32];
- const char *keys[8];
- const char *vals[8];
+ const char *keys[9];
+ const char *vals[9];
_edi_aipanel_prompt_templates_ensure();
+ _edi_aipanel_command_policy_ensure();
state = _edi_aipanel_repo_state_get();
output_clip = _edi_aipanel_text_clip(output, EDI_AI_RUN_MAX_OUTPUT);
@@ -428,7 +600,8 @@ _edi_aipanel_prompt_build_followup(const char *executed_cmd, int exit_code,
keys[5] = "COMMAND_OUTPUT"; vals[5] = output_clip ?: "";
keys[6] = "FILE_RESULT"; vals[6] = file_preview ?: "";
keys[7] = "STATE"; vals[7] = state ?: "";
- out = _edi_aipanel_prompt_template_render(_edi_ai_prompt_tmpl_followup, keys, vals, 8);
+ keys[8] = "COMMAND_POLICY"; vals[8] = _edi_aipanel_command_policy_text_get();
+ out = _edi_aipanel_prompt_template_render(_edi_ai_prompt_tmpl_followup, keys, vals, 9);
free(state);
free(output_clip);
@@ -445,10 +618,11 @@ _edi_aipanel_prompt_build_resume(const char *user_prompt)
char *out;
char stage_buf[32];
char exit_buf[32];
- const char *keys[9];
- const char *vals[9];
+ const char *keys[10];
+ const char *vals[10];
_edi_aipanel_prompt_templates_ensure();
+ _edi_aipanel_command_policy_ensure();
state = _edi_aipanel_repo_state_get();
snprintf(stage_buf, sizeof(stage_buf), "%u", _edi_ai_last_stage_index);
@@ -463,7 +637,8 @@ _edi_aipanel_prompt_build_resume(const char *user_prompt)
keys[6] = "LAST_COMMAND_OUTPUT"; vals[6] = _edi_ai_last_stage_output ?: "";
keys[7] = "STATE"; vals[7] = state ?: "";
keys[8] = "RESUME_REQUEST"; vals[8] = user_prompt ?: "";
- out = _edi_aipanel_prompt_template_render(_edi_ai_prompt_tmpl_resume, keys, vals, 9);
+ keys[9] = "COMMAND_POLICY"; vals[9] = _edi_aipanel_command_policy_text_get();
+ out = _edi_aipanel_prompt_template_render(_edi_ai_prompt_tmpl_resume, keys, vals, 10);
free(state);
if (out)
@@ -1023,63 +1198,11 @@ _edi_aipanel_inspection_state_update(const char *cmd, int exit_code)
static Eina_Bool
_edi_aipanel_command_allowed(const char *cmd, const char **reason)
{
- static const char *allowed[] =
- {
- "pwd", "ls", "find", "grep", "sed", "awk", "cat", "head", "tail", "wc",
- "cut", "sort", "uniq", "tr", "xargs", "mkdir", "touch", "cp", "mv",
- "git", "printf", "echo", "meson", "ninja", "make", "cmake", "sh", "set",
- "cd", "tee", "chmod", "test"
- };
- static const char *blocked[] =
- {
- "rm -rf", "sudo ", "doas ", "git reset --hard", "git checkout --",
- };
- char token[64];
- char *redir_target = NULL;
- Eina_Bool has_redirection = EINA_FALSE;
- unsigned int i;
-
- if (!cmd || !cmd[0])
- {
- *reason = "Empty command.";
- return EINA_FALSE;
- }
-
- for (i = 0; i < sizeof(blocked) / sizeof(blocked[0]); i++)
- {
- if (strstr(cmd, blocked[i]))
- {
- *reason = "Blocked dangerous command.";
- return EINA_FALSE;
- }
- }
-
- if (!_edi_aipanel_command_token_get(cmd, token, sizeof(token)))
- {
- *reason = "Missing command token.";
- return EINA_FALSE;
- }
-
- if (!_edi_aipanel_redirections_validate(cmd, &has_redirection, &redir_target))
- {
- *reason = "Unsafe redirection target.";
- return EINA_FALSE;
- }
- if (has_redirection && !redir_target)
- {
- *reason = "Missing redirection target.";
- return EINA_FALSE;
- }
- free(redir_target);
-
- for (i = 0; i < sizeof(allowed) / sizeof(allowed[0]); i++)
- {
- if (!strcmp(token, allowed[i]))
- return EINA_TRUE;
- }
-
- *reason = "Command not in allow list.";
- return EINA_FALSE;
+ _edi_aipanel_command_policy_ensure();
+ return edi_ai_command_policy_check(_edi_ai_allowed_commands,
+ _edi_ai_denied_patterns,
+ cmd,
+ reason);
}
static char *
@@ -1152,6 +1275,13 @@ _edi_aipanel_command_run(const char *command, int *exit_code)
if (exit_code)
*exit_code = -1;
+ if (!_edi_aipanel_edits_enabled_get())
+ {
+ if (exit_code)
+ *exit_code = 126;
+ return strdup("Error: AI command execution is disabled in Settings -> AI.");
+ }
+
precreate_path = _edi_aipanel_precreate_path_guess(command);
if (precreate_path)
precreate_quoted = _edi_aipanel_shell_quote(precreate_path);
@@ -1419,6 +1549,7 @@ _edi_aipanel_response_cb(const char *response, const char *error, void *data EIN
char *run_cmd = NULL;
Eina_Bool apply_response = EINA_TRUE;
Eina_Bool ui_ready = (_edi_ai_code && _edi_ai_widget);
+ Eina_Bool edits_enabled = _edi_aipanel_edits_enabled_get();
if (!ui_ready)
{
@@ -1442,9 +1573,21 @@ _edi_aipanel_response_cb(const char *response, const char *error, void *data EIN
}
else if (response && response[0])
{
- run_cmd = _edi_aipanel_edits_enabled_get()
+ run_cmd = edits_enabled
? _edi_aipanel_response_command_extract(response)
: NULL;
+ if (!edits_enabled && strstr(response, EDI_AI_RUN_OPEN))
+ {
+ const char *msg = "Error: Agent requested <edi-run>, but AI command "
+ "execution is disabled in Settings -> AI.";
+
+ if (_edi_ai_stream_row)
+ _edi_aipanel_stream_error_set(msg);
+ else
+ _edi_aipanel_append_line(msg);
+ apply_response = EINA_FALSE;
+ }
+
if (run_cmd && _edi_ai_agent_steps < _edi_aipanel_steps_max_get())
{
const char *deny_reason = NULL;
@@ -1756,6 +1899,7 @@ _edi_aipanel_config_changed_cb(void *data EINA_UNUSED, int type EINA_UNUSED,
elm_code_widget_font_set(_edi_ai_widget, _edi_project_config->font.name, _edi_project_config->font.size);
edi_theme_elm_code_set(_edi_ai_widget, _edi_project_config->gui.theme);
edi_theme_elm_code_alpha_set(_edi_ai_widget);
+ _edi_aipanel_edits_mode_notice_maybe_append();
return ECORE_CALLBACK_RENEW;
}
@@ -1832,8 +1976,7 @@ edi_aipanel_add(Evas_Object *parent)
_edi_aipanel_append_line("AI panel ready.");
_edi_aipanel_append_line("Configure provider/auth in Settings -> AI.");
- if (_edi_aipanel_edits_enabled_get())
- _edi_aipanel_append_line("Agent can request local commands with <edi-run>...</edi-run>.");
+ _edi_aipanel_edits_mode_notice_maybe_append();
_edi_aipanel_append_line("");
elm_box_pack_end(hbox, entry);
diff --git a/src/bin/meson.build b/src/bin/meson.build
index 2b6a9b9..27b132e 100644
--- a/src/bin/meson.build
+++ b/src/bin/meson.build
@@ -5,6 +5,8 @@ src = ""
'edi_agent.h',
'edi_agent_parse.c',
'edi_agent_parse.h',
+ 'edi_ai_command_policy.c',
+ 'edi_ai_command_policy.h',
'edi_aipanel.c',
'edi_aipanel.h',
'edi_config.c',
diff --git a/src/tests/edi_suite.c b/src/tests/edi_suite.c
index 7374a8a..4d9f988 100644
--- a/src/tests/edi_suite.c
+++ b/src/tests/edi_suite.c
@@ -21,6 +21,7 @@ static const struct {
{ "exe", edi_test_exe },
{ "content_provider", edi_test_content_provider },
{ "agent_parse", edi_test_agent_parse },
+ { "ai_command_policy", edi_test_ai_command_policy },
{ "language_provider", edi_test_language_provider },
{ "language_provider_c", edi_test_language_provider_c }
};
diff --git a/src/tests/edi_suite.h b/src/tests/edi_suite.h
index c53a20f..3a96afe 100644
--- a/src/tests/edi_suite.h
+++ b/src/tests/edi_suite.h
@@ -12,6 +12,7 @@ void edi_test_create(TCase *tc);
void edi_test_exe(TCase *tc);
void edi_test_content_provider(TCase *tc);
void edi_test_agent_parse(TCase *tc);
+void edi_test_ai_command_policy(TCase *tc);
void edi_test_language_provider(TCase *tc);
void edi_test_language_provider_c(TCase *tc);
diff --git a/src/tests/edi_test_ai_command_policy.c b/src/tests/edi_test_ai_command_policy.c
new file mode 100644
index 0000000..298160a
--- /dev/null
+++ b/src/tests/edi_test_ai_command_policy.c
@@ -0,0 +1,185 @@
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "edi_suite.h"
+#include "../bin/edi_ai_command_policy.h"
+
+static Eina_List *
+_edi_test_policy_list_from_array(const char **items, unsigned int count)
+{
+ Eina_List *list = NULL;
+ unsigned int i;
+
+ for (i = 0; i < count; i++)
+ list = eina_list_append(list, strdup(items[i]));
+
+ return list;
+}
+
+static void
+_edi_test_policy_list_free(Eina_List *list)
+{
+ char *item;
+
+ EINA_LIST_FREE(list, item)
+ free(item);
+}
+
+static void
+_edi_test_policy_prepare(Eina_List **allowed, Eina_List **denied)
+{
+ static const char *allowed_items[] =
+ {
+ "pwd", "ls", "find", "grep", "sed", "awk", "cat", "head", "tail", "wc",
+ "cut", "sort", "uniq", "tr", "mkdir", "touch", "cp", "mv", "git",
+ "printf", "echo", "meson", "ninja", "make", "cmake", "cd", "tee",
+ "chmod", "test"
+ };
+ static const char *denied_items[] =
+ {
+ "rm -rf", "sudo ", "doas ", "git reset --hard", "git checkout --"
+ };
+
+ *allowed = _edi_test_policy_list_from_array(allowed_items,
+ sizeof(allowed_items) / sizeof(allowed_items[0]));
+ *denied = _edi_test_policy_list_from_array(denied_items,
+ sizeof(denied_items) / sizeof(denied_items[0]));
+}
+
+START_TEST(edi_ai_policy_allows_simple_read)
+{
+ Eina_List *allowed = NULL;
+ Eina_List *denied = NULL;
+ const char *reason = NULL;
+
+ _edi_test_policy_prepare(&allowed, &denied);
+ ck_assert(edi_ai_command_policy_check(allowed, denied, "ls -la", &reason));
+ ck_assert_ptr_null(reason);
+
+ _edi_test_policy_list_free(allowed);
+ _edi_test_policy_list_free(denied);
+}
+END_TEST
+
+START_TEST(edi_ai_policy_blocks_shell_control_operators)
+{
+ Eina_List *allowed = NULL;
+ Eina_List *denied = NULL;
+ const char *reason = NULL;
+
+ _edi_test_policy_prepare(&allowed, &denied);
+ ck_assert(!edi_ai_command_policy_check(allowed, denied, "echo ok; ls", &reason));
+ ck_assert_ptr_nonnull(reason);
+
+ _edi_test_policy_list_free(allowed);
+ _edi_test_policy_list_free(denied);
+}
+END_TEST
+
+START_TEST(edi_ai_policy_blocks_multiline)
+{
+ Eina_List *allowed = NULL;
+ Eina_List *denied = NULL;
+ const char *reason = NULL;
+
+ _edi_test_policy_prepare(&allowed, &denied);
+ ck_assert(!edi_ai_command_policy_check(allowed, denied, "ls\npwd", &reason));
+ ck_assert_ptr_nonnull(reason);
+
+ _edi_test_policy_list_free(allowed);
+ _edi_test_policy_list_free(denied);
+}
+END_TEST
+
+START_TEST(edi_ai_policy_blocks_unsafe_redirection_path)
+{
+ Eina_List *allowed = NULL;
+ Eina_List *denied = NULL;
+ const char *reason = NULL;
+
+ _edi_test_policy_prepare(&allowed, &denied);
+ ck_assert(!edi_ai_command_policy_check(allowed, denied, "echo hi > /tmp/x", &reason));
+ ck_assert_ptr_nonnull(reason);
+
+ _edi_test_policy_list_free(allowed);
+ _edi_test_policy_list_free(denied);
+}
+END_TEST
+
+START_TEST(edi_ai_policy_blocks_unsafe_mutating_arg_path)
+{
+ Eina_List *allowed = NULL;
+ Eina_List *denied = NULL;
+ const char *reason = NULL;
+
+ _edi_test_policy_prepare(&allowed, &denied);
+ ck_assert(!edi_ai_command_policy_check(allowed, denied, "touch ../oops.txt", &reason));
+ ck_assert_ptr_nonnull(reason);
+
+ _edi_test_policy_list_free(allowed);
+ _edi_test_policy_list_free(denied);
+}
+END_TEST
+
+START_TEST(edi_ai_policy_allows_safe_cp_and_mv)
+{
+ Eina_List *allowed = NULL;
+ Eina_List *denied = NULL;
+ const char *reason = NULL;
+
+ _edi_test_policy_prepare(&allowed, &denied);
+ ck_assert(edi_ai_command_policy_check(allowed, denied, "cp src/a.c src/b.c", &reason));
+ ck_assert(edi_ai_command_policy_check(allowed, denied, "mv src/a.c src/b.c", &reason));
+
+ _edi_test_policy_list_free(allowed);
+ _edi_test_policy_list_free(denied);
+}
+END_TEST
+
+START_TEST(edi_ai_policy_blocks_cp_missing_destination)
+{
+ Eina_List *allowed = NULL;
+ Eina_List *denied = NULL;
+ const char *reason = NULL;
+
+ _edi_test_policy_prepare(&allowed, &denied);
+ ck_assert(!edi_ai_command_policy_check(allowed, denied, "cp src/a.c", &reason));
+ ck_assert_ptr_nonnull(reason);
+
+ _edi_test_policy_list_free(allowed);
+ _edi_test_policy_list_free(denied);
+}
+END_TEST
+
+START_TEST(edi_ai_policy_git_subcommand_restrictions)
+{
+ Eina_List *allowed = NULL;
+ Eina_List *denied = NULL;
+ const char *reason = NULL;
+
+ _edi_test_policy_prepare(&allowed, &denied);
+ ck_assert(edi_ai_command_policy_check(allowed, denied, "git add src/main.c", &reason));
+ ck_assert(!edi_ai_command_policy_check(allowed, denied, "git checkout main", &reason));
+ ck_assert(!edi_ai_command_policy_check(allowed, denied, "git reset --hard HEAD~1", &reason));
+
+ _edi_test_policy_list_free(allowed);
+ _edi_test_policy_list_free(denied);
+}
+END_TEST
+
+void
+edi_test_ai_command_policy(TCase *tc)
+{
+ tcase_add_test(tc, edi_ai_policy_allows_simple_read);
+ tcase_add_test(tc, edi_ai_policy_blocks_shell_control_operators);
+ tcase_add_test(tc, edi_ai_policy_blocks_multiline);
+ tcase_add_test(tc, edi_ai_policy_blocks_unsafe_redirection_path);
+ tcase_add_test(tc, edi_ai_policy_blocks_unsafe_mutating_arg_path);
+ tcase_add_test(tc, edi_ai_policy_allows_safe_cp_and_mv);
+ tcase_add_test(tc, edi_ai_policy_blocks_cp_missing_destination);
+ tcase_add_test(tc, edi_ai_policy_git_subcommand_restrictions);
+}
diff --git a/src/tests/meson.build b/src/tests/meson.build
index 8aa4ca8..fa87287 100644
--- a/src/tests/meson.build
+++ b/src/tests/meson.build
@@ -3,11 +3,13 @@ src = ""
'edi_suite.c',
'edi_test_content_provider.c',
'edi_test_agent_parse.c',
+ 'edi_test_ai_command_policy.c',
'edi_test_create.c',
'edi_test_exe.c',
'edi_test_language_provider.c',
'edi_test_language_provider_c.c',
'edi_test_path.c',
+ '../bin/edi_ai_command_policy.c',
])
check = dependency('check')
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.