Project

General

Profile

Defect #44228 ยป 0001-Sanitize-NUL-bytes-in-filenames-when-uploading-attac.patch

Go MAEDA, 2026-07-03 04:26

View differences:

app/controllers/wiki_controller.rb
extension = '.txt'
# Keep this character set aligned with Attachment#sanitize_filename.
# Unlike attachments, do not drop path-like components from wiki titles.
sanitized_title = page.title.tr('\\', '_').gsub(/[\/?%*:|"'<>\n\r]+/, '_')
sanitized_title = page.title.tr('\\', '_').gsub(/[\/?%*:|"'<>\n\r\x00]+/, '_')
filename = "#{sanitized_title}#{extension}"
dup_count = 0
app/models/attachment.rb
just_filename = value.gsub(/\A.*(\\|\/)/m, '')
# Finally, replace invalid characters with underscore
just_filename.gsub(/[\/?%*:|"'<>\n\r]+/, '_')
just_filename.gsub(/[\/?%*:|"'<>\n\r\x00]+/, '_')
end
# Returns the subdirectory in which the attachment will be saved
test/integration/api_test/attachments_test.rb
assert_match /_test\.txt$/, attachment.diskfile
end
test "POST /uploads.json should sanitize NUL in :filename param" do
set_tmp_attachments_directory
assert_difference 'Attachment.count' do
post(
'/uploads.json?filename=hello%00world.txt',
:headers => {
"RAW_POST_DATA" => 'File content',
"CONTENT_TYPE" => 'application/octet-stream'
}.merge(credentials('jsmith'))
)
assert_response :created
end
attachment = Attachment.order(id: :desc).first
assert_equal 'hello_world.txt', attachment.filename
end
test "POST /uploads.xml should not accept other content types" do
set_tmp_attachments_directory
assert_no_difference 'Attachment.count' do
test/unit/attachment_test.rb
assert(
Attachment.update_attachments(
attachments,
{2 => {:filename => 'newname?.txt'}}
{2 => {:filename => "new\x00name?.txt"}}
)
)
attachment = Attachment.find(2)
assert_equal 'newname_.txt', attachment.filename
assert_equal 'new_name_.txt', attachment.filename
end
def test_latest_attach
    (1-1/1)