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

Aperta Adatta ai principianti
#7,268 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript, node.js

Direzione di ricerca

Inizia da lib/server.js, in corrispondenza di getAddresses(), e segui il modo in cui logServerAddress() gestisce gli indirizzi restituiti per un binding wildcard. Riproduci l’errore facendo sì che os.networkInterfaces() generi un’eccezione, poi controlla i test del server relativi alla registrazione degli indirizzi e aggiungi test per il fallback. Il lavoro è terminato quando il server si avvia e segnala l’indirizzo a cui è associato se l’enumerazione delle interfacce non riesce.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
Lingua principale
JavaScript
Stelle
37.5k
Fork
3.2k
Merge medio
2g 1h
PR unite (30g)
22

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di fastify/fastify

Tutte le issue di fastify/fastify

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.