Skip to content

Conversation

loli0123456789
Copy link

Ⅰ. Describe what this PR did

Fix the transformer plugin issue: file information is lost when processing body with files that the body type is multipart/form-data

Ⅱ. Does this pull request fix one issue?

fixes #2257

Ⅲ. Why don't you add test cases (unit test/integration test)?

Comprehensive local testing has been performed to verify the fix works correctly with various multipart/form-data scenarios.

Ⅳ. Describe how to verify it

  1. Deploy the plugin using higress/gateway:v2.1.5 image with httpbin as the test backend
  2. Send POST requests with multiple files to httpbin directly and record the response
  3. Send the same POST requests through the gateway and verify that the response matches the direct httpbin response (particularly the files section)
  4. Verify that file information (filenames, content-types, and file content) is properly preserved in the gateway response

Ⅴ. Special notes for reviews

  • This fix only affects ContentTypeMultipartForm cases, no impact on other content types
  • Only processes fields with non-empty filenames, no impact on existing ContentTypeMultipartForm form data fields
  • File content is base64 encoded/decoded during processing, which may have minor performance impact
  • Backward compatibility is maintained for all existing functionality

…ssing body with files that the body type is multipart/form-data
@CLAassistant
Copy link

CLAassistant commented Aug 1, 2025

CLA assistant check
All committers have signed the CLA.

Copy link

lingma-agents bot commented Aug 1, 2025

修复 transformer 插件处理 multipart/form-data 时文件信息丢失的问题

变更概述
  • 问题修复

    • 修复了在处理 multipart/form-data 类型请求体时,文件信息(如文件名、内容类型和文件内容)丢失的问题。
    • 在解析阶段,对带有文件名的字段进行特殊处理,提取并保存文件名、内容类型及文件内容(Base64 编码)。
    • 在构造阶段,根据保存的文件信息重新构建包含文件的 multipart 表单数据,确保文件信息完整传递。
  • 实现细节

    • 引入 encoding/base64net/textproto 包以支持文件内容的编码与 multipart header 构造。
    • 修改 parseBody 函数,增加对文件字段的识别逻辑,将文件名、内容类型和内容分别存储为 .filename.content-type.content 后缀的键。
    • 修改 constructBody 函数,优先处理文件字段,通过 .filename.content 键还原文件部分,并使用 textproto.MIMEHeader 设置正确的 Content-Disposition 和 Content-Type 头。
    • 增加了处理过程中对字段重复处理的避免机制,提升代码健壮性。
变更文件
文件路径 变更说明
plugins/​wasm-go/​extensions/​transformer/​utils.​go 增加了对 multipart/form-data 中文件字段的处理逻辑,包括解析时提取文件信息并 Base64 编码,以及构造时还原文件字段。同时引入了必要的包以支持该功能。
时序图
sequenceDiagram
    participant PB as parseBody
    participant CB as constructBody
    PB->>PB: 解析 multipart/form-data
    PB->>PB: 识别文件字段
    PB->>PB: 提取并保存文件名、内容类型和内容(Base64)
    PB-->>CB: 返回处理后的 body 数据
    CB->>CB: 构造 multipart/form-data
    CB->>CB: 优先处理文件字段
    CB->>CB: 使用 .filename 和 .content 还原文件部分
    CB->>CB: 设置 Content-Disposition 和 Content-Type
    CB-->>User: 返回构造完成的请求体
Loading

💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @lingma-agents 分析这个方法的性能瓶颈并提供优化建议。

  • @lingma-agents 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @lingma-agents 请总结上述讨论并提出解决方案。

  • @lingma-agents 请根据讨论内容生成优化代码。

@loli0123456789
Copy link
Author

@CH3CHO @erasernoob @johnlanni @rinfx

Hi team, could you please review this PR and approve the pending workflows? The CLA has been signed and automated checks are passing.

@codecov-commenter
Copy link

codecov-commenter commented Aug 3, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 44.99%. Comparing base (ef31e09) to head (ebce2ce).
⚠️ Report is 688 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2703      +/-   ##
==========================================
+ Coverage   35.91%   44.99%   +9.08%     
==========================================
  Files          69       82      +13     
  Lines       11576    13347    +1771     
==========================================
+ Hits         4157     6006    +1849     
+ Misses       7104     6993     -111     
- Partials      315      348      +33     

see 80 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@loli0123456789
Copy link
Author

loli0123456789 commented Aug 4, 2025

@hanxiantao 感谢建议,无用log注释代码已删除。


@hanxiantao Thanks for the suggestion, useless log comment code has been removed.

@hanxiantao hanxiantao self-requested a review August 4, 2025 08:56
@hanxiantao
Copy link
Collaborator

hanxiantao commented Aug 4, 2025

cc @WeixinX


Cao Cao @weix

@johnlanni
Copy link
Collaborator

@loli0123456789 可以参考这里的例子补充一下这个功能的单元测试吗:https://github.com/alibaba/higress/tree/main/plugins/wasm-go#%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95

@loli0123456789
Copy link
Author

loli0123456789 commented Sep 5, 2025

@loli0123456789 可以参考这里的例子补充一下这个功能的单元测试吗:https://github.com/alibaba/higress/tree/main/plugins/wasm-go#%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95

@johnlanni 您好,单元测试相关内容已参照更新,请审核。


@loli0123456789 Can you refer to the example here to add unit tests of this function: https://github.com/alibaba/higress/tree/main/plugins/wasm-go#%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95

@johnlanni Hello, the unit test related content has been updated with reference, please review it.

@loli0123456789
Copy link
Author

@johnlanni
请教下这个失败怎么处理,contenttype不支持multipart/form-data; boundary=...,但是文件上传就是这个类型

Build and Test Plugins / higress-wasmplugin-test (GO) (pull_request)
Build and Test Plugins / higress-wasmplugin-test (GO) (pull_request)Failing after 43m

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants