Repository navigation
Expand file tree
/
Copy pathGitHubReadmeQuery.php
More file actions
166 lines (138 loc) · 6.53 KB
/
Copy pathGitHubReadmeQuery.php
File metadata and controls
166 lines (138 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
declare(strict_types=1);
namespace App\Queries;
use App\Contracts\Services\GitHubProvider;
use App\Models\User;
use Illuminate\Http\Client\HttpClientException;
use Illuminate\Support\Facades\Cache;
use Symfony\Component\HtmlSanitizer\HtmlSanitizer;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerAction;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig;
final readonly class GitHubReadmeQuery
{
/**
* Profile READMEs are mostly badges and banners, whose long proxied image URLs add up.
*/
private const int MaxLength = 200_000;
/**
* Images uploaded to GitHub are served from links signed for five minutes.
*/
private const int SignedImagesTtl = 240;
private const int FailureTtl = 300;
public function __construct(private GitHubProvider $github)
{
//
}
/**
* The README of the developer's GitHub profile repository, the one named after them, as GitHub renders it. Its
* images come from GitHub's own hosts, so readers never reach the hosts they come from. Cached for a day, a missing
* README included, but only for a few minutes when it holds uploaded images, whose signed links expire after five.
* Null when there is none, false when GitHub didn't answer, which is remembered for a few minutes so an outage
* doesn't call GitHub again on every view.
*/
public function get(User $user): string|false|null
{
$key = $this->key($user);
$failedKey = $this->failedKey($user);
/** @var string|null $html */
$html = Cache::get($key);
if ($html === null) {
if (Cache::has($failedKey)) {
return false;
}
try {
$html = $this->fetch($user);
} catch (HttpClientException) {
Cache::put($failedKey, true, self::FailureTtl);
return false;
}
Cache::put($key, $html, str_contains($html, 'private-user-images.githubusercontent.com') ? self::SignedImagesTtl : 86400);
}
return $html === '' ? null : $html;
}
/**
* Read the README from GitHub again on the next view, as someone who syncs their profile expects to see theirs.
*/
public function forget(User $user): void
{
Cache::forget($this->key($user));
Cache::forget($this->failedKey($user));
}
private function key(User $user): string
{
return "github-readme:{$user->github_id}";
}
private function failedKey(User $user): string
{
return "github-readme-failed:{$user->github_id}";
}
/**
* @throws HttpClientException
*/
private function fetch(User $user): string
{
$login = $this->github->login($user->github_id);
if ($login === null) {
return '';
}
$html = $this->github->readme($login);
return $html === null ? '' : $this->sanitize($this->resolveRelativeImages($html, $login));
}
/**
* GitHub's rendered README keeps an image path relative to the README as written, like ./assets/banner.png, and the
* sanitizer drops relative images, so a banner kept beside the README never showed. It's resolved against the
* profile repository on raw.githubusercontent.com instead, where HEAD follows the default branch, as GitHub itself
* shows it. Only the path changes: the sanitizer still checks the scheme and host of the result.
*/
private function resolveRelativeImages(string $html, string $login): string
{
return preg_replace_callback(
'~<img\b[^>]*?\ssrc="\K(?![a-z][a-z0-9+.-]*:|//)[^"]+~i',
fn (array $match): string => "https://raw.githubusercontent.com/{$login}/{$login}/HEAD/".mb_ltrim(preg_replace('~^(?:\./)+~', '', $match[0]) ?? $match[0], '/'),
$html,
) ?? $html;
}
/**
* Never trusted as is: only plain formatting, links and images from GitHub's hosts are kept. Classes, ids and
* styles are dropped, so a README can't take on the page's own styles, cover it or pose as part of it.
*/
private function sanitize(string $html): string
{
$config = new HtmlSanitizerConfig()
// Only what a README is written with: a fixed list, so attributes like srcset or name, and elements like
// dialog, never come along with the browser's idea of what is safe.
->allowElement('a', ['href', 'title'])
->allowElement('img', ['src', 'alt', 'title', 'width', 'height', 'align'])
->allowElement('p', ['align'])
->allowElement('div', ['align'])
->allowElement('h1', ['align'])
->allowElement('h2', ['align'])
->allowElement('h3', ['align'])
->allowElement('h4', ['align'])
->allowElement('h5', ['align'])
->allowElement('h6', ['align'])
->allowElement('th', ['align', 'colspan', 'rowspan'])
->allowElement('td', ['align', 'colspan', 'rowspan'])
->allowElement('ol', ['start'])
->allowElement('details', ['open']);
foreach (['article', 'span', 'ul', 'li', 'table', 'thead', 'tbody', 'tr', 'pre', 'code', 'blockquote', 'summary', 'picture', 'br', 'hr', 'b', 'strong', 'i', 'em', 'sub', 'sup', 'del', 's', 'kbd'] as $element) {
$config = $config->allowElement($element);
}
// Any other element goes, but what it holds stays: GitHub's own wrappers like g-emoji or themed-picture, and
// the rarer text elements like samp or dl, keep their words. Only these go with everything in them.
$config = $config->defaultAction(HtmlSanitizerAction::Block);
foreach (['script', 'style', 'template', 'noscript', 'iframe', 'object', 'embed', 'svg', 'math', 'dialog', 'map', 'form', 'textarea', 'select', 'button', 'head', 'title', 'source', 'video', 'audio'] as $element) {
$config = $config->dropElement($element);
}
$config = $config
->allowLinkSchemes(['https', 'http', 'mailto'])
->allowRelativeLinks(false)
->allowMediaSchemes(['https'])
->allowMediaHosts(['camo.githubusercontent.com', 'github.com', 'raw.githubusercontent.com', 'user-images.githubusercontent.com', 'private-user-images.githubusercontent.com'])
->allowRelativeMedias(false)
->forceAttribute('a', 'rel', 'noopener noreferrer nofollow ugc')
->forceAttribute('a', 'target', '_blank')
->withMaxInputLength(self::MaxLength);
return mb_trim(new HtmlSanitizer($config)->sanitize($html));
}
}