Fix #387: sanity check environment variables before sourcing

This fixes an issue when finding a global environment variable with
spaces in the variable name:

    set COLORTERM=yes

Literally, 'set COLORTERM' was the name of the variable.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2023-11-12 19:07:54 +01:00
1 parent 0b5c555c7f
commit ca90bbfb04
3 files changed
+42 -3

No files matched your search

+17
View File
@@ -545,6 +545,23 @@ static void parse_env(char *line)
*end-- = 0; *end-- = 0;
} }
/* strip any leading 'set ' */
end = key;
if (!strncmp(key, "set", 3))
end += 3;
/* check key, no spaces allowed */
while (*end && isspace(*end))
end++;
key = end;
while (*end && !isspace(*end))
end++;
if (*end != 0) {
warnx("'%s=%s': not a valid identifier", key, val);
return; /* invalid key */
}
dbg("Global env '%s'='%s'", key, val);
setenv(key, val, 1); setenv(key, val, 1);
node = malloc(sizeof(*node)); node = malloc(sizeof(*node));
+16
View File
@@ -471,6 +471,22 @@ static void source_env(svc_t *svc)
*end-- = 0; *end-- = 0;
} }
/* strip any leading 'set ' */
end = key;
if (!strncmp(key, "set", 3))
end += 3;
/* check key, no spaces allowed */
while (*end && isspace(*end))
end++;
key = end;
while (*end && !isspace(*end))
end++;
if (*end != 0) {
warnx("'%s=%s': not a valid identifier", key, val);
continue; /* invalid key */
}
switch (wordexp(value, &we, 0)) { switch (wordexp(value, &we, 0)) {
case 0: case 0:
for (i = 0, *val = 0; i < we.we_wordc; i++) { for (i = 0, *val = 0; i < we.we_wordc; i++) {
+9 -3
View File
@@ -2,6 +2,11 @@
set -eu set -eu
TEST_DIR=$(dirname "$0") TEST_DIR=$(dirname "$0")
# shellcheck disable=SC2034
BOOTSTRAP="foo=bar\n\
set COLORTERM=yes\n\
b aq=oroszoe\n\
baz=qux"
test_setup() test_setup()
{ {
@@ -19,13 +24,14 @@ test_teardown()
# shellcheck source=/dev/null # shellcheck source=/dev/null
. "$TEST_DIR/lib/setup.sh" . "$TEST_DIR/lib/setup.sh"
say "Waiting for runlevel 2"
sleep 2
#run "initctl debug" #run "initctl debug"
say "Add custom envs to $FINIT_CONF ..."
run "echo 'foo=bar' > $FINIT_CONF"
run "echo 'baz=qux' >> $FINIT_CONF"
say "Add service stanza to $FINIT_CONF ..." say "Add service stanza to $FINIT_CONF ..."
run "echo 'service serv -np -e foo:bar -- Verify foo=bar' >> $FINIT_CONF" run "echo 'service serv -np -e foo:bar -- Verify foo=bar' >> $FINIT_CONF"
#run "cat $FINIT_CONF"
say 'Reload Finit' say 'Reload Finit'
run "initctl reload" run "initctl reload"