mirror of
https://github.com/apple/swift-nio
synced 2026-11-22 04:30:41 +00:00
Create temporary directories with owner-only permissions.
### Motivation:
`FileSystem.createTemporaryDirectory(template:)` (and
`withTemporaryDirectory`, which uses it) creates the directory with
`FilePermissions.defaultsForDirectory`, i.e. 0755. When the template
points into a shared directory such as `/tmp`, other local users can
list the temporary directory and read anything written into it with the
default 0644 file permissions. `mkdtemp(3)`, Go's `os.MkdirTemp` and
Python's `tempfile.mkdtemp` all use 0700 for this reason.
Repro with a umask of 022:
```swift
let dir = try await FileSystem.shared.createTemporaryDirectory(template: "/tmp/nio-repro-XXXXXX")
try await FileSystem.shared.withFileHandle(forWritingAt: "\(dir)/secret", options: .newFile(replaceExisting: false)) { _ in }
// before: dir is drwxr-xr-x, secret is -rw-r--r--, readable by every local user
// after: dir is drwx------
```
### Modifications:
- Pass `.ownerReadWriteExecute` when `_createTemporaryDirectory` creates
the directory, in both `NIOFS` and `_NIOFileSystem`. Intermediary
directories created from the template get the same permissions, since
they are created by the same `_createDirectory` call.
- Document the permissions on `createTemporaryDirectory(template:)`.
- Add `testCreateTemporaryDirectoryIsOnlyAccessibleByOwner`.
### Result:
Temporary directories are only accessible by their owner.
I verified the before/after modes for both modules with a small
executable against this branch on macOS (`swift build` is clean). I
couldn't run the XCTest target locally because this machine only has the
Command Line Tools.
---------
Co-authored-by: Cory Benfield <lukasa@apple.com>