Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions lib/web/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1645,19 +1645,7 @@ async function httpNetworkOrCacheFetch (
// includeCredentials is true, and request’s traversable for user prompts is
// a traversable navigable:
if (response.status === 401 && httpRequest.responseTainting !== 'cors' && includeCredentials && isTraversableNavigable(request.traversableForUserPrompts)) {
// 2. If request’s body is non-null, then:
if (request.body != null) {
// 1. If request’s body’s source is null, then return a network error.
if (request.body.source == null) {
return makeNetworkError('expected non-null body source')
}

// 2. Set request’s body to the body of the result of safely extracting
// request’s body’s source.
request.body = safelyExtractBody(request.body.source)[0]
}

// 3. If request’s use-URL-credentials flag is unset or isAuthenticationFetch is
// 2. If request’s use-URL-credentials flag is unset or isAuthenticationFetch is
// true, then:
if (request.useURLCredentials === undefined || isAuthenticationFetch) {
// 1. If fetchParams is canceled, then return the appropriate network error
Expand All @@ -1682,6 +1670,18 @@ async function httpNetworkOrCacheFetch (
return response
}

// 3. If request’s body is non-null, then:
if (request.body != null) {
// 1. If request’s body’s source is null, then return a network error.
if (request.body.source == null) {
return makeNetworkError('expected non-null body source')
}

// 2. Set request’s body to the body of the result of safely extracting
// request’s body’s source.
request.body = safelyExtractBody(request.body.source)[0]
}

// 4. Set response to the result of running HTTP-network-or-cache fetch given
// fetchParams and true.
fetchParams.controller.connection.destroy()
Expand Down
23 changes: 23 additions & 0 deletions test/fetch/401-statuscode-no-infinite-loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,26 @@ test('Receiving a 401 status code should not cause infinite retry loop', async (
const response = await fetch(`http://localhost:${server.address().port}`)
assert.strictEqual(response.status, 401)
})

test('Receiving a 401 status code should not fail for stream-backed request bodies', async (t) => {
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
res.statusCode = 401
res.end('Unauthorized')
}).listen(0)

t.after(closeServerAsPromise(server))
await once(server, 'listening')

const response = await fetch(`http://localhost:${server.address().port}`, {
method: 'PUT',
duplex: 'half',
body: new ReadableStream({
start (controller) {
controller.enqueue(Buffer.from('hello world'))
controller.close()
}
})
})

assert.strictEqual(response.status, 401)
})
Loading