bug: directory index route root priority is overwritten when wildcard is false
@TalhaHunter101 ci sta già lavorando.
Dal 11/10/2026.
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
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
- I have read the contribution guidelines.
- I have searched for similar issues before opening this one.
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
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di fastify/fastify-static
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
fastify/fastify-static#612 · 1 commento ·
-
10.1.4 regression: path spelling validation calls readdir() on every request, O(n) latency for large directoriesForse già presa @mcollina l’ha presa 40 giorni fa. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 52/100
fastify/fastify-static#613 ·
-
Use built-in glob from `node:fs`Forse già presa @Ankitraj-Sharma l’ha presa 36 giorni fa. Apertagood first issue
Difficoltà 3/5 1-2 giorni Idoneità per principianti 25/100
fastify/fastify-static#551 · 8 commenti · 1 reazione ·
-
feature request
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
fastify/fastify-static#432 · 2 commenti ·
-
discussion
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
fastify/fastify-static#385 · 2 commenti ·
Tutte le issue di fastify/fastify-static
Issue simili
-
clientV2: Application Info header separators show as missing-character boxes without a CJK fontApertabug new-client
Difficoltà 1/5 1-3 ore Idoneità per principianti 88/100
NUWCDIVNPT/stig-manager#2285 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
Fei2411/DeepSeek-Bot#922 · 1 reazione ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
DuarteSantos8/openGym#998 ·
I maintainer di solito rispondono entro 4 giorni
-
server errored
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
OpenTermsArchive/contrib-declarations#4273 ·
I maintainer di solito rispondono entro 1 giorno
-
needs-area-label
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
I maintainer di solito rispondono entro 2 giorni