Skip to content

Commit 0b648d4

Browse files
adamkoppedeNamelessCoder
authored andcommitted
[BUGFIX] Fix corrupted JavaScript assets loaded by website users
Website users loaded corrupted JavaScript assets for files generated by EXT:vhs when EXT:vhs was in the middle of re-writing the contents of the requested asset file. This happens because EXT:core `GeneralUtility::writeFile()` isn't implemented in an atomic style causing observers (such as visiting website users) to read incomplete asset file contents. Incomplete JavaScript files likely cause syntax errors when interpreted by the user's user-agent rendering front end sites unable to reach JavaScript-based interactivity. Fix the issue by (non-atomically) writing into a temporary file and replacing the destination asset file with a single atomic `rename()` operation instead. This ensures that observers of the destination asset file either see the complete old contents or the complete new contents but never any intermediate state of the asset content. See: https://man7.org/linux/man-pages/man2/rename.2.html See: https://github.com/TYPO3/typo3/blob/v10.4.37/typo3/sysext/core/Classes/Utility/GeneralUtility.php#L1835-L1861
1 parent 3a4514f commit 0b648d4

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

Classes/Service/AssetService.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,24 @@ protected function writeFile(string $file, string $contents): void
712712
$signalSlotDispatcher->dispatch(__CLASS__, static::ASSET_SIGNAL, [&$file, &$contents]);
713713
*/
714714

715-
GeneralUtility::writeFile($file, $contents, true);
715+
$tmpFile = @tempnam(dirname($file), basename($file));
716+
if ($tmpFile === false) {
717+
$error = error_get_last();
718+
$details = $error !== null ? ": {$error['message']}" : ".";
719+
throw new \RuntimeException(
720+
"Failed to create temporary file for writing asset {$file}{$details}",
721+
1733258066
722+
);
723+
}
724+
GeneralUtility::writeFile($tmpFile, $contents, true);
725+
if (@rename($tmpFile, $file) === false) {
726+
$error = error_get_last();
727+
$details = $error !== null ? ": {$error['message']}" : ".";
728+
throw new \RuntimeException(
729+
"Failed to move asset-backing file {$file} into final destination{$details}",
730+
1733258156
731+
);
732+
}
716733
}
717734

718735
protected function mergeArrays(array $array1, array $array2): array

0 commit comments

Comments
 (0)