diff --git a/KNOWLEDGE_BASE.md b/KNOWLEDGE_BASE.md index e5ad549a6..4a57c8557 100644 --- a/KNOWLEDGE_BASE.md +++ b/KNOWLEDGE_BASE.md @@ -687,7 +687,7 @@ Sources: [export settings](resources/js/pages/user-profile/edit.tsx), [export co ### Deleting your account -Settings → Profile → **Delete account** opens a confirmation for permanent deletion of your /dev/null account; an app can also delete it through the signed API. It does not delete your GitHub account. Your top-level commits and every reply under those threads are erased. Your replies are also removed, except that replies from other people beneath them survive under authorless `[deleted]` placeholders. Your devices, the messages and reactions you sent, and your personal uploaded images are erased too; other people's messages in your conversations stay with them. A guild you moderated is paused and keeps its shared icon and description, memberships and other people's posts. Account deletion is not covered by the 30-second commit undo. +Settings → Profile → **Delete account** opens a confirmation for permanent deletion of your /dev/null account; an app can also delete it through the signed API. It does not delete your GitHub account. Your top-level commits and every reply under those threads are erased. Your replies are also removed, except that replies from other people beneath them survive under authorless `[deleted]` placeholders. Your devices, the messages and reactions you sent, and your personal uploaded images are erased too; other people's messages in your conversations stay with them. A guild you moderated is paused and keeps its shared icon and description, memberships and other people's posts. Link cards elsewhere that point to the erased commits are removed as well, since they repeat their text, and so are cards of replies to your commits, since their titles name you. Account deletion is not covered by the 30-second commit undo. You can sign in with GitHub again later, but start from a new account rather than restoring the deleted one. Export your data first if you want a copy. Deleting from /dev/null cannot retract copies of public content held elsewhere. diff --git a/app/Actions/DeleteUser.php b/app/Actions/DeleteUser.php index 440267d5e..32f8829d7 100644 --- a/app/Actions/DeleteUser.php +++ b/app/Actions/DeleteUser.php @@ -16,6 +16,7 @@ use App\Models\ConversationParticipant; use App\Models\Guild; use App\Models\Headline; +use App\Models\LinkPreview; use App\Models\Message; use App\Models\MessageImage; use App\Models\MessageVoice; @@ -29,6 +30,8 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Str; +use Uri\WhatWg\Url; final readonly class DeleteUser { @@ -43,8 +46,9 @@ public function __construct(private UpdateCommitHotScores $updateCommitHotScores * What they posted for organizations stays, and only forgets who posted it. * The database cascades the image rows of erased commits and the user's scheduled commits, and the voice notes of their * messages, but the files are erased here. Orgs the user was the last one in go too, with everything sent in them, - * as they do when the last member leaves. Only their GitHub ID is kept, so looking their handle up never brings their - * page back as an unclaimed one, until they sign in again. + * as they do when the last member leaves. So do the previews of links to the erased commits, which hold their bodies, + * and to the replies to their commits, whose titles name them. Only their GitHub ID is kept, so looking their handle up + * never brings their page back as an unclaimed one, until they sign in again. */ public function handle(User $user): void { @@ -83,6 +87,16 @@ public function handle(User $user): void ->whereIn('commit_id', $this->touchedCommits($user)->select('id'))) ->update(['summary' => null, 'summary_created_at' => null]); DB::table('commit_headline')->whereIn('commit_id', $this->touchedCommits($user)->select('id'))->delete(); + $linked = $this->touchedCommits($user)->orWhereIn('parent_id', $user->commits()->withTrashed()->select('id')); + $linkedIds = $linked->clone()->pluck('id')->flip(); + LinkPreview::query() + ->whereIn(DB::raw("substring(lower(url) from '/commits/([0-9a-f-]{36})')"), $linked->selectRaw('id::text')) + // A link with escapes, dot segments or backslashes still opens the commit, since the preview fetcher resolves the + // path and the router decodes it, so those are read the same way. + ->orWhereIn('id', LinkPreview::query()->where('url', '~', '^[^?#]*([%\\\\]|/\.)')->select(['id', 'url'])->lazyById() + ->filter(fn (LinkPreview $preview): bool => $linkedIds->has(Str::match('#/commits/([0-9a-f-]{36})#', mb_strtolower(rawurldecode((string) Url::parse($preview->url)?->getPath()))))) + ->pluck('id')->all()) + ->delete(); $user->commits()->withTrashed()->topLevel()->forceDelete(); CommitImage::query()->whereIn('commit_id', $this->replies($user)->select('id'))->delete(); CommitReport::query()->whereIn('commit_id', $this->replies($user)->select('id'))->delete(); diff --git a/tests/Unit/Actions/DeleteUserTest.php b/tests/Unit/Actions/DeleteUserTest.php index c746f144d..fa49504af 100644 --- a/tests/Unit/Actions/DeleteUserTest.php +++ b/tests/Unit/Actions/DeleteUserTest.php @@ -79,6 +79,66 @@ ->and(DB::table('commit_topics')->pluck('commit_id')->all())->toBe([$unrelated->id]); }); +it('erases the previews of links to the commits it erases, which hold their bodies', function (): void { + $user = User::factory()->create(); + $commit = Commit::factory()->for($user)->create(); + $reply = Commit::factory()->for($commit, 'parent')->create(); + $tombstone = Commit::factory()->for($user)->for(Commit::factory(), 'parent')->create(); + Commit::factory()->for($tombstone, 'parent')->create(); + $previews = collect([$commit, $reply, $tombstone])->map(fn (Commit $linked): LinkPreview => LinkPreview::factory()->create([ + 'url' => route('commit.show', [$linked->user, $linked]), + 'description' => $linked->body, + ])); + $shouted = LinkPreview::factory()->create(['url' => str_replace($commit->id, mb_strtoupper($commit->id), route('commit.show', [$user, $commit]))]); + $linking = Commit::factory()->create(['link_preview_id' => $previews->first()->id]); + $other = LinkPreview::factory()->create(['url' => route('commit.show', [$linking->user, $linking])]); + + resolve(DeleteUser::class)->handle($user); + + $previews->push($shouted)->each(fn (LinkPreview $preview) => $this->assertModelMissing($preview)); + $this->assertModelExists($other); + expect($linking->fresh()->link_preview_id)->toBeNull(); +}); + +it('erases the previews of links to its commits written another way that still opens them', function (string $plain, string $escaped): void { + $user = User::factory()->create(); + $commit = Commit::factory()->for($user)->create(['id' => '01a16ffc-740e-7105-82dc-e3a7ed20aaaa']); + $other = Commit::factory()->create(['id' => '01a16ffc-740e-7105-82dc-e3a7ed20bbbb']); + $preview = LinkPreview::factory()->create(['url' => str_replace($plain, $escaped, route('commit.show', [$user, $commit]))]); + $kept = LinkPreview::factory()->create(['url' => str_replace($plain, $escaped, route('commit.show', [$other->user, $other]))]); + + expect($preview->url)->not->toBe(route('commit.show', [$user, $commit])); + + resolve(DeleteUser::class)->handle($user); + + $this->assertModelMissing($preview); + $this->assertModelExists($kept); +})->with([ + 'a dash' => ['-7105-', '%2D7105%2d'], + 'a digit' => ['01a16ffc', '%301a16ffc'], + 'a letter' => ['16ffc', '16%66fc'], + 'an uppercase letter' => ['e3a7', 'e3%41%37'], + 'the path' => ['/commits/', '%2F%63%6F%6d%6d%69%74%73%2f'], + 'an escaped dot segment' => ['/commits/', '/commits/%2e/'], + 'a parent segment' => ['/commits/', '/commits/x/../'], + 'backslashes' => ['/commits/', '\\commits\\'], +]); + +it('erases the previews of links to replies to its commits in other threads, whose titles name it', function (): void { + $user = User::factory()->create(); + $thread = Commit::factory()->create(); + $answer = Commit::factory()->for(Commit::factory()->for($user)->for($thread, 'parent'), 'parent')->create(); + $sibling = Commit::factory()->for($thread, 'parent')->create(); + $answered = LinkPreview::factory()->create(['url' => route('commit.show', [$answer->user, $answer])]); + $kept = LinkPreview::factory()->create(['url' => route('commit.show', [$sibling->user, $sibling])]); + + resolve(DeleteUser::class)->handle($user); + + $this->assertModelMissing($answered); + $this->assertModelExists($kept); + $this->assertModelExists($answer); +}); + it('keeps the quotes of others when erasing the quoted commits of a deleted user', function (): void { $user = User::factory()->create(); $quote = Commit::factory()->for(Commit::factory()->for($user), 'quotedCommit')->create();