Summary
A code injection flaw in Vim's netrw plugin allows directory names from the filesystem to be written into the .netrwhist history file without proper escaping of single quotes inside Vimscript string literals.
An attacker who can create specially crafted directory names can inject arbitrary Vimscript, which is executed when the history file is later sourced, potentially leading to shell command execution.
Affected context
This vulnerability appears in environments where Vim's netrw plugin is enabled and users browse directories whose names originate from the filesystem.
The risk is present when:
- netrw persists history to
~/.vim/.netrwhist - the history file is automatically or manually sourced on subsequent Vim launches
- directory names are taken as-is from the filesystem, including potentially untrusted paths such as shared directories, removable media, or project workspaces controlled by others
In such cases, unescaped characters in directory names can alter the Vimscript code that reconstructs history.
Exploit preconditions
- Attack surface: Local text editing workflows using Vim with the netrw plugin and history write/read enabled.
- Required access: Ability to create or control directory names that the victim will browse with netrw (e.g., creating or renaming directories on a shared filesystem or within a project tree).
- Trigger point: The victim uses netrw to open a crafted directory; later, when Vim sources
~/.vim/.netrwhist, the injected Vimscript executes. - Why exploitation is possible: Directory paths are interpolated directly into single-quoted Vimscript string literals without escaping embedded single quotes, allowing an attacker-controlled path to terminate the string and inject additional Vimscript statements.
Why this happens
The core issue is improper neutralization of special elements when constructing code, aligning with CWE-94: Improper Control of Generation of Code and CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component.
The netrw function s:NetrwBookHistSave() serializes directory names into a Vimscript file by embedding them in single-quoted string literals.
Because single quotes inside these directory names are not escaped, the resulting script becomes syntactically malformed in a way that allows injected content after the prematurely closed string to be interpreted as executable Vimscript.
This pattern reflects a broader class of vulnerabilities where data from external sources is concatenated into code or configuration without rigorous escaping or use of safe serialization mechanisms.
Potential impact
- Execution of arbitrary Vimscript code when the
.netrwhisthistory file is sourced. - Invocation of shell commands via Vimscript primitives such as
system()or:!, which can run with the privileges of the user running Vim. - Modification of editor configuration or state, including altering mappings, autocmds, or loading additional malicious scripts.
- Potential compromise of local files, exfiltration of sensitive information, or staging of further attacks in shared development environments.
General mitigation guidance
Mitigation typically centers on treating any data that might end up in generated code as untrusted and constraining how it is serialized.
Common defensive patterns include:
- Applying strict escaping rules for all special characters when embedding external data into code-like contexts, ensuring that delimiters such as quotes, backslashes, and control characters are neutralized.
- Using structured serialization APIs or helper routines that generate safe literals, rather than manually concatenating strings to produce script files.
- Reducing trust boundaries for filesystem-derived names by validating or normalizing directory paths before persisting them into history or configuration files.
- Limiting the circumstances under which history or configuration files are automatically sourced, and separating pure data storage from executable script logic where feasible.
- Operating with least privilege and cautious shell integration, so that even if some injected script runs, its ability to perform harmful shell actions is minimized.
How teams detect issues like this
Detection efforts can focus on both code and runtime behavior:
- Static analysis and code review: Look for patterns where filesystem paths, user input, or other external data are interpolated into script files, command strings, or configuration that will later be executed or sourced.
In Vimscript and similar languages, pay particular attention to constructs that build command lines or script content using direct string concatenation and single- or double-quoted literals.
- Pattern-based checks: Enforce rules that require all embedded data in code generation routines to pass through escaping or encoding functions; automated linters can flag direct use of unescaped variables in literal contexts.
- Runtime monitoring: In managed environments, monitor for unexpected modifications to history or configuration files that are interpreted as code, and watch for anomalous invocations of shell commands originating from editor processes.
- Policy enforcement: Establish guidelines for plugin authors and internal tooling that prohibit writing executable scripts from raw data sources without structured serialization and escaping, and integrate these checks into CI for editor-related tooling.

