Skip to content

Commit

Permalink
Prevent EmbedParser to follow infinite redirect for Twitter links
Browse files Browse the repository at this point in the history
fix according to oscarotero/Embed#520 (comment)

remp/helpdesk#2594
  • Loading branch information
miroc committed Mar 28, 2024
1 parent ede45c6 commit 6a5d169
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- Fixed issue with oversize images in MS Outlook. remp/remp#1330
- Fixed issue with persistent embed cookies stored in system `tmp` folder, which were shared across releases. remp/helpdesk#2587
- Each release now stores embed cookies in its own temp folder.
- Added ability to set custom CURL settings for `EmbedParser`. remp/helpdesk#2594

## Archive

Expand Down
7 changes: 6 additions & 1 deletion Mailer/app/Models/Generators/EmbedParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public function setTwitterLinkText(string $twitterLinkText = null): void
$this->twitterLinkText = $twitterLinkText;
}

private function isTwitterLink($link): bool
{
return str_contains($link, 'twitt');
}

public function createEmbedMarkup(string $link, ?string $title = null, ?string $image = null, bool $isVideo = false): string
{
$html = "<br>";
Expand All @@ -20,7 +25,7 @@ public function createEmbedMarkup(string $link, ?string $title = null, ?string $

if (!is_null($image) && !is_null($title)) {
$html .= "<img src='{$image}' alt='{$title}' style='outline:none;text-decoration:none;-ms-interpolation-mode:bicubic;max-width:100%;clear:both;display:inline;width:100%;height:auto;'>";
} elseif (preg_match('/twitt/', $link)) {
} elseif ($this->isTwitterLink($link)) {
return "<br><a style=\"display: block;margin: 0 0 20px;padding: 7px 10px;text-decoration: none;text-align: center;font-weight: bold;font-family:'Helvetica Neue', Helvetica, Arial;color: #249fdc; background: #ffffff; border: 3px solid #249fdc;margin: 16px 0 16px 0\" href=\"{$link}\" target=\"_blank\">{$this->twitterLinkText}</a>";
} else {
$html .= "<span style='text-decoration: underline; color: #1F3F83;'>" . $link . "</span>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,31 @@ class EmbedParser
{
protected ?string $videoLinkText;

public function __construct(private CurlClient $curlClient)
{
}
protected array $curlSettings = [];

public function setVideoLinkText(?string $videoLinkText = null): void
{
$this->videoLinkText = $videoLinkText;
}

public function setCurlSettings(array $settings)
{
$this->curlSettings = $settings;
}

private function fetch(string $url): ?array
{
$embed = new Embed(new Crawler($this->curlClient));
$curlSettings = [
// Twitter may generate infinite redirect (2024/03),
// fix according to https://github.com/oscarotero/Embed/issues/520#issue-1782756560
'follow_location' => !$this->isTwitterLink($url),
...$this->curlSettings,
];

$curlClient = new CurlClient();
$curlClient->setSettings($curlSettings);

$embed = new Embed(new Crawler($curlClient));
$embed = $embed->get($url);

$oEmbed = $embed->getOEmbed();
Expand All @@ -38,17 +51,22 @@ private function fetch(string $url): ?array
'link' => $embed->url->__toString(),
'title' => $embed->title ?? '',
'image' => $image,
'isVideo' => $type === 'video'
'isVideo' => $type === 'video',
];
}

private function isTwitterLink($link)
{
return str_contains($link, 'twitt');
}

public function parse(string $link): ?string
{
$link = trim($link);

if (preg_match('/^(?:(?:https?:)?\/\/)?(?:www\.)?facebook\.com\/[a-zA-Z0-9.]+\/videos\/(?:[a-zA-Z0-9.]+\/)?([0-9]+)/', $link)
|| str_contains($link, 'youtu')
|| str_contains($link, 'twitt')
|| $this->isTwitterLink($link)
) {
try {
if ($data = $this->fetch($link)) {
Expand Down
7 changes: 2 additions & 5 deletions Mailer/extensions/mailer-module/src/config/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,12 @@ services:
- Remp\MailerModule\Models\Generators\WordpressHelpers
embedParser:
factory: Remp\MailerModule\Models\Generators\EmbedParser
setup:
- setCurlSettings([cookies_path: %tempDir%/embed-cookies.txt])
articleLocker:
factory: Remp\MailerModule\Models\Generators\ArticleLocker

# healtCheck - inject temp for storage check
- Remp\MailerModule\Presenters\HealthPresenter(%tempDir%)

- Remp\MailerModule\Components\MailLinkStats\MailLinkStats

embedCurlClient:
factory: Embed\Http\CurlClient
setup:
- setSettings([cookies_path: %tempDir%/embed-cookies.txt])

0 comments on commit 6a5d169

Please sign in to comment.