The vulnerability is an OS command injection in the NodejsFunction local bundling feature of aws-cdk-lib. The root cause is the construction of a shell command string using unsanitized user input, which is then executed in a shell.
The analysis of the patch commits reveals two key functions involved in the vulnerability:
-
Bundling.createBundlingCommand: This function was responsible for creating the esbuild command string. It took several bundling options that could be controlled by a user (e.g., externalModules, define, esbuildArgs) and concatenated them into a single string. Because the inputs were not escaped, an attacker could provide input with shell metacharacters (e.g., '; ls -la') to inject arbitrary commands.
-
Bundling.getLocalBundlingProvider: This function returned the tryBundle method which performed the local bundling. The tryBundle method would call createBundlingCommand to get the command string and then execute it using exec with a shell (bash -c on Linux/macOS and cmd /c on Windows). This shell execution is what allowed the injected commands to run.
The patch addresses this vulnerability by completely removing the shell-based execution for the main bundling command. It refactors the code to build an array of arguments for esbuild and then uses spawnSync to execute the command directly, without involving a shell. This prevents any shell metacharacters in the input from being interpreted as commands.