diff --git a/database/migrations/2026_11_17_120000_erase_the_link_previews_of_erased_commits.php b/database/migrations/2026_11_17_120000_erase_the_link_previews_of_erased_commits.php new file mode 100644 index 0000000000..4e83f6681e --- /dev/null +++ b/database/migrations/2026_11_17_120000_erase_the_link_previews_of_erased_commits.php @@ -0,0 +1,51 @@ +string('app.url'))?->getAsciiHost(); + + DB::table('link_previews')->select(['id', 'url'])->chunkById(1000, function (Collection $previews) use ($host): void { + /** @var Collection $previews */ + $linked = $previews->mapWithKeys(fn (object $preview): array => [$preview->id => $this->commitId($preview->url, $host)])->filter(); + $kept = DB::table('commits') + ->whereIn('id', $linked->unique()->values()->all()) + ->whereNotNull('author_id') + ->where(fn (Builder $query): Builder => $query->whereNull('parent_id') + ->orWhereExists(fn (Builder $parent): Builder => $parent->from('commits as parents') + ->whereColumn('parents.id', 'commits.parent_id') + ->whereNotNull('parents.author_id'))) + ->pluck('id'); + + DB::table('link_previews')->whereIn('id', $linked->reject(fn (string $commit): bool => $kept->contains($commit))->keys()->all())->delete(); + }); + } + + /** + * The commit a link to this site opens, read the way the preview fetcher and the router read it, since escapes, dot + * segments and backslashes still open it. Frozen here so later changes to either cannot change this cleanup. + */ + private function commitId(string $url, string $host): string + { + $link = Url::parse($url); + $linkHost = (string) $link?->getAsciiHost(); + $id = Str::match('#^/@[^/]+/commits/([0-9a-f-]{36})#', mb_strtolower(rawurldecode((string) $link?->getPath()))); + + return ($linkHost === $host || str_ends_with($linkHost, ".{$host}")) && Str::isUuid($id) ? $id : ''; + } +}; diff --git a/tests/Unit/Actions/DeleteUserTest.php b/tests/Unit/Actions/DeleteUserTest.php index c746f144dc..9b788603d1 100644 --- a/tests/Unit/Actions/DeleteUserTest.php +++ b/tests/Unit/Actions/DeleteUserTest.php @@ -484,6 +484,50 @@ expect(PushSubscription::query()->count())->toBe(0); }); +it('erases the previews of links to commits that deleted accounts already took with them', function (): void { + $tombstone = Commit::factory()->create(['user_id' => null, 'body' => '', 'deleted_at' => now()]); + $live = Commit::factory()->create(); + $posted = Commit::factory()->create(['user_id' => null, 'author_id' => Organization::factory()->create()->id]); + $gone = fake()->uuid(); + $preview = fn (string $url): LinkPreview => LinkPreview::factory()->create(['url' => $url]); + $leftovers = collect([ + url("/@gone/commits/{$tombstone->id}"), + url('/@gone/commits/'.mb_strtoupper($gone)), + str_replace('://', '://www.', url("/@gone/commits/{$gone}")), + (string) preg_replace('#^(https?://[^/:]+)(:\d+)?#', '$1:80', url("/@gone/commits/{$gone}")), + url('/@gone/commits/'.str_replace('-', '%2D', $tombstone->id)), + url("/@gone/%63ommits/{$gone}"), + url("/@gone/commits/%2e/{$gone}"), + str_replace('/commits/', '\\commits\\', url("/@gone/commits/{$gone}")), + url('/@someone/commits/'.Commit::factory()->for($tombstone, 'parent')->create()->id), + ])->map($preview); + $kept = collect([ + url("/@{$live->user->handle}/commits/{$live->id}"), + url('/@someone/commits/'.Commit::factory()->create(['deleted_at' => now()])->id), + url('/@someone/commits/'.Commit::factory()->for($live, 'parent')->create()->id), + url("/@org/commits/{$posted->id}"), + url('/@org/commits/'.str_replace('-', '%2D', $posted->id)), + url('/@someone/commits/'.Commit::factory()->for($posted, 'parent')->create()->id), + url('/@someone/commits/'.str_replace('-', '%2D', $live->id)), + url("/@someone/%63ommits/{$live->id}"), + url("/@someone/commits/%2e/{$live->id}"), + url("/@someone/commits/{$gone}/../{$live->id}"), + url('/@someone/commits/'.str_repeat('-', 36)), + "https://example.com/@gone/commits/{$gone}", + "https://example.com/@gone/%63ommits/{$gone}", + str_replace('://', '://not', url("/@gone/commits/{$gone}")), + str_replace('://', '://example.com\\.', url("/@gone/commits/{$gone}")), + 'https://example.com/redirect?to='.url("/@gone/commits/{$gone}"), + url("/@gone?redirect=/commits/{$gone}"), + ])->map($preview); + + $migration = require database_path('migrations/2026_11_17_120000_erase_the_link_previews_of_erased_commits.php'); + $migration->up(); + + $leftovers->each(fn (LinkPreview $preview) => $this->assertModelMissing($preview)); + $kept->each(fn (LinkPreview $preview) => $this->assertModelExists($preview)); +}); + it('pauses moderated guilds and preserves other peoples posts memberships and scheduled posts', function (): void { Storage::fake(); $user = User::factory()->create();