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-chat-send-validate-space.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@googleworkspace/cli": patch
---

fix(security): validate space name in chat +send to prevent path traversal
36 changes: 25 additions & 11 deletions src/helpers/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ TIPS:
Box::pin(async move {
if let Some(matches) = matches.subcommand_matches("+send") {
// Parse arguments into our config struct config
let config = parse_send_args(matches);
let config = parse_send_args(matches)?;
// The `?` operator here will propagate any errors from `build_send_request`
// immediately, returning `Err(GwsError)` from the async block.
let (params_str, body_str, scopes) = build_send_request(&config, doc)?;
Expand Down Expand Up @@ -175,14 +175,14 @@ pub struct SendConfig {
/// # Returns
///
/// * `SendConfig` - The populated configuration struct.
pub fn parse_send_args(matches: &ArgMatches) -> SendConfig {
SendConfig {
// We clone the strings here because ArgMatches owns the original strings,
// and we need to pass ownership of these values to our config struct
// to decouple it from the clap lifetime.
space: matches.get_one::<String>("space").unwrap().clone(),
pub fn parse_send_args(matches: &ArgMatches) -> Result<SendConfig, GwsError> {
let space = matches.get_one::<String>("space").unwrap().clone();
crate::validate::validate_resource_name(&space)?;

Ok(SendConfig {
space,
text: matches.get_one::<String>("text").unwrap().clone(),
}
})
Comment on lines +179 to +185
Copy link
Contributor

Choose a reason for hiding this comment

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

high

While clap is configured to require the space and text arguments, it's a good practice to avoid unwrap() in application code. Panics should be reserved for unrecoverable errors. Since this function already returns a Result, it would be more robust to handle the (unlikely) absence of these arguments by returning an error instead of panicking. This makes the function's error handling more consistent and also cleans up the struct instantiation.

    let space = matches
        .get_one::<String>("space")
        .ok_or_else(|| {
            GwsError::Validation("Internal error: required --space argument missing".to_string())
        })?
        .clone();
    crate::validate::validate_resource_name(&space)?;

    let text = matches
        .get_one::<String>("text")
        .ok_or_else(|| {
            GwsError::Validation("Internal error: required --text argument missing".to_string())
        })?
        .clone();

    Ok(SendConfig { space, text })

}

#[cfg(test)]
Expand Down Expand Up @@ -241,12 +241,26 @@ mod tests {

#[test]
fn test_parse_send_args() {
let matches = make_matches_send(&["test", "--space", "s", "--text", "t"]);
let config = parse_send_args(&matches);
assert_eq!(config.space, "s");
let matches = make_matches_send(&["test", "--space", "valid-space", "--text", "t"]);
let config = parse_send_args(&matches).unwrap();
assert_eq!(config.space, "valid-space");
assert_eq!(config.text, "t");
}

#[test]
fn test_parse_send_args_rejects_traversal_in_space() {
let matches = make_matches_send(&["test", "--space", "../etc/passwd", "--text", "t"]);
let result = parse_send_args(&matches);
assert!(result.is_err(), "space with path traversal should be rejected");
}

#[test]
fn test_parse_send_args_rejects_query_injection_in_space() {
let matches = make_matches_send(&["test", "--space", "spaces/AAA?key=injected", "--text", "t"]);
let result = parse_send_args(&matches);
assert!(result.is_err(), "space with query characters should be rejected");
}

#[test]
fn test_inject_commands() {
let helper = ChatHelper;
Expand Down
Loading