reply.fromParameters() drops query params after a second "?"

未关闭 适合新手
#491 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

@RaphaelFakhri 已经在做这个了。

开始于 2026年11月5日。

  • #492 来自 @RaphaelFakhri —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃

调研方向

Issue 指出了 extractUrlComponents() 和有问题的调用 urlString.split('?', 2);找到该函数并阅读周围的 URL 处理代码。使用提供的、包含第二个 ? 的请求复现问题,然后检查两个查询参数是否都传递到了 upstream。查询字符串得到保留且相关测试覆盖此情况后,即为完成。

由索引模型根据 Issue 内容生成。

描述

Prerequisites
  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported
Fastify version

5.x

Plugin version

11.6.6

Node.js version

22.x

Description

reply.fromParameters() truncates the query string at the second ?. Everything after it, including later &key=value pairs, is lost.

A literal ? is valid inside a query string (RFC 3986, section 3.4), and it is common in redirect parameters such as ?next=/a?b=1.

The cause is urlString.split('?', 2) in extractUrlComponents(). String.prototype.split with a limit discards the rest of the string instead of keeping it in the last element.

The default handler is not affected, because it removes the query string from the destination and @fastify/reply-from forwards the original one. The problem appears when you use reply.fromParameters() as documented in the README and pass its url to reply.from().

Steps to reproduce
const Fastify = require('fastify')
const proxy = require('@fastify/http-proxy')

const origin = Fastify()
origin.get('/api2/echo', async (request) => request.query)

const server = Fastify()
server.register(proxy, {
  upstream: 'http://localhost:3001',
  prefix: '/api',
  rewritePrefix: '/api2',
  preHandler (request, reply, done) {
    const { url, options } = reply.fromParameters(request.url, request.params, '/api')
    reply.from(url, options)
  }
})

await origin.listen({ port: 3001 })
await server.listen({ port: 3000 })
curl 'http://localhost:3000/api/echo?next=/a?b=1&c=2'
Expected behavior

The upstream receives {"next":"/a?b=1","c":"2"}.

The actual response is {"next":"/a"}.

主要语言
JavaScript
星标
394
派生
105
平均合并
9 小时 34 分钟
30 天内合并 PR
2

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

fastify/fastify-http-proxy 的其他 Issue

查看 fastify/fastify-http-proxy 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。