diff --git a/changes-entries/mod_env_setenvfromfile.txt b/changes-entries/mod_env_setenvfromfile.txt new file mode 100644 index 00000000000..2ed101b7b25 --- /dev/null +++ b/changes-entries/mod_env_setenvfromfile.txt @@ -0,0 +1,3 @@ + *) mod_env: Add the SetEnvFromFile directive to set internal environment + variables from a file of name=value lines, read at configuration time. + [Cornel Isbiceanu ] diff --git a/docs/manual/mod/mod_env.xml b/docs/manual/mod/mod_env.xml index 73062037bb7..090c02e6811 100644 --- a/docs/manual/mod/mod_env.xml +++ b/docs/manual/mod/mod_env.xml @@ -118,5 +118,89 @@ SSI pages - + +SetEnvFromFile +Sets environment variables from a file +SetEnvFromFile file-path +server configvirtual host +directory +Available in version 2.4.72 and later. + + +

Sets internal environment variables read from a file, which are then + available to Apache HTTP Server modules, and passed on to CGI scripts and + SSI pages. This is equivalent to a series of SetEnv directives, one per variable, but keeps + the values in a separate file.

+ +

The file-path is either an absolute path or a path relative + to the ServerRoot. The file is read + once when the configuration is parsed; changes to it take effect only after + the server is restarted.

+ +

Each line of the file has the form name=value. Blank lines + and lines beginning with # are ignored, and leading and + trailing whitespace is stripped. If a line contains no =, the + variable is set to an empty string. A line ending in a backslash + (\) is continued on the following line, just as in the main + configuration files.

+ +

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" + + + + Example file (<code>conf/app.env</code>) + +# Application settings +APP_MODE=production +SPECIAL_PATH=/foo/bin +PADDED_VALUE=" value with surrounding spaces " +EMPTY_VALUE="" +LITERAL_QUOTE="'value" +ESCAPED_QUOTE='\'value' + + + +

As with SetEnv, the + variables are set after most early request processing directives + are run, such as access control and URI-to-filename mapping.

+
+ Security +

Unlike SetEnv, this directive + reads the contents of a file into the internal environment, where + they may be exposed through CGI, SSI, logging and other consumers. To + prevent an untrusted author from disclosing the contents of any file + readable by the server (for example configuration or credential files + elsewhere on the host), SetEnvFromFile is + not permitted in .htaccess files; it may only + be used in the main server configuration.

+
+ +
+ +Environment Variables + +SetEnv + +
+ + diff --git a/modules/metadata/mod_env.c b/modules/metadata/mod_env.c index 52594e9cf20..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 @@ -146,6 +147,95 @@ static const char *add_env_module_vars_unset(cmd_parms *cmd, void *sconf_, return NULL; } +static const char *add_env_module_vars_from_file(cmd_parms *cmd, void *sconf_, + const char *arg) +{ + env_dir_config_rec *sconf = sconf_; + const char *fname; + ap_configfile_t *file; + apr_status_t rv; + char line[MAX_STRING_LEN]; + + fname = ap_server_root_relative(cmd->temp_pool, arg); + if (!fname) { + return apr_pstrcat(cmd->pool, cmd->cmd->name, + ": Invalid file path ", arg, NULL); + } + + rv = ap_pcfg_openfile(&file, cmd->temp_pool, fname); + if (rv != APR_SUCCESS) { + return apr_psprintf(cmd->pool, "%s: Could not open file %s: %pm", + cmd->cmd->name, fname, &rv); + } + + /* Each line is "name=value"; blank lines and '#' comments are ignored. + * ap_cfg_getline() strips surrounding whitespace and handles line + * continuations. Names are read at config time and stored in the same + * table used by SetEnv, so they merge and reach r->subprocess_env the + * same way. + */ + while ((rv = ap_cfg_getline(line, sizeof(line), file)) == APR_SUCCESS) { + const char *rest = line; + const char *name, *value, *quoted; + + if (line[0] == '#' || line[0] == '\0') { + continue; + } + + name = ap_getword(cmd->pool, &rest, '='); + if (!name[0]) { + ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server, + APLOGNO(10632) "%s: Skipping malformed line " + "(no variable name) in %s", cmd->cmd->name, fname); + continue; + } + + /* rest points just past the '='; no '=' means an empty value. */ + 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; +} + static const command_rec env_module_cmds[] = { AP_INIT_ITERATE("PassEnv", add_env_module_vars_passed, NULL, @@ -154,6 +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_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."), {NULL}, }; diff --git a/test/modules/metadata/test_002_setenvfromfile.py b/test/modules/metadata/test_002_setenvfromfile.py index 4b37be16a33..6dc4a845097 100644 --- a/test/modules/metadata/test_002_setenvfromfile.py +++ b/test/modules/metadata/test_002_setenvfromfile.py @@ -5,17 +5,18 @@ from pyhttpd.backport import skip_not_backported from pyhttpd.conf import HttpdConf -pytestmark = skip_not_backported("r1938506", "SetEnvFromFile") +pytestmark = skip_not_backported("r1938506", "SetEnvFromFile", + backported_in="2.4.72") -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 +205,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)