-
-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace App\HtmlSanitizer; | ||
|
||
use Composer\Pcre\Preg; | ||
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig; | ||
use Symfony\Component\HtmlSanitizer\Visitor\AttributeSanitizer\AttributeSanitizerInterface; | ||
|
||
class ReadmeImageSanitizer implements AttributeSanitizerInterface | ||
{ | ||
public function __construct(private string|null $host, private string $ownerRepo, private string $basePath) | ||
{ | ||
} | ||
|
||
public function getSupportedAttributes(): ?array | ||
{ | ||
return ['src']; | ||
} | ||
|
||
public function getSupportedElements(): ?array | ||
{ | ||
return ['img']; | ||
} | ||
|
||
public function sanitizeAttribute(string $element, string $attribute, string $value, HtmlSanitizerConfig $config): ?string | ||
{ | ||
if (!str_contains($value, '//')) { | ||
return match ($this->host) { | ||
'github.com' => 'https://raw.github.com/'.$this->ownerRepo.'/HEAD/'.$this->basePath.$value, | ||
'gitlab.com' => 'https://gitlab.com/'.$this->ownerRepo.'/-/raw/HEAD/'.$this->basePath.$value, | ||
'bitbucket.org' => 'https://bitbucket.org/'.$this->ownerRepo.'/raw/HEAD/'.$this->basePath.$value, | ||
default => $value, | ||
}; | ||
} | ||
|
||
if (str_starts_with($value, 'https://private-user-images.githubusercontent.com/')) { | ||
return Preg::replace('{^https://private-}', 'https://', $value, 1); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace App\HtmlSanitizer; | ||
|
||
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig; | ||
use Symfony\Component\HtmlSanitizer\Visitor\AttributeSanitizer\AttributeSanitizerInterface; | ||
|
||
class ReadmeLinkSanitizer implements AttributeSanitizerInterface | ||
{ | ||
public function __construct(private string|null $host, private string $ownerRepo, private string $basePath) | ||
{ | ||
} | ||
|
||
public function getSupportedAttributes(): ?array | ||
{ | ||
return ['href', 'target', 'id']; | ||
} | ||
|
||
public function getSupportedElements(): ?array | ||
{ | ||
return ['a']; | ||
} | ||
|
||
/** | ||
* @param 'href'|'target'|'id' $attribute | ||
* @param string $value | ||
*/ | ||
public function sanitizeAttribute(string $element, string $attribute, string $value, HtmlSanitizerConfig $config): ?string | ||
{ | ||
if ($attribute === 'target') { | ||
if ($value !== '') { | ||
return '_blank'; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
if ($attribute === 'id') { | ||
if (!str_starts_with($value, 'user-content-')) { | ||
return 'user-content-'.$value; | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
if (str_starts_with($value, '#') && !str_starts_with($value, '#user-content-')) { | ||
return '#user-content-'.substr($value, 1); | ||
} | ||
|
||
if (str_starts_with($value, 'mailto:')) { | ||
return $value; | ||
} | ||
|
||
if ($this->host === 'github.com' && !str_contains($value, '//')) { | ||
return 'https://github.com/'.$this->ownerRepo.'/blob/HEAD/'.$this->basePath.$value; | ||
} | ||
|
||
if ($this->host === 'gitlab.com' && !str_contains($value, '//')) { | ||
return 'https://gitlab.com/'.$this->ownerRepo.'/-/blob/HEAD/'.$this->basePath.$value; | ||
} | ||
|
||
return $value; | ||
} | ||
} |