feat(core): add support to mailto: and tel: links, closes #5521 (#5544)

This commit is contained in:
Lucas Fernandes Nogueira 2022-12-12 19:57:39 -08:00 committed by GitHub
parent 9db9e6c037
commit d0d873e39a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 18 additions and 13 deletions

View File

@ -0,0 +1,5 @@
---
"tauri": minor
---
Added support to `mailto:` and `tel:` links on the shell API.

View File

@ -2211,7 +2211,7 @@
"description": "Defines the `shell > open` api scope.",
"anyOf": [
{
"description": "If the shell open API should be enabled.\n\nIf enabled, the default validation regex (`^https?://`) is used.",
"description": "If the shell open API should be enabled.\n\nIf enabled, the default validation regex (`^((mailto:\\w+)|(tel:\\w+)|(https?://\\w+)).+`) is used.",
"type": "boolean"
},
{

View File

@ -414,7 +414,7 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
let shell_scope_open = match &config.tauri.allowlist.shell.open {
ShellAllowlistOpen::Flag(false) => quote!(::std::option::Option::None),
ShellAllowlistOpen::Flag(true) => {
quote!(::std::option::Option::Some(#root::regex::Regex::new("^https?://").unwrap()))
quote!(::std::option::Option::Some(#root::regex::Regex::new(r#"^((mailto:\w+)|(tel:\w+)|(https?://\w+)).+"#).unwrap()))
}
ShellAllowlistOpen::Validate(regex) => match Regex::new(regex) {
Ok(_) => quote!(::std::option::Option::Some(#root::regex::Regex::new(#regex).unwrap())),

View File

@ -1599,7 +1599,7 @@ pub struct ShellAllowlistScope(pub Vec<ShellAllowedCommand>);
pub enum ShellAllowlistOpen {
/// If the shell open API should be enabled.
///
/// If enabled, the default validation regex (`^https?://`) is used.
/// If enabled, the default validation regex (`^((mailto:\w+)|(tel:\w+)|(https?://\w+)).+`) is used.
Flag(bool),
/// Enable the shell open API, with a custom regex that the opened path must match against.

File diff suppressed because one or more lines are too long

View File

@ -96,7 +96,7 @@
if (target.matches('a')) {
if (
target.href &&
target.href.startsWith('http') &&
(['http://', 'https://', 'mailto:', 'tel:'].some(v => target.href.startsWith(v))) &&
target.target === '_blank'
) {
window.__TAURI_INVOKE__('tauri', {

View File

@ -91,7 +91,7 @@ impl Program {
/// Opens path or URL with the program specified in `with`, or system default if `None`.
///
/// The path will be matched against the shell open validation regex, defaulting to `^https?://`.
/// The path will be matched against the shell open validation regex, defaulting to `^((mailto:\w+)|(tel:\w+)|(https?://\w+)).+`.
/// A custom validation regex may be supplied in the config in `tauri > allowlist > scope > open`.
///
/// # Examples

View File

@ -301,7 +301,7 @@ impl Scope {
/// Open a path in the default (or specified) browser.
///
/// The path is validated against the `tauri > allowlist > shell > open` validation regex, which
/// defaults to `^https?://`.
/// defaults to `^((mailto:\w+)|(tel:\w+)|(https?://\w+)).+`.
#[cfg(feature = "shell-open-api")]
pub fn open(&self, path: &str, with: Option<Program>) -> Result<(), ScopeError> {
// ensure we pass validation if the configuration has one

View File

@ -32,7 +32,7 @@
* ### Restricting access to the {@link open | `open`} API
*
* On the allowlist, `open: true` means that the {@link open} API can be used with any URL,
* as the argument is validated with the `^https?://` regex.
* as the argument is validated with the `^((mailto:\w+)|(tel:\w+)|(https?://\w+)).+` regex.
* You can change that regex by changing the boolean value to a string, e.g. `open: ^https://github.com/`.
*
* ### Restricting access to the {@link Command | `Command`} APIs
@ -553,7 +553,7 @@ type CommandEvent =
*
* @param path The path or URL to open.
* This value is matched against the string regex defined on `tauri.conf.json > tauri > allowlist > shell > open`,
* which defaults to `^https?://`.
* which defaults to `^((mailto:\w+)|(tel:\w+)|(https?://\w+)).+`.
* @param openWith The app to open the file or URL with.
* Defaults to the system default application for the specified path type.
*

View File

@ -2211,7 +2211,7 @@
"description": "Defines the `shell > open` api scope.",
"anyOf": [
{
"description": "If the shell open API should be enabled.\n\nIf enabled, the default validation regex (`^https?://`) is used.",
"description": "If the shell open API should be enabled.\n\nIf enabled, the default validation regex (`^((mailto:\\w+)|(tel:\\w+)|(https?://\\w+)).+`) is used.",
"type": "boolean"
},
{