Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions docs/manual/mod/mod_env.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,25 @@ SSI pages</description>
(<code>\</code>) is continued on the following line, just as in the main
configuration files.</p>

<example><title>Example</title>
<p>A value beginning with a single or double quote (after any whitespace
following <code>=</code>) is treated as quoted, as with
<directive module="mod_env">SetEnv</directive>. The surrounding quotes
are removed, and whitespace inside them is preserved. Whitespace before
the opening quote or after the closing quote is ignored. Within a quoted
value, a backslash escapes another backslash or the matching quote
character; other backslashes are preserved. To include a literal quote,
escape it inside a quoted value or use the other quote character to
enclose the value. Unquoted values may contain spaces and are not
unescaped. An unterminated quoted value or unexpected text after the
closing quote causes a configuration error.</p>

<p>If the file cannot be read completely, including because a line
(after joining continued lines) is too long, configuration parsing
fails with an error identifying the file and line number.</p>

<example><title>Example using quoted values</title>
<highlight language="config">
SetEnvFromFile conf/app.env
SetEnvFromFile "conf/app.env"
</highlight>
</example>

Expand All @@ -156,6 +172,10 @@ SSI pages</description>
# Application settings
APP_MODE=production
SPECIAL_PATH=/foo/bin
PADDED_VALUE=" value with surrounding spaces "
EMPTY_VALUE=""
LITERAL_QUOTE="'value"
ESCAPED_QUOTE='\'value'
</highlight>
</example>

Expand Down
43 changes: 40 additions & 3 deletions modules/metadata/mod_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "apr.h"
#include "apr_lib.h"
#include "apr_strings.h"

