Skip to content

Uploading a file whose name ends in a long number loops forever when the name is taken #19978

Backlog

Description

@mGallee

Describe the Bug

Uploading a file whose name ends in a dash and more than 15 digits never finishes when a document with that filename already exists. getSafeFileName keeps calling incrementName, which returns the same name, so the while loop runs forever, querying the database on every pass. The request never answers. In next dev the server's heap grew to 12 GB and the process crashed with "JavaScript heap out of memory".

incrementName (packages/payload/src/uploads/getSafeFilename.ts) adds 1 to the digits after the last dash with Number():

const regex = /(.*)-(\d+)$/
const found = baseFilename.match(regex)
// ...
const incremented = Number(matchedNumber) + 1
incrementedName = `${matchedName}-${incremented}`

Above Number.MAX_SAFE_INTEGER, adding 1 gives back the same number:

Name incrementName(name) incrementName again
beleg-7.pdf beleg-8.pdf beleg-9.pdf
beleg-10000000000000000000.pdf beleg-10000000000000000000.pdf the same
beleg-12345678901234567890.pdf beleg-12345678901234567000.pdf the same

getSafeFileName has no retry limit, so once the candidate stops changing it never returns.

We hit it with real receipts: names ending in long reference numbers, uploaded a second time after a failed import.

#19158 fixed this among other filename edge cases ("stop making progress when numeric suffixes exceed JavaScript's safe integer range") but was closed because there was no issue tied to it. This is that issue.

Link to the code that reproduces this issue

https://github.com/mGallee/payload/tree/fix/upload-filename-long-number-loop

Reproduction Steps

The branch adds one test to test/_community/int.spec.ts, using the community suite's existing media collection (upload: true):

test(
  'a second upload of a name ending in a long number gets a new name',
  { timeout: 30_000 },
  async ({ payload }) => {
    const file = {
      name: 'beleg-10000000000000000000.txt',
      data: Buffer.from('hello'),
      mimetype: 'text/plain',
      size: 5,
    }

    const first = await payload.create({ collection: mediaSlug, data: {}, file })
    const second = await payload.create({ collection: mediaSlug, data: {}, file })

    expect(second.filename).not.toEqual(first.filename)
  },
)
  1. pnpm install
  2. PAYLOAD_DATABASE=sqlite pnpm test:int test/_community/int.spec.ts
  3. The test does not fail after its 30 s timeout. It keeps running, because the loop never yields a result, until the process is killed; I stopped it after 4 minutes. Changing the name to beleg-7.txt makes all 3 tests pass in about 4 s.

We found it through the REST API: POST /api/<upload collection> with a file of such a name, when a document with that name already existed. That request never answered, and the dev server's heap grew until it crashed.

Expected: the second upload gets a free name (for example beleg-10000000000000000000-1.txt, which is what a name that cannot be incremented safely could fall back to), or a clear error after a bounded number of attempts.

Which area(s) are affected?

area: core

Environment Info

Found in a project on payload 3.93.0, @payloadcms/db-postgres 3.93.0, next 16.4.6, Node.js 24.22.3, macOS (arm64).
Reproduced on payloadcms/payload main at 7f2d2ff with @payloadcms/db-sqlite. The code is unchanged in v3.94.2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugPayload OSSarea: core

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions