Summary
Vim's Python omni-completion reconstructs code from the current buffer and executes it with exec() to build completion suggestions, inserting docstrings directly into triple-quoted strings.
A malicious buffer can craft docstrings that escape the string literal and inject arbitrary Python code, turning a seemingly benign completion request into code execution within the editor process under user context.
Affected context
This vulnerability occurs when Vim users enable or invoke Python omni-completion features while editing buffers that may contain untrusted or partially trusted Python code.
The completion logic reconstructs function and class definitions by combining identifiers, signatures, and docstrings, then executing that reconstruction using exec() within the editor's Python environment.
Because docstrings are inserted verbatim into triple-quoted literals without escaping or validation, attacker-controlled buffer content can transform what should be passive text analysis into active code execution.
Exploit preconditions
- Attack surface: Local editing session using Vim with Python omni-completion (python3complete.vim or legacy pythoncomplete.vim).
- Required access: Ability to influence or provide the Python source in the buffer (e.g., opening untrusted files, cloned repositories, shared snippets, or generated code).
- Trigger point: User action that invokes omni-completion for Python (such as completion keystrokes in insert mode) while cursor is positioned in or near hostile constructs.
- Why exploitation is possible: The completion engine reconstructs definitions and executes them using
exec()without sanitizing docstrings, allowing crafted literals to escape triple quotes and run attacker-supplied Python in the editor's runtime environment.
Why this happens
The underlying flaw aligns with CWE-94: Code Injection, where dynamically constructed code from external input is executed without sufficient validation or constraints.
Vim's Python omni-completion assembles a Python source string from buffer content—including docstrings—then passes that string directly to exec() as part of the completion logic.
Docstrings are treated as inert text but are interpolated between triple quotes without escaping special characters or delimiters, so an attacker can embed closing quotes and additional statements to break out of the intended literal.
This design assumes buffer contents are trustworthy, blurring the line between data and executable code and enabling arbitrary code injection during completion processing.
Potential impact
- Arbitrary Python code execution within the Vim process under the user's account.
- Modification of local files opened in Vim, including insertion or alteration of source code or configuration.
- Exfiltration or tampering of sensitive data accessible to the user, such as SSH keys, API tokens, or project secrets on disk.
- Invocation of external commands via Python's standard library, potentially enabling broader system compromise depending on user privileges.
- Persistent editor or environment manipulation (e.g., altering Vim configuration files or plugin scripts to maintain influence).
General mitigation guidance
Effective mitigation focuses on reducing trust in buffer content and eliminating dynamic execution of reconstructed code during completion.
General patterns include:
- Avoid constructing executable code strings from buffer-derived text; instead, parse and analyze code using structured APIs or static inspection without
exec()oreval(). - Treat docstrings and comments as untrusted text inputs and apply robust escaping or encoding wherever they are embedded in template literals or generated code.
- Constrain interpreter capabilities used inside tooling features: prefer restricted execution contexts, minimized imports, and clear separation of analysis routines from execution paths.
- Apply least-privilege principles to editor and plugin behavior, limiting file system and network access where feasible for code-introspection features.
- Favor secure-by-default plugin architectures where completion and analysis operate on parse trees or language server protocols rather than executing reconstructed user code.
How teams detect issues like this
High-level detection approaches typically involve:
- Static analysis or grep-based rules that flag uses of
exec()andeval()where inputs derive from buffer content, user-editable strings, or docstrings in editor plugins or tooling scripts. - Code review practices that scrutinize completion, introspection, and automation features for patterns that execute reconstructed user code or configuration without sanitization.
- Policy enforcement in plugin ecosystems: mandating that contributions avoid direct dynamic code execution from documents being edited and documenting safe parsing patterns.
- Use of automated security scanners or linters on Vimscript and Python-based plugins to identify injection-prone APIs and missing input validation around text interpolation.

