os.networkInterfaces() can throw and crash the process when listening on 0.0.0.0

Open Beginner friendly
#7,268 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript, node.js

Research direction

Start in lib/server.js at getAddresses() and trace how logServerAddress() handles the addresses returned for a wildcard bind. Reproduce the failure by making os.networkInterfaces() throw, then check the server tests for address logging and add coverage for the fallback. Done means the server starts and reports the bound address when interface enumeration fails.

Written by the indexing model from the issue text.

Description

Describe the bug

When the server binds to the wildcard address 0.0.0.0, logServerAddress() calls getAddresses(), which calls os.networkInterfaces() unguarded. On platforms where enumerating the interface list is not permitted, os.networkInterfaces() throws, and because the call happens inside the server's listening event handler the exception escapes as an uncaught exception — the process dies instead of the server starting.

Concrete case: Android. A Node.js binary linked against glibc (e.g. a single-executable build) calls getifaddrs(), which needs CAP_NET_ADMIN for its NETLINK_ROUTE RTM_GETLINK dump and also reads /proc/net/if_inet6. Android denies both to ordinary apps, so getifaddrs() returns EACCES and Node throws:

SystemError [ERR_SYSTEM_ERROR]: A system error occurred: uv_interface_addresses returned Unknown system error 13 (Unknown system error 13)
    at Object.networkInterfaces (node:os:218:16)
    at getAddresses (/path/to/fastify/lib/server.js:...)
    at Object.logServerAddress (/path/to/fastify/lib/server.js:...)
    at Server.listeningEventHandler (/path/to/fastify/lib/server.js:...)

The same crash happens in any sandbox/container that forbids reading the interface list, not only Android. Note that binding to a specific address is unaffected — getAddresses() only calls os.networkInterfaces() for the 0.0.0.0 case.

Steps to reproduce
// repro.mjs
import os from 'node:os'
import Fastify from 'fastify'

// Stand in for a platform where interface enumeration is denied.
// (On Android this happens for real, see the stack trace above.)
const real = os.networkInterfaces
os.networkInterfaces = () => {
  throw Object.assign(new Error('uv_interface_addresses returned Unknown system error 13'), {
    code: 'ERR_SYSTEM_ERROR',
  })
}

const app = Fastify()
app.get('/', async () => 'hello')

await app.listen({ host: '0.0.0.0', port: 0 })
console.log('listening')

The await never settles: the exception is thrown in the listening event handler, so it is reported as an uncaught exception and the process exits with a non-zero code, instead of the server starting.

Expected behavior

Failing to enumerate the interface list should not be fatal. The server should still start, and the address list should fall back to the bound address (as it already does for a non-wildcard bind).

Suggested fix

Guard the call in getAddresses() and fall back to the bound address:

function getAddresses (address) {
  if (address.address === '0.0.0.0') {
    let interfaces
    try {
      interfaces = os.networkInterfaces()
    } catch {
      return [address.address]
    }
    return Object.values(interfaces).flatMap((iface) => {
      return iface.filter((iface) => iface.family === 'IPv4')
    }).sort((iface) => {
      /* c8 ignore next 2 */
      // Order the interfaces so that internal ones come first
      return iface.internal ? -1 : 1
    }).map((iface) => { return iface.address })
  }
  return [address.address]
}
System Info
  • fastify version: 5.8.5
  • Node.js version: v24.15.0
  • Platform: Android 12 (aarch64), Node linked against glibc
Dominant language
JavaScript
Stars
37.5k
Forks
3.2k
Avg merge
2d 1h
Merged PRs (30d)
22

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from fastify/fastify

All issues in fastify/fastify

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.