curl: exit on config file parser errors

Like when trying to import an environment variable that does not exist.

Also fix a bug for reading env variables when there is a default value
set.

Bug: https://curl.se/mail/archive-2024-02/0008.html
Reported-by: Brett Buddin

Add test 462 to verify.

Closes #12862
This commit is contained in:
Daniel Stenberg 2024-02-05 15:04:31 +01:00
parent 1d96828582
commit 0f0edc283c
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
4 changed files with 58 additions and 8 deletions

View File

@ -125,11 +125,11 @@ int parseconfig(const char *filename, struct GlobalConfig *global)
int lineno = 0;
bool dashed_option;
struct curlx_dynbuf buf;
bool fileerror;
bool fileerror = FALSE;
curlx_dyn_init(&buf, MAX_CONFIG_LINE_LENGTH);
DEBUGASSERT(filename);
while(my_get_line(file, &buf, &fileerror)) {
while(!rc && my_get_line(file, &buf, &fileerror)) {
int res;
bool alloced_param = FALSE;
lineno++;
@ -264,8 +264,9 @@ int parseconfig(const char *filename, struct GlobalConfig *global)
res != PARAM_VERSION_INFO_REQUESTED &&
res != PARAM_ENGINES_REQUESTED) {
const char *reason = param2text(res);
warnf(operation->global, "%s:%d: warning: '%s' %s",
filename, lineno, option, reason);
errorf(operation->global, "%s:%d: '%s' %s",
filename, lineno, option, reason);
rc = res;
}
}

View File

@ -42,6 +42,7 @@
#include "memdebug.h" /* keep this as LAST include */
#define MAX_EXPAND_CONTENT 10000000
#define MAX_VAR_LEN 128 /* max length of a name */
static char *Memdup(const char *data, size_t len)
{
@ -233,7 +234,7 @@ ParameterError varexpand(struct GlobalConfig *global,
line = &envp[2];
}
else if(envp) {
char name[128];
char name[MAX_VAR_LEN];
size_t nlen;
size_t i;
char *funcp;
@ -393,6 +394,7 @@ ParameterError setvariable(struct GlobalConfig *global,
ParameterError err = PARAM_OK;
bool import = FALSE;
char *ge = NULL;
char buf[MAX_VAR_LEN];
if(*input == '%') {
import = TRUE;
@ -402,12 +404,20 @@ ParameterError setvariable(struct GlobalConfig *global,
while(*line && (ISALNUM(*line) || (*line == '_')))
line++;
nlen = line - name;
if(!nlen || (nlen > 128)) {
if(!nlen || (nlen >= MAX_VAR_LEN)) {
warnf(global, "Bad variable name length (%zd), skipping", nlen);
return PARAM_OK;
}
if(import) {
ge = curl_getenv(name);
/* this does not use curl_getenv() because we want "" support for blank
content */
if(*line) {
/* if there is a default action, we need to copy the name */
memcpy(buf, name, nlen);
buf[nlen] = 0;
name = buf;
}
ge = getenv(name);
if(!*line && !ge) {
/* no assign, no variable, fail */
errorf(global, "Variable '%s' import fail, not set", name);
@ -459,6 +469,5 @@ ParameterError setvariable(struct GlobalConfig *global,
if(contalloc)
free(content);
}
curl_free(ge);
return err;
}

View File

@ -73,6 +73,7 @@ test426 test427 test428 test429 test430 test431 test432 test433 test434 \
test435 test436 test437 test438 test439 test440 test441 test442 test443 \
test444 test445 test446 test447 test448 test449 test450 test451 test452 \
test453 test454 test455 test456 test457 test458 test459 test460 test461 \
test462 \
\
test490 test491 test492 test493 test494 test495 test496 test497 test498 \
test499 test500 test501 test502 test503 test504 test505 test506 test507 \

39
tests/data/test462 Normal file
View File

@ -0,0 +1,39 @@
<testcase>
<info>
<keywords>
variables
--config
</keywords>
</info>
#
# Server-side
<reply>
</reply>
#
# Client-side
<client>
<server>
none
</server>
<name>
Missing environment variables in config file
</name>
<file name="%LOGDIR/cmd">
variable %MISSING
expand-data {{MISSING}}
</file>
<command>
http://%HOSTIP:%HTTPPORT/%TESTNUMBER -K %LOGDIR/cmd
</command>
</client>
#
# Verify data after the test has been "shot"
<verify>
<errorcode>
26
</errorcode>
</verify>
</testcase>