Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 5 additions & 2 deletions lib/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ function middie (complete, options = {}) {
const holder = pool.get()
holder.req = req
holder.res = res
holder.normalizedUrl = normalizePathForMatching(sanitizeUrl(req.url), normalizationOptions)
holder.normalizedReqUrl = normalizePathForMatching(req.url, normalizationOptions)
const sanitizedUrl = sanitizeUrl(req.url)
const queryString = req.url.slice(sanitizedUrl.length)
const normalizedPath = normalizePathForMatching(sanitizedUrl, normalizationOptions)
holder.normalizedUrl = normalizedPath
holder.normalizedReqUrl = normalizedPath + queryString
holder.context = ctx
holder.done()
}
Expand Down
31 changes: 31 additions & 0 deletions test/req-url-stripping.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,37 @@ test('req.url stripping with trailing slash', async (t) => {
t.assert.strictEqual(capturedUrl, '/data', '/secret/data/ should strip to /data')
})

test('req.url stripping preserves query string', async (t) => {
const app = Fastify()
t.after(() => app.close())

await app.register(middiePlugin)

let capturedUrl = null

app.use('/api', (req, _res, next) => {
capturedUrl = req.url
next()
})

app.get('/api/resource', async () => ({ ok: true }))

// Query string should be preserved after prefix stripping
capturedUrl = null
await app.inject({ method: 'GET', url: '/api/resource?foo=bar&baz=qux' })
t.assert.strictEqual(capturedUrl, '/resource?foo=bar&baz=qux', 'query string should be preserved')

// Simple query string
capturedUrl = null
await app.inject({ method: 'GET', url: '/api/resource?key=value' })
t.assert.strictEqual(capturedUrl, '/resource?key=value', 'simple query string should be preserved')

// No query string should still work
capturedUrl = null
await app.inject({ method: 'GET', url: '/api/resource' })
t.assert.strictEqual(capturedUrl, '/resource', 'no query string should work normally')
})

test('req.url stripping with all normalization options combined', async (t) => {
const app = Fastify({
routerOptions: {
Expand Down