Make r2 -j work as a hashbang handler for qjs ##lang

* Fix "script not found" issue, solve command injection bugs
This commit is contained in:
pancake 2022-12-22 10:40:27 +01:00 committed by pancake
parent f58043efb7
commit cb764ac449
4 changed files with 29 additions and 13 deletions

View File

@ -1381,31 +1381,30 @@ R_API bool r_core_run_script(RCore *core, const char *file) {
ret = 1;
} else if (!strcmp (ext, "qjs")) {
if (r_lang_use (core->lang, "qjs")) {
r_lang_run_file (core->lang, file);
ret = r_lang_run_file (core->lang, file);
} else {
R_LOG_ERROR ("r2pm -ci rlang-qjs");
ret = false;
}
ret = 1;
} else if (!strcmp (ext, "wren")) {
if (r_lang_use (core->lang, "wren")) {
r_lang_run_file (core->lang, file);
ret = r_lang_run_file (core->lang, file);
} else {
R_LOG_ERROR ("r2pm -ci rlang-wren");
ret = false;
}
ret = 1;
} else if (!strcmp (ext, "tiny")) {
if (r_lang_use (core->lang, "tiny")) {
r_lang_run_file (core->lang, file);
ret = r_lang_run_file (core->lang, file);
} else {
R_LOG_ERROR ("r2pm -ci rlang-tiny");
ret = false;
}
ret = 1;
} else if (!strcmp (ext, "pl")) {
char *cmd = cmdstr ("perl");
r_lang_use (core->lang, "pipe");
lang_run_file (core, core->lang, cmd);
ret = lang_run_file (core, core->lang, cmd);
free (cmd);
ret = 1;
} else if (!strcmp (ext, "py")) {
char *fp = r_file_path ("python3");
if (!fp) {
@ -1421,17 +1420,17 @@ R_API bool r_core_run_script(RCore *core, const char *file) {
char *cmd = r_str_newf ("%s '%s'", fp, file);
#endif
r_lang_use (core->lang, "pipe");
lang_run_file (core, core->lang, cmd);
ret = lang_run_file (core, core->lang, cmd);
free (cmd);
ret = 1;
free (fp);
} else {
R_LOG_ERROR ("Cannot find python in PATH");
ret = false;
}
} else {
if (r_file_is_executable (file)) {
r_core_cmdf (core, "#!pipe %s%s", (*file=='/')?"":"./", file);
ret = 1;
ret = true;
} else {
ret = r_core_cmd_file (core, file);
}
@ -1583,7 +1582,12 @@ static int cmd_join(void *data, const char *input) { // "join"
if (input[0] == 's') { // "js"
if (input[1] == ':' || input[1] == '.') { // "js:"
if (input[2]) {
r_core_cmdf (core, ". %s", input + 2);
if (r_lang_use (core->lang, "qjs")) {
const char *fn = r_str_trim_head_ro (input + 2);
if (!r_lang_run_file (core->lang, fn)) {
R_LOG_ERROR ("Cannot find %s", fn);
}
}
} else {
if (r_config_get_b (core->config, "scr.interactive")) {
if (r_lang_use (core->lang, "qjs")) {

View File

@ -304,6 +304,7 @@ static bool lang_quickjs_file(RLangSession *s, const char *file) {
if (code) {
rc = eval (k->ctx, code) == 0;
free (code);
rc = true;
}
return rc;
}

View File

@ -913,6 +913,15 @@ R_API int r_main_radare2(int argc, const char **argv) {
}
}
}
if (json) {
if (opt.ind < argc) {
r_core_cmd_callf (r, "js:%s", argv[opt.ind]);
} else {
r_core_cmd_call (r, "js:");
}
r_core_free (r);
return 0;
}
{
const char *dbg_profile = r_config_get (r->config, "dbg.profile");
if (dbg_profile && *dbg_profile) {

View File

@ -19,7 +19,7 @@
.Op Fl r Ar rarun2
.Op Fl R Ar rr2rule
.Op Fl s Ar addr
.Op Fl 0AdDwntLquvVxX
.Op Fl 0AdDjwntLquvVxX
.Ar -|--|=|file
.Sh DESCRIPTION
radare2 is a command-line hexadecimal editor.
@ -56,6 +56,8 @@ Enable debug mode. Set cfg.debug=true
Set configuration eval variable key=value. For example \-e scr.color=false
.It Fl f
Blocksize = file size
.It Fl j
Run QJS script if an argument is passed or enter the qjs shell if no file is specified
.It Fl i Ar file
Run script file. After the file is loaded
.It Fl I Ar file