Skip to content

Commit 99cce7e

Browse files
committedJul 1, 2024
Fix command injection when passing bash commands into the dns provider configuration
- Use built in node functions to write the file - And to delete the file
1 parent 120d50e commit 99cce7e

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed
 

‎backend/internal/certificate.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -861,9 +861,8 @@ const internalCertificate = {
861861
logger.info(`Requesting Let'sEncrypt certificates via ${dnsPlugin.name} for Cert #${certificate.id}: ${certificate.domain_names.join(', ')}`);
862862

863863
const credentialsLocation = '/etc/letsencrypt/credentials/credentials-' + certificate.id;
864-
// Escape single quotes and backslashes
865-
const escapedCredentials = certificate.meta.dns_provider_credentials.replaceAll('\'', '\\\'').replaceAll('\\', '\\\\');
866-
const credentialsCmd = 'mkdir -p /etc/letsencrypt/credentials 2> /dev/null; echo \'' + escapedCredentials + '\' > \'' + credentialsLocation + '\' && chmod 600 \'' + credentialsLocation + '\'';
864+
fs.mkdirSync('/etc/letsencrypt/credentials', { recursive: true });
865+
fs.writeFileSync(credentialsLocation, certificate.meta.dns_provider_credentials, {mode: 0o600});
867866

868867
// Whether the plugin has a --<name>-credentials argument
869868
const hasConfigArg = certificate.meta.dns_provider !== 'route53';
@@ -898,17 +897,15 @@ const internalCertificate = {
898897
mainCmd = mainCmd + ' --dns-duckdns-no-txt-restore';
899898
}
900899

901-
logger.info('Command:', `${credentialsCmd} && && ${mainCmd}`);
900+
logger.info('Command:', mainCmd);
902901

903902
try {
904-
await utils.exec(credentialsCmd);
905903
const result = await utils.exec(mainCmd);
906904
logger.info(result);
907905
return result;
908906
} catch (err) {
909-
// Don't fail if file does not exist
910-
const delete_credentialsCmd = `rm -f '${credentialsLocation}' || true`;
911-
await utils.exec(delete_credentialsCmd);
907+
// Don't fail if file does not exist, so no need for action in the callback
908+
fs.unlink(credentialsLocation, () => {});
912909
throw err;
913910
}
914911
},

0 commit comments

Comments
 (0)