Skip to content

fix: use JSON encoding for args to preserve spaces in values#17

Draft
12end wants to merge 1 commit into
masterfrom
openhands/fix-arg-encoding-with-spaces
Draft

fix: use JSON encoding for args to preserve spaces in values#17
12end wants to merge 1 commit into
masterfrom
openhands/fix-arg-encoding-with-spaces

Conversation

@12end

@12end 12end commented Jun 28, 2026

Copy link
Copy Markdown
Owner

Summary

Fix issue #15: Arguments containing spaces were incorrectly split after encryption/decryption because the code used strings.Join/strings.Split with space as delimiter.

Problem

The original code in cargs.go used:

input := []byte(strings.Join(os.Args[2:], " "))
// ...
os.Args = append(os.Args[:1], strings.Split(string(output), " ")...)

This caused arguments with internal spaces (e.g., --name "hello world") to be split incorrectly after decryption, resulting in the argument being broken into multiple arguments.

Solution

Use JSON encoding (json.Marshal/json.Unmarshal) to serialize the args slice instead of space-based join/split. This correctly handles:

  • Arguments with spaces (e.g., "hello world")
  • Empty arguments
  • Arguments with special characters
  • Multiple consecutive spaces

Changes

  1. encoding.go (new file): Added encodeArgsJSON and decodeArgsJSON functions that use JSON serialization
  2. cargs.go: Updated to use the new JSON encoding functions instead of strings.Join/strings.Split
  3. cargs_test.go (new file): Added comprehensive tests for the encoding/decoding functions

Testing

All tests pass:

  • TestEncodeDecodeArgs - Tests base64 per-argument encoding
  • TestEncodeDecodeArgsJSON - Tests JSON encoding (the actual implementation)
  • TestEncodeDecodeEmptyArgs - Tests empty argument handling
  • TestEncodeDecodeEmptyArgsJSON - Tests JSON empty argument handling

Closes #15

@12end can click here to continue refining the PR

Fix issue #15: Arguments containing spaces were incorrectly split after
encryption/decryption because the code used strings.Join/Split with space
as delimiter.

The fix uses JSON encoding (encodeArgsJSON/decodeArgsJSON) to serialize
the args slice, which correctly handles:
- Arguments with spaces (e.g., 'hello world')
- Empty arguments
- Arguments with special characters
- Multiple consecutive spaces
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug:包含空格的参数值在加解密后会被错误拆分,导致参数个数/内容丢失

2 participants