Skip to content
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

bugfix: escape special characters in URL prefixes used in a regex #301

Merged
merged 2 commits into from
Feb 6, 2023
Merged
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
13 changes: 10 additions & 3 deletions crates/shared/src/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ pub fn regex_for_domain(domain: &str) -> String {

pub fn regex_for_prefix(prefix: &str) -> String {
if prefix.ends_with('$') {
return format!("^{prefix}");
let escaped = regex::escape(prefix.strip_suffix('$').unwrap());
format!("^{escaped}$")
} else {
let escaped = regex::escape(prefix);
format!("^{escaped}.*")
}

format!("^{prefix}.*")
}

/// Convert a robots.txt rule into a proper regex string
Expand Down Expand Up @@ -133,5 +135,10 @@ mod test {
] {
assert!(!regex.is_match(test));
}

let prefix = "https://en.wikipedia.org/wiki/\'($";
let regex = Regex::new(&regex_for_prefix(prefix)).unwrap();
dbg!(&regex);
assert!(regex.is_match("https://en.wikipedia.org/wiki/\'("));
}
}
2 changes: 2 additions & 0 deletions crates/spyglass/src/api/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,8 @@ mod test {
.await
.expect("Unable to find indexed docs");
assert_eq!(indexed.len(), 0);
// Add a small delay so that the documents can be properly committed
std::thread::sleep(std::time::Duration::from_millis(500));
assert_eq!(state.index.reader.searcher().num_docs(), 0);
}
}
17 changes: 10 additions & 7 deletions crates/tauri/src/menu.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

use shared::config::Config;
use strum_macros::{Display, EnumString};
use tauri::{
utils::assets::EmbeddedAssets, Context, CustomMenuItem, Menu, MenuItem, Submenu,
utils::assets::EmbeddedAssets, Context, CustomMenuItem, Menu,
SystemTrayMenu, SystemTrayMenuItem, SystemTraySubmenu,
};
#[cfg(not(target_os = "linux"))]
use tauri::{MenuItem, Submenu};

#[derive(Display, Debug, EnumString)]
#[allow(non_camel_case_types, clippy::upper_case_acronyms)]
Expand Down Expand Up @@ -95,16 +98,16 @@ pub fn get_tray_menu(ctx: &Context<EmbeddedAssets>, config: &Config) -> SystemTr
.add_item(quit)
}

pub fn get_app_menu(ctx: &Context<EmbeddedAssets>) -> Menu {
if cfg!(target_os = "linux") {
return Menu::new();
}
pub fn get_app_menu(_ctx: &Context<EmbeddedAssets>) -> Menu {
#[cfg(target_os = "linux")]
return Menu::new();

#[cfg(not(target_os = "linux"))]
Menu::new().add_submenu(Submenu::new(
&ctx.package_info().name,
&_ctx.package_info().name,
Menu::new()
.add_native_item(MenuItem::About(
ctx.package_info().name.to_string(),
_ctx.package_info().name.to_string(),
Default::default(),
))
// Currently we need to include these so that the shortcuts for these
Expand Down