Skip to content

Redesigning as pure Python distribution #396

Closed

Description

@sanmai-NL

I would like to discuss the use of non-pure Python package dependencies in pre-commit.
In particular the use of C code adds build difficulty and security risk, which I hope can be carefully weighed against the need for it.

Background
You could envision two possible designs of pre-commit:

  1. a pure Python tool that can be used by dev teams in almost any setting, vs.
  2. a CPython-dependent tool that requires custom C code, Python header files and C toolchain to install.

This trade-off is of course not particular to pre-commit.
Sure, in Python-based projects, depending on C libraries and certainly on CPython is very common.
For pre-commit however, it has more impact than usual: adding pre-commit to a CI setup is more difficult in design 2, and I see that as a major use-case (it is for me, in practice).

Current state
As far as I found, pre-commit currently

  1. optionally depends on LibYAML via PyYAML; and
  2. depends on the CPython header files via pyterminalsize.
  3. LibYAML has not seen maintainance in the last two years and is still at 0.1.6.
    It is not widely available from OS packaging (e.g., not on Alpine Linux), and appears to not be installed automatically following the installation/build instructions for pre-commit.
    Building without LibYAML succeeds though, so this is less of a problem.
  4. pyterminalsize is @asottile's own, still young library.

Discussion
I am wondering, how important is it to keep pyterminalsize with just that small snippet of C code as a dependency? Is it easily possible to take a different approach, and avoid CI containers having to install a C toolchain?

