agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Bug]:RemoteFilesystem.uploadFiles corrupts non-UTF-8 binary content

Open wangxuefen88 opened this issue 4 months ago • 2 comments

RemoteFilesystem.uploadFiles accepts byte[] but converts every payload to a UTF-8 String first.

new String(byte[], StandardCharsets.UTF_8) replaces malformed byte sequences instead of throwing, so the Base64 fallback is not effective. Downloading the stored value cannot restore the original bytes.

Reproduction:

byte[] input = {0, (byte) 0xff, 1, (byte) 0x80}; filesystem.uploadFiles(ctx, List.of(Map.entry("/image.png", input))); byte[] output = filesystem.downloadFiles(ctx, List.of("/image.png")) .get(0).content();

Expected: Arrays.equals(input, output) == true

Suggested fix: Use a UTF-8 decoder configured with CodingErrorAction.REPORT. Store malformed content as Base64, and add a binary upload/download round-trip test.

The fix can remain limited to RemoteFilesystem and does not require a schema change.

wangxuefen88 avatar Jul 28 '26 05:07 wangxuefen88

🤖 Bot Assessment — Bug Confirmed ✅

Root cause verified in code. RemoteFilesystem.uploadFiles() (line 506):

contentStr = new String(content, StandardCharsets.UTF_8);
encoding = "utf-8";

The catch (Exception e) Base64 fallback on line 509 is dead code — new String(byte[], Charset) in Java uses CodingErrorAction.REPLACE by default, silently substituting malformed bytes with \uFFFD instead of throwing.

For input {0, 0xFF, 1, 0x80}:

  • Bytes 0xFF and 0x80 are invalid UTF-8 → replaced with \uFFFD
  • The stored string no longer matches the original bytes
  • Download returns corrupted data; round-trip fails

Suggested fix is correct — use a strict decoder:

CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder()
    .onMalformedInput(CodingErrorAction.REPORT)
    .onUnmappableCharacter(CodingErrorAction.REPORT);
contentStr = decoder.decode(ByteBuffer.wrap(content)).toString();

This will throw CharacterCodingException for non-UTF-8 bytes, activating the Base64 fallback as intended.

oss-maintainer avatar Jul 28 '26 05:07 oss-maintainer

Thanks for confirming the issue. I'd like to work on this fix.

I will keep the change scoped to RemoteFilesystem.uploadFiles() and add a binary upload/download round-trip regression test. No schema or API changes will be introduced.

wangxuefen88 avatar Jul 28 '26 05:07 wangxuefen88