Repository navigation
ext/session: Truncate session file after successful write #23653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+123
−5
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| --TEST-- | ||
| Session files handler truncates repeated writes to the current data length | ||
| --EXTENSIONS-- | ||
| session | ||
| --INI-- | ||
| session.save_handler=files | ||
| session.save_path={PWD} | ||
| session.use_cookies=0 | ||
| session.cache_limiter= | ||
| session.gc_probability=0 | ||
| --FILE-- | ||
| <?php | ||
| $id = 'session-files-repeated-write'; | ||
| $file = __DIR__ . '/sess_' . $id; | ||
| $handler = new SessionHandler(); | ||
| session_set_save_handler($handler); | ||
| $values = ['longer data', 'short', 'SHORT', 'much longer data', '']; | ||
|
|
||
| for ($count = 1; $count <= count($values); $count++) { | ||
| file_put_contents($file, ''); | ||
| session_id($id); | ||
| session_start(); | ||
| foreach (array_slice($values, 0, $count) as $value) { | ||
| var_dump($handler->write($id, $value)); | ||
| } | ||
| session_abort(); | ||
| var_dump(file_get_contents($file)); | ||
| } | ||
| ?> | ||
| --CLEAN-- | ||
| <?php | ||
| unlink(__DIR__ . '/sess_session-files-repeated-write'); | ||
| ?> | ||
| --EXPECT-- | ||
| bool(true) | ||
| string(11) "longer data" | ||
| bool(true) | ||
| bool(true) | ||
| string(5) "short" | ||
| bool(true) | ||
| bool(true) | ||
| bool(true) | ||
| string(5) "SHORT" | ||
| bool(true) | ||
| bool(true) | ||
| bool(true) | ||
| bool(true) | ||
| string(16) "much longer data" | ||
| bool(true) | ||
| bool(true) | ||
| bool(true) | ||
| bool(true) | ||
| bool(true) | ||
| string(0) "" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| --TEST-- | ||
| Session files handler must not truncate session file when write fails | ||
| --EXTENSIONS-- | ||
| session | ||
| posix | ||
| pcntl | ||
| --INI-- | ||
| error_reporting=E_ALL | ||
| display_errors=1 | ||
| session.use_strict_mode=0 | ||
| session.save_handler=files | ||
| session.serialize_handler=php | ||
| --FILE-- | ||
| <?php | ||
| $dir = __DIR__ . '/session_write_failure_keeps_data'; | ||
| mkdir($dir); | ||
| ini_set('session.save_path', $dir); | ||
|
|
||
| $sid = 'write-failure'; | ||
| $file = $dir . '/sess_' . $sid; | ||
|
|
||
| ob_start(); | ||
|
|
||
| session_id($sid); | ||
| session_start(); | ||
| $_SESSION['data'] = 'original data'; | ||
| session_write_close(); | ||
|
|
||
| $before = file_get_contents($file); | ||
| echo "Original contents: "; | ||
| var_dump($before); | ||
|
|
||
| // A zero file size limit makes the next write fail before any byte lands; | ||
| // SIGXFSZ has to be ignored or the process is killed instead. | ||
| pcntl_signal(SIGXFSZ, SIG_IGN); | ||
| echo "Set file size limit to zero: "; | ||
| var_dump(posix_setrlimit(POSIX_RLIMIT_FSIZE, 0, POSIX_RLIMIT_INFINITY)); | ||
|
|
||
| session_start(); | ||
| $_SESSION['data'] = 'new'; | ||
| @session_write_close(); | ||
|
|
||
| echo "Contents unchanged after failed write: "; | ||
| var_dump(file_get_contents($file) === $before); | ||
|
|
||
| ob_end_flush(); | ||
| ?> | ||
| --CLEAN-- | ||
| <?php | ||
| $dir = __DIR__ . '/session_write_failure_keeps_data'; | ||
| foreach (glob($dir . '/sess_*') as $f) { | ||
| unlink($f); | ||
| } | ||
| rmdir($dir); | ||
| ?> | ||
| --EXPECT-- | ||
| Original contents: string(26) "data|s:13:"original data";" | ||
| Set file size limit to zero: bool(true) | ||
| Contents unchanged after failed write: bool(true) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.