🛡️ Sentinel: [CRITICAL] Fix command injection in PowerShell scripts#283
🛡️ Sentinel: [CRITICAL] Fix command injection in PowerShell scripts#283Dexploarer wants to merge 1 commit intomainfrom
Conversation
Escaping single quotes as `''` in Node.js when shelling out to PowerShell `-Command` can be bypassed.
This changes the user input parsing to encode input as Base64 in Node (`Buffer.from(text).toString('base64')`) and decode it inside the PowerShell string natively to ensure that execution behaves safely without injection vulnerabilities.
- Fixed `playAudio`
- Fixed `performType`
- Fixed `performKeypress`
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical security vulnerability that allowed for arbitrary code execution on Windows sandbox instances. The solution enhances the security posture by implementing a more secure method for handling user input passed to PowerShell commands, thereby protecting against command injection attacks. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
| throw new Error("No audio playback tool available."); | ||
| } | ||
| } else if (os === "win32") { | ||
| const escapedPath = tmpFile.replace(/\//g, "\\").replace(/'/g, "''"); | ||
| const base64Path = Buffer.from(tmpFile.replace(/\//g, "\\")).toString("base64"); | ||
| runCommand( | ||
| "powershell", | ||
| [ | ||
| "-Command", | ||
| `(New-Object Media.SoundPlayer '${escapedPath}').PlaySync()`, | ||
| `(New-Object Media.SoundPlayer ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64Path}')))).PlaySync()`, | ||
| ], | ||
| 60000, | ||
| ); |
There was a problem hiding this comment.
Lack of error handling for runCommand failures in playAudio (Windows branch):
If the PowerShell command fails to play the audio file (e.g., due to file corruption or OS-level issues), the error will propagate and may not be handled gracefully. Consider wrapping the runCommand invocation in a try-catch block and providing a meaningful error response or fallback.
Example:
try {
runCommand(...);
} catch (err) {
throw new Error(`Audio playback failed: ${err instanceof Error ? err.message : String(err)}`);
}| throw new Error("xdotool required for keyboard input on Linux."); | ||
| } | ||
| } else if (os === "win32") { | ||
| const escaped = text.replace(/'/g, "''"); | ||
| const base64 = Buffer.from(text).toString("base64"); | ||
| runCommand( | ||
| "powershell", | ||
| [ | ||
| "-Command", | ||
| `Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait('${escaped}')`, | ||
| `Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64}')))`, | ||
| ], | ||
| 10000, | ||
| ); |
There was a problem hiding this comment.
Lack of error handling for runCommand failures in performType (Windows branch):
If the PowerShell SendKeys command fails (e.g., due to OS restrictions or invalid input), the error will not be caught, potentially leading to silent failures. It is advisable to wrap the runCommand invocation in a try-catch block and handle errors appropriately.
Example:
try {
runCommand(...);
} catch (err) {
throw new Error(`Keyboard input failed: ${err instanceof Error ? err.message : String(err)}`);
}There was a problem hiding this comment.
Code Review
This pull request effectively addresses a critical command injection vulnerability by Base64-encoding user input before passing it to PowerShell scripts. The approach is sound and correctly implemented across all instances. My feedback focuses on minor improvements to code readability and maintainability by suggesting a way to shorten the PowerShell commands and noting code duplication that could be addressed in a future refactoring.
| [ | ||
| "-Command", | ||
| `(New-Object Media.SoundPlayer '${escapedPath}').PlaySync()`, | ||
| `(New-Object Media.SoundPlayer ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64Path}')))).PlaySync()`, |
There was a problem hiding this comment.
To make this long line more concise, you can use the [Convert] type accelerator in PowerShell, which is an alias for [System.Convert].
| `(New-Object Media.SoundPlayer ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64Path}')))).PlaySync()`, | |
| `(New-Object Media.SoundPlayer ([System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('${base64Path}')))).PlaySync()` |
| [ | ||
| "-Command", | ||
| `Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait('${escaped}')`, | ||
| `Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64}')))`, |
There was a problem hiding this comment.
To make this long line more concise, you can use the [Convert] type accelerator in PowerShell, which is an alias for [System.Convert].
| `Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64}')))`, | |
| `Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait([System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('${base64}')))` |
| [ | ||
| "-Command", | ||
| `Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait('${escaped}')`, | ||
| `Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64}')))`, |
There was a problem hiding this comment.
To make this long line more concise, you can use the [Convert] type accelerator in PowerShell, which is an alias for [System.Convert].
Also, note that this win32 implementation is nearly identical to the one in performType. To improve maintainability, consider extracting this logic into a shared helper function in a future refactoring.
| `Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64}')))`, | |
| `Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait([System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('${base64}')))` |
🚨 Severity: CRITICAL
💡 Vulnerability: Unsanitized arbitrary user string interpolation into
powershell -CommandviaexecSync/runCommandallows for command injection. Standard single-quote escaping (text.replace(/'/g, "''")) is insufficient.🎯 Impact: Arbitrary code execution (ACE) on Windows machines running the sandbox instance.
🔧 Fix: Changed all instances to Base64 encode the user input within Node.js, and inject it as a Base64 string into PowerShell, natively decoding it inside the script block before execution. This ensures safe rendering of the user input regardless of shell metacharacters.
✅ Verification: Verified by ensuring unit test suites pass successfully for
sandbox-routes.test.tsand E2Esandbox-onboarding.test.tslocally without regressions. Documented learning insentinel.mdjournal.PR created automatically by Jules for task 13329050723241638503 started by @Dexploarer