Repository navigation
fix(task-plugin): set multipart content type for upstream requests - #7655
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (2)
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review. Walkthrough
ChangesMultipart request headers
Priority: ⬇️ Low Estimated code review effort: 2 (Simple) | ~8 minutes Change: Bug fix Suggested reviewers: Merge Risk: ⚪ Minimal · up to The rebuilt multipart body and upstream header use the same boundary, and the test checks that pairing. No actionable merge-blocking risk remains in the reviewed changes. Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to The change aligns multipart headers with the generated request body without changing destinations or access privileges. No introduced security concern was established, but exceptional execution paths have only partial validation. Retained concerns Security review detailsSecurity Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. A rabbit checks the headers with care, Comment |
BuildRequestBody stores writer.FormDataContentType() on the client request right before BuildRequestHeader runs, so the media type and boundary checks could never fail. Copy the value directly, as the sora adaptor did before task plugins. The test plugin now forwards the client's Content-Type, whose boundary differs from the rebuilt body, so the test also fails if a plugin header wins over the host value.
…uantumNous#7655) * fix(task-plugin): set multipart content type for upstream requests * refactor(task-plugin): copy the rebuilt multipart Content-Type as is BuildRequestBody stores writer.FormDataContentType() on the client request right before BuildRequestHeader runs, so the media type and boundary checks could never fail. Copy the value directly, as the sora adaptor did before task plugins. The test plugin now forwards the client's Content-Type, whose boundary differs from the rebuilt body, so the test also fails if a plugin header wins over the host value. --------- Co-authored-by: CaIon <[email protected]>
…uantumNous#7655) * fix(task-plugin): set multipart content type for upstream requests * refactor(task-plugin): copy the rebuilt multipart Content-Type as is BuildRequestBody stores writer.FormDataContentType() on the client request right before BuildRequestHeader runs, so the media type and boundary checks could never fail. Copy the value directly, as the sora adaptor did before task plugins. The test plugin now forwards the client's Content-Type, whose boundary differs from the rebuilt body, so the test also fails if a plugin header wins over the host value. --------- Co-authored-by: CaIon <[email protected]>
English template:
.github/PULL_REQUEST_TEMPLATE/en.mdImportant
🔗 关联任务 / Related Issue
新功能请填写下方 Issue 编号;若还没有对应 Issue,请先自行创建。功能讨论请放在 Issue 中进行。
改动较大或方向性变更,请先在关联 Issue 中与维护者达成一致,再提交 PR。
Bug 修复请关联对应 Issue。设计取舍、理解偏差或预期不一致,更适合作为讨论或功能请求。
Closes #
🚀 变更类型 / Type of change
📝 变更描述 / Description
任务插件中,当构造钩子返回的
bodyType为multipart时,宿主会重新构造请求体:此时会生成一个新的boundary,宿主在
DoTaskApiRequest重新构建了一个上游请求:新的
req.Header并不会自动继承c.Request.Header,而BuildRequestHeader只复制插件声明的 Header:这就导致了实际发往上游的请求中缺少了由宿主重新生成的
Content-Type,虽然插件允许在构造钩子返回 Header,但由于 Content-Type boundary 由宿主构建请求体时动态生成,无法在插件中动态获取或使用ctx.requestHeaders["Content-Type"]解决这个问题 (boundary不一致会导致multipart: NextPart: EOF。修复方案:
在
BuildRequestHeader中为BodyType为multipart类型的请求设置由宿主生成的Content-Type:📸 运行证明 / Proof of Work
未修复前:
由于缺少
Content-Type,所以上游无法正常解析body内容:{"error":{"code":"","message":"未指定模型名称,模型名称不能为空 (request id: ...)","type":"new_api_error"}} {"error":{"message":"failed to parse request body","type":"invalid_request_error"}}当在插件中的构造钩子声明
"Content-Type": ctx.requestHeaders["Content-Type"],则会因为 boundary 不一致导致无法正常解析请求体:{"error":{"message":"read multipart body: multipart: NextPart: EOF","type":"invalid_request_error"}}修复后:
修正后构建的上游请求正常携带由宿主生成的
Content-Type。Test:
go test ./relay/channel/task/jsplugin: ok github.com/QuantumNous/new-api/relay/channel/task/jsplugin 2.775s✅ 提交前检查项 / Checklist
New feature,我已关联对应 Issue;若尚无 Issue,我已先自行创建。go build通过或测试通过,不视为有效证明。Summary by CodeRabbit
Content-Typeheader, preserving its boundary so multipart data can be parsed correctly. Non-multipart submissions continue to use the plugin-providedContent-Type.