Skip to content

fetch: following a 307 for a PUT with empty body fails #2027

@macno

Description

@macno

Bug Description

following an error with octokit, we discovered that a fetch with method PUT (but I guess also other methods too) and an empty body {body: ''} that receives a 307 from the server, fails with the code AssertionError [ERR_ASSERTION]: '' == true

this is the error we see with octokit

RequestError [HttpError]: fetch failed
    at /app/node_modules/@octokit/request/dist-node/index.js:110:11
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Job.doExecute (/app/node_modules/bottleneck/light.js:405:18) {
  status: 500,
  request: {
    method: 'PUT',
    url: 'https://api.github.com/repos/[REDACTED]/pulls/2764/merge',
    headers: {
      accept: 'application/vnd.github.v3+json',
      'user-agent': 'octokit.js/2.0.7 octokit-core.js/4.0.5 Node.js/18.13.0 (linux; x64)',
      authorization: 'token [REDACTED]'
    },
    body: '',
    request: {
      hook: [Function: bound bound register],
      retryCount: 3,
      retries: 3,
      retryAfter: 16
    }
  }
}

Reproducible By

> const headers = {
..       accept: 'application/vnd.github.v3+json',
...       'user-agent': 'octokit.js/2.0.14 octokit-core.js/4.2.0 Node.js/18.15.0 (darwin; arm64)',
...       authorization: 'token [REDACTED]' };
> await globalThis.fetch('https://api.github.com/[redacted]/pulls/2772/merge', { method: 'PUT', headers, body: '' })
Uncaught TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:14062:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async REPL15:1:33 {
  cause: AssertionError [ERR_ASSERTION]: '' == true
      at httpRedirectFetch (node:internal/deps/undici/undici:13682:9)
      at httpFetch (node:internal/deps/undici/undici:13638:28)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async schemeFetch (node:internal/deps/undici/undici:13546:18)
      at async node:internal/deps/undici/undici:13422:20
      at async mainFetch (node:internal/deps/undici/undici:13418:20) {
    generatedMessage: true,
    code: 'ERR_ASSERTION',
    actual: '',
    expected: true,
    operator: '=='
  }
}

Expected Behavior

that fetch follows the redirect without any error

Logs & Screenshots

Environment

we see the error with the following envs:

  • alpine 3.17 node 18.13 (amd64)
  • alpine 3.17 node 18.15 (amd64 / arm64)
  • macos 13.2.1 node 18.15 (arm64)

Additional context

Issue seems to be here because assert('') throws an error
I will propose to drop the assertion since a check that verifies body.source is not null is already present here

Happy to provide a PR if needed

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions