From 091b60de21be9b9341c4db690d49b579ba2ddcdf Mon Sep 17 00:00:00 2001 From: qabasel Date: Wed, 11 Nov 2026 15:50:42 +0200 Subject: [PATCH 1/3] mod_env: Fix SetEnvFromFile read errors and quoted values Reject non-EOF read failures with file and line diagnostics instead of silently ignoring the remaining entries. Support single- and double-quoted values using SetEnv escaping rules. Preserve quoted whitespace and existing unquoted behavior, and reject malformed quoted entries. Add regression tests and document the parsing rules. --- docs/manual/mod/mod_env.xml | 15 ++ modules/metadata/mod_env.c | 43 ++++- .../metadata/test_002_setenvfromfile.py | 164 +++++++++++++++++- 3 files changed, 217 insertions(+), 5 deletions(-) diff --git a/docs/manual/mod/mod_env.xml b/docs/manual/mod/mod_env.xml index 668f44e06c7..51a20fff8fd 100644 --- a/docs/manual/mod/mod_env.xml +++ b/docs/manual/mod/mod_env.xml @@ -145,6 +145,19 @@ SSI pages (\) is continued on the following line, just as in the main configuration files.

+

A value may be enclosed in single or double quotes, 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. 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 SetEnvFromFile conf/app.env @@ -156,6 +169,8 @@ SSI pages # Application settings APP_MODE=production SPECIAL_PATH=/foo/bin + PADDED_VALUE=" value with surrounding spaces " + EMPTY_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..ab08549fe2f 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,163 @@ 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(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) From 7386f3b70ea3438b253285896fdb6e5eea77097e Mon Sep 17 00:00:00 2001 From: qabasel Date: Wed, 11 Nov 2026 16:48:59 +0200 Subject: [PATCH 2/3] mod_env: Make SetEnvFromFile quote parsing opt-in Add an optional Quoted argument to enable SetEnv-style quoted values. Preserve literal quotes and backslashes when the argument is omitted, and reject unsupported options. Update the manual and expand regression coverage for both parsing modes and option validation. --- docs/manual/mod/mod_env.xml | 28 ++++--- modules/metadata/mod_env.c | 15 +++- .../metadata/test_002_setenvfromfile.py | 81 +++++++++++++++---- 3 files changed, 94 insertions(+), 30 deletions(-) diff --git a/docs/manual/mod/mod_env.xml b/docs/manual/mod/mod_env.xml index 51a20fff8fd..700d8cfbd9f 100644 --- a/docs/manual/mod/mod_env.xml +++ b/docs/manual/mod/mod_env.xml @@ -121,7 +121,7 @@ SSI pages SetEnvFromFile Sets environment variables from a file -SetEnvFromFile file-path +SetEnvFromFile file-path [Quoted] server configvirtual host directory Available in version 2.5.1 and later. @@ -145,22 +145,28 @@ SSI pages (\) is continued on the following line, just as in the main configuration files.

-

A value may be enclosed in single or double quotes, 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. Unquoted values may contain - spaces and are not unescaped. An unterminated quoted value or unexpected - text after the closing quote causes a configuration error.

+

By default, values are not unquoted or unescaped. Quote characters, + including unmatched quotes, remain part of the value. For example, + NAME="value" sets NAME to + "value", including the surrounding quotes.

+ +

