Skip to content

cloud-setup-databases: shell-escape values passed to EncryptionCLI - #14229

Open
jdscreations wants to merge 1 commit into
apache:4.22from
jdscreations:fix/issue-14186
Open

jdscreations wants to merge 1 commit into
apache:4.22from
jdscreations:fix/issue-14186

Conversation

@jdscreations

Copy link
Copy Markdown

Description

processEncryptionStuff()'s encrypt() builds the java ... EncryptionCLI command by hand-wrapping each dynamic value (jar path, value to encrypt, management server secret key) in literal double quotes, then hands the space-joined string to runCmd(), which runs it via subprocess.Popen(' '.join(cmds), shell=True, ...).

Double quotes do not stop the shell from expanding "$..." inside them. So a password containing $ — e.g. pa$sword — is silently truncated to pa by the shell before it ever reaches EncryptionCLI, and the wrong value gets encrypted into db.properties. This matches the exact behavior reported in the issue (decrypting what was stored yields pa, not pa$sword).

Fix: use shlex.quote() instead of the manual double-quote wrapping, for all three dynamic values passed into the command list. shlex.quote() produces shell-safe quoting for arbitrary values, including but not limited to $.

I kept the change scoped to encrypt() — the other runCmd() caller for passwords (mysqlCmds, ~line 160) already uses single quotes ('--password=\'%s\''), which correctly prevents shell expansion, so that path isn't affected by this bug and I left it untouched.

Fixes: #14186

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Feature/Enhancement Scale or Bug Severity

Bug Severity

  • Major

How Has This Been Tested?

The full script is a .in template requiring the CloudStack build (waf/Maven) to substitute @COMMONLIBDIR@/@PYTHONDIR@ and produce a runnable executable, and EncryptionCLI itself lives in cloudstack-utils.jar. Rather than standing up the full build, I isolated the actual bug — shell quoting through runCmd()'s shell=True pattern — in a standalone reproduction that shells out exactly the same way, substituting a trivial stand-in for the Java program so the arguments it actually receives are directly observable:

=== OLD (double-quote wrapping) ===
'pa\npa\n'          # password 'pa$sword' arrives truncated to 'pa'

=== NEW (shlex.quote) ===
'pa$sword\npa$sword\n'   # password arrives intact

Also verified python3 -m py_compile setup/bindir/cloud-setup-databases.in passes (valid Python syntax) and confirmed the same bug is present on this branch (4.22) prior to the fix.

How did you try to break this feature and the system with this change?

shlex.quote() is the standard-library-correct way to safely pass arbitrary strings through a shell=True invocation — it handles $, single quotes, backticks, semicolons, and whitespace generically, not just the $ case in the report. Checked the other three runCmd() call sites in this file (mysql invocation, build-classpath, DatabaseConfig) to confirm none of them pass user-controlled dynamic values needing the same treatment, and none were touched by this change.

processEncryptionStuff()'s encrypt() built the java EncryptionCLI
command by hand-wrapping each dynamic value in literal double quotes,
then handed the joined string to runCmd(), which runs it through
`subprocess.Popen(..., shell=True)`. Double quotes do not stop the
shell from expanding "$..." inside them, so a password like
`pa$sword` is silently truncated to `pa` before it ever reaches
EncryptionCLI, and the wrong value gets encrypted into db.properties.

Use shlex.quote() instead of manual double-quote wrapping for the jar
path, the value being encrypted, and the management server secret
key. shlex.quote() produces shell-safe quoting for arbitrary values,
including but not limited to '$'.

Verified with a standalone reproduction that shells out the same way
runCmd() does: with the old double-quote wrapping, a password of
`pa$sword` arrives at the child process as `pa`; with shlex.quote(),
it arrives intact as `pa$sword`.

Fixes: apache#14186
Signed-off-by: SiddharthSanch <111047247+SiddharthSanch@users.noreply.github.com>
@boring-cyborg

boring-cyborg Bot commented Sep 23, 2026

Copy link
Copy Markdown

Congratulations on your first Pull Request and welcome to the Apache CloudStack community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/cloudstack/blob/main/CONTRIBUTING.md)
Here are some useful points:

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants