Skip to content

fix(feishu): encode non-ASCII filenames in file uploads#31328

Merged
Takhoffman merged 3 commits intoopenclaw:mainfrom
Kay-051:fix/feishu-chinese-filename-encoding
Mar 2, 2026
Merged

fix(feishu): encode non-ASCII filenames in file uploads#31328
Takhoffman merged 3 commits intoopenclaw:mainfrom
Kay-051:fix/feishu-chinese-filename-encoding

Conversation

@Kay-051
Copy link
Contributor

@Kay-051 Kay-051 commented Mar 2, 2026

Summary

Fixes #31179

Feishu file uploads silently fail when file_name contains non-ASCII characters (Chinese, em-dash , full-width brackets (), etc.). The multipart/form-data serialization sends raw UTF-8 bytes in header fields where RFC 2388 expects 7-bit ASCII, causing Feishu's API to treat the upload as text instead of an attachment.

Changes

  • extensions/feishu/src/media.ts: Add sanitizeFileNameForUpload() that RFC 5987 percent-encodes the file basename while preserving the extension. ASCII-only filenames pass through unchanged. Applied in uploadFileFeishu() before the SDK call.
  • extensions/feishu/src/media.test.ts: Add tests for:
    • Chinese filenames are encoded in upload calls
    • ASCII filenames remain unchanged
    • Special characters (em-dash, full-width brackets) are encoded
    • Unit tests for sanitizeFileNameForUpload() covering edge cases (no extension, mixed ASCII/non-ASCII, RFC 5987 special chars)

Test Results

Filename Before After
测试文档.pdf ❌ Text link ✅ Encoded for upload
report-2026.pdf ✅ Works ✅ Unchanged
报告—详情(2026).md ❌ Text link ✅ Encoded for upload

Test plan

  • All 18 existing + new tests pass (vitest run extensions/feishu/src/media.test.ts)
  • ASCII filenames pass through unchanged (no regression)
  • Chinese filenames are percent-encoded in the file_name field
  • File extension is preserved after encoding
  • RFC 5987 special characters (', (, )) are properly encoded

Made with Cursor

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a14f7a75ce

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +224 to +228
const encoded = encodeURIComponent(base)
.replace(/'/g, "%27")
.replace(/\(/g, "%28")
.replace(/\)/g, "%29");
return encoded + ext;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Encode non-ASCII extension bytes before upload

sanitizeFileNameForUpload only percent-encodes the basename and then appends ext unchanged, so filenames like 报告.数据 or 示例.测试 still send raw non-ASCII bytes in file_name. In those inputs, the new safeguard does not actually make the multipart metadata ASCII-safe, so uploads can still fail for a subset of non-ASCII filenames even after this fix.

Useful? React with 👍 / 👎.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 2, 2026

Greptile Summary

Adds RFC 5987 percent-encoding for non-ASCII characters in Feishu file upload filenames, fixing silent upload failures. The implementation introduces a sanitizeFileNameForUpload() helper that:

  • Efficiently passes through ASCII-only filenames unchanged
  • Encodes non-ASCII basenames while preserving file extensions
  • Applies additional RFC 5987 encoding for special characters (', (, )) when needed

The fix is applied in uploadFileFeishu() before passing filenames to the Feishu SDK. Test coverage is comprehensive, including Chinese characters, special characters, mixed ASCII/non-ASCII, and edge cases like files without extensions.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk - it's a focused bug fix with comprehensive tests and no breaking changes.
  • The implementation is clean, well-tested, and solves a specific problem without affecting existing functionality. ASCII filenames pass through unchanged (no regression risk), and the encoding logic correctly handles the documented edge cases. The 18 tests provide thorough coverage of both the bug fix and edge cases.
  • No files require special attention

Last reviewed commit: a14f7a7

@Takhoffman Takhoffman force-pushed the fix/feishu-chinese-filename-encoding branch from a14f7a7 to b3b07c3 Compare March 2, 2026 22:49
Kay-051 and others added 3 commits March 2, 2026 16:52
Feishu file uploads silently fail when file_name contains Chinese
characters or special characters (em-dash, full-width brackets, etc.)
because the multipart/form-data serialization requires 7-bit ASCII
headers.

Add sanitizeFileNameForUpload() that RFC 5987 percent-encodes the
basename while preserving the file extension.  ASCII-only filenames
pass through unchanged.

Fixes openclaw#31179

Made-with: Cursor
@Takhoffman Takhoffman force-pushed the fix/feishu-chinese-filename-encoding branch from b3b07c3 to 3681204 Compare March 2, 2026 22:56
@Takhoffman Takhoffman merged commit 905c335 into openclaw:main Mar 2, 2026
8 of 9 checks passed
@Takhoffman
Copy link
Contributor

PR #31328 - fix(feishu): encode non-ASCII filenames in file uploads (#31328)

Merged after verification.

  • Merge commit: 905c335
  • Verified: pnpm test extensions/feishu/src/media.test.ts
  • Autoland updates:
    M\tCHANGELOG.md
    M\textensions/feishu/src/media.test.ts
    M\textensions/feishu/src/media.ts
  • Changelog: CHANGELOG.md updated=true required=true opt_out=false

eminemead pushed a commit to eminemead/evi that referenced this pull request Mar 3, 2026
…) thanks @Kay-051

Verified:
- pnpm test extensions/feishu/src/media.test.ts

Co-authored-by: Kay-051 <210470990+Kay-051@users.noreply.github.com>
Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
(cherry picked from commit 905c335)
dawi369 pushed a commit to dawi369/davis that referenced this pull request Mar 3, 2026
…) thanks @Kay-051

Verified:
- pnpm test extensions/feishu/src/media.test.ts

Co-authored-by: Kay-051 <210470990+Kay-051@users.noreply.github.com>
Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
OWALabuy pushed a commit to kcinzgg/openclaw that referenced this pull request Mar 4, 2026
…) thanks @Kay-051

Verified:
- pnpm test extensions/feishu/src/media.test.ts

Co-authored-by: Kay-051 <210470990+Kay-051@users.noreply.github.com>
Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
zooqueen pushed a commit to hanzoai/bot that referenced this pull request Mar 6, 2026
…) thanks @Kay-051

Verified:
- pnpm test extensions/feishu/src/media.test.ts

Co-authored-by: Kay-051 <210470990+Kay-051@users.noreply.github.com>
Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: feishu Channel integration: feishu size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feishu] File upload fails with Chinese/special characters in filename

2 participants