Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-script-id-path-encoding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@googleworkspace/cli": patch
---

Preserve RFC 3986 unreserved characters in Apps Script IDs used in URL paths.
37 changes: 37 additions & 0 deletions crates/google-workspace-cli/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,10 @@ fn render_path_template(
let encoded = if is_plus {
let validated = crate::validate::validate_resource_name(&val_str)?;
crate::validate::encode_path_preserving_slashes(validated)
} else if key == "scriptId" {
// Apps Script's :run endpoint rejects percent-encoded RFC 3986
// unreserved characters (`-`, `_`, `.`, `~`) in script IDs.
encode_script_id_path_segment(&val_str)
} else {
crate::validate::encode_path_segment(&val_str)
};
Expand All @@ -729,6 +733,14 @@ fn render_path_template(
Ok(rendered)
}

fn encode_script_id_path_segment(value: &str) -> String {
crate::validate::encode_path_segment(value)
.replace("%2D", "-")
.replace("%5F", "_")
.replace("%2E", ".")
.replace("%7E", "~")
}

/// Attempts to extract a GCP console enable URL from a Google API `accessNotConfigured`
/// error message.
///
Expand Down Expand Up @@ -1882,6 +1894,31 @@ mod tests {
);
}

#[test]
fn test_build_url_preserves_unreserved_script_id_characters() {
let doc = RestDescription {
base_url: Some("https://script.googleapis.com/".to_string()),
..Default::default()
};
let method = RestMethod {
path: "v1/scripts/{scriptId}:run".to_string(),
flat_path: Some("v1/scripts/{scriptId}:run".to_string()),
..Default::default()
};
let mut params = Map::new();
params.insert(
"scriptId".to_string(),
json!("1-ukYIb6_NbryM5UJXBwAMeY5GKtSBA05csXty3iY7DhO-P"),
);

let (url, _) = build_url(&doc, &method, &params, false).unwrap();

assert_eq!(
url,
"https://script.googleapis.com/v1/scripts/1-ukYIb6_NbryM5UJXBwAMeY5GKtSBA05csXty3iY7DhO-P:run"
);
}

#[test]
fn test_build_url_errors_for_path_param_not_in_template() {
let doc = RestDescription {
Expand Down
Loading