Fix attachments on customer share links

The attachments patch skipped the login redirect for share-link holders
but left Redmine's read_authorize, find_container and
find_downloadable_attachments in place, which deny the anonymous user.
It relied on an Attachment#visible? patch that never existed, so every
file on a shared issue sent the customer to the login page.

Those checks now pass when the session holds an active share token for
the file's issue (show, download, thumbnail, Download all), and defer
to Redmine otherwise. Files on private notes stay hidden, matching the
share page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-18 18:44:30 -05:00
1 parent cf5328cb21
commit 7c87f4f9c5
2 files changed
+136 -22

No files matched your search

@@ -11,17 +11,45 @@
module RedmineQbo module RedmineQbo
module Patches module Patches
module AttachmentsControllerPatch module AttachmentsControllerPatch
# Redmine's own access checks, which deny the anonymous viewer of a share link. Each one defers
# to Redmine unless the session holds a customer share token for the issue the file belongs to.
module CustomerTokenAccess
private
def read_authorize
valid_customer_token? || super
end
def find_container
return super unless customer_token_for_download_all?
@container = Issue.find(params[:object_id])
@project = @container.project
rescue ActiveRecord::RecordNotFound
render_404
end
def find_downloadable_attachments
return super unless customer_token_for_download_all?
@attachments = @container.attachments.select(&:readable?)
bulk_download_max_size = Setting.bulk_download_max_size.to_i.kilobytes
if @attachments.sum(&:filesize) > bulk_download_max_size
flash[:error] = l(:error_bulk_download_size_too_big, max_size: number_to_human_size(bulk_download_max_size.to_i))
redirect_back_or_default(container_url, referer: true)
end
end
end
def self.apply def self.apply
AttachmentsController.prepend(CustomerTokenAccess)
AttachmentsController.class_eval do AttachmentsController.class_eval do
# 1. PREPEND: Must run before ANY of Redmine's ApplicationController filters # Must run before any of Redmine's ApplicationController filters
prepend_before_action :load_customer_token prepend_before_action :load_customer_token
# 2. Skip global login redirects if the user holds a valid token for this file # Skip the login redirect if the session holds a valid token for this file
skip_before_action :check_if_login_required, if: :valid_customer_token? skip_before_action :check_if_login_required, if: :valid_customer_token?
skip_before_action :check_project_privacy, raise: false, if: :valid_customer_token?
# Note: We do NOT need to skip :read_authorize anymore.
# Because we patched Attachment#visible?, read_authorize will pass naturally!
private private
@@ -31,30 +59,29 @@ module RedmineQbo
@customer_token = session[:token].present? ? CustomerToken.active.find_by(token: session[:token]) : nil @customer_token = session[:token].present? ? CustomerToken.active.find_by(token: session[:token]) : nil
end end
def customer_token_for_download_all?
@customer_token.present? && params[:action] == 'download_all' &&
params[:object_type] == 'issues' && params[:object_id].to_i == @customer_token.issue_id
end
def valid_customer_token? def valid_customer_token?
token = @customer_token token = @customer_token
return false unless token return false unless token
return customer_token_for_download_all? if params[:action] == 'download_all'
return false unless %w[show download thumbnail].include?(params[:action])
# Handle "Download All" zip requests (object_type=issues, object_id=ID)
if params[:action] == 'download_all'
return params[:object_type] == 'issues' && params[:object_id].to_i == token.issue_id
end
# Handle normal single attachment requests (show, download, thumbnail)
attachment = Attachment.find_by(id: params[:id]) attachment = Attachment.find_by(id: params[:id])
return false unless attachment return false unless attachment
# Allow if attachment belongs directly to the Issue case attachment.container_type
if attachment.container_type == 'Issue' && attachment.container_id == token.issue_id when 'Issue'
return true attachment.container_id == token.issue_id
when 'Journal'
journal = attachment.container
journal&.journalized_type == 'Issue' && journal.journalized_id == token.issue_id && !journal.private_notes?
else
false
end end
# Allow if attachment belongs to a Journal (comment) on the Issue
if attachment.container_type == 'Journal' && attachment.container.journalized_type == 'Issue' && attachment.container.journalized_id == token.issue_id
return true
end
false
end end
end end
end end
@@ -0,0 +1,87 @@
#The MIT License (MIT)
#
#Copyright (c) 2026 rick barrette
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
require File.expand_path('../test_helper', __dir__)
# Files on an issue shared through a customer link (customers/view/:token) open for the anonymous
# visitor holding the link, and nothing else does.
class CustomerShareAttachmentsTest < Redmine::IntegrationTest
fixtures :projects, :users, :email_addresses, :members, :member_roles, :roles, :issues, :trackers,
:projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :attachments, :journals
# Issue 4 is in a private project; attachment 7 is on it, attachment 1 is on issue 3.
def setup
set_fixtures_attachments_directory
CustomerToken.delete_all
User.current = User.find(1)
@token = CustomerToken.create!(issue_id: 4, expires_at: 1.week.from_now)
User.current = nil
end
def teardown
set_tmp_attachments_directory
end
def open_share_link
get "/customers/view/#{@token.token}"
assert_response :success
end
def test_without_the_link_files_need_a_login
with_settings login_required: '1' do
get '/attachments/download/7/archive.zip'
assert_redirected_to %r{/login}
end
end
def test_share_link_opens_the_issues_files
with_settings login_required: '1' do
open_share_link
assert_select 'a[href=?]', '/attachments/7'
get '/attachments/7'
assert_response :success
get '/attachments/download/7/archive.zip'
assert_response :success
assert_equal 'application/zip', response.media_type
get '/attachments/issues/4/download'
assert_response :success
end
end
def test_share_link_does_not_open_other_files
with_settings login_required: '1' do
open_share_link
get '/attachments/download/1/error281.txt'
assert_redirected_to %r{/login}
get '/attachments/issues/3/download'
assert_redirected_to %r{/login}
end
end
def test_files_on_private_notes_stay_hidden
journal = Journal.create!(journalized: Issue.find(4), user: User.find(1), notes: 'internal', private_notes: true)
Attachment.find(1).update_columns(container_type: 'Journal', container_id: journal.id)
with_settings login_required: '1' do
open_share_link
get '/attachments/download/1/error281.txt'
assert_redirected_to %r{/login}
end
end
def test_expired_link_opens_nothing
with_settings login_required: '1' do
open_share_link
@token.update_columns(expires_at: 1.minute.ago)
get '/attachments/download/7/archive.zip'
assert_redirected_to %r{/login}
end
end
end