#if APR_HAVE_STDLIB_H
Expand Down Expand Up @@ -173,9 +174,9 @@ static const char *add_env_module_vars_from_file(cmd_parms *cmd, void *sconf_,
* table used by SetEnv, so they merge and reach r->subprocess_env the
* same way.
*/
while (!ap_cfg_getline(line, sizeof(line), file)) {
while ((rv = ap_cfg_getline(line, sizeof(line), file)) == APR_SUCCESS) {
const char *rest = line;
const char *name, *value;
const char *name, *value, *quoted;

if (line[0] == '#' || line[0] == '\0') {
continue;
Expand All @@ -190,12 +191,48 @@ static const char *add_env_module_vars_from_file(cmd_parms *cmd, void *sconf_,
}

/* rest points just past the '='; no '=' means an empty value. */
value = apr_pstrdup(cmd->pool, rest);
quoted = rest;
while (apr_isspace(*quoted)) {
++quoted;
}
if (*quoted == '"' || *quoted == '\'') {
const char *end;

/* ap_getword_conf() does not report unterminated quotes. */
for (end = quoted + 1; *end && *end != *quoted; ++end) {
if (*end == '\\' && (end[1] == *quoted || end[1] == '\\')) {
++end;
}
}
if (!*end) {
ap_cfg_closefile(file);
return apr_psprintf(cmd->pool,
"%s: Error reading %s at line %u: "
"Unterminated quoted value",
cmd->cmd->name, fname, file->line_number);
}

value = ap_getword_conf(cmd->pool, &quoted);
if (*quoted) {
ap_cfg_closefile(file);
return apr_psprintf(cmd->pool,
"%s: Error reading %s at line %u: "
"Unexpected text after quoted value",
cmd->cmd->name, fname, file->line_number);
}
}
else {
value = apr_pstrdup(cmd->pool, rest);
}
apr_table_setn(sconf->vars, name, value);
}

ap_cfg_closefile(file);

if (rv != APR_EOF) {
return ap_pcfg_strerror(cmd->pool, file, rv);
}

return NULL;
}

Expand Down
168 changes: 166 additions & 2 deletions test/modules/metadata/test_002_setenvfromfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
pytestmark = skip_not_backported("r1938506", "SetEnvFromFile")


def _write_ssi_echo(path, names):
def _write_ssi_echo(path, names, encoding="entity"):
"""An SSI page echoing each variable as NAME=[value]. An unset
variable echoes the default "(none)", so a set-but-empty variable
("[]") is distinguishable from one that never made it into the table.
"""
with open(path, "w") as f:
for name in names:
f.write(f'{name}=[<!--#echo var="{name}" -->]\n')
f.write(f'{name}=[<!--#echo encoding="{encoding}" var="{name}" -->]\n')


class TestSetEnvFromFile:
Expand Down Expand Up @@ -204,3 +204,167 @@ def test_metadata_002_04_missing_file(self, env):
good.add_vhost_test1()
good.install()
assert env.apache_restart() == 0


class TestSetEnvFromFileConfig:

def check_config(self, env, contents):
env_file = os.path.join(env.gen_dir, "setenv-read.env")
with open(env_file, "w", newline="") as f:
f.write(contents)

conf = HttpdConf(env, extras={
'base': f'SetEnvFromFile "{env_file}"',
})
conf.add_vhost_test1()
conf.install()
return env.run([os.path.join(env.bin_dir, "httpd"),
"-t", "-d", env.server_dir,
"-f", os.path.join(env.server_dir, "conf", "httpd.conf")])

@pytest.mark.parametrize("line_length", [8191, 8192, 16384])
@pytest.mark.parametrize("ending", ["\n", ""], ids=["newline", "eof"])
def test_metadata_002_05_long_line(self, env, line_length, ending):
line = "ENV_LONG=" + "x" * (line_length - len("ENV_LONG="))
contents = "ENV_BEFORE=before\n" + line + ending
if ending:
contents += "ENV_AFTER=after\n"
r = self.check_config(env, contents)
assert r.exit_code != 0, "oversized line was accepted"
assert "setenv-read.env at line 2: Line too long" in r.stderr

def test_metadata_002_06_long_continuation(self, env):
r = self.check_config(
env, "ENV_BEFORE=before\nENV_LONG=" + "x" * 4096
+ "\\\n" + "y" * 4096 + "\nENV_AFTER=after\n")
assert r.exit_code != 0, "oversized continued line was accepted"
assert "setenv-read.env at line 3: Line too long" in r.stderr

# The 8192-byte buffer needs room for a newline and the terminating NUL.
@pytest.mark.parametrize("contents", [
pytest.param("", id="empty-file"),
pytest.param("ENV_OK=present", id="no-final-newline"),
pytest.param("ENV_OK=present\n", id="final-newline"),
pytest.param("ENV_LONG=" + "x" * (8190 - len("ENV_LONG=")),
id="max-line-no-newline"),
pytest.param("ENV_LONG=" + "x" * (8190 - len("ENV_LONG=")) + "\n",
id="max-line-with-newline"),
pytest.param("ENV_LONG=" + "x" * 4096 + "\\\n"
+ "y" * (8190 - len("ENV_LONG=") - 4096) + "\n",
id="max-continued-line"),
])
def test_metadata_002_07_normal_eof(self, env, contents):
r = self.check_config(env, contents)
assert r.exit_code == 0, r.stderr

@pytest.mark.parametrize("value, message", [
pytest.param('"unfinished', "Unterminated quoted value",
id="unclosed-double"),
pytest.param("'unfinished", "Unterminated quoted value",
id="unclosed-single"),
pytest.param('"', "Unterminated quoted value", id="double-quote-only"),
pytest.param("'", "Unterminated quoted value", id="single-quote-only"),
pytest.param(r'"escaped\"', "Unterminated quoted value",
id="escaped-double-closer"),
pytest.param(r"'escaped\'", "Unterminated quoted value",
id="escaped-single-closer"),
pytest.param('"closed"extra', "Unexpected text after quoted value",
id="adjacent-text"),
pytest.param("'closed' extra", "Unexpected text after quoted value",
id="separate-text"),
pytest.param('"" "extra"', "Unexpected text after quoted value",
id="extra-quoted-token"),
pytest.param(' \t"unfinished', "Unterminated quoted value",
id="unclosed-after-whitespace"),
])
def test_metadata_002_11_malformed_quotes(self, env, value, message):
r = self.check_config(
env, f"ENV_BEFORE=before\nENV_BAD={value}\nENV_AFTER=after\n")
assert r.exit_code != 0, "malformed quoted value was accepted"
assert f"setenv-read.env at line 2: {message}" in r.stderr


class TestSetEnvFromFileQuoted:

def check_value(self, env, file_value, expected):
env_file = os.path.join(env.gen_dir, "setenv-quoted.env")
with open(env_file, "w", newline="") as f:
f.write(f"ENV_FILE={file_value}\nENV_AFTER=present\n")

doc_dir = os.path.join(env.server_dir, "htdocs", "test1")
os.makedirs(doc_dir, exist_ok=True)
_write_ssi_echo(os.path.join(doc_dir, "quoted.shtml"),
["ENV_FILE", "ENV_DIRECT", "ENV_AFTER"], encoding="none")

direct_value = expected.replace("\\", "\\\\").replace('"', '\\"')
conf = HttpdConf(env, extras={
'base': f"""
SetEnvFromFile "{env_file}"
SetEnv ENV_DIRECT "{direct_value}"
<Directory "{doc_dir}">
Options +Includes
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</Directory>
""",
})
conf.add_vhost_test1()
conf.install()
assert env.apache_restart() == 0

r = env.curl_get(env.mkurl("http", "test1", "/quoted.shtml"))
assert r.response, "no response: server may have crashed"
assert r.response["status"] == 200
assert r.response["body"].decode("utf-8").splitlines() == [
f"ENV_FILE=[{expected}]",
f"ENV_DIRECT=[{expected}]",
"ENV_AFTER=[present]",
]

@pytest.mark.parametrize("quote", ['"', "'"], ids=["double", "single"])
@pytest.mark.parametrize("expected", [
pytest.param("hello", id="plain"),
pytest.param("hello ", id="trailing-space"),
pytest.param(" hello", id="leading-space"),
pytest.param(" hello ", id="surrounding-spaces"),
pytest.param("", id="empty"),
pytest.param(" \t ", id="whitespace-only"),
pytest.param("hello world", id="embedded-space"),
pytest.param('say "hello" and \'bye\'', id="quote-escaping"),
pytest.param('"unfinished', id="literal-double-quote-prefix"),
pytest.param("'unfinished", id="literal-single-quote-prefix"),
pytest.param('"hello"', id="literal-double-quotes"),
pytest.param("'hello'", id="literal-single-quotes"),
pytest.param(r"C:\app\file", id="backslash-escaping"),
pytest.param("C:\\app\\", id="trailing-backslash"),
pytest.param(r"\n\t", id="literal-escapes"),
pytest.param("key=value # literal", id="equals-and-hash"),
])
def test_metadata_002_08_quoted(self, env, quote, expected):
value = expected.replace("\\", "\\\\").replace(quote, "\\" + quote)
self.check_value(env, quote + value + quote, expected)

@pytest.mark.parametrize("quote", ['"', "'"], ids=["double", "single"])
def test_metadata_002_09_quoted_whitespace(self, env, quote):
self.check_value(env, f" \t{quote} hello {quote} \t", " hello ")

@pytest.mark.parametrize("quote", ['"', "'"], ids=["double", "single"])
def test_metadata_002_10_quoted_continuation(self, env, quote):
self.check_value(env, f"{quote}first \\\nsecond {quote}", "first second ")

@pytest.mark.parametrize("file_value, expected", [
pytest.param("hello world", "hello world", id="embedded-space"),
pytest.param(" hello world ", " hello world", id="whitespace"),
pytest.param(r"C:\app\file", r"C:\app\file", id="literal-backslashes"),
pytest.param(r"a\\b", r"a\\b", id="doubled-backslash"),
pytest.param('hello "world"', 'hello "world"', id="embedded-quotes"),
pytest.param("", "", id="empty"),
pytest.param(" ", "", id="whitespace-only"),
])
def test_metadata_002_12_unquoted(self, env, file_value, expected):
self.check_value(env, file_value, expected)

@pytest.mark.parametrize("quote", ['"', "'"], ids=["double", "single"])
def test_metadata_002_13_literal_escapes(self, env, quote):
value = r"C:\app\n\t"
self.check_value(env, quote + value + quote, value)
Loading