diff --git a/docs/manual/mod/mod_env.xml b/docs/manual/mod/mod_env.xml
index 668f44e06c7..956c90a9177 100644
--- a/docs/manual/mod/mod_env.xml
+++ b/docs/manual/mod/mod_env.xml
@@ -145,9 +145,25 @@ SSI pages
(\) is continued on the following line, just as in the main
configuration files.
- Example
+ A value beginning with a single or double quote (after any whitespace
+ following =) is treated as quoted, as with
+ SetEnv. 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.
+
+ 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.
+
+ Example using quoted values
- SetEnvFromFile conf/app.env
+ SetEnvFromFile "conf/app.env"
@@ -156,6 +172,10 @@ SSI pages
# Application settings
APP_MODE=production
SPECIAL_PATH=/foo/bin
+ PADDED_VALUE=" value with surrounding spaces "
+ EMPTY_VALUE=""
+ LITERAL_QUOTE="'value"
+ ESCAPED_QUOTE='\'value'
diff --git a/modules/metadata/mod_env.c b/modules/metadata/mod_env.c
index a7e13f37605..e9c0137fece 100644
--- a/modules/metadata/mod_env.c
+++ b/modules/metadata/mod_env.c
@@ -15,6 +15,7 @@
*/
#include "apr.h"
+#include "apr_lib.h"
#include "apr_strings.h"
#if APR_HAVE_STDLIB_H
@@ -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;
@@ -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, "ed);
+ 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;
}
diff --git a/test/modules/metadata/test_002_setenvfromfile.py b/test/modules/metadata/test_002_setenvfromfile.py
index 4b37be16a33..e6ce041bdbb 100644
--- a/test/modules/metadata/test_002_setenvfromfile.py
+++ b/test/modules/metadata/test_002_setenvfromfile.py
@@ -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}=[]\n')
+ f.write(f'{name}=[]\n')
class TestSetEnvFromFile:
@@ -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}"
+
+ Options +Includes
+ AddType text/html .shtml
+ AddOutputFilter INCLUDES .shtml
+
+ """,
+ })
+ 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)