Quellcode durchsuchen

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

Lucas Fernandes Nogueira vor 2 Jahren
Ursprung
Commit
d0d873e39a

+ 5 - 0
.changes/open-links-mail-tel.md

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

+ 1 - 1
core/config-schema/schema.json

@@ -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"
         },
         {

+ 1 - 1
core/tauri-codegen/src/context.rs

@@ -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())),

+ 1 - 1
core/tauri-utils/src/config.rs

@@ -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.

Datei-Diff unterdrückt, da er zu groß ist
+ 0 - 0
core/tauri/scripts/bundle.global.js


+ 1 - 1
core/tauri/scripts/core.js

@@ -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', {

+ 1 - 1
core/tauri/src/api/shell.rs

@@ -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

+ 1 - 1
core/tauri/src/scope/shell.rs

@@ -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

+ 2 - 2
tooling/api/src/shell.ts

@@ -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.
  *

+ 1 - 1
tooling/cli/schema.json

@@ -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"
         },
         {

Einige Dateien werden nicht angezeigt, da zu viele Dateien in diesem Diff geändert wurden.