Repository navigation
fix(browser): a click that leads to another site is a result, not a turn-ending permission failure #8732
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
Merged
Merged
fix(browser): a click that leads to another site is a result, not a turn-ending permission failure #8732
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
abad952
test(agent): cover browser offsite navigation not ending the turn
senamakel f7c37e6
feat(browser): add is_public_https URL guard
senamakel 8eb5d75
fix(browser): treat offsite redirects as recoverable results
senamakel 64ed925
Merge remote-tracking branch 'origin/main' into task-offsite-links
senamakel 2fb0c9d
feat(browser): add browser_never_open module
senamakel 16106c5
test(browser): add regression tests for never-open browser mode
senamakel 53c5b31
feat(browser): enforce never-open list before allow-all
senamakel 234454d
fix(browser): report normalized offsite url and drop open instruction
senamakel 0fad44d
test(browser): drop redundant offsite url assertion
senamakel 0dfc126
style(browser): reformat never-open and offsite code with rustfmt
senamakel 8fea04a
feat(browser): refuse never-open hosts on every navigation, fail closed
senamakel ceae656
feat(browser): enforce never-open list on browser dials
senamakel 3019ca5
test(browser): cover unicode hosts, trailing dots and unreadable lists
senamakel 666d0b9
fix(browser): enforce never-open list on every browser dial
senamakel 63efbdb
style(browser_never_open): wrap long lines to satisfy rustfmt
senamakel ece8cfa
fix(browser): enforce the never-open list on every browser dial
senamakel 7fa11d5
chore(browser): reorder module declarations alphabetically
senamakel 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
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
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
103 changes: 103 additions & 0 deletions
103
crates/openhuman-core/src/modules/browser_never_open.rs
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,103 @@ | ||
| //! The sites the browser never opens, read from the file the host names in | ||
| //! `OPENHUMAN_BROWSER_DENY_FILE` (Teeny's bin/spine points it at | ||
| //! `deploy/browser-deny.txt`). One host per line; `#` starts a comment; a | ||
| //! host covers itself and every subdomain of it. Unset, nothing is denied. | ||
| //! Set but unreadable, every page is refused: the list fails closed. | ||
| //! | ||
| //! Two places consult it. `check_url` refuses a navigation the core starts | ||
| //! (`open`, `task`, the `browser_open` tool). `BrowserEgressHost::authorize` | ||
| //! refuses every connection the browser itself makes, which is where a click | ||
| //! or a redirect to a listed host is stopped before its request is sent. A | ||
| //! page the browser has already left is not shown, but it is never fetched | ||
| //! by a dial the list refuses. | ||
|
|
||
| use std::ffi::OsStr; | ||
| use std::fs; | ||
|
|
||
| /// The environment variable that names the never-open file. | ||
| pub(crate) const DENY_FILE_ENV: &str = "OPENHUMAN_BROWSER_DENY_FILE"; | ||
|
|
||
| /// Why `url` may not be opened under the never-open list the environment | ||
| /// names, or None when the browser may open it. | ||
| pub(crate) fn refusal(url: &str) -> Option<String> { | ||
| refusal_for(std::env::var_os(DENY_FILE_ENV).as_deref(), url) | ||
| } | ||
|
|
||
| /// Why a connection to `hostname` is refused under the environment's list, or | ||
| /// None when it may be made. | ||
| pub(crate) fn dial_refusal(hostname: &str) -> Option<String> { | ||
| dial_refusal_for(std::env::var_os(DENY_FILE_ENV).as_deref(), hostname) | ||
| } | ||
|
|
||
| /// `refusal` for a given file (None: no list is set). | ||
| fn refusal_for(file: Option<&OsStr>, url: &str) -> Option<String> { | ||
| let file = file?; | ||
| match load(file) { | ||
| Ok(list) => check(url, &list), | ||
| Err(reason) => Some(reason), | ||
| } | ||
| } | ||
|
|
||
| /// `dial_refusal` for a given file (None: no list is set). | ||
| fn dial_refusal_for(file: Option<&OsStr>, hostname: &str) -> Option<String> { | ||
| let file = file?; | ||
| match load(file) { | ||
| Ok(list) => check_host(hostname, &list), | ||
| Err(reason) => Some(reason), | ||
| } | ||
| } | ||
|
|
||
| fn load(file: &OsStr) -> Result<Vec<String>, String> { | ||
| match fs::read_to_string(file) { | ||
| Ok(text) => Ok(parse(&text)), | ||
| Err(error) => Err(format!( | ||
| "the never-open list at {} could not be read ({error}), so no page is opened", | ||
| file.to_string_lossy() | ||
| )), | ||
| } | ||
| } | ||
|
|
||
| /// The canonical form of a host, the way the URL parser writes it: IDNA and | ||
| /// lower-case, without a trailing dot. None when it is not a host. | ||
| fn canonical(host: &str) -> Option<String> { | ||
| url::Host::parse(host.trim().trim_end_matches('.')) | ||
| .ok() | ||
| .map(|parsed| parsed.to_string()) | ||
| } | ||
|
|
||
| /// The hosts listed in `text`, canonical, without comments or blank lines. | ||
| /// Entries that are not hosts are dropped, never matched. | ||
| pub(crate) fn parse(text: &str) -> Vec<String> { | ||
| text.lines() | ||
| .map(|line| line.split('#').next().unwrap_or("").trim()) | ||
| .filter(|line| !line.is_empty()) | ||
| .filter_map(canonical) | ||
| .collect() | ||
| } | ||
|
|
||
| /// Why `url` is denied by `list`, when it is: its host is an entry or under one. | ||
| pub(crate) fn check(url: &str, list: &[String]) -> Option<String> { | ||
| let host = url::Url::parse(url) | ||
| .ok() | ||
| .and_then(|parsed| parsed.host_str().map(str::to_owned)); | ||
| match host { | ||
| Some(host) => check_host(&host, list), | ||
| None => Some(format!("'{url}' has no host the never-open list can check")), | ||
| } | ||
| } | ||
|
|
||
| /// Why `hostname` is denied by `list`, when it is. | ||
| pub(crate) fn check_host(hostname: &str, list: &[String]) -> Option<String> { | ||
| let Some(host) = canonical(hostname) else { | ||
| return Some(format!( | ||
| "'{hostname}' is not a host the never-open list can check" | ||
| )); | ||
| }; | ||
| list.iter() | ||
| .find(|denied| host == **denied || host.ends_with(&format!(".{denied}"))) | ||
| .map(|denied| format!("host '{host}' is on the never-open list (entry '{denied}')")) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[path = "browser_never_open_tests.rs"] | ||
| mod tests; | ||
93 changes: 93 additions & 0 deletions
93
crates/openhuman-core/src/modules/browser_never_open_tests.rs
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,93 @@ | ||
| use super::*; | ||
|
|
||
| fn list(text: &str) -> Vec<String> { | ||
| parse(text) | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_listed_host_and_its_subdomains_are_denied_but_not_a_lookalike() { | ||
| let deny = list("x.com\n"); | ||
| assert!(check("https://x.com/home", &deny).is_some()); | ||
| assert!(check("https://mobile.x.com/post/1", &deny).is_some()); | ||
| assert!(check("https://X.COM./", &deny).is_some()); | ||
| // A host that merely ends in the same letters is another site. | ||
| assert_eq!(check("https://notx.com/", &deny), None); | ||
| assert_eq!(check("https://example.com/x.com", &deny), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn comments_blank_lines_and_case_in_the_file_are_ignored() { | ||
| let deny = parse("# X and its kin\n\n X.com # the account keys\nMail.Google.com\n"); | ||
| assert_eq!( | ||
| deny, | ||
| vec!["x.com".to_string(), "mail.google.com".to_string()] | ||
| ); | ||
| assert!(check("https://mail.google.com/mail", &deny).is_some()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn an_unparseable_url_is_refused_when_a_list_is_set() { | ||
| assert!(check("not a url", &list("x.com\n")).is_some()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn no_list_set_denies_nothing() { | ||
| assert_eq!(refusal_for(None, "https://x.com/"), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_list_that_cannot_be_read_refuses_every_page() { | ||
| let missing = std::env::temp_dir().join("openhuman-never-open-does-not-exist.txt"); | ||
| let reason = | ||
| refusal_for(Some(missing.as_os_str()), "https://example.com/").expect("fails closed"); | ||
| assert!(reason.contains("could not be read"), "{reason}"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn the_file_named_by_the_environment_is_the_list() { | ||
| let dir = tempfile::tempdir().unwrap(); | ||
| let file = dir.path().join("browser-deny.txt"); | ||
| std::fs::write(&file, "never-open.example\n").unwrap(); | ||
| assert!(refusal_for(Some(file.as_os_str()), "https://www.never-open.example/a").is_some()); | ||
| assert_eq!( | ||
| refusal_for(Some(file.as_os_str()), "https://example.com/"), | ||
| None | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_unicode_entry_matches_its_punycode_host_and_subdomains() { | ||
| let deny = list("bücher.example\n"); | ||
| assert_eq!(deny, vec!["xn--bcher-kva.example".to_string()]); | ||
| assert!(check("https://xn--bcher-kva.example/", &deny).is_some()); | ||
| assert!(check("https://www.bücher.example/", &deny).is_some()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_dial_is_refused_for_a_listed_host_with_a_trailing_dot_or_case() { | ||
| let deny = list("x.com\n"); | ||
| assert!(check_host("X.com.", &deny).is_some()); | ||
| assert!(check_host("edge.x.com", &deny).is_some()); | ||
| assert_eq!(check_host("example.com", &deny), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_dial_with_a_list_that_cannot_be_read_is_refused() { | ||
| let missing = std::env::temp_dir().join("openhuman-never-open-missing-dial.txt"); | ||
| assert!(dial_refusal_for(Some(missing.as_os_str()), "example.com") | ||
| .expect("fails closed") | ||
| .contains("could not be read")); | ||
| assert_eq!(dial_refusal_for(None, "x.com"), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn an_empty_list_refuses_no_dial_and_no_page() { | ||
| let dir = tempfile::tempdir().unwrap(); | ||
| let file = dir.path().join("empty.txt"); | ||
| std::fs::write(&file, "# nothing yet\n").unwrap(); | ||
| assert_eq!(dial_refusal_for(Some(file.as_os_str()), "x.com"), None); | ||
| assert_eq!( | ||
| refusal_for(Some(file.as_os_str()), "https://example.com/path"), | ||
| None | ||
| ); | ||
| } |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the deny file contains a hostname ending in an IDNA dot-equivalent, such as
example.com。(U+3002), this strips trailing dots beforeurl::Host::parseconverts that character into an ASCII dot. The list consequently storesexample.com., whilecheck_host()canonicalizes a normal URL or dial hostname toexample.com. Neither the equality nor the subdomain comparison matches, so the deny entry fails to block the site at either enforcement point. Strip trailing dots from the parsed, IDNA-normalized hostname instead.Useful? React with 👍 / 👎.