Relationship Graph

Relationship Graph
related to related to child of child of duplicate of duplicate of

View Issue Details

IDProjectCategoryView StatusLast Update
0037505mantisbtsecuritypublic2026-11-22 16:11
Reportersuperbot-helper Assigned Todregad  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionduplicate 
Product Version2.28.4 
Summary0037505: A private note's attachment can be downloaded from a copy or clone of the issue
Description

Is it already known that copying or cloning an issue makes a private note's attachment readable by users who cannot see the note?

On 2.28.4 with the default configuration, a reporter is refused the attachment of a private note on the original issue (403). After that same reporter copies the issue with the Copy group action, or clones it with the Clone form with Copy attachments ticked, both of which need only the report threshold in the target project, the reporter downloads the same file from the new issue. file_copy_attachments() in core/file_api.php inserts the copied row without its bugnote_id, so file_download.php treats it as an attachment of the issue itself.

We measured ten of ten for each path on the release tarball, with a public note's attachment as the control. Only 2.28.4 was run. This is a different code path from issue 0037502.

A small tested patch follows as a note: with it a reporter's copy no longer carries the private note's attachment, a developer's copy keeps it linked to the copy's own note, and the developer still reads it.

If a CVE is assigned, please credit EZO Labs Security.

Disclosure: AI tools were used to find this issue and to run the reproduction.

EZO Labs Security
https://ezolabs.net
tony@ezolabs.net
GitHub: superbot-helper

TagsNo tags attached.

Relationships

duplicate of 0037027 closeddregad Clone Issue Copies Hidden Private-Note Attachments Into a Visible Child Issue 

Activities

superbot-helper

superbot-helper

2026-11-20 14:10

reporter   ~0071620

The tested patch, core/file_api.php part. Run on the same 2.28.4 install: with it the reporter's Copy and Clone no longer carry the private note's attachment (0 of 10 each, where the release gave 10 of 10), a public note's attachment is still copied, and a developer's copy keeps the attachment linked to the copy's own note and still downloads it. The attached git format-patch is the complete change: it also passes the note map from bug_copy() and IssueAddCommand and adds a PHPUnit test (FileCopyAttachmentsTest) that fails before the change and passes after.

