2
0
Fork 0
forked from zynk/zynk

fix: replace popen with fork/exec to prevent shell injection

This commit is contained in:
Armin 2026-06-29 16:57:51 +02:00
commit eee05fc169

80
calc.c
View file

@ -641,30 +641,33 @@ static void ai_ask(Session *s, const char *question, const char *reply_target, c
if (a3) plen += snprintf(prefix + plen, sizeof prefix - plen, "[%s] ", a3);
}
free(a1); free(a2); free(a3);
char escaped[4096], *d = escaped;
*d++ = '\'';
for (const char *p = prefix; *p && d < escaped + 4080; p++) {
if (*p == '\'') { memcpy(d, "'\\''", 4); d += 4; }
else *d++ = *p;
}
for (char *src = q; *src && d < escaped + 4080; src++) {
if (*src == '\'') { memcpy(d, "'\\''", 4); d += 4; }
else *d++ = *src;
}
*d++ = '\''; *d = 0;
char combined[4096];
snprintf(combined, sizeof combined, "%s%s", prefix, q ? q : "");
free(q);
char cmd[4500];
snprintf(cmd, sizeof cmd, OPENCODE_BIN " run --dangerously-skip-permissions %s 2>/dev/null", escaped);
FILE *f = popen(cmd, "r");
if (!f) { ai_set_answer("AI query failed (could not start opencode)"); db_close(DB); exit(0); }
int ai_pipe[2];
if (pipe(ai_pipe) < 0) { ai_set_answer("AI query failed (pipe error)"); db_close(DB); exit(0); }
pid_t cmd_pid = fork();
if (cmd_pid < 0) { close(ai_pipe[0]); close(ai_pipe[1]); ai_set_answer("AI query failed (fork error)"); db_close(DB); exit(0); }
if (cmd_pid == 0) {
close(ai_pipe[0]);
dup2(ai_pipe[1], STDOUT_FILENO);
close(ai_pipe[1]);
int dn = open("/dev/null", O_WRONLY);
if (dn >= 0) { dup2(dn, STDERR_FILENO); close(dn); }
execl(OPENCODE_BIN, "opencode", "run", "--dangerously-skip-permissions", combined, (char *)NULL);
_exit(127);
}
close(ai_pipe[1]);
char out[6144];
size_t olen = 0;
int c;
while ((c = fgetc(f)) != EOF && olen < sizeof out - 1) out[olen++] = c;
out[olen] = 0;
int rc = pclose(f);
ssize_t n;
while ((n = read(ai_pipe[0], out + olen, 6143 - olen)) > 0) olen += n;
close(ai_pipe[0]);
int status;
waitpid(cmd_pid, &status, 0);
while (olen > 0 && (out[olen-1] == '\n' || out[olen-1] == '\r')) olen--;
out[olen] = 0;
int rc = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
if (rc != 0) {
ai_set_answer("AI query failed (opencode exited with error)");
} else if (olen == 0) {
@ -682,25 +685,34 @@ static void ai_ask(Session *s, const char *question, const char *reply_target, c
/* irc_msg(s, reply_target, "Thinking..."); */
log_stamp(); fprintf(stderr, CLR_BLUE "AI started (PID %d) from %s:" CLR_RESET " %s\n", pid, src_nick, question);
}
static char *fetch_url(const char *url) {
char escaped[2048], *d = escaped;
*d++ = '\'';
for (const char *p = url; *p && d < escaped + 2032; p++) {
if (*p == '\'') { memcpy(d, "'\\''", 4); d += 4; }
else *d++ = *p;
static char *run_curl(const char *url) {
int fd[2];
if (pipe(fd) < 0) return NULL;
pid_t pid = fork();
if (pid < 0) { close(fd[0]); close(fd[1]); return NULL; }
if (pid == 0) {
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
close(fd[1]);
int dn = open("/dev/null", O_WRONLY);
if (dn >= 0) { dup2(dn, STDERR_FILENO); close(dn); }
execlp("curl", "curl", "-s", "--max-time", "10", url, (char *)NULL);
_exit(127);
}
*d++ = '\''; *d = 0;
char cmd[4096];
snprintf(cmd, sizeof cmd, "curl -s --max-time 10 %s", escaped);
FILE *f = popen(cmd, "r");
if (!f) return NULL;
close(fd[1]);
char buf[2048];
size_t len = 0;
int c;
while ((c = fgetc(f)) != EOF && len < sizeof buf - 1) buf[len++] = c;
ssize_t n;
while ((n = read(fd[0], buf + len, 2047 - len)) > 0) len += n;
close(fd[0]);
int status;
waitpid(pid, &status, 0);
buf[len] = 0;
int rc = pclose(f);
return rc == 0 ? strdup(buf) : NULL;
return status == 0 ? strdup(buf) : NULL;
}
static char *fetch_url(const char *url) {
return run_curl(url);
}
static char *fetch_forecast(const char *city_url, const char *city_display) {
char url[512];