Skip to content

Commit 25f86e2

Browse files
authored
Merge pull request rails#52496 from martin-mfv/improve-activestorage-filename-sanitized
Added new unsafe characters of Windows filename `", <, >, ?, *` to `ActiveStorage::Filename#sanitized`
2 parents f4a9b76 + 0d5bbb6 commit 25f86e2

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

activestorage/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Improve `ActiveStorage::Filename#sanitized` method to handle special characters more effectively.
2+
Replace the characters `"*?<>` with `-` if they exist in the Filename to match the Filename convention of Win OS.
3+
4+
*Luong Viet Dung(Martin)*
5+
16
* Improve InvariableError, UnpreviewableError and UnrepresentableError message.
27

38
Include Blob ID and content_type in the messages.
@@ -8,4 +13,5 @@
813

914
*Nate Matykiewicz*
1015

16+
1117
Please check [7-2-stable](https://github.com/rails/rails/blob/7-2-stable/activestorage/CHANGELOG.md) for previous changes.

activestorage/app/models/active_storage/filename.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def extension_without_delimiter
5757
#
5858
# Characters considered unsafe for storage (e.g. \, $, and the RTL override character) are replaced with a dash.
5959
def sanitized
60-
@filename.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "�").strip.tr("\u{202E}%$|:;/\t\r\n\\", "-")
60+
@filename.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "�").strip.tr("\u{202E}%$|:;/<>?*\"\t\r\n\\", "-")
6161
end
6262

6363
# Returns the sanitized version of the filename.

activestorage/test/models/filename_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ActiveStorage::FilenameTest < ActiveSupport::TestCase
2222
end
2323

2424
test "sanitize" do
25-
"%$|:;/\t\r\n\\".each_char do |character|
25+
"%$|:;/<>?*\"\t\r\n\\".each_char do |character|
2626
filename = ActiveStorage::Filename.new("foo#{character}bar.pdf")
2727
assert_equal "foo-bar.pdf", filename.sanitized
2828
assert_equal "foo-bar.pdf", filename.to_s

0 commit comments

Comments
 (0)