cr4nk
cr4nk

Reputation: 29

Perl: Access server $ENV in substitution

I've been looking for way to replace a string with the value of a server $ENV[KEY]by using perl substitution in httpd.conf of an Apache server. My code so far:

ExtFilterDefine htmlfilter mode=output intype=text/html cmd="/usr/bin/perl -pe 's|Mountainbike|qq(\") $ENV qq(\")|e'"

The snippet doesn't work. Does someone has any idea how to fix it? I guess it has something to do with the quotes

|qq(\") $ENV qq(\")|

I also tried another option and put my perl code inside a *.pl file and than put the following in my httpd.conf file:

ExtFilterDefine htmlfilter mode=output intype=text/html cmd="/usr/bin/perl -pe 's|Mountainbike|perl env_check.pl|e'"

This works as I expected and it's totaly fine. But how can I do it in the first one liner example above?

Upvotes: 0

Views: 182

Answers (1)

ikegami
ikegami

Reputation: 385897

Accessing the value of an env var is done using

$ENV{KEY}

The whole substitution looks like this:

s/Mountainbike/qq(") . $ENV{KEY} . qq(")/e

Simplified:

s/Mountainbike/qq("$ENV{KEY}")/e

Simplified:

s/Mountainbike/"$ENV{KEY}"/

One catch. The quotes tell me you're building a piece of code. What if the value of the environment var contains " or another special character? This suffers from a code injection bug. Assuming it's safe to escapes all special characters with \, you can solve that problem using the following:

s/Mountainbike/"\Q$ENV{KEY}\E"/

Finally, you need to include in in the directive.

... cmd="s/Mountainbike/\"\\Q$ENV{KEY}\\E\"/"

Upvotes: 1

Related Questions