Summary
A command injection flaw in Vim's netrw plugin allows crafted filenames to break out of the intended Ex command when deleting files from the directory browser.
By embedding command separators in a filename, an attacker can trigger arbitrary Vimscript execution, which may include shell commands via system() or :!. This becomes risky when users browse and delete files from directories containing attacker-controlled filenames.
Affected context
The vulnerability occurs in Vim's netrw file browser when a user initiates deletion of a local file and the filename is derived from the buffer's directory listing. In affected versions, the filename is interpolated into an Ex command executed via :execute with only backslashes escaped, leaving other metacharacters such as the bar (|) unescaped. When users operate on files in locations where names can be influenced by an attacker (e.g., shared directories, extracted archives, or cloned repositories), the risk of unintended command execution increases.
Exploit preconditions
- Attack surface: Local use of the Vim netrw plugin while browsing and deleting files.
- Required access: Ability for an attacker to create or place files with crafted names in a directory that the victim opens with netrw.
- Trigger point: User invokes a delete operation (e.g., pressing the delete key or the mapped command) on the malicious filename within the netrw file browser.
- Why exploitation is possible: The filename is concatenated into an Ex command string and passed to :execute with insufficient escaping, allowing the bar (|) character and other command delimiters to terminate the intended command and start a new one, enabling arbitrary Vimscript execution.
Why this happens
The underlying bug is insufficient sanitization of user-controlled data before constructing and executing interpreter commands. In netrw's s:NetrwLocalRmFile() function, filenames originating from the directory listing are embedded directly into an Ex command line. Only backslashes are escaped, leaving other special characters available to alter command structure. This pattern maps to CWE-78: OS Command Injection when Vimscript is used to invoke shell commands, and to CWE-94: Code Injection because the attacker-controlled filename effectively becomes part of the interpreted Vimscript. The insecure design trusts filesystem-derived strings and treats them as safe command arguments without proper quoting or escaping.
Potential impact
- An attacker may execute arbitrary Vimscript commands in the victim's editor session.
- An attacker could trigger execution of arbitrary shell commands via :call system() or :!.
- The victim's local files can be read, modified, or deleted through injected commands.
- The attacker may gain a foothold for further compromise of the user environment, such as credential theft or data exfiltration.
General mitigation guidance
Mitigation focuses on eliminating interpreter command construction that directly embeds untrusted data. Safer patterns include consistently quoting and escaping all filenames and path strings before passing them to :execute or other command-building APIs, and using structured Vimscript functions that take arguments rather than concatenated command strings. Reducing reliance on shell invocation from within the editor helps limit OS command injection risk. Applying least-privilege principles for the user environment, such as avoiding running editors with elevated privileges, further contains impact if interpreter-level issues occur.
How teams detect issues like this
High-level detection approaches include static analysis or pattern-based searches for :execute calls that build command strings from filenames, buffer names, or other user-controlled sources without comprehensive escaping. Code review should flag any use of concatenated command lines that mix literal commands with variables, particularly where metacharacters (|, ;, !) could alter flow. Security policies can encourage the use of command APIs that accept structured arguments instead of raw strings. Automated review tools or linting plugins can be configured to warn when Vimscript invokes shell commands or constructs dynamic Ex commands from untrusted inputs.

