Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload dialog: fix allowed characters for filename #6462

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions panel/src/components/Uploads/UploadItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
:novalidate="true"
:required="true"
:value="name"
allow="a-z0-9@._-"
class="k-upload-item-input"
type="slug"
@input="$emit('rename', $event)"
Expand Down
4 changes: 2 additions & 2 deletions panel/src/helpers/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ export function slug(string, rules = [], allowed = "", separator = "-") {
string = string.replace("/", separator);

// trim leading and trailing non-word-chars
string = string.replace(new RegExp("^[^" + allowed + "]+", "g"), "");
string = string.replace(new RegExp("[^" + allowed + "]+$", "g"), "");
string = string.replace(new RegExp("^[^a-z0-9]+", "g"), "");
string = string.replace(new RegExp("[^a-z0-9]+$", "g"), "");

return string;
}
Expand Down
5 changes: 5 additions & 0 deletions panel/src/helpers/string.slug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ describe.concurrent("$helper.string.slug()", () => {
expect(resultB).toBe("a-b");
});

it("should produces safe filenames", () => {
const result = slug("-what a [email protected]_", [], "a-z0-9@._-");
expect(result).toBe("[email protected]");
});

it("should return empty string when no param sent", () => {
const result = slug();
expect(result).toBe("");
Expand Down
4 changes: 3 additions & 1 deletion src/Cms/FileActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public function changeName(
string|null $extension = null
): static {
if ($sanitize === true) {
$name = F::safeName($name);
// sanitize the basename part only
// as the extension isn't included in $name
$name = F::safeBasename($name, false);
}

// if no extension is passed, make sure to maintain current one
Expand Down
28 changes: 20 additions & 8 deletions src/Filesystem/F.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,20 +756,32 @@ public static function safeName(string $string): string
* Sanitize a file's name (without extension)
* @since 4.0.0
*/
public static function safeBasename(string $string): string
{
$name = static::name($string);
return Str::slug($name, '-', 'a-z0-9@._-');
public static function safeBasename(
string $string,
bool $extract = true
): string {
// extract only the name part from whole filename string
if ($extract === true) {
$string = static::name($string);
}

return Str::slug($string, '-', 'a-z0-9@._-');
}

/**
* Sanitize a file's extension
* @since 4.0.0
*/
public static function safeExtension(string $string): string
{
$extension = static::extension($string);
return Str::slug($extension);
public static function safeExtension(
string $string,
bool $extract = true
): string {
// extract only the extension part from whole filename string
if ($extract === true) {
$string = static::extension($string);
}

return Str::slug($string);
}

/**
Expand Down
18 changes: 12 additions & 6 deletions tests/Filesystem/FTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -763,14 +763,17 @@ public function testSafeName()
// without extension
$this->assertSame('uber-genious', F::safeName('über genious'));

// with leading dash
$this->assertSame('super.jpg', F::safeName('-super.jpg'));
// with leading and trailing dash
$this->assertSame('super.jpg', F::safeName('-super.jpg-'));

// with leading and trailing underscore
$this->assertSame('super.jpg', F::safeName('_super.jpg_'));

// with leading underscore
$this->assertSame('super.jpg', F::safeName('_super.jpg'));
// with leading and trailing dot
$this->assertSame('super.jpg', F::safeName('.super.jpg.'));

// with leading dot
$this->assertSame('super.jpg', F::safeName('.super.jpg'));
// leave allowed characters untouched
$this->assertSame('file.a@b_c-d.jpg', F::safeName('file.a@b_c-d.jpg'));
}

/**
Expand All @@ -786,6 +789,8 @@ public function testSafeBasename()

// without extension
$this->assertSame('uber-genious', F::safeBasename('über genious'));
$this->assertSame('uber', F::safeBasename('über.genious'));
$this->assertSame('uber.genious', F::safeBasename('über.genious', false));

// with leading dash
$this->assertSame('super', F::safeBasename('-super.jpg'));
Expand All @@ -801,6 +806,7 @@ public function testSafeExtension()

$this->assertSame('txt', F::safeExtension('über genious.txt'));
$this->assertSame('taxt', F::safeExtension('über genious.täxt'));
$this->assertSame('taxt', F::safeExtension('täxt', false));
$this->assertSame('', F::safeExtension('über genious'));
$this->assertSame('jpg', F::safeExtension('-super.jpg'));
}
Expand Down
5 changes: 4 additions & 1 deletion tests/Toolkit/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,10 @@ public function testSlug()
// Allow underscores
$this->assertSame('a_b', Str::slug('a_b', '-', 'a-z0-9_'));

// store default defaults
// Trim non-alphanum characters
$this->assertSame('[email protected]', Str::slug('.a@b c.b-', '-', 'a-z0-9@._-'));

// Store default defaults
$defaults = Str::$defaults['slug'];

// Custom str defaults
Expand Down
Loading