diff --git a/core/file_api.php b/core/file_api.php
index 479e3b4..3d433a7 100644
--- a/core/file_api.php
+++ b/core/file_api.php
@@ -1447,7 +1447,7 @@ function file_move_bug_attachments( $p_bug_id, $p_project_id_to ) {
  * @throws ClientException
  * @throws Exception
  */
-function file_copy_attachments( $p_source_bug_id, $p_dest_bug_id ) {
+function file_copy_attachments( $p_source_bug_id, $p_dest_bug_id, array $p_bugnote_map = array() ) {
    db_param_push();
    $t_query = 'SELECT * FROM {bug_file} WHERE bug_id = ' . db_param();
    $t_result = db_query( $t_query, array( $p_source_bug_id ) );
@@ -1458,6 +1458,21 @@ function file_copy_attachments( $p_source_bug_id, $p_dest_bug_id ) {
    for( $i = 0;$i < $t_count;$i++ ) {
        $t_bug_file = db_fetch_array( $t_result );

+       # An attachment of a note is visible only to the users who can view that note.
+       $t_bugnote_id = (int)$t_bug_file['bugnote_id'];
+       if( $t_bugnote_id > 0 ) {
+           if( !file_can_view_bugnote_attachments( $t_bugnote_id, (int)$t_bug_file['user_id'], $p_source_bug_id ) ) {
+               continue;
+           }
+           if( isset( $p_bugnote_map[$t_bugnote_id] ) ) {
+               $t_bugnote_id = (int)$p_bugnote_map[$t_bugnote_id];
+           } elseif( bugnote_get_field( $t_bugnote_id, 'view_state' ) == VS_PUBLIC ) {
+               $t_bugnote_id = 0;
+           } else {
+               continue;
+           }
+       }
+
        # prepare the new diskfile name and then copy the file
        $t_source_file = $t_bug_file['folder'] . $t_bug_file['diskfile'];
        if( ( config_get( 'file_upload_method' ) == DISK ) ) {
@@ -1480,7 +1495,7 @@ function file_copy_attachments( $p_source_bug_id, $p_dest_bug_id ) {

        db_param_push();
        $t_query = 'INSERT INTO {bug_file} (
-               bug_id, title, description, diskfile, filename, folder,
+               bug_id, bugnote_id, title, description, diskfile, filename, folder,
                filesize, file_type, date_added, user_id, content
            )
            VALUES ( '
@@ -1490,7 +1505,7 @@ function file_copy_attachments( $p_source_bug_id, $p_dest_bug_id ) {
            . db_param() . ', ' . db_param() .
            ')';
        db_query( $t_query, array(
-           $p_dest_bug_id, $t_bug_file['title'], $t_bug_file['description'],
+           $p_dest_bug_id, $t_bugnote_id, $t_bug_file['title'], $t_bug_file['description'],
            $t_new_diskfile_name, $t_new_file_name, $t_file_path,
            $t_bug_file['filesize'], $t_bug_file['file_type'], $t_bug_file['date_added'],
            $t_bug_file['user_id'], $t_bug_file['content']
0001-Keep-a-note-s-attachment-as-private-as-its-note-when-an-issue-is-copied.patch (11,485 bytes)   
From 436977ff2430036d4544deb7e7ffa93109399a83 Mon Sep 17 00:00:00 2001
From: EZO Labs Security <tony@ezolabs.net>
Date: Thu, 19 Nov 2026 21:24:14 +0000
Subject: [PATCH] Keep a note's attachment as private as its note when an issue
 is copied

file_copy_attachments() inserted every attachment row of the source issue without its bugnote_id, so the attachment of a private note became an issue-level attachment of the new issue and was served to every user who could view it. Copy an attachment that belongs to a note only when the user performing the copy can view the note, link the copy to the destination issue's copy of that note, and copy a public note's attachment unlinked as before.

The new FileCopyAttachmentsTest covers both halves: the developer who may read the note sees the attachment on the copy, and the reporter who may not does not.
---
 core/bug_api.php                         |  6 +-
 core/commands/IssueAddCommand.php        |  8 +-
 core/file_api.php                        | 37 ++++++++--
 tests/Mantis/FileCopyAttachmentsTest.php | 94 ++++++++++++++++++++++++
 4 files changed, 136 insertions(+), 9 deletions(-)
 create mode 100644 tests/Mantis/FileCopyAttachmentsTest.php

diff --git a/core/bug_api.php b/core/bug_api.php
index cb3caa7..ff0bb8a 100644
--- a/core/bug_api.php
+++ b/core/bug_api.php
@@ -1382,6 +1382,7 @@ function bug_copy( $p_bug_id, $p_target_project_id = null, $p_copy_custom_fields
 	}
 
 	# Copy bugnotes
+	$t_bugnote_map = array();
 	if( $p_copy_bugnotes ) {
 		db_param_push();
 		$t_query = 'SELECT * FROM {bugnote} WHERE bug_id=' . db_param();
@@ -1414,12 +1415,15 @@ function bug_copy( $p_bug_id, $p_target_project_id = null, $p_copy_custom_fields
 						   			' . db_param() . ',
 						   			' . db_param() . ')';
 			db_query( $t_query2, array( $t_new_bug_id, $t_bug_note['reporter_id'], $t_bugnote_text_insert_id, $t_bug_note['view_state'], $t_bug_note['date_submitted'], $t_bug_note['last_modified'] ) );
+
+			# remember which note of the destination issue is the copy of this one
+			$t_bugnote_map[(int)$t_bug_note['id']] = (int)db_insert_id( db_get_table( 'bugnote' ) );
 		}
 	}
 
 	# Copy attachments
 	if( $p_copy_attachments ) {
-	    file_copy_attachments( $t_bug_id, $t_new_bug_id );
+	    file_copy_attachments( $t_bug_id, $t_new_bug_id, $t_bugnote_map );
 	}
 
 	# Copy users monitoring bug
diff --git a/core/commands/IssueAddCommand.php b/core/commands/IssueAddCommand.php
index fa63e3a..918cd0f 100644
--- a/core/commands/IssueAddCommand.php
+++ b/core/commands/IssueAddCommand.php
@@ -429,13 +429,14 @@ class IssueAddCommand extends Command {
 			history_log_event_special( $t_master_issue_id, BUG_CLONED_TO, '', $t_issue_id );
 
 			# copy notes from parent
+			$t_bugnote_map = array();
 			if( isset( $t_clone_info['copy_notes'] ) &&  $t_clone_info['copy_notes'] ) {
 				$t_parent_bugnotes = bugnote_get_all_bugnotes( $t_master_issue_id );
 
 				foreach ( $t_parent_bugnotes as $t_parent_bugnote ) {
 					$t_private = $t_parent_bugnote->view_state == VS_PRIVATE;
 
-					bugnote_add(
+					$t_new_bugnote_id = bugnote_add(
 						$t_issue_id,
 						$t_parent_bugnote->note,
 						$t_parent_bugnote->time_tracking,
@@ -448,13 +449,16 @@ class IssueAddCommand extends Command {
 						0,
 						false );
 
+					# remember which note of the clone is the copy of this one
+					$t_bugnote_map[(int)$t_parent_bugnote->id] = (int)$t_new_bugnote_id;
+
 					# Note: we won't trigger mentions in the clone scenario.
 				}
 			}
 
 			# copy attachments from parent
 			if( isset( $t_clone_info['copy_files'] ) &&  $t_clone_info['copy_files'] ) {
-				file_copy_attachments( $t_master_issue_id, $t_issue_id );
+				file_copy_attachments( $t_master_issue_id, $t_issue_id, $t_bugnote_map );
 			}
 
 			if( isset( $t_clone_info['relationship_type'] ) &&  $t_clone_info['relationship_type'] > BUG_REL_ANY ) {
diff --git a/core/file_api.php b/core/file_api.php
index 479e3b4..7e360b5 100644
--- a/core/file_api.php
+++ b/core/file_api.php
@@ -1440,14 +1440,19 @@ function file_move_bug_attachments( $p_bug_id, $p_project_id_to ) {
  *
  * Does not perform history logging and does not perform access checks.
  *
- * @param int $p_source_bug_id Source Bug.
- * @param int $p_dest_bug_id   Destination Bug.
+ * An attachment that belongs to a note follows that note's visibility: it is
+ * copied only when the user performing the copy can view the note, and it is
+ * linked to the destination issue's copy of that note when one exists.
+ *
+ * @param int   $p_source_bug_id Source Bug.
+ * @param int   $p_dest_bug_id   Destination Bug.
+ * @param array $p_bugnote_map   Map of source note id to the destination issue's note id.
  *
  * @return void
  * @throws ClientException
  * @throws Exception
  */
-function file_copy_attachments( $p_source_bug_id, $p_dest_bug_id ) {
+function file_copy_attachments( $p_source_bug_id, $p_dest_bug_id, array $p_bugnote_map = array() ) {
 	db_param_push();
 	$t_query = 'SELECT * FROM {bug_file} WHERE bug_id = ' . db_param();
 	$t_result = db_query( $t_query, array( $p_source_bug_id ) );
@@ -1458,6 +1463,26 @@ function file_copy_attachments( $p_source_bug_id, $p_dest_bug_id ) {
 	for( $i = 0;$i < $t_count;$i++ ) {
 		$t_bug_file = db_fetch_array( $t_result );
 
+		# An attachment of a note is visible only to the users who can view that note.
+		$t_bugnote_id = (int)$t_bug_file['bugnote_id'];
+		if( $t_bugnote_id > 0 ) {
+			if( !file_can_view_bugnote_attachments( $t_bugnote_id, (int)$t_bug_file['user_id'], $p_source_bug_id ) ) {
+				# The user copying the issue may not read the note: its attachment is not copied.
+				continue;
+			}
+			if( isset( $p_bugnote_map[$t_bugnote_id] ) ) {
+				$t_bugnote_id = (int)$p_bugnote_map[$t_bugnote_id];
+			} elseif( bugnote_get_field( $t_bugnote_id, 'view_state' ) == VS_PUBLIC ) {
+				# A public note's attachment is already visible to every user: an unlinked
+				# copy of it discloses nothing.
+				$t_bugnote_id = 0;
+			} else {
+				# A private note with no copy on the destination issue: an unlinked copy of
+				# its attachment would be served to every viewer of the new issue.
+				continue;
+			}
+		}
+
 		# prepare the new diskfile name and then copy the file
 		$t_source_file = $t_bug_file['folder'] . $t_bug_file['diskfile'];
 		if( ( config_get( 'file_upload_method' ) == DISK ) ) {
@@ -1480,17 +1505,17 @@ function file_copy_attachments( $p_source_bug_id, $p_dest_bug_id ) {
 
 		db_param_push();
 		$t_query = 'INSERT INTO {bug_file} (
-				bug_id, title, description, diskfile, filename, folder,
+				bug_id, bugnote_id, title, description, diskfile, filename, folder,
 				filesize, file_type, date_added, user_id, content
 			)
 			VALUES ( '
 			. db_param() . ', ' . db_param() . ', ' . db_param() . ', '
 			. db_param() . ', ' . db_param() . ', ' . db_param() . ', '
 			. db_param() . ', ' . db_param() . ', ' . db_param() . ', '
-			. db_param() . ', ' . db_param() .
+			. db_param() . ', ' . db_param() . ', ' . db_param() .
 			')';
 		db_query( $t_query, array(
-			$p_dest_bug_id, $t_bug_file['title'], $t_bug_file['description'],
+			$p_dest_bug_id, $t_bugnote_id, $t_bug_file['title'], $t_bug_file['description'],
 			$t_new_diskfile_name, $t_new_file_name, $t_file_path,
 			$t_bug_file['filesize'], $t_bug_file['file_type'], $t_bug_file['date_added'],
 			$t_bug_file['user_id'], $t_bug_file['content']
diff --git a/tests/Mantis/FileCopyAttachmentsTest.php b/tests/Mantis/FileCopyAttachmentsTest.php
new file mode 100644
index 0000000..565e855
--- /dev/null
+++ b/tests/Mantis/FileCopyAttachmentsTest.php
@@ -0,0 +1,94 @@
+<?php declare(strict_types=1);
+# MantisBT - A PHP based bugtracking system
+
+# MantisBT is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+
+# MantisBT is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with MantisBT.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Test cases for copying the attachments of an issue
+ *
+ * @package    Tests
+ * @subpackage FileAPI
+ * @copyright  Copyright 2026  MantisBT Team - mantisbt-dev@lists.sourceforge.net
+ * @link       http://www.mantisbt.org
+ *
+ * @noinspection PhpIllegalPsrClassPathInspection
+ */
+
+namespace Mantis\tests\Mantis;
+
+/**
+ * PHPUnit tests for file_copy_attachments()
+ */
+class FileCopyAttachmentsTest extends MantisCoreBase {
+
+	public static function setUpBeforeClass(): void {
+		parent::setUpBeforeClass();
+		self::login();
+	}
+
+	/**
+	 * A copy of an issue keeps a private note's attachment as private as the
+	 * note: the user who may read the note sees the attachment on the copy, and
+	 * the user who may not read it does not.
+	 */
+	public function testCopiedAttachmentKeepsTheVisibilityOfItsNote(): void {
+		$t_suffix = bin2hex( random_bytes( 4 ) );
+
+		$t_project_id = project_create( 'Copy fixture ' . $t_suffix, '', 10, VS_PUBLIC );
+		user_create( 'copy-rep-' . $t_suffix, 'Password123', 'copy-rep-' . $t_suffix . '@example.invalid', REPORTER );
+		user_create( 'copy-dev-' . $t_suffix, 'Password123', 'copy-dev-' . $t_suffix . '@example.invalid', DEVELOPER );
+		$t_reporter = (int)user_get_id_by_name( 'copy-rep-' . $t_suffix );
+		$t_developer = (int)user_get_id_by_name( 'copy-dev-' . $t_suffix );
+
+		$t_category_id = category_get_id_by_name( 'General', $t_project_id, false );
+		if( $t_category_id === false ) {
+			category_add( $t_project_id, 'General' );
+			$t_category_id = category_get_id_by_name( 'General', $t_project_id, false );
+		}
+
+		$t_bug = new \BugData;
+		$t_bug->project_id = $t_project_id;
+		$t_bug->reporter_id = $t_reporter;
+		$t_bug->summary = 'Copy fixture ' . $t_suffix;
+		$t_bug->description = 'copy fixture';
+		$t_bug->category_id = (int)$t_category_id;
+		$t_issue_id = (int)$t_bug->create();
+
+		current_user_set( $t_developer );
+		$t_private_note = (int)bugnote_add( $t_issue_id, 'private note', '0:00', true, BUGNOTE, '', $t_developer, false, 0, 0, false, false, false );
+		$t_private_name = 'private-note-' . $t_suffix . '.txt';
+		$t_private_path = '/tmp/' . $t_private_name;
+		file_put_contents( $t_private_path, 'private note attachment' );
+		file_add( $t_issue_id, array(
+			'name' => $t_private_name,
+			'tmp_name' => $t_private_path,
+			'size' => filesize( $t_private_path ),
+			'type' => 'text/plain',
+			'error' => UPLOAD_ERR_OK,
+		), 'bug', '', '', $t_developer, 0, false, $t_private_note );
+
+		$t_copy_id = bug_copy( $t_issue_id, $t_project_id, true, true, false, true, true, false );
+
+		$t_names = array_column( file_get_visible_attachments( $t_copy_id ), 'display_name' );
+		self::assertContains( $t_private_name, $t_names,
+			'the developer who may read the private note sees its attachment on the copy' );
+
+		current_user_set( $t_reporter );
+		$t_names = array_column( file_get_visible_attachments( $t_copy_id ), 'display_name' );
+		self::assertNotContains( $t_private_name, $t_names,
+			'the reporter who may not read the private note does not see its attachment on the copy' );
+
+		unlink( $t_private_path );
+	}
+}
-- 
2.53.0

dregad

dregad

2026-11-20 20:02

developer   ~0071621

Thanks for the report. This vulnerability has already been identified previously by another security researcher (see 0037027), so I'm closing this issue as duplicate. You will be co-credited for the finding.