Skip to content

Commit ed49e0f

Browse files
fstirlitzbnoordhuis
authored andcommitted
Fix shell injection bug in std.urlGet
Refs: bellard/quickjs#61
1 parent ef4d8ab commit ed49e0f

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

quickjs-libc.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ static JSValue js_std_file_putByte(JSContext *ctx, JSValue this_val,
12911291
/* urlGet */
12921292
#if !defined(__wasi__)
12931293

1294-
#define URL_GET_PROGRAM "curl -s -i"
1294+
#define URL_GET_PROGRAM "curl -s -i --"
12951295
#define URL_GET_BUF_SIZE 4096
12961296

12971297
static int http_get_header_line(FILE *f, char *buf, size_t buf_size,
@@ -1364,16 +1364,22 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValue this_val,
13641364
}
13651365

13661366
js_std_dbuf_init(ctx, &cmd_buf);
1367-
dbuf_printf(&cmd_buf, "%s ''", URL_GET_PROGRAM);
1367+
dbuf_printf(&cmd_buf, "%s '", URL_GET_PROGRAM);
13681368
len = strlen(url);
13691369
for(i = 0; i < len; i++) {
1370-
c = url[i];
1371-
if (c == '\'' || c == '\\')
1370+
switch (c = url[i]) {
1371+
case '\'':
1372+
dbuf_putstr(&cmd_buf, "'\\''");
1373+
break;
1374+
case '[': case ']': case '{': case '}': case '\\':
13721375
dbuf_putc(&cmd_buf, '\\');
1373-
dbuf_putc(&cmd_buf, c);
1376+
/* FALLTHROUGH */
1377+
default:
1378+
dbuf_putc(&cmd_buf, c);
1379+
}
13741380
}
13751381
JS_FreeCString(ctx, url);
1376-
dbuf_putstr(&cmd_buf, "''");
1382+
dbuf_putstr(&cmd_buf, "'");
13771383
dbuf_putc(&cmd_buf, '\0');
13781384
if (dbuf_error(&cmd_buf)) {
13791385
dbuf_free(&cmd_buf);

0 commit comments

Comments
 (0)