-
-
Notifications
You must be signed in to change notification settings - Fork 374
Fix slipery zip extraction #4592
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2d8a995
Fix slipery zip extraction
ildyria 3d937e6
Improved validation
ildyria 0fbfe5c
fix
ildyria ad5abad
fix test
ildyria d2667eb
make the rule more flexible for tests
ildyria 67a551e
more armors
ildyria f916f6b
formatting
ildyria File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-License-Identifier: MIT | ||
| * Copyright (c) 2017-2018 Tobias Reich | ||
| * Copyright (c) 2018-2026 LycheeOrg. | ||
| */ | ||
|
|
||
| namespace App\Rules; | ||
|
|
||
| use Illuminate\Contracts\Validation\ValidationRule; | ||
| use Illuminate\Support\Facades\App; | ||
| use Illuminate\Support\Str; | ||
|
|
||
| /** | ||
| * This rule is designed specifically to avoid path injection. | ||
| */ | ||
| final class FilenameRule implements ValidationRule | ||
| { | ||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function validate(string $attribute, mixed $value, \Closure $fail): void | ||
| { | ||
| if (is_string($value) === false) { | ||
| $fail(':attribute is not a string.'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if ($value === '.') { | ||
| $fail(':attribute is not a valid file name.'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $dir_name = pathinfo($value, PATHINFO_DIRNAME); | ||
|
|
||
| // Allow test files to be in subdirectories of tests/Samples, | ||
| // but not in any other subdirectory. | ||
| if (App::runningUnitTests() && | ||
| str_starts_with($dir_name, 'tests/Samples') && | ||
| !str_contains($dir_name, '..') | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| if ($dir_name !== '' && $dir_name !== '.') { | ||
| $fail(':attribute contains a directory, give proper filename.'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (Str::of($value)->contains(['..', '/', '\\'])) { | ||
| $fail(':attribute contains invalid characters:.'); | ||
|
|
||
| return; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-License-Identifier: MIT | ||
| * Copyright (c) 2017-2018 Tobias Reich | ||
| * Copyright (c) 2018-2026 LycheeOrg. | ||
| */ | ||
|
|
||
| /** | ||
| * We don't care for unhandled exceptions in tests. | ||
| * It is the nature of a test to throw an exception. | ||
| * Without this suppression we had 100+ Linter warning in this file which | ||
| * don't help anything. | ||
| * | ||
| * @noinspection PhpDocMissingThrowsInspection | ||
| * @noinspection PhpUnhandledExceptionInspection | ||
| */ | ||
|
|
||
| namespace Tests\Unit\Rules; | ||
|
|
||
| use App\Rules\FilenameRule; | ||
| use Tests\AbstractTestCase; | ||
|
|
||
| class FilenameRuleTest extends AbstractTestCase | ||
| { | ||
| private function assertPasses(mixed $value): void | ||
| { | ||
| $rule = new FilenameRule(); | ||
| $failed = false; | ||
| $rule->validate('file_name', $value, function () use (&$failed): void { $failed = true; }); | ||
| self::assertFalse($failed, 'Expected "' . var_export($value, true) . '" to pass but it failed.'); | ||
| } | ||
|
|
||
| private function assertFails(mixed $value, ?string $expected_message = null): void | ||
| { | ||
| $rule = new FilenameRule(); | ||
| $msg = null; | ||
| $rule->validate('file_name', $value, function ($message) use (&$msg): void { $msg = $message; }); | ||
| self::assertNotNull($msg, 'Expected "' . var_export($value, true) . '" to fail but it passed.'); | ||
| if ($expected_message !== null) { | ||
| self::assertSame($expected_message, $msg); | ||
| } | ||
| } | ||
|
|
||
| public function testValidFilenames(): void | ||
| { | ||
| $this->assertPasses('test.jpg'); | ||
| $this->assertPasses('IMG_1234.png'); | ||
| $this->assertPasses('.hidden'); | ||
| $this->assertPasses('file with spaces.jpg'); | ||
| $this->assertPasses('café.jpg'); | ||
| $this->assertPasses('a'); | ||
| $this->assertPasses('file.tar.gz'); | ||
| } | ||
|
|
||
| public function testNonStringValues(): void | ||
| { | ||
| $this->assertFails(123, ':attribute is not a string.'); | ||
| $this->assertFails(1.5, ':attribute is not a string.'); | ||
| $this->assertFails(true, ':attribute is not a string.'); | ||
| $this->assertFails(null, ':attribute is not a string.'); | ||
| $this->assertFails([], ':attribute is not a string.'); | ||
| $this->assertFails(['file.jpg'], ':attribute is not a string.'); | ||
| } | ||
|
|
||
| public function testContainsDirectory(): void | ||
| { | ||
| $this->assertFails('foo/bar.jpg', ':attribute contains a directory, give proper filename.'); | ||
| $this->assertFails('/etc/passwd', ':attribute contains a directory, give proper filename.'); | ||
| $this->assertFails('/file.jpg', ':attribute contains a directory, give proper filename.'); | ||
| $this->assertFails('a/b/c.jpg', ':attribute contains a directory, give proper filename.'); | ||
| } | ||
|
|
||
| public function testPathTraversalAttempts(): void | ||
| { | ||
| $this->assertFails('.'); | ||
| $this->assertFails('../secret.jpg'); | ||
| $this->assertFails('test/Samples/../secret.jpg'); | ||
| $this->assertFails('../../etc/passwd'); | ||
| $this->assertFails('..\\..\\windows\\win.ini'); | ||
| $this->assertFails('foo\\bar.jpg'); | ||
| $this->assertFails('foo/../bar.jpg'); | ||
| } | ||
|
|
||
| public function testDoubleDotWithoutSeparatorStillFails(): void | ||
| { | ||
| // ".." alone is not treated as a directory by pathinfo(), but it must | ||
| // still be rejected because it is caught by the invalid-characters check. | ||
| $this->assertFails('..'); | ||
| $this->assertFails('a..b.jpg'); | ||
| } | ||
|
|
||
| public function testEmptyStringPasses(): void | ||
| { | ||
| // pathinfo('', PATHINFO_DIRNAME) === '' and it contains none of the | ||
| // forbidden substrings, so the rule itself does not flag it. Emptiness | ||
| // should be rejected by a separate `required` rule upstream. | ||
| $this->assertPasses(''); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.