Commit cbdf728
committed
bug symfony#65597 [Filesystem] Keep tempnam() files private when a suffix is given (iliaal)
This PR was merged into the 6.4 branch.
Discussion
----------
[Filesystem] Keep tempnam() files private when a suffix is given
| Q | A
| ------------- | ---
| Branch? | 6.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Issues | -
| License | MIT
`Filesystem::tempnam()` has two branches. Without a suffix, and when the directory lives on the plain file scheme, it delegates to PHP's `tempnam()`, which always creates the file with mode `0600` whatever the process umask is. With a suffix, or on any other stream, it builds the name itself and creates the file with `fopen($tmpFile, 'x+')`. That call goes through `open(2)` with mode `0666`, so it honours the umask: under the common `0022` umask the file was created `0644`, and under a `0000` umask it was created `0666`.
The two branches of the same method therefore did not offer the same guarantee, and the private one is the one the component relies on: `dumpFile()` writes the payload into the temporary file before relaxing its mode, so the content is exposed to every local user for as long as the file is wider than `0600`.
This forces the umask to `0077` around the `fopen()` call and restores it right after. The file is private from the moment it exists, so there is no window where another process can open it, and no `chmod()` is needed. Custom stream wrappers benefit too: many do not implement `stream_metadata`, so a `chmod()` on them would have done nothing at all.
`umask()` is the same mechanism the component already uses in `copy()` and `dumpFile()`, and the same pattern as `Console\Input\File\InputFile`.
Reproduction under a `0022` umask:
```php
$fs = new Filesystem();
printf("%o\n", fileperms($fs->tempnam(sys_get_temp_dir(), 'a')) & 0777);
// 600, both before and after
printf("%o\n", fileperms($fs->tempnam(sys_get_temp_dir(), 'b', '.txt')) & 0777);
// 644 before, 600 after
```
Commits
-------
f228697 [Filesystem] Keep tempnam() files private when a suffix is given2 files changed
+27-1Lines changed: 27 additions & 1 deletion
File tree
Expand file treeCollapse file tree
Open diff view settings
Filter options
- src/Symfony/Component/Filesystem
- Tests
Expand file treeCollapse file tree
Open diff view settings
Collapse file
src/Symfony/Component/Filesystem/Filesystem.php
Copy file name to clipboardExpand all lines: src/Symfony/Component/Filesystem/Filesystem.php+9-1Lines changed: 9 additions & 1 deletion
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
650 | 650 | |
651 | 651 | |
652 | 652 | |
653 | | - |
| 653 | + |
| 654 | + |
| 655 | + |
| 656 | + |
| 657 | + |
| 658 | + |
| 659 | + |
| 660 | + |
| 661 | + |
654 | 662 | |
655 | 663 | |
656 | 664 | |
|
Collapse file
src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Copy file name to clipboardExpand all lines: src/Symfony/Component/Filesystem/Tests/FilesystemTest.php+18Lines changed: 18 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
1494 | 1494 | |
1495 | 1495 | |
1496 | 1496 | |
| 1497 | + |
| 1498 | + |
| 1499 | + |
| 1500 | + |
| 1501 | + |
| 1502 | + |
| 1503 | + |
| 1504 | + |
| 1505 | + |
| 1506 | + |
| 1507 | + |
| 1508 | + |
| 1509 | + |
| 1510 | + |
| 1511 | + |
| 1512 | + |
| 1513 | + |
| 1514 | + |
1497 | 1515 | |
1498 | 1516 | |
1499 | 1517 | |
|
0 commit comments