Improve updates integrity checks - #924
Open
giuscris wants to merge 3 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the Formwork self-update flow by adding integrity checks for downloaded update archives, improving download atomicity via temporary files, and introducing a lock file to reduce the risk of concurrent update runs.
Changes:
- Add optional SHA256 checksum handling for release assets and verify downloaded archives before extraction.
- Download to a temporary file and move into place only after a complete transfer.
- Add an update lock file and new
updates.lockTimeoutconfiguration.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
formwork/src/Updater/Updater.php |
Adds checksum parsing/verification, refactors extraction into helpers, and introduces an update lock mechanism. |
formwork/src/Http/Client.php |
Makes downloads write to a temp file first, validates byte counts, then renames into place. |
formwork/config/system.yaml |
Adds updates.lockTimeout configuration for stale-lock handling. |
馃挕 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+248
to
+251
| $root = ROOT_PATH; | ||
| $destination = FileSystem::joinPaths($root, $filename); | ||
| $destinationDirectory = dirname($destination); | ||
|
|
Comment on lines
+126
to
130
| $temporaryFile = FileSystem::createTemporaryFile(dirname($file)); | ||
|
|
||
| if (($destination = @fopen($temporaryFile, 'w')) === false) { | ||
| throw new RuntimeException(sprintf('Cannot open temporary file "%s" for writing', $temporaryFile)); | ||
| } |
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
Suppressed comments (2)
formwork/src/Updater/Updater.php:292
- The lock acquisition is not actually exclusive: the existence check and subsequent write are not atomic, so two concurrent processes can both pass the check and both write the lock file, resulting in concurrent updates despite the lock. Consider creating the lock file atomically (e.g.,
FileSystem::createFile()usingx+) and only reusing it when it is stale, to guarantee single-writer behavior.
if (FileSystem::exists($lockFile) && (time() - FileSystem::lastModifiedTime($lockFile)) < $this->options['lockTimeout']) {
throw new RuntimeException('An update is already in progress');
}
FileSystem::write($lockFile, (string) time());
formwork/src/Http/Client.php:143
rename($temporaryFile, $file)is executed while$destinationis still open. On some platforms (notably Windows) renaming an open file can fail, causing downloads to error even though the copy succeeded. Close the stream (and ideally flush it) before attempting the rename; also delete the created temp file iffopen()fails aftercreateTemporaryFile()succeeded.
if (!@rename($temporaryFile, $file)) {
throw new RuntimeException(sprintf('Cannot move temporary file "%s" to destination "%s"', $temporaryFile, $file));
}
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This pull request significantly improves the reliability and security of the update process. The most important changes are the addition of checksum verification for downloaded update archives, better handling of temporary files during downloads, and the introduction of a locking mechanism to prevent concurrent updates. These changes help ensure that updates are applied safely and only once, and that downloaded files are not corrupted or tampered with.
Update Process Security and Integrity:
Concurrency and Reliability Improvements:
Data Structure and Metadata Enhancements:
checksumfield, updating all relevant type annotations and logic to handle this new field. [1] [2]