bug: directory index route root priority is overwritten when wildcard is false

Aperta Adatta ai principianti
#617 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

@TalhaHunter101 ci sta già lavorando.

Dal 11/10/2026.

  • #618 di @TalhaHunter101 — aperta
  • #622 di @eknathdj — aperta
  • #623 di @DoctorGuidance — aperta

Valutazione

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

Direzione di ricerca

Il rapporto indica le righe 206–209 di index.js e descrive una riproduzione con due directory radice e wildcard: false. Leggi la logica di quella route dell’indice ed esegui la riproduzione o i test esistenti pertinenti. Il lavoro è completato quando le richieste alle directory continuano a servire il file indice corrispondente dalla prima directory radice nell’ordine di priorità configurato.

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

Descrizione

Prerequisites
Describe the Bug

When wildcard: false and multiple root directories are configured (e.g. root: [root1, root2]), the documentation states that directories are searched in priority order ("first found, first served"):

An array of directories can be provided to serve multiple static directories under a single prefix. Files are served in a "first found, first served" manner, so list directories in order of priority.

However, in index.js lines 206–209:

const key = path.posix.basename(route)
if (indexes.has(key) && !indexDirs.has(key)) {
  indexDirs.set(path.posix.dirname(route), rootPath)
}

The condition checks !indexDirs.has(key), where key is the filename (e.g. 'index.html' or 'foobar.html'), but the map entries in indexDirs use directory paths (path.posix.dirname(route)) as keys!

Because the filename string is never a key in indexDirs (which contains directory paths like '/' or '/sub'), !indexDirs.has(key) always evaluates to true whenever an index file is found in subsequent roots with a different alternative index name or secondary root. As a result, subsequent lower-priority roots overwrite higher-priority roots in indexDirs.set(path.posix.dirname(route), rootPath), causing directory requests (e.g. / or /<dir>/) to serve from the wrong root!

Steps to Reproduce
const Fastify = require('fastify')
const fastifyStatic = require('@fastify/static')
const path = require('node:path')
const fs = require('node:fs')

// Setup two roots: root1 (high priority) and root2 (lower priority)
const root1 = path.join(__dirname, 'root1')
const root2 = path.join(__dirname, 'root2')
fs.mkdirSync(path.join(root1, 'sub'), { recursive: true })
fs.mkdirSync(path.join(root2, 'sub'), { recursive: true })

fs.writeFileSync(path.join(root1, 'sub', 'index.htm'), 'from root1')
fs.writeFileSync(path.join(root2, 'sub', 'index.html'), 'from root2')

const fastify = Fastify()
fastify.register(fastifyStatic, {
  root: [root1, root2],
  wildcard: false,
  index: ['index.htm', 'index.html']
})

await fastify.listen({ port: 0 })
const port = fastify.server.address().port
const res = await fetch(`http://localhost:${port}/sub/`)
console.log(await res.text())
// Expected: 'from root1'
// Actual: 'from root2' (lower-priority root overwrote higher-priority root!)
Root Cause

indexDirs is a Map from directory path to rootPath. Line 207 checks whether indexDirs contains key (the basename), rather than checking whether indexDirs already has the directory path path.posix.dirname(route).

Expected Behavior

indexDirs should check whether the directory path already has a registered root (!indexDirs.has(dir)), preserving the higher-priority root.

Lingua principale
JavaScript
Stelle
499
Fork
139
Merge medio
4g 21h
PR unite (30g)
4

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-static

Tutte le issue di fastify/fastify-static

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.