Skip to content

Render 'exrc' + 'secure' actually secure #1015

Open

Description

@Jehan

Hi,

set exrc allows to read per-project .vimrc files. Since Vim configuration files are really versatile and can run any random shell command, you want to use it together with set secure. But the help of 'secure' says:

On Unix this option is only used if the ".vimrc" or ".exrc" is not owned by you.

This makes the option useless and even dangerous if it makes people believe that the reading of local vimrc is safe! Typically when you clone a git repository, all the files (any hidden .vimrc hanging there included) have your local user as owner for instance. Quite a common use case nowadays.

Could 'secure' actually be made to work always?


For info: we have added local config files for various editors (and Vim too) in GIMP source tree but we can't encourage anyone to set exrc if it's unsafe (I advise instead to source the config with:

autocmd BufNewFile,BufRead /path/to/gimp/*.[ch] source /path/to/gimp/devel-docs/c.vim

But this sucks since it is definitely not as useful as auto-loading a hidden .vimrc. Other editors we added config for can load their per-project config automatically apparently.

Our current config only uses "setlocal/set" and some other harmless commands like "highlight/match" (to highlight tabs and trailing spaces)… the idea being simply to help contributors with our syntax.

Activity

  1. brammool commented on Aug 27, 2016

    @brammool
    Contributor

    Jehan wrote:

    set exrc allows to read per-project .vimrc files. Since Vim configuration files are really versatile and can run any random shell command, you want to use it together with set secure. But the help of 'secure' says:

    On Unix this option is only used if the ".vimrc" or ".exrc" is not owned by you.

    This makes the option useless and even dangerous if it makes people believe that the reading of local vimrc is safe! Typically when you clone a git repository, all the files (any hidden .vimrc hanging there included) have your local user as owner for instance. Quite a common use case nowadays.

    Could 'secure' actually be made to work always?


    For info: we have added local config files for various editors (and Vim too) in GIMP source tree but we can't encourage anyone to set exrc if it's unsafe (I advise instead to source the config with:

    autocmd BufNewFile,BufRead /path/to/gimp/*.[ch] source /path/to/gimp/devel-docs/c.vim

    But this sucks since it is definitely not as useful as auto-loading a hidden .vimrc. Other editors we added config for can load their per-project config automatically apparently.

    Our current config only uses "setlocal/set" and some other harmless commands like "highlight/match" (to highlight tabs and trailing spaces)… the idea being simply to help contributors with our syntax.

    I recommend never setting the 'exrc' option. The protection that
    'secure' gives you might have a hole. Also, I don't like changing the
    semantics of an option, there is always someone who relies on it.

    Better define an autocommand to set options for a matching directory.

    From "know your smileys":
    2B|^2B Message from Shakespeare

    /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \
    /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \
    \ an exciting new programming language -- http://www.Zimbu.org ///
    \ help me help AIDS victims -- http://ICCF-Holland.org ///

  2. Jehan commented on Aug 27, 2016

    @Jehan
    Author

    I recommend never setting the 'exrc' option.

    I won't ever in the current state, definitely. :-)

    The protection that 'secure' gives you might have a hole. Also, I don't like changing the semantics of an option, there is always someone who relies on it.

    I can understand these 2 reasons. What about creating a new option then? Local vimrc files which would be sourced this way could for instance only allow a safe subset of commands (like set/highlight… these kinds of commands).

    Better define an autocommand to set options for a matching directory.

    This works and it's acceptable for now. But the point of having a local vimrc is for contributors to easily get one's favorite editor (Vim in the current case) immediately configured for the project syntax. If there is no autoload, first of all most contributors will simply never even discover that there is such a file in our tree. Second, even if they knew that such files can be set, they may not immediately think of checking if there is one, especially if their goal is to only write a single patch and contribute only once before deleting the project (you are probably not going to add an autocommand in your global vimrc for a one-time patch).
    Last, this is even annoying for people who contribute to a lot of projects, even when keeping them. I have a few dozen repositories in my hard drive, sometimes I add new ones, sometimes I delete some when I realize I haven't contributed in a while (and don't plan to, for the time being). If each of these projects were to provide a local vimrc, I would have to manually check the contents of each (since I can't fully trust any random file), explicitly add an autocommand for each of these projects and manage these. Also I would never do it for a project which I don't trust enough (random projects from github) because I can't be sure they won't update the vimrc with malicious command at some point which I would get after a git pull.

    In other words, that's always a risk and a bother to use this way.

    On the other hand, if there is a safe command to allow auto-execution of local vimrc (allowing only a safe subset of commands), it's easy to use: just open a file, and I get the adapted syntax. And it is actually safe even on non-trusted repositories.

  3. tbodt commented on Jul 12, 2017

    @tbodt
    Contributor

    bump

  4. ZyX-I commented on Jul 12, 2017

    @ZyX-I

    There secure set of useful commands to use with exrc is not too big. You can set things like textwidth, shiftwidth, etc but this is already covered by .editorconfig, most other variants will either disturb user own settings, are insecure or not what you would ever want to set in a project-local file. What I am talking about is specifically indentation script: when setlocal cindent cinkeys=… cinoptions=… is not enough you normally write a function (basically always when you are not using a C-like language, sometimes with C-like as well: e.g. https://github.com/vim-scripts/google.vim/blob/master/indent/google.vim is for C++ and has a function). :match is the “disturbing” thing (some people use that in their config in place of matchadd(), I heard questions in gitter), :highlight is as well (use predefined group or it would not work well with the user colorscheme). :syntax is one of the options not covered by editorconfig though.

    I would rather want to see editorconfig included and enabled by default, secure DSL for indenting compilable to VimL and some grammar definition DSL for syntax which would be an alternative to existing regex-based :syntax. Though these all (except for the last, :syntax is harder to use) have any sense only if used by a number of editors (syntax may also be used by highlighters like pygments). Editorconfig is, secure indent DSL is possible to create without any assistance from Bram should anybody be actually wanting to invest time, but this is not the case for syntax which requires changes in core.

    There is another option for indent though: stick secure bit to the :function definitions and allow only secure things inside, stick it to &indentexpr as well should it be created in a project-local file. Unless one is using shell commands for indentation this is all what is needed for indent to work.

  5. mcepl commented on Jul 12, 2017

    @mcepl
    Contributor

    Problem with editorconfig in my opinion is that it is horribly limited. E.g., see linked issue about missing support for the equivalent of vim's spell and spelllang settings.

  6. xdhmoore commented on Jul 9, 2020

    @xdhmoore

    Perhaps there is a way to make the recommended "add an autocommand for the directory" more fluid, without sacrificing security. I'm thinking of the flow that direnv uses. When it detects a .envrc file, it lets you know that there is a file that does not have access to run. Then you can explicitly run a command to allow that file, upon which it gets added to a global list of allowed files.

    I'm just making things up here, but would it work to have something like a file, ~/.vimrcsallowed, that is a global list of allowed project local vimrcs? Then when vim starts up, if the local project vimrc setting is enabled and if it detects a local project vimrc, it could alert the user that there is a file without permissions to run. The user could then run a command to :allow ./local/project/.vimrc, which would add it to the global file.

    ~/.vimrcsallowed could be a list of directories and the associated project vimrcs allowed to run when opening files in those directories. Or maybe there's a way to do it without listing the directories.

    To me this seems similar in paradigm to the autocommand suggestion (an explicit user-controlled list of allowed local project files tied to certain directories), but it makes the experience a little smoother.

    I suppose you might want a way to turn off the permissions message for that file if you saw it over and over again on a file you didn't want to run.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions