Skip to content

plugin-cloud-storage: clientUploads renames a file for a name held in another prefix, ignoring filenameCompoundIndex #19246

Open

Description

@rastandy

Describe the Bug

With upload.filenameCompoundIndex: ['filename', 'prefix'], a filename is unique per prefix: the default unique index on filename is replaced by a compound one. Server uploads respect this, because generateFileData passes data.prefix to getSafeFileName. Direct uploads (clientUploads) don't: resolveSignedURLKey calls getSafeFileName without a prefix, so the duplicate lookup runs across the whole collection. A file is renamed (logo-1.png) because a different prefix already holds logo.png.

When each tenant of a multi-tenant app has its own prefix, one tenant's uploads are renamed because of another tenant's filenames, and the rename shows that the other tenant has a file with that name.

Expected: when the collection's filename is unique per prefix, signing looks for a duplicate only within the signed prefix, the same lookup generateFileData does on save.

Link to the code that reproduces this issue

https://github.com/payloadcms/payload/blob/v3.92.1/packages/plugin-cloud-storage/src/utilities/resolveSignedURLKey.ts#L45-L56

The reproduction is the standalone script below, so no repository is needed. It calls the real signing endpoint of @payloadcms/storage-s3 with a stub request.

Reproduction Steps

  1. npm i @payloadcms/[email protected] [email protected] in an empty ESM package ("type": "module").
  2. Run the script with node repro.mjs.
  3. It prints { docPrefix: 'media/site-b', filename: 'logo-1.png' }. Expected: filename: 'logo.png', because no document in media/site-b holds that name.
// One document already holds `logo.png` in prefix `media/site-a`;
// a direct upload of `logo.png` is signed for `media/site-b`.
import { s3Storage } from '@payloadcms/storage-s3'

const upload = { filenameCompoundIndex: ['filename', 'prefix'] }
const config = await s3Storage({
  bucket: 'bucket',
  clientUploads: true,
  collections: { media: { prefix: 'media' } },
  config: { region: 'us-east-1', credentials: { accessKeyId: 'a', secretAccessKey: 's' } },
})({ collections: [{ slug: 'media', fields: [], upload }], endpoints: [] })
const sign = config.endpoints.find((e) => e.path === '/storage-s3-generate-signed-url').handler

const docs = [{ filename: 'logo.png', prefix: 'media/site-a' }]
const findOne = async ({ where }) =>
  docs.find((d) => d.filename === where.filename.equals && (!where.prefix || d.prefix === where.prefix.equals)) ?? null

const res = await sign({
  user: { id: 1, collection: 'users' },
  payload: {
    secret: 'secret',
    config: { upload: {} },
    collections: { media: { config: { slug: 'media', upload, access: { create: () => true } } } },
    db: { findOne },
  },
  json: async () => ({ collectionSlug: 'media', docPrefix: 'media/site-b', filename: 'logo.png', filesize: 10, mimeType: 'image/png' }),
})
const { docPrefix, filename } = await res.json()
console.log({ docPrefix, filename }) // filename: 'logo-1.png'; expected 'logo.png'

In an app, with an upload collection that has filenameCompoundIndex: ['filename', 'prefix'] and s3Storage with clientUploads: true:

  1. From the admin, upload logo.png into prefix A.
  2. Upload logo.png into prefix B.
  3. The second file is saved as logo-1.png.

A possible fix. We apply it as a local patch. It resolves the prefix before the lookup, and scopes the lookup to that prefix only when the filename is unique per prefix. Without the compound index the unique index on filename is global, so the lookup must stay global:

  const rawBaseDocPrefix = useCompositePrefixes ? docPrefix : docPrefix || collectionPrefix
  const { sanitizedDocPrefix } = buildUploadPrefix({
    collectionPrefix,
    docPrefix: rawBaseDocPrefix,
    useCompositePrefixes,
  })
  const uploadConfig = req.payload.collections[collectionSlug]?.config.upload
  const uniquePerPrefix =
    typeof uploadConfig === 'object' && uploadConfig.filenameCompoundIndex?.includes('prefix')
  const sanitizedFilename = await getSafeFileName({
    collectionSlug,
    desiredFilename: getSanitizedUploadFilename(filename),
    prefix: uniquePerPrefix ? sanitizedDocPrefix : undefined,
    req,
  })

After this change, the prefix that signing looks in is the same prefix generateFileData looks in on save: create copies the receipt's prefix into data.prefix (getUploadDestination) before generateFileData runs. So the signed filename is the stored one.

Which area(s) are affected?

plugin: cloud-storage, plugin: storage-*

Environment Info

payload: 3.91.0 (reproduced with 3.92.1)
@payloadcms/plugin-cloud-storage: 3.91.0 (reproduced with 3.92.1)
@payloadcms/storage-s3: 3.91.0 (reproduced with 3.92.1)
@payloadcms/db-postgres: 3.91.0
next: 16.4.2
node: 24.14.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions