robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqvim-patch:9.2.0699: [security]: possible code execution with python complete (#40363)7d0adc08f7

master
23.8 KiB661 linesraw
1"python3complete.vim - Omni Completion for python
2" Maintainer: <vacancy>
3" Previous Maintainer: Aaron Griffin <aaronmgriffin@gmail.com>
4" Version: 0.10
5" Last Updated: 2026 Jun 21
6"
7" Roland Puntaier: this file contains adaptations for python3 and is parallel to pythoncomplete.vim
8"
9" Changes
10" TODO:
11" 'info' item output can use some formatting work
12" Add an "unsafe eval" mode, to allow for return type evaluation
13" Complete basic syntax along with import statements
14"   i.e. "import url<c-x,c-o>"
15" Continue parsing on invalid line??
16"
17" v 0.10 by Vim project
18"   * disables importing local modules, unless the global Vim variable
19"     g:pythoncomplete_allow_import is set to non-zero
20"   * strip default values and annotations from function parameter lists
21"     before exec(), and whitelist class base lists to dotted names: the
22"     previous code passed buffer-supplied expressions to exec() which
23"     Python evaluates at definition time, allowing arbitrary code
24"     execution via crafted def/class headers
25"   * use repr() on doc strings to prevent code execution
26"
27" v 0.9
28"   * Fixed docstring parsing for classes and functions
29"   * Fixed parsing of *args and **kwargs type arguments
30"   * Better function param parsing to handle things like tuples and
31"     lambda defaults args
32"
33" v 0.8
34"   * Fixed an issue where the FIRST assignment was always used instead of
35"   using a subsequent assignment for a variable
36"   * Fixed a scoping issue when working inside a parameterless function
37"
38"
39" v 0.7
40"   * Fixed function list sorting (_ and __ at the bottom)
41"   * Removed newline removal from docs.  It appears vim handles these better in
42"   recent patches
43"
44" v 0.6:
45"   * Fixed argument completion
46"   * Removed the 'kind' completions, as they are better indicated
47"   with real syntax
48"   * Added tuple assignment parsing (whoops, that was forgotten)
49"   * Fixed import handling when flattening scope
50"
51" v 0.5:
52" Yeah, I skipped a version number - 0.4 was never public.
53"  It was a bugfix version on top of 0.3.  This is a complete
54"  rewrite.
55"
56
57if !has('python3')
58    echo 'Error: Requires python3 + pynvim.  :help provider-python'
59    finish
60endif
61
62function! python3complete#Complete(findstart, base)
63    "findstart = 1 when we need to get the text length
64    if a:findstart == 1
65        let line = getline('.')
66        let idx = col('.')
67        while idx > 0
68            let idx -= 1
69            let c = line[idx]
70            if c =~ '\w'
71                continue
72            elseif ! c =~ '\.'
73                let idx = -1
74                break
75            else
76                break
77            endif
78        endwhile
79
80        return idx
81    "findstart = 0 when we need to return the list of completions
82    else
83        "vim no longer moves the cursor upon completion... fix that
84        let line = getline('.')
85        let idx = col('.')
86        let cword = ''
87        while idx > 0
88            let idx -= 1
89            let c = line[idx]
90            if c =~ '\w' || c =~ '\.'
91                let cword = c . cword
92                continue
93            elseif strlen(cword) > 0 || idx == 0
94                break
95            endif
96        endwhile
97	execute "py3 vimpy3complete('" . escape(cword, "'") . "', '" . escape(a:base, "'") . "')"
98        return g:python3complete_completions
99    endif
100endfunction
101
102function! s:DefPython()
103py3 << PYTHONEOF
104import warnings
105warnings.simplefilter(action='ignore', category=FutureWarning)
106
107import sys, tokenize, io, types
108from token import NAME, DEDENT, NEWLINE, STRING
109import re
110
111# Used by Class.get_code(): a base class expression is only included in the
112# code passed to exec() if it is a pure dotted name (e.g. "Base", "mod.Base",
113# "pkg.sub.Cls").  Anything containing calls, subscripts, "=", ":" or other
114# operators is dropped, since exec()-ing it would evaluate buffer-supplied
115# expressions.  See the security note in the file header.
116_DOTTED_NAME_RE = re.compile(r'^[A-Za-z_]\w*(\s*\.\s*[A-Za-z_]\w*)*$')
117
118def _strip_param(p):
119    # Return the bare parameter name from a parameter spec harvested by
120    # _parenparse(), discarding any default value or annotation.  Default
121    # values and annotations would otherwise be evaluated by exec() at
122    # function-definition time.  Star prefixes ("*args", "**kw") and bare
123    # "*" / "/" are preserved as written.
124    p = p.split('=', 1)[0]
125    p = p.split(':', 1)[0]
126    return p.strip()
127
128debugstmts=[]
129def dbg(s): debugstmts.append(s)
130def showdbg():
131    for d in debugstmts: print("DBG: %s " % d)
132
133def vimpy3complete(context,match):
134    global debugstmts
135    debugstmts = []
136    try:
137        import vim
138        cmpl = Completer()
139        cmpl.evalsource('\n'.join(vim.current.buffer),vim.eval("line('.')"))
140        all = cmpl.get_completions(context,match)
141        all.sort(key=lambda x:x['abbr'].replace('_','z'))
142        dictstr = '['
143        # have to do this for double quoting
144        for cmpl in all:
145            dictstr += '{'
146            for x in cmpl: dictstr += '"%s":"%s",' % (x,cmpl[x])
147            dictstr += '"icase":0},'
148        if dictstr[-1] == ',': dictstr = dictstr[:-1]
149        dictstr += ']'
150        #dbg("dict: %s" % dictstr)
151        vim.command("silent let g:python3complete_completions = %s" % dictstr)
152        #dbg("Completion dict:\n%s" % all)
153    except vim.error:
154        dbg("VIM Error: %s" % vim.error)
155
156class Completer(object):
157    def __init__(self):
158       self.compldict = {}
159       self.parser = PyParser()
160
161    def evalsource(self,text,line=0):
162        # vim is imported locally in vimpy3complete(); re-import here so the
163        # vim.eval() below works (otherwise NameError, silently caught).
164        import vim
165        sc = self.parser.parse(text,line)
166        try: allow_imports = int(
167          vim.eval("get(g:, 'pythoncomplete_allow_import', 0)"))
168        except Exception:
169          allow_imports = 0
170        src = sc.get_code()
171        dbg("source: %s" % src)
172        try: exec(src,self.compldict)
173        except: dbg("parser: %s, %s" % (sys.exc_info()[0],sys.exc_info()[1]))
174        for l in sc.locals:
175            # Executing import/from statements harvested from the buffer runs
176            # arbitrary package code; only do so when the user opted in.
177            if not allow_imports and (l.startswith('import')
178                                            or l.startswith('from ')):
179                continue
180            try: exec(l,self.compldict)
181            except: dbg("locals: %s, %s [%s]" % (sys.exc_info()[0],sys.exc_info()[1],l))
182
183    def _cleanstr(self,doc):
184        return doc.replace('"',' ').replace("'",' ')
185
186    def get_arguments(self,func_obj):
187        def _ctor(class_ob):
188            try: return class_ob.__init__
189            except AttributeError:
190                for base in class_ob.__bases__:
191                    rc = _ctor(base)
192                    if rc is not None: return rc
193            return None
194
195        arg_offset = 1
196        if type(func_obj) == type: func_obj = _ctor(func_obj)
197        elif type(func_obj) == types.MethodType: arg_offset = 1
198        else: arg_offset = 0
199
200        arg_text=''
201        if type(func_obj) in [types.FunctionType, types.LambdaType,types.MethodType]:
202            try:
203                cd = func_obj.__code__
204                real_args = cd.co_varnames[arg_offset:cd.co_argcount]
205                defaults = func_obj.__defaults__ or []
206                defaults = ["=%s" % name for name in defaults]
207                defaults = [""] * (len(real_args)-len(defaults)) + defaults
208                items = [a+d for a,d in zip(real_args,defaults)]
209                if func_obj.__code__.co_flags & 0x4:
210                    items.append("...")
211                if func_obj.__code__.co_flags & 0x8:
212                    items.append("***")
213                arg_text = (','.join(items)) + ')'
214            except:
215                dbg("arg completion: %s: %s" % (sys.exc_info()[0],sys.exc_info()[1]))
216                pass
217        if len(arg_text) == 0:
218            # The doc string sometimes contains the function signature
219            #  this works for a lot of C modules that are part of the
220            #  standard library
221            doc = func_obj.__doc__
222            if doc:
223                doc = doc.lstrip()
224                pos = doc.find('\n')
225                if pos > 0:
226                    sigline = doc[:pos]
227                    lidx = sigline.find('(')
228                    ridx = sigline.find(')')
229                    if lidx > 0 and ridx > 0:
230                        arg_text = sigline[lidx+1:ridx] + ')'
231        if len(arg_text) == 0: arg_text = ')'
232        return arg_text
233
234    def get_completions(self,context,match):
235        #dbg("get_completions('%s','%s')" % (context,match))
236        stmt = ''
237        if context: stmt += str(context)
238        if match: stmt += str(match)
239        try:
240            result = None
241            all = {}
242            ridx = stmt.rfind('.')
243            if len(stmt) > 0 and stmt[-1] == '(':
244                result = eval(_sanitize(stmt[:-1]), self.compldict)
245                doc = result.__doc__
246                if doc is None: doc = ''
247                args = self.get_arguments(result)
248                return [{'word':self._cleanstr(args),'info':self._cleanstr(doc)}]
249            elif ridx == -1:
250                match = stmt
251                all = self.compldict
252            else:
253                match = stmt[ridx+1:]
254                stmt = _sanitize(stmt[:ridx])
255                result = eval(stmt, self.compldict)
256                all = dir(result)
257
258            dbg("completing: stmt:%s" % stmt)
259            completions = []
260
261            try: maindoc = result.__doc__
262            except: maindoc = ' '
263            if maindoc is None: maindoc = ' '
264            for m in all:
265                if m == "_PyCmplNoType": continue #this is internal
266                try:
267                    dbg('possible completion: %s' % m)
268                    if m.find(match) == 0:
269                        if result is None: inst = all[m]
270                        else: inst = getattr(result,m)
271                        try: doc = inst.__doc__
272                        except: doc = maindoc
273                        typestr = str(inst)
274                        if doc is None or doc == '': doc = maindoc
275
276                        wrd = m[len(match):]
277                        c = {'word':wrd, 'abbr':m,  'info':self._cleanstr(doc)}
278                        if "function" in typestr:
279                            c['word'] += '('
280                            c['abbr'] += '(' + self._cleanstr(self.get_arguments(inst))
281                        elif "method" in typestr:
282                            c['word'] += '('
283                            c['abbr'] += '(' + self._cleanstr(self.get_arguments(inst))
284                        elif "module" in typestr:
285                            c['word'] += '.'
286                        elif "type" in typestr:
287                            c['word'] += '('
288                            c['abbr'] += '('
289                        completions.append(c)
290                except:
291                    i = sys.exc_info()
292                    dbg("inner completion: %s,%s [stmt='%s']" % (i[0],i[1],stmt))
293            return completions
294        except:
295            i = sys.exc_info()
296            dbg("completion: %s,%s [stmt='%s']" % (i[0],i[1],stmt))
297            return []
298
299class Scope(object):
300    def __init__(self,name,indent,docstr=''):
301        self.subscopes = []
302        self.docstr = docstr
303        self.locals = []
304        self.parent = None
305        self.name = name
306        self.indent = indent
307
308    def add(self,sub):
309        #print('push scope: [%s@%s]' % (sub.name,sub.indent))
310        sub.parent = self
311        self.subscopes.append(sub)
312        return sub
313
314    def doc(self,str):
315        """ Clean up a docstring """
316        d = str.replace('\n',' ')
317        d = d.replace('\t',' ')
318        while d.find('  ') > -1: d = d.replace('  ',' ')
319        while d[0] in '"\'\t ': d = d[1:]
320        while d[-1] in '"\'\t ': d = d[:-1]
321        dbg("Scope(%s)::docstr = %s" % (self,d))
322        self.docstr = d
323
324    def local(self,loc):
325        self._checkexisting(loc)
326        self.locals.append(loc)
327
328    def copy_decl(self,indent=0):
329        """ Copy a scope's declaration only, at the specified indent level - not local variables """
330        return Scope(self.name,indent,self.docstr)
331
332    def _checkexisting(self,test):
333        "Convienance function... keep out duplicates"
334        if test.find('=') > -1:
335            var = test.split('=')[0].strip()
336            for l in self.locals:
337                if l.find('=') > -1 and var == l.split('=')[0].strip():
338                    self.locals.remove(l)
339
340    def get_code(self):
341        str = ""
342        if len(self.docstr) > 0: str += repr(self.docstr)+'\n'
343        str += 'class _PyCmplNoType:\n    def __getattr__(self,name):\n        return None\n'
344        for sub in self.subscopes:
345            str += sub.get_code()
346        for l in self.locals:
347            if not l.startswith('import') and not l.startswith('from '): str += l+'\n'
348
349        return str
350
351    def pop(self,indent):
352        #print('pop scope: [%s] to [%s]' % (self.indent,indent))
353        outer = self
354        while outer.parent != None and outer.indent >= indent:
355            outer = outer.parent
356        return outer
357
358    def currentindent(self):
359        #print('parse current indent: %s' % self.indent)
360        return '    '*self.indent
361
362    def childindent(self):
363        #print('parse child indent: [%s]' % (self.indent+1))
364        return '    '*(self.indent+1)
365
366class Class(Scope):
367    def __init__(self, name, supers, indent, docstr=''):
368        Scope.__init__(self,name,indent, docstr)
369        self.supers = supers
370    def copy_decl(self,indent=0):
371        c = Class(self.name,self.supers,indent, self.docstr)
372        for s in self.subscopes:
373            c.add(s.copy_decl(indent+1))
374        return c
375    def get_code(self):
376        str = '%sclass %s' % (self.currentindent(),self.name)
377        # Only include base class expressions that are pure dotted names.
378        # Anything else (calls, subscripts, conditionals, ...) is dropped
379        # because exec() would evaluate it at class-definition time.  See
380        # the security note in the file header.
381        safe_supers = [s.strip() for s in self.supers
382                       if _DOTTED_NAME_RE.match(s.strip())]
383        if len(safe_supers) > 0: str += '(%s)' % ','.join(safe_supers)
384        str += ':\n'
385        if len(self.docstr) > 0: str += self.childindent()+repr(self.docstr)+'\n'
386        if len(self.subscopes) > 0:
387            for s in self.subscopes: str += s.get_code()
388        else:
389            str += '%spass\n' % self.childindent()
390        return str
391
392
393class Function(Scope):
394    def __init__(self, name, params, indent, docstr=''):
395        Scope.__init__(self,name,indent, docstr)
396        self.params = params
397    def copy_decl(self,indent=0):
398        return Function(self.name,self.params,indent, self.docstr)
399    def get_code(self):
400        # Strip default values and annotations from each parameter before
401        # joining: exec() evaluates these at definition time and a hostile
402        # buffer could otherwise execute arbitrary code via crafted def
403        # headers.  See file header for details.
404        safe_params = [_strip_param(p) for p in self.params]
405        safe_params = [p for p in safe_params if p]
406        str = "%sdef %s(%s):\n" % \
407            (self.currentindent(),self.name,','.join(safe_params))
408        if len(self.docstr) > 0: str += self.childindent()+repr(self.docstr)+'\n'
409        str += "%spass\n" % self.childindent()
410        return str
411
412class PyParser:
413    def __init__(self):
414        self.top = Scope('global',0)
415        self.scope = self.top
416        self.parserline = 0
417
418    def _parsedotname(self,pre=None):
419        #returns (dottedname, nexttoken)
420        name = []
421        if pre is None:
422            tokentype, token, indent = self.donext()
423            if tokentype != NAME and token != '*':
424                return ('', token)
425        else: token = pre
426        name.append(token)
427        while True:
428            tokentype, token, indent = self.donext()
429            if token != '.': break
430            tokentype, token, indent = self.donext()
431            if tokentype != NAME: break
432            name.append(token)
433        return (".".join(name), token)
434
435    def _parseimportlist(self):
436        imports = []
437        while True:
438            name, token = self._parsedotname()
439            if not name: break
440            name2 = ''
441            if token == 'as': name2, token = self._parsedotname()
442            imports.append((name, name2))
443            while token != "," and "\n" not in token:
444                tokentype, token, indent = self.donext()
445            if token != ",": break
446        return imports
447
448    def _parenparse(self):
449        name = ''
450        names = []
451        level = 1
452        while True:
453            tokentype, token, indent = self.donext()
454            if token in (')', ',') and level == 1:
455                if '=' not in name: name = name.replace(' ', '')
456                names.append(name.strip())
457                name = ''
458            if token == '(':
459                level += 1
460                name += "("
461            elif token == ')':
462                level -= 1
463                if level == 0: break
464                else: name += ")"
465            elif token == ',' and level == 1:
466                pass
467            else:
468                name += "%s " % str(token)
469        return names
470
471    def _parsefunction(self,indent):
472        self.scope=self.scope.pop(indent)
473        tokentype, fname, ind = self.donext()
474        if tokentype != NAME: return None
475
476        tokentype, open, ind = self.donext()
477        if open != '(': return None
478        params=self._parenparse()
479
480        tokentype, colon, ind = self.donext()
481        if colon != ':': return None
482
483        return Function(fname,params,indent)
484
485    def _parseclass(self,indent):
486        self.scope=self.scope.pop(indent)
487        tokentype, cname, ind = self.donext()
488        if tokentype != NAME: return None
489
490        super = []
491        tokentype, thenext, ind = self.donext()
492        if thenext == '(':
493            super=self._parenparse()
494        elif thenext != ':': return None
495
496        return Class(cname,super,indent)
497
498    def _parseassignment(self):
499        assign=''
500        tokentype, token, indent = self.donext()
501        if tokentype == tokenize.STRING or token == 'str':
502            return '""'
503        elif token == '(' or token == 'tuple':
504            return '()'
505        elif token == '[' or token == 'list':
506            return '[]'
507        elif token == '{' or token == 'dict':
508            return '{}'
509        elif tokentype == tokenize.NUMBER:
510            return '0'
511        elif token == 'open' or token == 'file':
512            return 'file'
513        elif token == 'None':
514            return '_PyCmplNoType()'
515        elif token == 'type':
516            return 'type(_PyCmplNoType)' #only for method resolution
517        else:
518            assign += token
519            level = 0
520            while True:
521                tokentype, token, indent = self.donext()
522                if token in ('(','{','['):
523                    level += 1
524                elif token in (']','}',')'):
525                    level -= 1
526                    if level == 0: break
527                elif level == 0:
528                    if token in (';','\n'): break
529                    assign += token
530        return "%s" % assign
531
532    def donext(self):
533        type, token, (lineno, indent), end, self.parserline = next(self.gen)
534        if lineno == self.curline:
535            #print('line found [%s] scope=%s' % (line.replace('\n',''),self.scope.name))
536            self.currentscope = self.scope
537        return (type, token, indent)
538
539    def _adjustvisibility(self):
540        newscope = Scope('result',0)
541        scp = self.currentscope
542        while scp != None:
543            if type(scp) == Function:
544                slice = 0
545                #Handle 'self' params
546                if scp.parent != None and type(scp.parent) == Class:
547                    slice = 1
548                    newscope.local('%s = %s' % (scp.params[0],scp.parent.name))
549                for p in scp.params[slice:]:
550                    i = p.find('=')
551                    if len(p) == 0: continue
552                    pvar = ''
553                    ptype = ''
554                    if i == -1:
555                        pvar = p
556                        ptype = '_PyCmplNoType()'
557                    else:
558                        pvar = p[:i]
559                        ptype = _sanitize(p[i+1:])
560                    if pvar.startswith('**'):
561                        pvar = pvar[2:]
562                        ptype = '{}'
563                    elif pvar.startswith('*'):
564                        pvar = pvar[1:]
565                        ptype = '[]'
566
567                    newscope.local('%s = %s' % (pvar,ptype))
568
569            for s in scp.subscopes:
570                ns = s.copy_decl(0)
571                newscope.add(ns)
572            for l in scp.locals: newscope.local(l)
573            scp = scp.parent
574
575        self.currentscope = newscope
576        return self.currentscope
577
578    #p.parse(vim.current.buffer[:],vim.eval("line('.')"))
579    def parse(self,text,curline=0):
580        self.curline = int(curline)
581        buf = io.StringIO(''.join(text) + '\n')
582        self.gen = tokenize.generate_tokens(buf.readline)
583        self.currentscope = self.scope
584
585        try:
586            freshscope=True
587            while True:
588                tokentype, token, indent = self.donext()
589                #dbg( 'main: token=[%s] indent=[%s]' % (token,indent))
590
591                if tokentype == DEDENT or token == "pass":
592                    self.scope = self.scope.pop(indent)
593                elif token == 'def':
594                    func = self._parsefunction(indent)
595                    if func is None:
596                        print("function: syntax error...")
597                        continue
598                    dbg("new scope: function")
599                    freshscope = True
600                    self.scope = self.scope.add(func)
601                elif token == 'class':
602                    cls = self._parseclass(indent)
603                    if cls is None:
604                        print("class: syntax error...")
605                        continue
606                    freshscope = True
607                    dbg("new scope: class")
608                    self.scope = self.scope.add(cls)
609
610                elif token == 'import':
611                    imports = self._parseimportlist()
612                    for mod, alias in imports:
613                        loc = "import %s" % mod
614                        if len(alias) > 0: loc += " as %s" % alias
615                        self.scope.local(loc)
616                    freshscope = False
617                elif token == 'from':
618                    mod, token = self._parsedotname()
619                    if not mod or token != "import":
620                        print("from: syntax error...")
621                        continue
622                    names = self._parseimportlist()
623                    for name, alias in names:
624                        loc = "from %s import %s" % (mod,name)
625                        if len(alias) > 0: loc += " as %s" % alias
626                        self.scope.local(loc)
627                    freshscope = False
628                elif tokentype == STRING:
629                    if freshscope: self.scope.doc(token)
630                elif tokentype == NAME:
631                    name,token = self._parsedotname(token)
632                    if token == '=':
633                        stmt = self._parseassignment()
634                        dbg("parseassignment: %s = %s" % (name, stmt))
635                        if stmt != None:
636                            self.scope.local("%s = %s" % (name,stmt))
637                    freshscope = False
638        except StopIteration: #thrown on EOF
639            pass
640        except:
641            dbg("parse error: %s, %s @ %s" %
642                (sys.exc_info()[0], sys.exc_info()[1], self.parserline))
643        return self._adjustvisibility()
644
645def _sanitize(str):
646    val = ''
647    level = 0
648    for c in str:
649        if c in ('(','{','['):
650            level += 1
651        elif c in (']','}',')'):
652            level -= 1
653        elif level == 0:
654            val += c
655    return val
656
657sys.path.extend(['.','..'])
658PYTHONEOF
659endfunction
660
661call s:DefPython()