From: Martijn Dekker <martijn@inlv.org>
To: zsh-users@zsh.org
Subject: Re: Interpret Parameter Value at Time of Function Definition
Date: Tue, 5 Sep 2017 20:21:18 +0200 [thread overview]
Message-ID: <4ccab1d5-7944-dc27-9ccf-be377ff8b038@inlv.org> (raw)
In-Reply-To: <CACeGjnVYHHZHrGcZzMeaEq-feeuG9XShK5AM9XzAs8t+W6xCZA@mail.gmail.com>
Op 05-09-17 om 15:47 schreef Vin Shelton:
[...]
> $ aaa=bbb
> $ function t {
> print aaa = \"$aaa\"
> }
> $ t
> aaa = "bbb"
> $ aaa=ccc t
> aaa = "ccc"
>
> I would like to interpret $aaa at read time, so that:
>
> aaa=ccc t
>
> would print:
>
> aaa = "bbb"
>
> How do I do that?
AFAIK, the only way to do this without side effects is by using 'eval':
aaa=bbb
eval "function t {
print \"aaa = $aaa\"
}"
typeset -f t
The value of "$aaa" is now hard-coded in the function definition, as you
can see by the output of 'typeset -f t'.
Note that you have to be very careful to backslash-escape everything
except $aaa correctly so that only $aaa is interpreted at read time,
otherwise you're going to get hard-to-trace bugs.
Also, this should ONLY be done this way if $aaa is guaranteed to contain
trusted data only. If the value of $aaa can be given by other users from
the outside, you've got a code injection vulnerability, as any shell
grammar in $aaa will be interpreted and executed by 'eval'. For
instance, setting
aaa='"; echo injection"'
would print "injection".
You can avoid the vulnerability by shell-quoting the value of $aaa
before using it, which in zsh is easy to do using the (q) parameter
expansion flag:
# ... $aaa contains some untrusted data ...
eval "function t {
print -r \"aaa =\" ${(q)aaa}
}"
typeset -f t
The expansion ${(q)aaa} is appropriately quoted as a string using shell
grammar so it will never be interpreted as code.
This is advanced stuff, so be careful.
- Martijn
next prev parent reply other threads:[~2017-09-05 18:21 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-05 13:47 Vin Shelton
2017-09-05 16:17 ` Daniel Shahaf
2017-09-05 20:51 ` Bart Schaefer
2017-09-05 18:21 ` Martijn Dekker [this message]
2017-09-06 3:08 ` Ray Andrews
2017-09-06 4:16 ` Bart Schaefer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4ccab1d5-7944-dc27-9ccf-be377ff8b038@inlv.org \
--to=martijn@inlv.org \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).