Fail early if "trusted_certificate" is a directory.

Previously, the error was caused by enormous std::string allocation.
This commit is contained in:
Pavel Pautov 2024-11-21 13:57:43 -08:00 committed by p-pautov
parent c9136f2ec8
commit f633a8eef2

View file

@ -711,7 +711,8 @@ char* addResourceAttr(ngx_conf_t* cf, ngx_command_t* cmd, void* conf)
return NGX_CONF_OK; return NGX_CONF_OK;
} }
char* setTrustedCertificate(ngx_conf_t* cf, ngx_command_t* cmd, void* conf) { char* setTrustedCertificate(ngx_conf_t* cf, ngx_command_t* cmd, void* conf)
{
auto path = ((ngx_str_t*)cf->args->elts)[1]; auto path = ((ngx_str_t*)cf->args->elts)[1];
auto mcf = getMainConf(cf); auto mcf = getMainConf(cf);
@ -727,11 +728,13 @@ char* setTrustedCertificate(ngx_conf_t* cf, ngx_command_t* cmd, void* conf) {
return (char*)NGX_CONF_ERROR; return (char*)NGX_CONF_ERROR;
} }
file.exceptions(std::ios::failbit | std::ios::badbit); file.exceptions(std::ios::failbit | std::ios::badbit);
file.seekg(0, std::ios::end); file.peek(); // trigger early error for dirs
size_t size = file.tellg();
mcf->trustedCert.resize(size); size_t size = file.seekg(0, std::ios::end).tellg();
file.seekg(0); file.seekg(0);
file.read(&mcf->trustedCert[0], mcf->trustedCert.size());
mcf->trustedCert.resize(size);
file.read(&mcf->trustedCert[0], size);
} catch (const std::exception& e) { } catch (const std::exception& e) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"failed to read \"%V\": %s", &path, e.what()); "failed to read \"%V\": %s", &path, e.what());