Use the optional Quoted argument to enable single- or + double-quoted values, as with SetEnv. + In this mode, 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. + Unquoted values may contain spaces and are not unescaped. An unterminated + quoted value or unexpected text after the closing quote causes a + configuration error only when Quoted is specified.

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 + Example using quoted values - SetEnvFromFile conf/app.env + SetEnvFromFile "conf/app.env" Quoted diff --git a/modules/metadata/mod_env.c b/modules/metadata/mod_env.c index e9c0137fece..0d39c6c0552 100644 --- a/modules/metadata/mod_env.c +++ b/modules/metadata/mod_env.c @@ -148,7 +148,8 @@ static const char *add_env_module_vars_unset(cmd_parms *cmd, void *sconf_, } static const char *add_env_module_vars_from_file(cmd_parms *cmd, void *sconf_, - const char *arg) + const char *arg, + const char *option) { env_dir_config_rec *sconf = sconf_; const char *fname; @@ -156,6 +157,11 @@ static const char *add_env_module_vars_from_file(cmd_parms *cmd, void *sconf_, apr_status_t rv; char line[MAX_STRING_LEN]; + if (option && ap_cstr_casecmp(option, "Quoted") != 0) { + return apr_pstrcat(cmd->pool, cmd->cmd->name, ": Invalid option ", + option, "; expected Quoted", NULL); + } + fname = ap_server_root_relative(cmd->temp_pool, arg); if (!fname) { return apr_pstrcat(cmd->pool, cmd->cmd->name, @@ -195,7 +201,7 @@ static const char *add_env_module_vars_from_file(cmd_parms *cmd, void *sconf_, while (apr_isspace(*quoted)) { ++quoted; } - if (*quoted == '"' || *quoted == '\'') { + if (option && (*quoted == '"' || *quoted == '\'')) { const char *end; /* ap_getword_conf() does not report unterminated quotes. */ @@ -244,9 +250,10 @@ AP_INIT_TAKE12("SetEnv", add_env_module_vars_set, NULL, OR_FILEINFO, "an environment variable name and optional value to pass to CGI."), AP_INIT_ITERATE("UnsetEnv", add_env_module_vars_unset, NULL, OR_FILEINFO, "a list of variables to remove from the CGI environment."), -AP_INIT_TAKE1("SetEnvFromFile", add_env_module_vars_from_file, NULL, +AP_INIT_TAKE12("SetEnvFromFile", add_env_module_vars_from_file, NULL, RSRC_CONF | ACCESS_CONF, - "the path to a file of name=value lines to pass to CGI."), + "the path to a file of name=value lines to pass to CGI, " + "optionally followed by Quoted."), {NULL}, }; diff --git a/test/modules/metadata/test_002_setenvfromfile.py b/test/modules/metadata/test_002_setenvfromfile.py index ab08549fe2f..e6e727c8bfc 100644 --- a/test/modules/metadata/test_002_setenvfromfile.py +++ b/test/modules/metadata/test_002_setenvfromfile.py @@ -182,10 +182,11 @@ def test_metadata_002_03_htaccess_rejected(self, env): class TestSetEnvFromFileMissing: """Pointing at a file that cannot be opened is a fatal config error.""" - def test_metadata_002_04_missing_file(self, env): + @pytest.mark.parametrize("option", ["", "Quoted"], ids=["default", "quoted"]) + def test_metadata_002_04_missing_file(self, env, option): missing = os.path.join(env.gen_dir, "does-not-exist.env") conf = HttpdConf(env, extras={ - 'base': f'SetEnvFromFile "{missing}"', + 'base': f'SetEnvFromFile "{missing}" {option}', }) conf.add_vhost_test1() conf.install() @@ -208,13 +209,13 @@ def test_metadata_002_04_missing_file(self, env): class TestSetEnvFromFileConfig: - def check_config(self, env, contents): + def check_config(self, env, contents, option=""): 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}"', + 'base': f'SetEnvFromFile "{env_file}" {option}', }) conf.add_vhost_test1() conf.install() @@ -222,25 +223,28 @@ def check_config(self, env, contents): "-t", "-d", env.server_dir, "-f", os.path.join(env.server_dir, "conf", "httpd.conf")]) + @pytest.mark.parametrize("option", ["", "Quoted"], ids=["default", "quoted"]) @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): + def test_metadata_002_05_long_line(self, env, line_length, ending, option): 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) + r = self.check_config(env, contents, option=option) 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): + @pytest.mark.parametrize("option", ["", "Quoted"], ids=["default", "quoted"]) + def test_metadata_002_06_long_continuation(self, env, option): r = self.check_config( env, "ENV_BEFORE=before\nENV_LONG=" + "x" * 4096 - + "\\\n" + "y" * 4096 + "\nENV_AFTER=after\n") + + "\\\n" + "y" * 4096 + "\nENV_AFTER=after\n", option=option) 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("option", ["", "Quoted"], ids=["default", "quoted"]) @pytest.mark.parametrize("contents", [ pytest.param("", id="empty-file"), pytest.param("ENV_OK=present", id="no-final-newline"), @@ -253,8 +257,8 @@ def test_metadata_002_06_long_continuation(self, env): + "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) + def test_metadata_002_07_normal_eof(self, env, contents, option): + r = self.check_config(env, contents, option=option) assert r.exit_code == 0, r.stderr @pytest.mark.parametrize("value, message", [ @@ -279,14 +283,27 @@ def test_metadata_002_07_normal_eof(self, env, contents): ]) 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") + env, f"ENV_BEFORE=before\nENV_BAD={value}\nENV_AFTER=after\n", + option="Quoted") assert r.exit_code != 0, "malformed quoted value was accepted" assert f"setenv-read.env at line 2: {message}" in r.stderr + @pytest.mark.parametrize("option", ["Raw", "Unknown", "Quotedx"]) + def test_metadata_002_16_invalid_option(self, env, option): + r = self.check_config(env, "ENV_OK=present\n", option=option) + assert r.exit_code != 0, "invalid option was accepted" + assert (f"SetEnvFromFile: Invalid option {option}; expected Quoted" + in r.stderr) + + def test_metadata_002_17_extra_argument(self, env): + r = self.check_config(env, "ENV_OK=present\n", option="Quoted extra") + assert r.exit_code != 0, "extra argument was accepted" + assert "SetEnvFromFile takes 1-2 arguments" in r.stderr + class TestSetEnvFromFileQuoted: - def check_value(self, env, file_value, expected): + def check_value(self, env, file_value, expected, option="Quoted"): 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") @@ -299,7 +316,7 @@ def check_value(self, env, file_value, expected): direct_value = expected.replace("\\", "\\\\").replace('"', '\\"') conf = HttpdConf(env, extras={ 'base': f""" - SetEnvFromFile "{env_file}" + SetEnvFromFile "{env_file}" {option} SetEnv ENV_DIRECT "{direct_value}" Options +Includes @@ -348,6 +365,7 @@ def test_metadata_002_09_quoted_whitespace(self, env, quote): def test_metadata_002_10_quoted_continuation(self, env, quote): self.check_value(env, f"{quote}first \\\nsecond {quote}", "first second ") + @pytest.mark.parametrize("option", ["", "Quoted"], ids=["default", "quoted"]) @pytest.mark.parametrize("file_value, expected", [ pytest.param("hello world", "hello world", id="embedded-space"), pytest.param(" hello world ", " hello world", id="whitespace"), @@ -357,10 +375,43 @@ def test_metadata_002_10_quoted_continuation(self, env, quote): 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) + def test_metadata_002_12_unquoted(self, env, file_value, expected, option): + self.check_value(env, file_value, expected, option=option) @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) + + @pytest.mark.parametrize("file_value, expected", [ + pytest.param('"hello"', '"hello"', id="double-quoted"), + pytest.param("'hello'", "'hello'", id="single-quoted"), + pytest.param('" hello "', '" hello "', id="quoted-whitespace"), + pytest.param('""', '""', id="empty-double-quotes"), + pytest.param("''", "''", id="empty-single-quotes"), + pytest.param('"unfinished', '"unfinished', id="unclosed-double"), + pytest.param("'unfinished", "'unfinished", id="unclosed-single"), + pytest.param('"', '"', id="double-quote-only"), + pytest.param("'", "'", id="single-quote-only"), + pytest.param(r'"escaped\"', r'"escaped\"', id="escaped-double-closer"), + pytest.param(r"'escaped\'", r"'escaped\'", id="escaped-single-closer"), + pytest.param('"closed"extra', '"closed"extra', id="adjacent-text"), + pytest.param("'closed' extra", "'closed' extra", id="separate-text"), + pytest.param('"" "extra"', '"" "extra"', id="extra-quoted-token"), + pytest.param(' \t"unfinished', ' \t"unfinished', + id="unclosed-after-whitespace"), + pytest.param(' \t"hello" \t', ' \t"hello"', id="outside-whitespace"), + pytest.param(r'"hello"\n', r'"hello"\n', id="flattened-final-newline"), + pytest.param(r"'hello'\r\n", r"'hello'\r\n", + id="flattened-final-crlf"), + pytest.param(r'"C:\\app\\file"', r'"C:\\app\\file"', + id="quoted-doubled-backslashes"), + pytest.param(r"line1\nline2\r", r"line1\nline2\r", + id="flattened-multiline"), + ]) + def test_metadata_002_14_default_literal(self, env, file_value, expected): + self.check_value(env, file_value, expected, option="") + + @pytest.mark.parametrize("option", ["quoted", "QUOTED", "qUoTeD"]) + def test_metadata_002_15_option_case(self, env, option): + self.check_value(env, '"hello "', "hello ", option=option) From 6764053e5aa21f1c2812bdb8690438987f91d847 Mon Sep 17 00:00:00 2001 From: qabasel Date: Fri, 13 Nov 2026 10:57:08 +0200 Subject: [PATCH 3/3] removed Quoted option from SetEnvFromFile Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- docs/manual/mod/mod_env.xml | 31 ++++--- modules/metadata/mod_env.c | 15 +--- .../metadata/test_002_setenvfromfile.py | 85 +++++-------------- 3 files changed, 38 insertions(+), 93 deletions(-) diff --git a/docs/manual/mod/mod_env.xml b/docs/manual/mod/mod_env.xml index 700d8cfbd9f..956c90a9177 100644 --- a/docs/manual/mod/mod_env.xml +++ b/docs/manual/mod/mod_env.xml @@ -121,7 +121,7 @@ SSI pages SetEnvFromFile Sets environment variables from a file -SetEnvFromFile file-path [Quoted] +SetEnvFromFile file-path server configvirtual host directory Available in version 2.5.1 and later. @@ -145,20 +145,17 @@ SSI pages (\) is continued on the following line, just as in the main configuration files.

-

By default, values are not unquoted or unescaped. Quote characters, - including unmatched quotes, remain part of the value. For example, - NAME="value" sets NAME to - "value", including the surrounding quotes.

- -

Use the optional Quoted argument to enable single- or - double-quoted values, as with SetEnv. - In this mode, 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. - Unquoted values may contain spaces and are not unescaped. An unterminated - quoted value or unexpected text after the closing quote causes a - configuration error only when Quoted is specified.

+

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 @@ -166,7 +163,7 @@ SSI pages Example using quoted values - SetEnvFromFile "conf/app.env" Quoted + SetEnvFromFile "conf/app.env" @@ -177,6 +174,8 @@ SSI pages 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 0d39c6c0552..e9c0137fece 100644 --- a/modules/metadata/mod_env.c +++ b/modules/metadata/mod_env.c @@ -148,8 +148,7 @@ static const char *add_env_module_vars_unset(cmd_parms *cmd, void *sconf_, } static const char *add_env_module_vars_from_file(cmd_parms *cmd, void *sconf_, - const char *arg, - const char *option) + const char *arg) { env_dir_config_rec *sconf = sconf_; const char *fname; @@ -157,11 +156,6 @@ static const char *add_env_module_vars_from_file(cmd_parms *cmd, void *sconf_, apr_status_t rv; char line[MAX_STRING_LEN]; - if (option && ap_cstr_casecmp(option, "Quoted") != 0) { - return apr_pstrcat(cmd->pool, cmd->cmd->name, ": Invalid option ", - option, "; expected Quoted", NULL); - } - fname = ap_server_root_relative(cmd->temp_pool, arg); if (!fname) { return apr_pstrcat(cmd->pool, cmd->cmd->name, @@ -201,7 +195,7 @@ static const char *add_env_module_vars_from_file(cmd_parms *cmd, void *sconf_, while (apr_isspace(*quoted)) { ++quoted; } - if (option && (*quoted == '"' || *quoted == '\'')) { + if (*quoted == '"' || *quoted == '\'') { const char *end; /* ap_getword_conf() does not report unterminated quotes. */ @@ -250,10 +244,9 @@ AP_INIT_TAKE12("SetEnv", add_env_module_vars_set, NULL, OR_FILEINFO, "an environment variable name and optional value to pass to CGI."), AP_INIT_ITERATE("UnsetEnv", add_env_module_vars_unset, NULL, OR_FILEINFO, "a list of variables to remove from the CGI environment."), -AP_INIT_TAKE12("SetEnvFromFile", add_env_module_vars_from_file, NULL, +AP_INIT_TAKE1("SetEnvFromFile", add_env_module_vars_from_file, NULL, RSRC_CONF | ACCESS_CONF, - "the path to a file of name=value lines to pass to CGI, " - "optionally followed by Quoted."), + "the path to a file of name=value lines to pass to CGI."), {NULL}, }; diff --git a/test/modules/metadata/test_002_setenvfromfile.py b/test/modules/metadata/test_002_setenvfromfile.py index e6e727c8bfc..e6ce041bdbb 100644 --- a/test/modules/metadata/test_002_setenvfromfile.py +++ b/test/modules/metadata/test_002_setenvfromfile.py @@ -182,11 +182,10 @@ def test_metadata_002_03_htaccess_rejected(self, env): class TestSetEnvFromFileMissing: """Pointing at a file that cannot be opened is a fatal config error.""" - @pytest.mark.parametrize("option", ["", "Quoted"], ids=["default", "quoted"]) - def test_metadata_002_04_missing_file(self, env, option): + def test_metadata_002_04_missing_file(self, env): missing = os.path.join(env.gen_dir, "does-not-exist.env") conf = HttpdConf(env, extras={ - 'base': f'SetEnvFromFile "{missing}" {option}', + 'base': f'SetEnvFromFile "{missing}"', }) conf.add_vhost_test1() conf.install() @@ -209,13 +208,13 @@ def test_metadata_002_04_missing_file(self, env, option): class TestSetEnvFromFileConfig: - def check_config(self, env, contents, option=""): + 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}" {option}', + 'base': f'SetEnvFromFile "{env_file}"', }) conf.add_vhost_test1() conf.install() @@ -223,28 +222,25 @@ def check_config(self, env, contents, option=""): "-t", "-d", env.server_dir, "-f", os.path.join(env.server_dir, "conf", "httpd.conf")]) - @pytest.mark.parametrize("option", ["", "Quoted"], ids=["default", "quoted"]) @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, option): + 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, option=option) + 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 - @pytest.mark.parametrize("option", ["", "Quoted"], ids=["default", "quoted"]) - def test_metadata_002_06_long_continuation(self, env, option): + 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", option=option) + + "\\\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("option", ["", "Quoted"], ids=["default", "quoted"]) @pytest.mark.parametrize("contents", [ pytest.param("", id="empty-file"), pytest.param("ENV_OK=present", id="no-final-newline"), @@ -257,8 +253,8 @@ def test_metadata_002_06_long_continuation(self, env, option): + "y" * (8190 - len("ENV_LONG=") - 4096) + "\n", id="max-continued-line"), ]) - def test_metadata_002_07_normal_eof(self, env, contents, option): - r = self.check_config(env, contents, option=option) + 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", [ @@ -283,27 +279,14 @@ def test_metadata_002_07_normal_eof(self, env, contents, option): ]) 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", - option="Quoted") + 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 - @pytest.mark.parametrize("option", ["Raw", "Unknown", "Quotedx"]) - def test_metadata_002_16_invalid_option(self, env, option): - r = self.check_config(env, "ENV_OK=present\n", option=option) - assert r.exit_code != 0, "invalid option was accepted" - assert (f"SetEnvFromFile: Invalid option {option}; expected Quoted" - in r.stderr) - - def test_metadata_002_17_extra_argument(self, env): - r = self.check_config(env, "ENV_OK=present\n", option="Quoted extra") - assert r.exit_code != 0, "extra argument was accepted" - assert "SetEnvFromFile takes 1-2 arguments" in r.stderr - class TestSetEnvFromFileQuoted: - def check_value(self, env, file_value, expected, option="Quoted"): + 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") @@ -316,7 +299,7 @@ def check_value(self, env, file_value, expected, option="Quoted"): direct_value = expected.replace("\\", "\\\\").replace('"', '\\"') conf = HttpdConf(env, extras={ 'base': f""" - SetEnvFromFile "{env_file}" {option} + SetEnvFromFile "{env_file}" SetEnv ENV_DIRECT "{direct_value}" Options +Includes @@ -348,6 +331,10 @@ def check_value(self, env, file_value, expected, option="Quoted"): 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"), @@ -365,7 +352,6 @@ def test_metadata_002_09_quoted_whitespace(self, env, quote): def test_metadata_002_10_quoted_continuation(self, env, quote): self.check_value(env, f"{quote}first \\\nsecond {quote}", "first second ") - @pytest.mark.parametrize("option", ["", "Quoted"], ids=["default", "quoted"]) @pytest.mark.parametrize("file_value, expected", [ pytest.param("hello world", "hello world", id="embedded-space"), pytest.param(" hello world ", " hello world", id="whitespace"), @@ -375,43 +361,10 @@ def test_metadata_002_10_quoted_continuation(self, env, quote): pytest.param("", "", id="empty"), pytest.param(" ", "", id="whitespace-only"), ]) - def test_metadata_002_12_unquoted(self, env, file_value, expected, option): - self.check_value(env, file_value, expected, option=option) + 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) - - @pytest.mark.parametrize("file_value, expected", [ - pytest.param('"hello"', '"hello"', id="double-quoted"), - pytest.param("'hello'", "'hello'", id="single-quoted"), - pytest.param('" hello "', '" hello "', id="quoted-whitespace"), - pytest.param('""', '""', id="empty-double-quotes"), - pytest.param("''", "''", id="empty-single-quotes"), - pytest.param('"unfinished', '"unfinished', id="unclosed-double"), - pytest.param("'unfinished", "'unfinished", id="unclosed-single"), - pytest.param('"', '"', id="double-quote-only"), - pytest.param("'", "'", id="single-quote-only"), - pytest.param(r'"escaped\"', r'"escaped\"', id="escaped-double-closer"), - pytest.param(r"'escaped\'", r"'escaped\'", id="escaped-single-closer"), - pytest.param('"closed"extra', '"closed"extra', id="adjacent-text"), - pytest.param("'closed' extra", "'closed' extra", id="separate-text"), - pytest.param('"" "extra"', '"" "extra"', id="extra-quoted-token"), - pytest.param(' \t"unfinished', ' \t"unfinished', - id="unclosed-after-whitespace"), - pytest.param(' \t"hello" \t', ' \t"hello"', id="outside-whitespace"), - pytest.param(r'"hello"\n', r'"hello"\n', id="flattened-final-newline"), - pytest.param(r"'hello'\r\n", r"'hello'\r\n", - id="flattened-final-crlf"), - pytest.param(r'"C:\\app\\file"', r'"C:\\app\\file"', - id="quoted-doubled-backslashes"), - pytest.param(r"line1\nline2\r", r"line1\nline2\r", - id="flattened-multiline"), - ]) - def test_metadata_002_14_default_literal(self, env, file_value, expected): - self.check_value(env, file_value, expected, option="") - - @pytest.mark.parametrize("option", ["quoted", "QUOTED", "qUoTeD"]) - def test_metadata_002_15_option_case(self, env, option): - self.check_value(env, '"hello "', "hello ", option=option)