Skip to content

Commit 2712927

Browse files
tinectshyim
andauthored
fix: determine recent security plugin version from unofficial shopware api (#374)
Co-authored-by: Shyim <[email protected]>
1 parent f849cef commit 2712927

File tree

1 file changed

+46
-17
lines changed

1 file changed

+46
-17
lines changed

src/Components/Health/Checker/HealthChecker/SwagSecurityChecker.php

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,44 @@ private function hasSecurityAdvisories(): bool
7878
});
7979
}
8080

81-
private function swagSecurityInstalled(): bool
81+
private function swagSecurityUpdateVersion(): ?string
8282
{
83-
$result = $this->connection->executeQuery(
84-
'SELECT COUNT(*) FROM plugin WHERE active = 1 AND installed_at IS NOT NULL AND upgrade_version IS NULL AND name = :pluginName',
85-
['pluginName' => 'SwagPlatformSecurity'],
83+
try {
84+
$cacheKey = \sprintf('recent-security-plugin-version-%s', $this->shopwareVersion);
85+
86+
$recentVersion = $this->cacheObject->get($cacheKey, function (ItemInterface $cacheItem) {
87+
$result = $this->httpClient->request('GET', \sprintf('https://api.shopware.com/pluginStore/pluginsByName?shopwareVersion=%s&technicalNames[]=SwagPlatformSecurity', $this->shopwareVersion))->getContent();
88+
89+
$data = \json_decode(trim($result), true, 512, \JSON_THROW_ON_ERROR);
90+
91+
if (!\is_array($data)) {
92+
throw new \RuntimeException('result is not decodeable');
93+
}
94+
95+
$recentVersion = $data[0]['version'] ?? null;
96+
97+
if ($recentVersion === null) {
98+
throw new \RuntimeException('could not determine recent version of SwagPlatformSecurity');
99+
}
100+
101+
$cacheItem->expiresAfter(3600 * 24);
102+
103+
return $recentVersion;
104+
});
105+
} catch (\Throwable $e) {
106+
throw new \RuntimeException(\sprintf('Could not fetch https://api.shopware.com/pluginStore/pluginsByName: %s', $e->getMessage()));
107+
}
108+
109+
$installedVersion = $this->connection->executeQuery(
110+
'SELECT version FROM plugin WHERE active = 1 AND installed_at IS NOT NULL AND name = :pluginName',
111+
['pluginName' => 'SwagPlatformSecurity', 'recentVersion' => $recentVersion],
86112
)->fetchOne();
87113

88-
return !empty($result);
114+
if (empty($installedVersion) || version_compare($installedVersion, $recentVersion, '<')) {
115+
return $recentVersion;
116+
}
117+
118+
return null;
89119
}
90120

91121
private function determineSecurityIssue(HealthCollection $collection): void
@@ -106,19 +136,18 @@ private function determineSecurityIssue(HealthCollection $collection): void
106136
);
107137
}
108138

109-
if ($this->swagSecurityInstalled()) {
110-
return;
139+
$securityUpdateVersion = $this->swagSecurityUpdateVersion();
140+
if ($securityUpdateVersion !== null) {
141+
$collection->add(
142+
SettingsResult::error(
143+
'security-update',
144+
'Security update',
145+
'Shopware outdated',
146+
\sprintf('Update Shopware to the latest version or install recent version of the plugin SwagPlatformSecurity %s', $securityUpdateVersion),
147+
'https://store.shopware.com/en/swag136939272659f/shopware-6-security-plugin.html',
148+
),
149+
);
111150
}
112-
113-
$collection->add(
114-
SettingsResult::error(
115-
'security-update',
116-
'Security update',
117-
'Shopware outdated',
118-
'Update Shopware to the latest version or install recent version of the plugin SwagPlatformSecurity',
119-
'https://store.shopware.com/en/swag136939272659f/shopware-6-security-plugin.html',
120-
),
121-
);
122151
}
123152

124153
private function determineEolSupport(HealthCollection $collection): void

0 commit comments

Comments
 (0)