Skip to content

Commit

Permalink
feat(core): add support to mailto: and tel: links, closes #5521 (#5544)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Dec 13, 2022
1 parent 9db9e6c commit d0d873e
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changes/open-links-mail-tel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": minor
---

Added support to `mailto:` and `tel:` links on the shell API.
2 changes: 1 addition & 1 deletion core/config-schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
{
Expand Down
2 changes: 1 addition & 1 deletion core/tauri-codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())),
Expand Down
2 changes: 1 addition & 1 deletion core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions core/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/tauri/scripts/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/api/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/scope/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tooling/api/src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion tooling/cli/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
{
Expand Down

0 comments on commit d0d873e

Please sign in to comment.