Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix shell injection bug in std.urlGet #277

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions quickjs-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@ static JSValue js_std_file_putByte(JSContext *ctx, JSValue this_val,
/* urlGet */
#if !defined(__wasi__)

#define URL_GET_PROGRAM "curl -s -i"
#define URL_GET_PROGRAM "curl -s -i --"
#define URL_GET_BUF_SIZE 4096

static int http_get_header_line(FILE *f, char *buf, size_t buf_size,
Expand Down Expand Up @@ -1337,7 +1337,7 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValue this_val,
DynBuf header_buf_s, *header_buf = &header_buf_s;
char *buf;
size_t i, len;
int c, status;
int status;
JSValue response = JS_UNDEFINED, ret_obj;
JSValue options_obj;
FILE *f;
Expand All @@ -1364,16 +1364,25 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValue this_val,
}

js_std_dbuf_init(ctx, &cmd_buf);
dbuf_printf(&cmd_buf, "%s ''", URL_GET_PROGRAM);
len = strlen(url);
for(i = 0; i < len; i++) {
c = url[i];
if (c == '\'' || c == '\\')
dbuf_printf(&cmd_buf, "%s '", URL_GET_PROGRAM);
for(i = 0; url[i] != '\0'; i++) {
unsigned char c = url[i];
switch (c) {
case '\'':
/* shell single quoted string does not support \' */
dbuf_putstr(&cmd_buf, "'\\''");
break;
case '[': case ']': case '{': case '}': case '\\':
/* prevent interpretation by curl as range or set specification */
dbuf_putc(&cmd_buf, '\\');
dbuf_putc(&cmd_buf, c);
/* FALLTHROUGH */
default:
dbuf_putc(&cmd_buf, c);
break;
}
}
JS_FreeCString(ctx, url);
dbuf_putstr(&cmd_buf, "''");
dbuf_putstr(&cmd_buf, "'");
dbuf_putc(&cmd_buf, '\0');
if (dbuf_error(&cmd_buf)) {
dbuf_free(&cmd_buf);
Expand Down
Loading