Skip to content

Commit ae6fa8d

Browse files
authoredFeb 12, 2024
Fix shell injection bug in std.urlGet (bellard#61)
1 parent 693449e commit ae6fa8d

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed
 

‎quickjs-libc.c

+12-6
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ static JSValue js_std_file_putByte(JSContext *ctx, JSValueConst this_val,
12821282

12831283
/* urlGet */
12841284

1285-
#define URL_GET_PROGRAM "curl -s -i"
1285+
#define URL_GET_PROGRAM "curl -s -i --"
12861286
#define URL_GET_BUF_SIZE 4096
12871287

12881288
static int http_get_header_line(FILE *f, char *buf, size_t buf_size,
@@ -1355,16 +1355,22 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValueConst this_val,
13551355
}
13561356

13571357
js_std_dbuf_init(ctx, &cmd_buf);
1358-
dbuf_printf(&cmd_buf, "%s ''", URL_GET_PROGRAM);
1358+
dbuf_printf(&cmd_buf, "%s '", URL_GET_PROGRAM);
13591359
len = strlen(url);
13601360
for(i = 0; i < len; i++) {
1361-
c = url[i];
1362-
if (c == '\'' || c == '\\')
1361+
switch (c = url[i]) {
1362+
case '\'':
1363+
dbuf_putstr(&cmd_buf, "'\\''");
1364+
break;
1365+
case '[': case ']': case '{': case '}': case '\\':
13631366
dbuf_putc(&cmd_buf, '\\');
1364-
dbuf_putc(&cmd_buf, c);
1367+
/* FALLTHROUGH */
1368+
default:
1369+
dbuf_putc(&cmd_buf, c);
1370+
}
13651371
}
13661372
JS_FreeCString(ctx, url);
1367-
dbuf_putstr(&cmd_buf, "''");
1373+
dbuf_putstr(&cmd_buf, "'");
13681374
dbuf_putc(&cmd_buf, '\0');
13691375
if (dbuf_error(&cmd_buf)) {
13701376
dbuf_free(&cmd_buf);

0 commit comments

Comments
 (0)