Skip to content

A free open source IT asset/license management system

Fixed GHSA-wq64-46j7-h5vr - don’t attempt to delete non-local avatars

snipe0d814b270750
diff --git a/app/Http/Requests/ImageUploadRequest.php b/app/Http/Requests/ImageUploadRequest.php
index 6022f6b6e2..699f3f98cd 100644
--- a/app/Http/Requests/ImageUploadRequest.php
+++ b/app/Http/Requests/ImageUploadRequest.php
@@ -186,20 +186,33 @@ class ImageUploadRequest extends Request
{
if ($item->{$db_fieldname} != '') {
+ // Absolute http(s) URLs (OAuth-sourced avatars from Google,
+ // Microsoft, Gravatar, etc.) do not point at anything on this
+ // disk and early return.
+ if (preg_match('#^https?://#i', (string) $item->{$db_fieldname}) === 1) {
+ $item->{$db_fieldname} = null;
+
+ return $item;
+ }
+
try {
// Same path normalization as handleImages. Branding callers
// pass '' for the disk root, and we don't want to produce a
// leading-slash key on S3.
$path = trim((string) $path, '/');
- $key = $path === '' ? $item->{$db_fieldname} : $path.'/'.$item->{$db_fieldname};
+
+ // Defense in depth against a stored value that carries
+ // path traversal (e.g. `../barcodes/target.png`). Every
+ // sanctioned writer of these image columns produces a bare
+ // filename, but a legacy row or a future writer that skips
+ // that step must not reach the delete with a
+ // composable-into-cross-directory key.
+ $filename = basename((string) $item->{$db_fieldname});
+ $key = $path === '' ? $filename : $path.'/'.$filename;
$deleted = Storage::disk('public')->delete($key);
// Only null the model reference if the delete actually
- // succeeded. Before, the field was cleared unconditionally
- // even when Storage::delete returned false (silent-fail
- // mode on the default local disk). The result was a model
- // row that reported "no image" while the file remained on
- // disk, orphaned.
+ // succeeded.
if ($deleted) {
$item->{$db_fieldname} = null;
} else {
diff --git a/app/Importer/UserImporter.php b/app/Importer/UserImporter.php
index 2ef7ef3734..1d7ac6a0f4 100644
--- a/app/Importer/UserImporter.php
+++ b/app/Importer/UserImporter.php
@@ -99,7 +99,7 @@ class UserImporter extends ItemImporter
$this->setItemFromCsvIfPresent($row, 'zip');
$this->setItemFromCsvIfPresent($row, 'employee_num');
$this->setItemFromCsvIfPresent($row, 'notes');
- $this->setItemFromCsvIfPresent($row, 'avatar');
+ $this->sanitizeAvatarFromCsv($row);
$this->setItemFromCsvIfPresent($row, 'scim_externalid');
$this->setItemFromCsvIfPresent($row, 'locale');
$this->setItemFromCsvIfPresent($row, 'ldap_import');
@@ -141,6 +141,35 @@ class UserImporter extends ItemImporter
$this->createUserIfNotExists($row, $csvId);
}
+ /**
+ * Store the imported avatar cell defensively. Local filenames are reduced
+ * to their basename, mirroring how AssetImporter and AccessoryImporter
+ * treat their `image` column. Absolute http(s) URLs are preserved so
+ * OAuth-sourced avatars (Google, Microsoft, Gravatar) still render
+ * correctly when a user record is re-imported. Without the basename step,
+ * a value like `../barcodes/target.png` reaches the profile image-delete
+ * sink and Flysystem normalizes the composed key into a cross-directory
+ * delete inside the public uploads tree.
+ */
+ private function sanitizeAvatarFromCsv(array $row): void
+ {
+ if (! $this->csvRowHas($row, 'avatar')) {
+ return;
+ }
+
+ $raw = (string) $this->findCsvMatch($row, 'avatar');
+
+ if ($raw === '') {
+ $this->item['avatar'] = null;
+
+ return;
+ }
+
+ $this->item['avatar'] = preg_match('#^https?://#i', $raw) === 1
+ ? $raw
+ : basename($raw);
+ }
+
/**
* Parse a pipe-separated company column value into an array of company IDs,
* creating companies that do not yet exist. Returns an empty array when the
diff --git a/tests/Feature/Importing/Api/ImportUsersTest.php b/tests/Feature/Importing/Api/ImportUsersTest.php
index 16fdb49966..9a52775cf5 100644
--- a/tests/Feature/Importing/Api/ImportUsersTest.php
+++ b/tests/Feature/Importing/Api/ImportUsersTest.php
@@ -577,4 +577,92 @@ class ImportUsersTest extends ImportDataTestCase implements TestsPermissionsRequ
$this->assertEquals("{$row['firstName']} {$row['lastName']}", $newUser->display_name);
}
+
+ /**
+ * GHSA-wq64-46j7-h5vr regression coverage: an imported avatar cell
+ * containing a relative traversal string ("../barcodes/target.png")
+ * used to persist verbatim into users.avatar, and the profile
+ * image-delete sink built a storage key by concatenating it onto
+ * "avatars/", giving a cross-directory delete inside the public
+ * uploads tree. The importer now reduces local filenames to their
+ * basename, mirroring how AssetImporter and AccessoryImporter treat
+ * their image cell.
+ */
+ #[Test]
+ public function import_reduces_avatar_path_traversal_to_basename(): void
+ {
+ $importFileBuilder = ImportFileBuilder::new(['avatar' => '../barcodes/target.png']);
+ $row = $importFileBuilder->firstRow();
+ $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create());
+ $this->importFileResponse(['import' => $import->id])->assertOk();
+
+ $newUser = User::query()->where('username', $row['username'])->sole();
+
+ $this->assertSame('target.png', $newUser->avatar);
+ }
+
+ /**
+ * Bare local filenames pass through untouched (basename of a bare
+ * name returns the same string), so importing a user with a
+ * pre-existing avatar filename does not corrupt the reference.
+ */
+ #[Test]
+ public function import_preserves_bare_avatar_filename(): void
+ {
+ $importFileBuilder = ImportFileBuilder::new(['avatar' => 'user-42-avatar.png']);
+ $row = $importFileBuilder->firstRow();
+ $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create());
+ $this->importFileResponse(['import' => $import->id])->assertOk();
+
+ $newUser = User::query()->where('username', $row['username'])->sole();
+
+ $this->assertSame('user-42-avatar.png', $newUser->avatar);
+ }
+
+ /**
+ * OAuth-sourced avatars (Google, Microsoft, Gravatar) arrive as
+ * absolute http(s) URLs and must be preserved verbatim. A naive
+ * basename() applied unconditionally would strip the scheme and host,
+ * leaving a value the display path then treats as a local filename
+ * and fails to render.
+ */
+ #[Test]
+ public function import_preserves_avatar_url_verbatim(): void
+ {
+ $avatarUrl = 'https://lh3.googleusercontent.com/a/ACg8ocKexampleAvatarPathSegment';
+ $importFileBuilder = ImportFileBuilder::new(['avatar' => $avatarUrl]);
+ $row = $importFileBuilder->firstRow();
+ $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create());
+ $this->importFileResponse(['import' => $import->id])->assertOk();
+
+ $newUser = User::query()->where('username', $row['username'])->sole();
+
+ $this->assertSame($avatarUrl, $newUser->avatar);
+ }
+
+ /**
+ * Empty avatar cell on import clears the DB value (present-but-empty
+ * semantics that every other importer field uses).
+ */
+ #[Test]
+ public function import_with_empty_avatar_cell_clears_the_column(): void
+ {
+ $user = User::factory()->create(['avatar' => 'user-42-avatar.png']);
+ $importFileBuilder = ImportFileBuilder::new([
+ 'username' => $user->username,
+ 'avatar' => '',
+ ]);
+ $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create());
+ $this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
+
+ $this->assertNull($user->fresh()->avatar);
+ }
}
diff --git a/tests/Feature/Uploads/ImageUploadRequestFailurePathsTest.php b/tests/Feature/Uploads/ImageUploadRequestFailurePathsTest.php
index 9869236710..417464fbfe 100644
--- a/tests/Feature/Uploads/ImageUploadRequestFailurePathsTest.php
+++ b/tests/Feature/Uploads/ImageUploadRequestFailurePathsTest.php
@@ -110,6 +110,62 @@ class ImageUploadRequestFailurePathsTest extends TestCase
);
}
+ /**
+ * GHSA-wq64-46j7-h5vr defense-in-depth: a stored avatar containing
+ * path traversal ("../foo/target.png") must not compose into a
+ * cross-directory storage key at the delete sink. The write-side
+ * fix (UserImporter basename) closes the specific vector. This test
+ * covers the sink regardless of which writer populated the field.
+ */
+ public function test_delete_existing_image_strips_traversal_from_stored_value(): void
+ {
+ Storage::fake('public');
+ $publicDisk = Storage::disk('public');
+ $publicDisk->put('barcodes/target.png', 'attacker target');
+ $publicDisk->put('avatars/legitimate.png', 'the caller owns this');
+
+ $user = User::factory()->create(['avatar' => '../barcodes/target.png']);
+
+ $request = app(\App\Http\Requests\ImageUploadRequest::class);
+ $request->deleteExistingImage($user, 'avatars', 'avatar');
+
+ $this->assertTrue(
+ $publicDisk->exists('barcodes/target.png'),
+ 'The cross-directory file must remain. basename() should have reduced the composed key to avatars/target.png, which does not exist.',
+ );
+ }
+
+ /**
+ * OAuth-sourced avatars (Google, Microsoft, Gravatar) arrive as
+ * absolute http(s) URLs. The delete sink must not try to compose
+ * them into a storage key. `avatars/https://...` never points at
+ * anything on this disk, and any embedded ".." would slip past
+ * basename. Skip the storage delete entirely and null the DB
+ * reference so the user's "remove avatar" action still takes effect.
+ */
+ public function test_delete_existing_image_skips_storage_delete_for_url_avatar_but_nulls_db(): void
+ {
+ Storage::fake('public');
+ $publicDisk = Storage::disk('public');
+ $publicDisk->put('avatars/other-user.png', 'not the attackers');
+
+ $user = User::factory()->create([
+ 'avatar' => 'https://lh3.googleusercontent.com/a/ACg8ocKexample',
+ ]);
+
+ $request = app(\App\Http\Requests\ImageUploadRequest::class);
+ $result = $request->deleteExistingImage($user, 'avatars', 'avatar');
+
+ $this->assertNull(
+ $result->avatar,
+ 'The user requested avatar removal, so the DB reference must clear even though the URL points off-disk.',
+ );
+ $this->assertTrue(
+ $publicDisk->exists('avatars/other-user.png'),
+ 'No file on this disk should be touched when the stored value is an http(s) URL.',
+ );
+ }
+
public function test_successful_write_still_replaces_existing_image(): void
{
// Sanity: the fix must not regress the happy path. Successful writes
diff --git a/tests/Support/Importing/UsersImportFileBuilder.php b/tests/Support/Importing/UsersImportFileBuilder.php
index 619f36c262..8744d0f1f7 100644
--- a/tests/Support/Importing/UsersImportFileBuilder.php
+++ b/tests/Support/Importing/UsersImportFileBuilder.php
@@ -29,6 +29,7 @@ class UsersImportFileBuilder extends FileBuilder
protected function getDictionary(): array
{
return [
+ 'avatar' => 'avatar',
'companyName' => 'Company',
'email' => 'email',
'employeeNumber' => 'Employee Number',
@@ -50,6 +51,7 @@ class UsersImportFileBuilder extends FileBuilder
$faker = fake();
return [
+ 'avatar' => '',
'companyName' => $faker->company,
'email' => $faker->safeEmail(),
'employeeNumber' => $faker->uuid,