PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.8.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.8.2
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / FileIntegrity.php
matomo / app / core Last commit date
API 3 months ago Access 3 months ago Application 3 months ago Archive 3 months ago ArchiveProcessor 3 months ago Archiver 2 years ago AssetManager 3 months ago Auth 6 months ago Category 6 months ago Changes 3 months ago CliMulti 1 year ago Columns 3 months ago Concurrency 3 months ago Config 3 months ago Container 3 months ago CronArchive 3 months ago DataAccess 3 months ago DataFiles 2 years ago DataTable 3 months ago Db 3 months ago DeviceDetector 1 year ago Email 2 years ago Exception 4 months ago Http 4 months ago Intl 3 months ago Log 2 years ago Mail 1 year ago Measurable 6 months ago Menu 3 months ago Metrics 3 months ago Notification 6 months ago Period 3 months ago Plugin 3 months ago Policy 3 months ago ProfessionalServices 1 year ago Report 1 year ago ReportRenderer 3 months ago Request 3 months ago Scheduler 3 months ago Segment 3 months ago Session 3 months ago Settings 3 months ago Tracker 3 months ago Translation 3 months ago Twig 1 year ago UpdateCheck 3 months ago Updater 4 months ago Updates 3 months ago Validators 1 year ago View 6 months ago ViewDataTable 3 months ago Visualization 1 year ago Widget 3 months ago .htaccess 2 years ago Access.php 3 months ago Archive.php 3 months ago ArchiveProcessor.php 4 months ago AssetManager.php 3 months ago Auth.php 6 months ago AuthResult.php 6 months ago BaseFactory.php 2 years ago Cache.php 2 years ago CacheId.php 4 months ago CliMulti.php 3 months ago Common.php 3 months ago Config.php 3 months ago Console.php 3 months ago Context.php 2 years ago Cookie.php 1 year ago CronArchive.php 3 months ago DI.php 3 months ago DataArray.php 5 months ago DataTable.php 3 months ago Date.php 3 months ago Db.php 3 months ago DbHelper.php 3 months ago Development.php 1 year ago ErrorHandler.php 6 months ago EventDispatcher.php 1 year ago ExceptionHandler.php 4 months ago FileIntegrity.php 3 months ago Filechecks.php 1 year ago Filesystem.php 3 months ago FrontController.php 4 months ago Http.php 4 months ago IP.php 1 year ago Log.php 3 months ago LogDeleter.php 1 year ago Mail.php 1 year ago Metrics.php 3 months ago NoAccessException.php 2 years ago Nonce.php 6 months ago Notification.php 6 months ago NumberFormatter.php 5 months ago Option.php 5 months ago Period.php 3 months ago Piwik.php 3 months ago Plugin.php 3 months ago Process.php 1 year ago Profiler.php 6 months ago ProxyHeaders.php 4 months ago ProxyHttp.php 5 months ago QuickForm2.php 3 months ago RankingQuery.php 5 months ago ReportRenderer.php 3 months ago Request.php 3 months ago Segment.php 3 months ago Sequence.php 6 months ago Session.php 3 months ago SettingsPiwik.php 3 months ago SettingsServer.php 1 year ago Singleton.php 2 years ago Site.php 4 months ago SiteContentDetector.php 3 months ago SupportedBrowser.php 2 years ago TCPDF.php 1 year ago Theme.php 1 year ago Timer.php 2 years ago Tracker.php 3 months ago Twig.php 3 months ago Unzip.php 1 year ago UpdateCheck.php 3 months ago Updater.php 3 months ago UpdaterErrorException.php 2 years ago Updates.php 3 months ago Url.php 3 months ago UrlHelper.php 3 months ago Version.php 3 months ago View.php 3 months ago bootstrap.php 1 year ago dispatch.php 2 years ago testMinimumPhpVersion.php 6 months ago
FileIntegrity.php
378 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 */
9 namespace Piwik;
10
11 use Piwik\Container\StaticContainer;
12 use Piwik\Plugins\CustomJsTracker\Exception\AccessDeniedException;
13 use Piwik\Plugins\CustomJsTracker\TrackerUpdater;
14 class FileIntegrity
15 {
16 /**
17 * Get file integrity information
18 *
19 * @return array(bool $success, array $messages)
20 */
21 public static function getFileIntegrityInformation()
22 {
23 $messages = array();
24 self::loadManifest();
25 if (!class_exists('Piwik\\Manifest')) {
26 $messages[] = \Piwik\Piwik::translate('General_WarningFileIntegrityNoManifest') . '<br/>' . \Piwik\Piwik::translate('General_WarningFileIntegrityNoManifestDeployingFromGit');
27 return array($success = \false, $messages);
28 }
29 $messages = self::getMessagesDirectoriesFoundButNotExpected($messages);
30 $messages = self::getMessagesFilesFoundButNotExpected($messages);
31 $messages = self::getMessagesFilesMismatch($messages);
32 return array($success = empty($messages), $messages);
33 }
34 /**
35 * Return just a list of the unexpected files
36 *
37 * @return array
38 */
39 public static function getUnexpectedFilesList() : array
40 {
41 self::loadManifest();
42 $files = self::getFilesFoundButNotExpected();
43 return $files;
44 }
45 /**
46 * Include the manifest
47 *
48 */
49 private static function loadManifest() : void
50 {
51 $manifest = PIWIK_INCLUDE_PATH . '/config/manifest.inc.php';
52 if (file_exists($manifest)) {
53 require_once $manifest;
54 }
55 }
56 protected static function getFilesNotInManifestButExpectedAnyway()
57 {
58 return StaticContainer::get('fileintegrity.ignore');
59 }
60 protected static function getMessagesDirectoriesFoundButNotExpected($messages)
61 {
62 $directoriesFoundButNotExpected = self::getDirectoriesFoundButNotExpected();
63 if (count($directoriesFoundButNotExpected) > 0) {
64 $messageDirectoriesToDelete = '';
65 foreach ($directoriesFoundButNotExpected as $directoryFoundNotExpected) {
66 $messageDirectoriesToDelete .= \Piwik\Piwik::translate('General_ExceptionDirectoryToDelete', htmlspecialchars($directoryFoundNotExpected)) . '<br/>';
67 }
68 $directories = array();
69 foreach ($directoriesFoundButNotExpected as $directoryFoundNotExpected) {
70 $directories[] = htmlspecialchars(realpath(dirname($directoryFoundNotExpected)) . \DIRECTORY_SEPARATOR . basename($directoryFoundNotExpected));
71 }
72 $deleteAllAtOnce = array();
73 $chunks = array_chunk($directories, 50);
74 $command = 'rm -Rf';
75 if (\Piwik\SettingsServer::isWindows()) {
76 $command = 'rmdir /s /q';
77 }
78 foreach ($chunks as $directories) {
79 $deleteAllAtOnce[] = sprintf('%s %s', $command, implode(' ', $directories));
80 }
81 $messages[] = \Piwik\Piwik::translate('General_ExceptionUnexpectedDirectory') . '<br/>' . '--> ' . \Piwik\Piwik::translate('General_ExceptionUnexpectedDirectoryPleaseDelete') . ' <--' . '<br/><br/>' . $messageDirectoriesToDelete . '<br/><br/>' . \Piwik\Piwik::translate('General_ToDeleteAllDirectoriesRunThisCommand') . '<br/>' . implode('<br />', $deleteAllAtOnce) . '<br/><br/>';
82 }
83 return $messages;
84 }
85 /**
86 * @param $messages
87 * @return array
88 */
89 protected static function getMessagesFilesFoundButNotExpected($messages)
90 {
91 $filesFoundButNotExpected = self::getFilesFoundButNotExpected();
92 if (count($filesFoundButNotExpected) > 0) {
93 $messageFilesToDelete = '';
94 foreach ($filesFoundButNotExpected as $fileFoundNotExpected) {
95 $messageFilesToDelete .= \Piwik\Piwik::translate('General_ExceptionFileToDelete', htmlspecialchars($fileFoundNotExpected)) . '<br/>';
96 }
97 $files = array();
98 foreach ($filesFoundButNotExpected as $fileFoundNotExpected) {
99 $files[] = '"' . htmlspecialchars(realpath(dirname($fileFoundNotExpected)) . \DIRECTORY_SEPARATOR . basename($fileFoundNotExpected)) . '"';
100 }
101 $deleteAllAtOnce = array();
102 $chunks = array_chunk($files, 50);
103 $command = 'rm';
104 if (\Piwik\SettingsServer::isWindows()) {
105 $command = 'del';
106 }
107 foreach ($chunks as $files) {
108 $deleteAllAtOnce[] = sprintf('%s %s', $command, implode(' ', $files));
109 }
110 $messages[] = \Piwik\Piwik::translate('General_ExceptionUnexpectedFile') . '<br/>' . '--> ' . \Piwik\Piwik::translate('General_ExceptionUnexpectedFilePleaseDelete') . ' <--' . '<br/><br/>' . $messageFilesToDelete . '<br/><br/>' . \Piwik\Piwik::translate('General_ToDeleteAllFilesRunThisCommand') . '<br/>' . implode('<br />', $deleteAllAtOnce) . '<br/><br/>';
111 return $messages;
112 }
113 return $messages;
114 }
115 /**
116 * Look for whole directories which are in the filesystem, but should not be
117 *
118 * @return array
119 */
120 protected static function getDirectoriesFoundButNotExpected()
121 {
122 static $cache = null;
123 if (!is_null($cache)) {
124 return $cache;
125 }
126 $pluginsInManifest = self::getPluginsFoundInManifest();
127 $directoriesInManifest = self::getDirectoriesFoundInManifest();
128 $directoriesFoundButNotExpected = array();
129 foreach (self::getPathsToInvestigate() as $file) {
130 $file = substr($file, strlen(PIWIK_DOCUMENT_ROOT));
131 // remove piwik path to match format in manifest.inc.php
132 $file = ltrim($file, "\\/");
133 $directory = dirname($file);
134 if (in_array($directory, $directoriesInManifest)) {
135 continue;
136 }
137 if (self::isFileNotInManifestButExpectedAnyway($file)) {
138 continue;
139 }
140 if (self::isFileFromPluginNotInManifest($file, $pluginsInManifest)) {
141 continue;
142 }
143 if (!in_array($directory, $directoriesFoundButNotExpected)) {
144 $directoriesFoundButNotExpected[] = $directory;
145 }
146 }
147 $cache = self::getParentDirectoriesFromListOfDirectories($directoriesFoundButNotExpected);
148 return $cache;
149 }
150 /**
151 * Look for files which are in the filesystem, but should not be
152 *
153 * @return array
154 */
155 protected static function getFilesFoundButNotExpected()
156 {
157 $files = \Piwik\Manifest::$files;
158 $pluginsInManifest = self::getPluginsFoundInManifest();
159 $filesFoundButNotExpected = array();
160 foreach (self::getPathsToInvestigate() as $file) {
161 if (is_dir($file)) {
162 continue;
163 }
164 $file = substr($file, strlen(PIWIK_DOCUMENT_ROOT));
165 // remove piwik path to match format in manifest.inc.php
166 $file = ltrim($file, "\\/");
167 if (self::isFileFromPluginNotInManifest($file, $pluginsInManifest)) {
168 continue;
169 }
170 if (self::isFileNotInManifestButExpectedAnyway($file)) {
171 continue;
172 }
173 if (self::isFileFromDirectoryThatShouldBeDeleted($file)) {
174 // we already report the directory as "Directory to delete" so no need to repeat the instruction for each file
175 continue;
176 }
177 if (!isset($files[$file])) {
178 $filesFoundButNotExpected[] = $file;
179 }
180 }
181 return $filesFoundButNotExpected;
182 }
183 protected static function isFileFromDirectoryThatShouldBeDeleted($file)
184 {
185 $directoriesWillBeDeleted = self::getDirectoriesFoundButNotExpected();
186 foreach ($directoriesWillBeDeleted as $directoryWillBeDeleted) {
187 if (strpos($file, $directoryWillBeDeleted) === 0) {
188 return \true;
189 }
190 }
191 return \false;
192 }
193 protected static function getDirectoriesFoundInManifest()
194 {
195 $files = \Piwik\Manifest::$files;
196 $directories = array();
197 foreach ($files as $file => $manifestIntegrityInfo) {
198 $directory = $file;
199 // add this directory and each parent directory
200 while (($directory = dirname($directory)) && $directory != '.' && $directory != '/') {
201 $directories[] = $directory;
202 }
203 }
204 $directories = array_unique($directories);
205 return $directories;
206 }
207 protected static function getPluginsFoundInManifest()
208 {
209 $files = \Piwik\Manifest::$files;
210 $pluginsInManifest = array();
211 foreach ($files as $file => $manifestIntegrityInfo) {
212 if (strpos($file, 'plugins/') === 0) {
213 $pluginName = self::getPluginNameFromFilepath($file);
214 $pluginsInManifest[] = $pluginName;
215 }
216 }
217 return $pluginsInManifest;
218 }
219 /**
220 * If a plugin folder is not tracked in the manifest then we don't try to report any files in this folder
221 * Could be a third party plugin or any plugin from the Marketplace
222 *
223 * @param $file
224 * @param $pluginsInManifest
225 * @return bool
226 */
227 protected static function isFileFromPluginNotInManifest($file, $pluginsInManifest)
228 {
229 if (strpos($file, 'plugins/') !== 0) {
230 return \false;
231 }
232 if (substr_count($file, '/') < 2) {
233 // must be a file plugins/abc.xyz and not a plugin directory
234 return \false;
235 }
236 $pluginName = self::getPluginNameFromFilepath($file);
237 if (in_array($pluginName, $pluginsInManifest)) {
238 return \false;
239 }
240 return \true;
241 }
242 protected static function isFileNotInManifestButExpectedAnyway($file)
243 {
244 $expected = self::getFilesNotInManifestButExpectedAnyway();
245 foreach ($expected as $expectedPattern) {
246 if (fnmatch($expectedPattern, $file, defined('FNM_CASEFOLD') ? \FNM_CASEFOLD : 0)) {
247 return \true;
248 }
249 }
250 return \false;
251 }
252 protected static function getMessagesFilesMismatch($messages)
253 {
254 $messagesMismatch = array();
255 $hasHashFile = function_exists('hash_file');
256 $files = \Piwik\Manifest::$files;
257 foreach ($files as $path => $props) {
258 $file = PIWIK_INCLUDE_PATH . '/' . $path;
259 if (!file_exists($file) || !is_readable($file)) {
260 $messagesMismatch[] = \Piwik\Piwik::translate('General_ExceptionMissingFile', $file);
261 } elseif (filesize($file) != $props[0]) {
262 if (self::isModifiedPathValid($path)) {
263 continue;
264 }
265 if (in_array(substr($path, -4), array('.gif', '.ico', '.jpg', '.png', '.swf'))) {
266 // files that contain binary data (e.g., images) must match the file size
267 $messagesMismatch[] = \Piwik\Piwik::translate('General_ExceptionFilesizeMismatch', array($file, $props[0], filesize($file)));
268 } else {
269 // convert end-of-line characters and re-test text files
270 $content = @file_get_contents($file);
271 $content = str_replace("\r\n", "\n", $content);
272 if (strlen($content) != $props[0] || @hash('sha256', $content) !== $props[1]) {
273 $messagesMismatch[] = \Piwik\Piwik::translate('General_ExceptionFilesizeMismatch', array($file, $props[0], filesize($file)));
274 }
275 }
276 } elseif ($hasHashFile && @hash_file('sha256', $file) !== $props[1]) {
277 if (self::isModifiedPathValid($path)) {
278 continue;
279 }
280 $messagesMismatch[] = \Piwik\Piwik::translate('General_ExceptionFileIntegrity', $file);
281 }
282 }
283 if (!$hasHashFile) {
284 $messages[] = \Piwik\Piwik::translate('General_WarningFileIntegrityNoHashFile');
285 }
286 if (!empty($messagesMismatch)) {
287 $messages[] = \Piwik\Piwik::translate('General_FileIntegrityWarningReupload');
288 $messages[] = '--> ' . \Piwik\Piwik::translate('General_FileIntegrityWarningReuploadBis') . ' <--<br/>';
289 $messages = array_merge($messages, $messagesMismatch);
290 }
291 return $messages;
292 }
293 protected static function isModifiedPathValid($path)
294 {
295 if ($path === 'piwik.js' || $path === 'matomo.js') {
296 // we could have used a postEvent hook to enrich "\Piwik\Manifest::$files;" which would also benefit plugins
297 // that want to check for file integrity but we do not want to risk to break anything right now. It is not
298 // as trivial because piwik.js might be already updated, or updated on the next request. We cannot define
299 // 2 or 3 different filesizes and md5 hashes for one file so we check it here.
300 if (\Piwik\Plugin\Manager::getInstance()->isPluginActivated('CustomJsTracker')) {
301 $trackerUpdater = new TrackerUpdater();
302 if ($trackerUpdater->getCurrentTrackerFileContent() === $trackerUpdater->getUpdatedTrackerFileContent()) {
303 // file was already updated, eg manually or via custom piwik.js, this is a valid piwik.js file as
304 // it was enriched by tracker plugins
305 return \true;
306 }
307 try {
308 // the piwik.js tracker file was not updated yet, but may be updated just after the update by
309 // one of the events CustomJsTracker is listening to or by a scheduled task.
310 // In this case, we check whether such an update will succeed later and if it will, the file is
311 // valid as well as it will be updated on the next request
312 $trackerUpdater->checkWillSucceed();
313 return \true;
314 } catch (AccessDeniedException $e) {
315 return \false;
316 }
317 }
318 }
319 return \false;
320 }
321 protected static function getPluginNameFromFilepath($file)
322 {
323 $pathRelativeToPlugins = substr($file, strlen('plugins/'));
324 $pluginName = substr($pathRelativeToPlugins, 0, strpos($pathRelativeToPlugins, '/'));
325 return $pluginName;
326 }
327 /**
328 * @return array
329 */
330 protected static function getPathsToInvestigate()
331 {
332 $filesToInvestigate = array_merge(
333 // all normal files
334 \Piwik\Filesystem::globr(PIWIK_DOCUMENT_ROOT, '*'),
335 // all hidden files
336 \Piwik\Filesystem::globr(PIWIK_DOCUMENT_ROOT, '.*')
337 );
338 return $filesToInvestigate;
339 }
340 /**
341 * @param $directoriesFoundButNotExpected
342 * @return array
343 */
344 protected static function getParentDirectoriesFromListOfDirectories($directoriesFoundButNotExpected)
345 {
346 sort($directoriesFoundButNotExpected);
347 $parentDirectoriesOnly = array();
348 foreach ($directoriesFoundButNotExpected as $directory) {
349 $directoryParent = self::getDirectoryParentFromList($directory, $directoriesFoundButNotExpected);
350 if ($directoryParent) {
351 $parentDirectoriesOnly[] = $directoryParent;
352 }
353 }
354 $parentDirectoriesOnly = array_unique($parentDirectoriesOnly);
355 return $parentDirectoriesOnly;
356 }
357 /**
358 * When the parent directory of $directory is found within $directories, return it.
359 *
360 * @param $directory
361 * @param $directories
362 * @return string
363 */
364 protected static function getDirectoryParentFromList($directory, $directories)
365 {
366 foreach ($directories as $directoryMaybeParent) {
367 if ($directory == $directoryMaybeParent) {
368 continue;
369 }
370 $isParentDirectory = strpos($directory, $directoryMaybeParent) === 0;
371 if ($isParentDirectory) {
372 return $directoryMaybeParent;
373 }
374 }
375 return null;
376 }
377 }
378