| Sylius | >= 1.12.0, <= 1.12.24 (semver) >= 1.13.0, <= 1.13.16 (semver) >= 1.14.0, <= 1.14.19 (semver) >= 2.0.0, <= 2.1.15 (semver) >= 2.2.0, <= 2.2.8 (semver) |
CPE- cpe:2.3:a:sylius:sylius:*:*:*:*:*:*:*:*
Remediation- Upgrade: 1.12.25moderate effort
- Upgrade: 1.13.17moderate effort
- Upgrade: 1.14.20moderate effort
- Upgrade: 2.1.16moderate effort
- Upgrade: 2.2.9moderate effort
- Workaround:low effort
Configure the trusted hosts in the framework:
```yaml
# config/packages/framework.yaml
framework:
trusted_hosts: ['^(www\.)?your-shop\.example$', '^admin\.your-shop\.example$']
```
Alternatively, configure the front-end web server to reject requests whose Host does not match a known virtual host. - Workaround:moderate effort
Override the mailer to generate a secure reset URL:
1. Create `src/Mailer/ChannelHostAdminResetPasswordEmailManager.php`:
```php
<?php
declare(strict_types=1);
namespace App\Mailer;
use Sylius\Bundle\CoreBundle\Mailer\Emails;
use Sylius\Bundle\CoreBundle\Mailer\ResetPasswordEmailManagerInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Channel\Context\ChannelNotFoundException;
use Sylius\Component\Channel\Model\ChannelInterface;
use Sylius\Component\Mailer\Sender\SenderInterface;
use Sylius\Component\User\Model\UserInterface;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\RouterInterface;
final class ChannelHostAdminResetPasswordEmailManager implements ResetPasswordEmailManagerInterface
{
public function __construct(
private ResetPasswordEmailManagerInterface $decoratedResetPasswordEmailManager,
private SenderInterface $emailSender,
private RouterInterface $router,
private ChannelContextInterface $channelContext,
private bool $unsecuredUrls = false,
) {
}
public function sendAdminResetPasswordEmail(UserInterface $user, string $localCode): void
{
$this->emailSender->send(
code: Emails::ADMIN_PASSWORD_RESET,
recipients: [$user->getEmail()],
data: [
'adminUser' => $user,
'localeCode' => $localCode,
'resetUrl' => $this->generateAdminResetPasswordUrl($user),
],
);
}
public function sendResetPasswordEmail(UserInterface $user, ChannelInterface $channel, string $localCode): void
{
$this->decoratedResetPasswordEmailManager->sendResetPasswordEmail($user, $channel, $localCode);
}
private function generateAdminResetPasswordUrl(UserInterface $user): ?string
{
try {
$hostname = $this->channelContext->getChannel()->getHostname();
} catch (ChannelNotFoundException) {
return null;
}
if (null === $hostname) {
return null;
}
try {
$path = $this->router->generate(
'sylius_admin_render_password_reset',
['token' => (string) $user->getPasswordResetToken()],
);
} catch (RouteNotFoundException) {
return null;
}
return ($this->unsecuredUrls ? 'http://' : 'https://') . $hostname . $path;
}
}
```
2. On 1.12, the administrator e-mail is sent by `Sylius\Bundle\CoreBundle\MessageHandler\Admin\Account\SendResetPasswordEmailHandler` rather than by the mailer, so decorate that handler instead, keeping the `generateAdminResetPasswordUrl()` body identical. On 1.13, 1.14, 2.1 and 2.2 the handler delegates to the mailer, so decorating the mailer covers both the form and the API endpoint.
3. Override `templates/bundles/SyliusCoreBundle/Email/adminPasswordReset.html.twig`:
Copy the core template and replace both `sylius_bundle_loaded_checker('SyliusAdminBundle')` conditions with `resetUrl is not null`, and the `url(...)` link with `resetUrl`:
```html
{% block content %}
<div style="text-align: center; margin-bottom: 30px;">
<div style="font-size: 24px;">
{{ 'sylius.email.admin_password_reset.hello'|trans({}, null, localeCode) }} <strong>{{ adminUser.firstName }} {{ adminUser.lastName }}</strong><br>
</div>
{% if resetUrl is not null %}
{{ 'sylius.email.password_reset.to_reset_your_password'|trans({}, null, localeCode) }}:
{% else %}
{{ 'sylius.email.admin_password_reset.to_reset_your_password_token'|trans({}, null, localeCode) }}:
{% endif %}
</div>
<div style="text-align: center;">
{% if resetUrl is not null %}
<a href="{{ resetUrl }}" style="display: inline-block; text-align: center; background: #1abb9c; padding: 18px 28px; color: #fff; text-decoration: none; border-radius: 3px;">
{{ 'sylius.email.admin_password_reset.reset_your_password'|trans({}, null, localeCode) }}
</a>
{% else %}
{{ 'sylius.email.admin_password_reset.token'|trans({}, null, localeCode) }}: {{ adminUser.passwordResetToken }}
{% endif %}
</div>
{% endblock %}
```
4. Register the decorator in `config/services.yaml`:
```diff
services:
+
+ App\Mailer\ChannelHostAdminResetPasswordEmailManager:
+ decorates: sylius.mailer.reset_password_email_manager
+ arguments:
+ $decoratedResetPasswordEmailManager: '@.inner'
+ $emailSender: '@sylius.email_sender'
+ $router: '@router'
+ $channelContext: '@sylius.context.channel'
+ $unsecuredUrls: '%sylius.unsecured_urls%'
```
5. Clear the cache:
```shell
bin/console cache:clear
```
|