Skip to content

Commit

Permalink
Add specific validator for webserver.paths.webhome ensuring there are…
Browse files Browse the repository at this point in the history
… slashes both at the beginning and the end of the string

Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Feb 24, 2025
1 parent ad1041e commit be6be2b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ static void initConfig(struct config *conf)
conf->webserver.paths.webhome.t = CONF_STRING;
conf->webserver.paths.webhome.f = FLAG_RESTART_FTL;
conf->webserver.paths.webhome.d.s = (char*)"/admin/";
conf->webserver.paths.webhome.c = validate_filepath;
conf->webserver.paths.webhome.c = validate_filepath_two_slash;

// sub-struct interface
conf->webserver.interface.boxed.k = "webserver.interface.boxed";
Expand Down
15 changes: 15 additions & 0 deletions src/config/validator.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,21 @@ bool validate_filepath(union conf_value *val, const char *key, char err[VALIDATO
return true;
}

// Validate a file path that needs to have both a slash at the beginning and at
// the end
bool validate_filepath_two_slash(union conf_value *val, const char *key, char err[VALIDATOR_ERRBUF_LEN])
{
// Check if the path starts and ends with a slash
if(strlen(val->s) < 1 || val->s[0] != '/' || val->s[strlen(val->s) - 1] != '/')
{
snprintf(err, VALIDATOR_ERRBUF_LEN, "%s: file path does not start and end with a slash (\"%s\")", key, val->s);
return false;
}

// Check if the path contains only valid characters
return validate_filepath(val, key, err);
}

// Validate file path (empty allowed)
bool validate_filepath_empty(union conf_value *val, const char *key, char err[VALIDATOR_ERRBUF_LEN])
{
Expand Down
1 change: 1 addition & 0 deletions src/config/validator.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bool validate_dns_domain(union conf_value *val, const char *key, char err[VALIDA
bool validate_cidr(union conf_value *val, const char *key, char err[VALIDATOR_ERRBUF_LEN]);
bool validate_domain(union conf_value *val, const char *key, char err[VALIDATOR_ERRBUF_LEN]);
bool validate_filepath(union conf_value *val, const char *key, char err[VALIDATOR_ERRBUF_LEN]);
bool validate_filepath_two_slash(union conf_value *val, const char *key, char err[VALIDATOR_ERRBUF_LEN]);
bool validate_filepath_empty(union conf_value *val, const char *key, char err[VALIDATOR_ERRBUF_LEN]);
bool validate_filepath_dash(union conf_value *val, const char *key, char err[VALIDATOR_ERRBUF_LEN]);
bool validate_regex_array(union conf_value *val, const char *key, char err[VALIDATOR_ERRBUF_LEN]);
Expand Down

0 comments on commit be6be2b

Please sign in to comment.