-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(auth): open OAuth URL in default browser automatically #875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nktks
wants to merge
7
commits into
googleworkspace:main
Choose a base branch
from
nktks:feat/auth-login-open-browser
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+181
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
78e1581
feat(auth): open OAuth URL in default browser automatically
nktks 452fb7b
fix(auth): reject dangerous Unicode (Cf category) in browser URL check
nktks 992d9d0
fix(auth): reject quote and shell metacharacters in browser URL check
nktks f14a78a
test(auth): cover quote and shell metacharacter rejection in URL check
nktks 739b2d7
refactor(auth): reference is_dangerous_unicode via crate::validate re…
nktks 2ad16b3
fix(auth): use explorer instead of rundll32 to open URLs on Windows
nktks a653b04
test(auth): rename Windows opener test to match explorer behavior
nktks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@googleworkspace/cli": minor | ||
| --- | ||
|
|
||
| `gws auth login` now attempts to open the OAuth URL in the default browser automatically (`open` on macOS, `xdg-open` on Linux, `explorer` on Windows), falling back to the existing copy-paste flow when no opener is available. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| //! Best-effort opening of a URL in the user's default browser. | ||
| //! | ||
| //! Used by `gws auth login` to spare the user from copy-pasting the OAuth | ||
| //! URL. Every failure path is silent by design: callers always print the | ||
| //! URL first, so the previous copy-paste behavior remains the fallback. | ||
|
|
||
| use std::process::{Command, Stdio}; | ||
|
|
||
| /// Returns the platform-specific program and arguments that open `url` in | ||
| /// the default browser, or `None` when the platform has no known opener. | ||
| fn opener_for_os(os: &str, url: &str) -> Option<(&'static str, Vec<String>)> { | ||
| match os { | ||
| "macos" => Some(("open", vec![url.to_string()])), | ||
| "linux" => Some(("xdg-open", vec![url.to_string()])), | ||
| // `start` is a cmd.exe built-in that re-parses `&` inside URLs, and | ||
| // `rundll32 url.dll,FileProtocolHandler` is a well-known LOLBIN | ||
| // technique that EDR/WDAC policies commonly block. Spawning the | ||
| // Windows shell directly avoids both: no cmd re-parsing, and | ||
| // explorer.exe cannot be blocked without breaking the OS GUI. | ||
| "windows" => Some(("explorer", vec![url.to_string()])), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| /// Validates that `url` is a plain https URL that is safe to hand to an | ||
| /// external opener program. Rejects control characters, whitespace, | ||
| /// dangerous Unicode (zero-width chars, bidi overrides) that | ||
| /// `char::is_control()` does not cover (`Cf` category), and quote/shell | ||
| /// metacharacters in case an opener forwards its argument through a shell | ||
| /// (e.g. `xdg-open` wrapper scripts) or mis-parses quotes (`rundll32`). | ||
| /// Properly percent-encoded OAuth URLs never contain any of these. | ||
| fn is_openable_url(url: &str) -> bool { | ||
| url.starts_with("https://") | ||
| && !url.chars().any(|c| { | ||
| c.is_control() | ||
| || c.is_whitespace() | ||
| || crate::validate::is_dangerous_unicode(c) | ||
| || matches!(c, '"' | '\'' | '\\' | ';' | '$' | '|' | '`' | '<' | '>') | ||
| }) | ||
| } | ||
|
nktks marked this conversation as resolved.
|
||
|
|
||
| /// Spawns `program` detached from this process. Returns `true` when the | ||
| /// process was spawned successfully (e.g. `false` when the program is not | ||
| /// installed). The child is reaped on a background thread so a slow opener | ||
| /// never blocks the OAuth callback accept loop. | ||
| fn spawn_detached(program: &str, args: &[String]) -> bool { | ||
| match Command::new(program) | ||
| .args(args) | ||
| .stdin(Stdio::null()) | ||
| .stdout(Stdio::null()) | ||
| .stderr(Stdio::null()) | ||
| .spawn() | ||
| { | ||
| Ok(mut child) => { | ||
| std::thread::spawn(move || { | ||
| let _ = child.wait(); | ||
| }); | ||
| true | ||
| } | ||
| Err(_) => false, | ||
| } | ||
| } | ||
|
|
||
| /// Attempts to open `url` in the default browser. Returns `true` when an | ||
| /// opener process was launched; `false` means the caller's printed URL is | ||
| /// the only way for the user to proceed. | ||
| pub(crate) fn try_open_browser(url: &str) -> bool { | ||
| if !is_openable_url(url) { | ||
| return false; | ||
| } | ||
| match opener_for_os(std::env::consts::OS, url) { | ||
| Some((program, args)) => spawn_detached(program, &args), | ||
| None => false, | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn opener_for_macos_uses_open() { | ||
| let (program, args) = opener_for_os("macos", "https://example.com").unwrap(); | ||
| assert_eq!(program, "open"); | ||
| assert_eq!(args, vec!["https://example.com".to_string()]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn opener_for_linux_uses_xdg_open() { | ||
| let (program, args) = opener_for_os("linux", "https://example.com").unwrap(); | ||
| assert_eq!(program, "xdg-open"); | ||
| assert_eq!(args, vec!["https://example.com".to_string()]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn opener_for_windows_uses_explorer() { | ||
| let (program, args) = opener_for_os("windows", "https://example.com").unwrap(); | ||
| assert_eq!(program, "explorer"); | ||
| assert_eq!(args, vec!["https://example.com".to_string()]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn opener_for_unknown_os_is_none() { | ||
| assert!(opener_for_os("freebsd", "https://example.com").is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn openable_url_accepts_https() { | ||
| assert!(is_openable_url( | ||
| "https://accounts.google.com/o/oauth2/auth?scope=x&client_id=y" | ||
| )); | ||
| } | ||
|
|
||
| #[test] | ||
| fn openable_url_rejects_non_https_schemes() { | ||
| assert!(!is_openable_url("http://example.com")); | ||
| assert!(!is_openable_url("javascript:alert(1)")); | ||
| assert!(!is_openable_url("file:///etc/passwd")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn openable_url_rejects_whitespace_and_control_chars() { | ||
| assert!(!is_openable_url("https://example.com/a b")); | ||
| assert!(!is_openable_url("https://example.com/\n")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn openable_url_rejects_quotes_and_shell_metacharacters() { | ||
| assert!(!is_openable_url("https://example.com/\"")); | ||
| assert!(!is_openable_url("https://example.com/'")); | ||
| assert!(!is_openable_url("https://example.com/\\")); | ||
| assert!(!is_openable_url("https://example.com/;evil")); | ||
| assert!(!is_openable_url("https://example.com/$(evil)")); | ||
| assert!(!is_openable_url("https://example.com/a|b")); | ||
| assert!(!is_openable_url("https://example.com/`evil`")); | ||
| assert!(!is_openable_url("https://example.com/<a>")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn openable_url_rejects_dangerous_unicode() { | ||
| // RLO (bidi override, category Cf — not caught by is_control) | ||
| assert!(!is_openable_url("https://example.com/\u{202E}evil")); | ||
| // ZWSP (zero-width space) | ||
| assert!(!is_openable_url("https://example.com/a\u{200B}b")); | ||
| } | ||
|
nktks marked this conversation as resolved.
|
||
|
|
||
| #[test] | ||
| fn try_open_browser_rejects_unsafe_url() { | ||
| assert!(!try_open_browser("javascript:alert(1)")); | ||
| } | ||
|
|
||
| #[cfg(unix)] | ||
| #[test] | ||
| fn spawn_detached_returns_true_for_existing_program() { | ||
| assert!(spawn_detached("true", &[])); | ||
| } | ||
|
|
||
| #[test] | ||
| fn spawn_detached_returns_false_for_missing_program() { | ||
| assert!(!spawn_detached( | ||
| "gws-test-nonexistent-program-9f2c", | ||
| &["https://example.com".to_string()] | ||
| )); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.