/

CVE-2026-47162

Severity:

HIGH RISK

(8.8)

Code injection (CWE-94) via netrw history in Vim

CVE-2026-47162

Modified

Vulnerability type

Category:

Code Injection

CWE:

CWE-74, CWE-94, CWE-140

Impact:

Arbitrary Code Execution

Severity:

HIGH RISK

(8.8)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

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 .netrwhist history 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.

Sources

SourceLink
github.comgithub.com/vim/vim/commit/f08ab2f4d7d2947c8dd6c179ae08ee6146a2694b
github.comgithub.com/vim/vim/releases/tag/v9.2.0495
github.comgithub.com/vim/vim/security/advisories/GHSA-crm5-rh6j-2c7c
access.redhat.comaccess.redhat.com/errata/RHSA-2026:38509
access.redhat.comaccess.redhat.com/errata/RHSA-2026:38510
access.redhat.comaccess.redhat.com/errata/RHSA-2026:38511
access.redhat.comaccess.redhat.com/errata/RHSA-2026:53371
access.redhat.comaccess.redhat.com/errata/RHSA-2026:54769
access.redhat.comaccess.redhat.com/errata/RHSA-2026:55431
access.redhat.comaccess.redhat.com/errata/RHSA-2026:58981
access.redhat.comaccess.redhat.com/errata/RHSA-2026:68711
access.redhat.comaccess.redhat.com/security/cve/CVE-2026-47162
bugzilla.redhat.combugzilla.redhat.com/show_bug.cgi?id=2487964
security.access.redhat.comsecurity.access.redhat.com/data/csaf/v2/vex/2026/cve-2026-47162.json

Frequently asked

questions

It is primarily a local attack vector. However, an attacker can influence victims remotely by controlling directory names on shared resources (e.g., network shares, synced folders, or source repositories) that the victim is likely to browse with netrw.

The attacker needs the ability to create or rename directories whose paths will be listed and opened by netrw.

They do not need direct access to the victim's machine or Vim configuration; controlling directory names visible to the victim's filesystem view is sufficient.

The safest pattern is to avoid constructing executable scripts via raw string concatenation of untrusted data.

Instead, use dedicated routines that encode or escape all special characters before embedding values into script literals, or separate data storage from executable logic entirely (e.g., storing history as pure data and interpreting it with a parser that does not execute it as code).

To prevent similar vulnerabilities, adopt a secure coding discipline where any time data crosses into code—whether Vimscript, shell, or another language—it is treated as untrusted and must be escaped, encoded, or handled by safe APIs.

Regular security-focused reviews of plugins and tools that serialize editor state, combined with automated checks for unsafe interpolation patterns, can further reduce the risk of code injection in scripting environments.

Find out what's already
exploitable in your codebase.