Skip to content

Commit 2a0feb3

Browse files
author
epriestley
committedMay 23, 2022
Only attach files that are both referenced in Remarkup and attached by explicit metadata
Summary: Ref T13682. When a user uploads a file, then changes their mind and deletes the reference to the file, we don't actually want to attach the file. When choosing which files to attach, only attach files which are both referenced in Remarkup and explicitly attached in remarkup metadata. Test Plan: - Dropped a file into a comment, submitted it, saw it attach normally. - Dropped a file into a comment, deleted the reference, submitted it, saw no attachment. Maniphest Tasks: T13682 Differential Revision: https://secure.phabricator.com/D21832
1 parent 8cd02e6 commit 2a0feb3

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed
 

‎src/applications/transactions/editor/PhabricatorApplicationTransactionEditor.php

+49-1
Original file line numberDiff line numberDiff line change
@@ -447,14 +447,31 @@ private function newFileTransactionInternalValues(
447447
$old_map = ipull($rows, 'attachmentMode', 'filePHID');
448448
}
449449

450+
$mode_ref = PhabricatorFileAttachment::MODE_REFERENCE;
451+
$mode_detach = PhabricatorFileAttachment::MODE_DETACH;
452+
450453
$new_map = $old_map;
451454

452455
foreach ($new as $file_phid => $attachment_mode) {
453-
if ($attachment_mode == PhabricatorFileAttachment::MODE_DETACH) {
456+
$is_ref = ($attachment_mode === $mode_ref);
457+
$is_detach = ($attachment_mode === $mode_detach);
458+
459+
if ($is_detach) {
454460
unset($new_map[$file_phid]);
455461
continue;
456462
}
457463

464+
$old_mode = idx($old_map, $file_phid);
465+
466+
// If we're adding a reference to a file but it is already attached,
467+
// don't touch it.
468+
469+
if ($is_ref) {
470+
if ($old_mode !== null) {
471+
continue;
472+
}
473+
}
474+
458475
$new_map[$file_phid] = $attachment_mode;
459476
}
460477

@@ -2246,11 +2263,42 @@ private function newFileTransaction(
22462263

22472264
$new_map = array();
22482265

2266+
$viewer = $this->getActor();
2267+
2268+
$old_blocks = mpull($remarkup_changes, 'getOldValue');
2269+
$new_blocks = mpull($remarkup_changes, 'getNewValue');
2270+
2271+
$old_refs = PhabricatorMarkupEngine::extractFilePHIDsFromEmbeddedFiles(
2272+
$viewer,
2273+
$old_blocks);
2274+
$old_refs = array_fuse($old_refs);
2275+
2276+
$new_refs = PhabricatorMarkupEngine::extractFilePHIDsFromEmbeddedFiles(
2277+
$viewer,
2278+
$new_blocks);
2279+
$new_refs = array_fuse($new_refs);
2280+
2281+
$add_refs = array_diff_key($new_refs, $old_refs);
2282+
foreach ($add_refs as $file_phid) {
2283+
$new_map[$file_phid] = PhabricatorFileAttachment::MODE_REFERENCE;
2284+
}
2285+
22492286
foreach ($remarkup_changes as $remarkup_change) {
22502287
$metadata = $remarkup_change->getMetadata();
22512288

22522289
$attached_phids = idx($metadata, 'attachedFilePHIDs', array());
22532290
foreach ($attached_phids as $file_phid) {
2291+
2292+
// If the blocks don't include a new embedded reference to this file,
2293+
// do not actually attach it. A common way for this to happen is for
2294+
// a user to upload a file, then change their mind and remove the
2295+
// reference. We do not want to attach the file if they decided against
2296+
// referencing it.
2297+
2298+
if (!isset($new_map[$file_phid])) {
2299+
continue;
2300+
}
2301+
22542302
$new_map[$file_phid] = PhabricatorFileAttachment::MODE_ATTACH;
22552303
}
22562304
}

0 commit comments

Comments
 (0)
Failed to load comments.