cli icon indicating copy to clipboard operation
cli copied to clipboard

Encode URL template path params in build_url (fix Sheets range and reserved chars)

Open KaishuShito opened this issue 8 months ago • 1 comments

Summary

build_url currently substitutes path template parameters with raw values. This causes malformed URLs when path params contain reserved characters (notably in Google Sheets range, e.g. Sheet/Name!A1:B2, hash#1!A1:B2, non-ASCII labels).

Reproduction

Using current npm release (gws 0.3.3), dry-run output for a slash in range:

gws sheets spreadsheets values get \
  --params '{"spreadsheetId":"dummy","range":"slash/1!A1:B2"}' \
  --dry-run

Output URL currently becomes:

"url": "https://sheets.googleapis.com/v4/spreadsheets/dummy/values/slash/1!A1:B2"

This incorrectly splits a single range value across path segments.

Expected Behavior

Path parameters should be encoded according to URI template expansion semantics:

  • {var}: encode as a path segment
  • {+var}: preserve / separators, but encode each segment

For the same example above, expected URL path is:

".../values/slash%2F1%21A1%3AB2"

Why this matters

gws is agent-first and handles raw JSON params. Correct, deterministic path encoding is needed to avoid request corruption and hard-to-debug failures for localized sheet names and reserved chars.

Proposed fix

In src/executor.rs::build_url:

  1. Encode {var} expansions with encode_path_segment
  2. Encode {+var} with per-segment encoding preserving /
  3. Fail fast if a location == "path" param is provided but no placeholder exists in template

I already prepared a patch with tests and can open a PR linked to this issue.

KaishuShito avatar Mar 05 '26 02:03 KaishuShito

Opened PR #84 with a fix and tests for path-template encoding in build_url. Please take a look when convenient: https://github.com/googleworkspace/cli/pull/84

KaishuShito avatar Mar 05 '26 02:03 KaishuShito