From dfc03c0330b96ff4fbe14df39ba895427b8fd004 Mon Sep 17 00:00:00 2001 Message-ID: From: Ihor Radchenko Date: Mon, 21 Aug 2023 09:57:50 +0300 Subject: [PATCH 1/2] org-macs: New common API function to quote shell arguments * lisp/org-macs.el (org-shell-arg-literal): New auxiliary constant. (org-make-shell-command): New function that returns shell command built from individual shell arguments, escaping them to prevent malicious code execution. Link: https://orgmode.org/list/ub549k$q11$1@ciao.gmane.io --- lisp/org-macs.el | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lisp/org-macs.el b/lisp/org-macs.el index 907e8bed7..95af9e45e 100644 --- a/lisp/org-macs.el +++ b/lisp/org-macs.el @@ -1593,6 +1593,37 @@ (defun org-sxhash-safe (obj &optional counter) (puthash hash obj org-sxhash-objects) (puthash obj hash org-sxhash-hashes))))) +(defconst org-shell-arg-literal (gensym "literal") + "Symbol to be used to mark shell arguments that should not be escaped. +See `org-make-shell-command'.") +(defun org-make-shell-command (command &rest args) + "Build safe shell command string to run COMMAND with ARGS. + +The resulting shell command is safe against malicious shell expansion. + +ARGS can be nil, strings, (LITERAL STRING), or a list of +such elements. LITERAL must be the value of `org-shell-arg-literal'. + +Strings will be quoted with `shell-quote-argument' while \(literal +STRING) will be used without quoting. nil values will be ignored." + (concat + command (when command " ") + (mapconcat + #'identity + (delq + nil + (mapcar + (lambda (str-def) + (pcase str-def + (`(or nil "") nil) + ((pred stringp) (shell-quote-argument str-def)) + (`(,(pred (eq org-shell-arg-literal)) ,(and (pred stringp) str)) + str) + ((pred listp) (apply #'org-make-shell-command nil str-def)) + (_ (error "Unknown ARG specification: %S" str-def)))) + args)) + " "))) + (defun org-compile-file (source process ext &optional err-msg log-buf spec) "Compile a SOURCE file using PROCESS. -- 2.41.0