Missing Feature Implied by Documentation: file mode

未關閉
#87 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
45/100
Issue 類型
功能
描述清晰度
基本清楚
活躍度
活躍
技術堆疊
ruby
領域
backend

研究方向

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.

由索引模型根據 Issue 內容生成。

描述

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!

主要語言
Ruby
星號
35
分支
11
平均合併
1 小時 57 分鐘
30 天內合併 PR
3

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

ruby/tempfile 的其他 Issue

查看 ruby/tempfile 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。