Missing Feature Implied by Documentation: file mode

Aberta
#87 0 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
3/5
Tempo estimado
1-2 dias
Facilidade para iniciantes
45/100
Tipo de issue
Funcionalidade
Clareza
Razoavelmente clara
Status de atividade
Ativa
Stack de tecnologia
ruby
Domínio
backend

Direção de pesquisa

The behaviour lives in lib/tempfile.rb: the Tempfile.create path ORs the internal open flags with the caller's mode argument, and the documentation block near lines 571-575 claims permissions are 0600 with a reference to File Permissions. Read that code and the matching docs, then decide whether to make the permission bits configurable or to document and warn about the flag handling. Done means the doc and behaviour agree, and the chosen approach is covered by tests in test/ for Tempfile.create.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

Context

I'm using Tempfile.create to update an existing file. I'm creating a Tempfile in my target directory, and inside the block of the ::create invocation, I'm writing to the file, closing it, and -- if everything checks out -- I'm using FileUtils.mv to move it into place atomically (potentially overwriting the existing file).

How I Encountered This

I was just trying to troubleshoot an issue where, despite me adding mode: 0o644 to Tempfile.create, I was creating a file (even before the move) with mode 0600. I quickly realized my mistake: the keyword argument mode, of course, refers to the open mode/flags given to File.open, rather than the argument ruby calls perms.

The Issues

While I fully admit that reading every line of the documentation and what it links to would have mostly cleared this up, this isn't what developers typically do (or should be assumed to do) and the documentation arguably encouraged my particular misunderstanding.

On top of that, even read completely, the documentation suggests a feature that doesn't exist and -- ideally -- should (barring that, it's absence should be documented):

https://github.com/ruby/tempfile/blob/214f4cd71e2b67337890ab9ee24fedcc81f2b8dd/lib/tempfile.rb#L571-L575

The issues are twofold:

  • Tempfile.create doesn't issue so much as a warning when open flags are explicitly given that aren't suitable for use with the creation of a temporary file.

    Instead, the flags needed by Tempfile are logically 'OR'ed with the ones passed (see code above), potentially causing other hard-to-trace errors in how the file gets created.

    For example, on Linux, File::RDWR is 2, but File::WRONLY is 1. These two flags are not normally meant to be used together, and have a special/specific meaning on Linux.

    Per man 2 open:

    Linux reserves the special, nonstandard access mode 3 (binary 11) in flags to mean: check for read and write permission on the file and return a file descriptor that can't be used for reading or writing. This nonstandard access mode is used by some Linux drivers to return a file descriptor that is to be used only for device-specific ioctl(2) operations.

    Lastly, though less likely (and inadvisable for security reasons), a value that intends to set the setuid and/or setgid flags in a file's mode would result in the it being inadvertently opened in append or non-block mode.

  • Potentially worse, the docs suggests the newly-created files' permissions are configurable.

    The issue here is that under With no block given and no arguments, creates and returns file whose:, the bulleted list that follows includes Permissions are 0600; see File Permissions.

    It is reasonable to assume from this that the permissions of the temporary file are configurable, when they are, in fact, hardcoded.

    In my case -- and I would have to imagine others have reached, and are liable to reach, the same conclusion -- the only argument listed that could plausibly control this seemed to be mode, so I assumed I didn't need to look any further.

Suggested Fix

Catching when the developer confuses open flags with the new file's mode is kind of tricky. Particularly because any red-flag values would be OS dependant and -- it could be argued -- don't even belong in this library. I'm also a little bit surprised the Linux doesn't seem to refuse to open a file if unrecognized open flags are given, but this might not be the case on other OSes or even other libc implementations.

In any case, I think this -- as well as the documentation confusion -- can both be easily addressed by just making the new file's permissions mode configurable, and documenting the argument that controls it. Like what is currently done for the open flags, any bits that need to be set for Tempfile to work can be logically ORed with the provided value. For example, at the very least, the user write bit.

On the other hand, whether the user read bit should be enabled depends, I think, on what it was intended the ORing of the open flags would do if File::WR_ONLY was passed explicitly. As mentioned, it probably depends on the OS and/or libc implementation which takes precedence. In any case, I would only automatically add the user read bit to the file's mode if the file isn't to be opened in read-write mode. If opening for writing isn't mandatory, and neither the open flags or file mode are specified, it may make sense to defer to if reading is blocked by the umask.

Lastly, I would at least document that -- even if the open mode is specified -- certain bits will be forced to on in the provided value. Honestly, I'm not sure if changing a non-default value is the best practice in the first place and think it makes more sense to let a relevant error be thrown, but that would obviously a breaking change. I think ideally, at least a warning should be omitted if the tempfile library alters these values.

Please let me know if I can be of any help in making this change. It's very likely I can do the PR if you can tell me this change would be approved.

Thanks!

Linguagem predominante
Ruby
Estrelas
35
Forks
11
Merge médio
1h 57min
PRs com merge (30d)
3

Preparar o ambiente

Este projeto não oferece contêiner de desenvolvimento, Dockerfile nem guia de contribuição, então a configuração fica por sua conta: comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de ruby/tempfile

Todas as issues de ruby/tempfile

Issues semelhantes

Mais issues de Ruby

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.