Activity

  1. asottile commented on Aug 16, 2016

    @asottile
    Member

    I believe this becomes a non issue if pyterminalsize had a built manylinux wheel is this correct?

    Could you elaborate on what you mean by security issues?

  2. asottile commented on Aug 16, 2016

    @asottile
    Member

    That said, there are ui designs which eliminate pyterminalsize which would be great to implement (basically use either minimum required dots or fixed at 79 characters)

  3. sanmai-NL commented on Aug 16, 2016

    @sanmai-NL
    Author

    Manylinux wheels, didn't know them, but it looks like it isn't robust and simple. Also, isn't the interaction with Windows support problematic? A good solution perhaps if the C code in your CPython project is absolutely needed and the range of user platforms is limited and well-known in advance.

    Using C code, especially for LibYAML is a security issue over using pure Python since any and all C code is at particular risk of memory security issues. See e.g. this paper. In practice, this means that someone writing malformed YAML could use that to execute arbitrary code. This kind of vulnerability happens now, to the best, and to popular widely used libraries. Moreover, the YAML spec is notoriously complex. For something potentially operating at the center of software development in an organization (e.g., on a CI host), that's unattractive.

  4. asottile commented on Aug 16, 2016

    @asottile
    Member

    There already exists wheels for Windows so not a problem there. I think manylinux should solve your primary gripes in ci.

    I'm not sure that being written in a specific language means it is any more or less vulnerable to memory security vulnerabilities. CPython itself is written in c, should I remove cpython support /s? More seriously there are other ways in pure python to execute arbitrary code.

    I hear you about yaml, I hate it :) would have rather used json from the start but it's nearly impossible to switch now and maintain any compatibility. There is one use case that yaml doesn't currently give us in rewritability -- I've planned to write a simpler round trip parser that only handles a strict subset of yaml which could alleviate your concerns there. That said, I don't think that using yaml as a configuration language means there's a problem in our case.

    To conclude, I believe your only valid concern is with build system setup and not actually security. This can be solved by using (manylinux) wheels for pyterminalsize and by allowing the fallback pure python implementation of pyyaml. I can build pyterminalsize manylinux wheels when I'm at a computer.

  5. sanmai-NL commented on Aug 16, 2016

    @sanmai-NL
    Author

    Thanks for all the wheels.

    I respectfully disagree with you that using one language over the other won't really make you more or less vulnerable to memory security vulnerabilities. As claimed in the paper and in many other, C code is particularly at risk of this. CPython is more carefully authored and reviewed than your average C code, so ceasing to use CPython isn't so inevitable.

  6. asottile commented on Aug 16, 2016

    @asottile
    Member

    Should work now with pip>8 without a C compiler installed:

    Uploading pyterminalsize-0.1.0-cp34-cp34m-manylinux1_x86_64.whl
    [================================] 15227/15227 - 00:00:02
    Uploading pyterminalsize-0.1.0-cp35-cp35m-manylinux1_x86_64.whl
    [================================] 15408/15408 - 00:00:02
    Uploading pyterminalsize-0.1.0-cp27-cp27mu-manylinux1_x86_64.whl
    [================================] 15240/15240 - 00:00:02
    Uploading pyterminalsize-0.1.0-cp26-cp26mu-manylinux1_x86_64.whl
    [================================] 15204/15204 - 00:00:02
    
  7. asottile commented on Aug 16, 2016

    @asottile
    Member

    It's also certainly not impossible:

    >>> # ideally, never do this
    >>> x = lambda: None
    >>> y = type(x.__code__)(*((0,) * 5), b'\x01', (), (), (), '', '', 0, b'')
    >>> type(x)(y, {})()
    Segmentation fault (core dumped)

    On the point of languages, I'll give you that it is certainly easier to write poorly written code which allows for memory issues. But I do think they are possible in any language, especially given most code needs to interface with C routines at some point.

    pypy has a more fun time:

    >>>> x = lambda: None
    >>>> y = type(x.__code__)(0,0,0,0,0,b'\x01', (), (), (), '', '', 0, b'')
    >>>> type(x)(y, {})()
    RPython traceback:
      File "pypy_goal_targetpypystandalone.c", line 1975, in entry_point
      File "pypy_interpreter_pyframe.c", line 2697, in PyFrame_execute_frame
      File "rpython_jit_metainterp_warmspot.c", line 1858, in ll_portal_runner__Unsigned_Bool_pypy_interpreter
      File "pypy_module_pypyjit_interp_jit.c", line 289, in portal_5
      File "pypy_interpreter_pyopcode.c", line 6699, in handle_bytecode__AccessDirect_None
      File "pypy_interpreter_pyopcode.c", line 9749, in dispatch_bytecode__AccessDirect_None
      File "pypy_interpreter_pyopcode.c", line 17141, in call_function__AccessDirect_None
      File "pypy_interpreter_pyframe.c", line 2697, in PyFrame_execute_frame
      File "rpython_jit_metainterp_warmspot.c", line 1858, in ll_portal_runner__Unsigned_Bool_pypy_interpreter
      File "pypy_module_pypyjit_interp_jit.c", line 289, in portal_5
      File "pypy_interpreter_pyopcode.c", line 6699, in handle_bytecode__AccessDirect_None
      File "pypy_interpreter_pyopcode.c", line 9711, in dispatch_bytecode__AccessDirect_None
      File "pypy_interpreter_pyopcode.c", line 17141, in call_function__AccessDirect_None
      File "pypy_interpreter_pyframe.c", line 2697, in PyFrame_execute_frame
      File "rpython_jit_metainterp_warmspot.c", line 1858, in ll_portal_runner__Unsigned_Bool_pypy_interpreter
      File "pypy_module_pypyjit_interp_jit.c", line 289, in portal_5
      File "pypy_interpreter_pyopcode.c", line 6699, in handle_bytecode__AccessDirect_None
      File "pypy_interpreter_pyopcode.c", line 9837, in dispatch_bytecode__AccessDirect_None
      File "pypy_interpreter_pyopcode.c", line 17141, in call_function__AccessDirect_None
      File "pypy_interpreter_pyframe.c", line 2697, in PyFrame_execute_frame
      File "rpython_jit_metainterp_warmspot.c", line 1858, in ll_portal_runner__Unsigned_Bool_pypy_interpreter
      File "pypy_module_pypyjit_interp_jit.c", line 289, in portal_5
      File "pypy_interpreter_pyopcode.c", line 6699, in handle_bytecode__AccessDirect_None
      File "pypy_interpreter_pyopcode.c", line 9711, in dispatch_bytecode__AccessDirect_None
      File "pypy_interpreter_pyopcode.c", line 16819, in CALL_FUNCTION__AccessDirect_None
      File "pypy_interpreter_pyframe.c", line 2697, in PyFrame_execute_frame
      File "rpython_jit_metainterp_warmspot.c", line 1858, in ll_portal_runner__Unsigned_Bool_pypy_interpreter
      File "pypy_module_pypyjit_interp_jit.c", line 289, in portal_5
      File "pypy_interpreter_pyopcode.c", line 6699, in handle_bytecode__AccessDirect_None
      File "pypy_interpreter_pyopcode.c", line 9861, in dispatch_bytecode__AccessDirect_None
      File "pypy_objspace_std_callmethod.c", line 2325, in CALL_METHOD__AccessDirect_star_1
      File "pypy_interpreter_pyframe.c", line 2697, in PyFrame_execute_frame
      File "rpython_jit_metainterp_warmspot.c", line 1858, in ll_portal_runner__Unsigned_Bool_pypy_interpreter
      File "pypy_module_pypyjit_interp_jit.c", line 289, in portal_5
      File "pypy_interpreter_pyopcode.c", line 6699, in handle_bytecode__AccessDirect_None
      File "pypy_interpreter_pyopcode.c", line 9861, in dispatch_bytecode__AccessDirect_None
      File "pypy_objspace_std_callmethod.c", line 2325, in CALL_METHOD__AccessDirect_star_1
      File "pypy_interpreter_pyframe.c", line 2697, in PyFrame_execute_frame
      File "rpython_jit_metainterp_warmspot.c", line 1858, in ll_portal_runner__Unsigned_Bool_pypy_interpreter
      File "pypy_module_pypyjit_interp_jit.c", line 289, in portal_5
      File "pypy_interpreter_pyopcode.c", line 6699, in handle_bytecode__AccessDirect_None
      File "pypy_interpreter_pyopcode.c", line 9861, in dispatch_bytecode__AccessDirect_None
      File "pypy_objspace_std_callmethod.c", line 2325, in CALL_METHOD__AccessDirect_star_1
      File "pypy_interpreter_pyframe.c", line 2697, in PyFrame_execute_frame
      ...
    Fatal RPython error: BytecodeCorruption
    Aborted (core dumped)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions