toolblox_lib/
widget_protocol.rs1use std::path::{Component, Path};
4
5use tauri::Manager;
6
7use crate::app_commands::Runtime;
8
9pub fn response(
15 context: tauri::UriSchemeContext<'_, tauri::Wry>,
16 request: tauri::http::Request<Vec<u8>>,
17) -> tauri::http::Response<Vec<u8>> {
18 if !crate::lifecycle::is_app_window(context.webview_label()) {
19 return error(
20 tauri::http::StatusCode::FORBIDDEN,
21 "Widget assets aren't available here.",
22 );
23 }
24 let path = request.uri().path().trim_start_matches('/');
25 let mut parts = Path::new(path).components();
26 let Some(Component::Normal(widget_id)) = parts.next() else {
27 return error(
28 tauri::http::StatusCode::BAD_REQUEST,
29 "The widget path isn't valid.",
30 );
31 };
32 let widget_id = widget_id.to_string_lossy();
33 if widget_id.is_empty()
34 || !widget_id
35 .bytes()
36 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-'))
37 {
38 return error(
39 tauri::http::StatusCode::BAD_REQUEST,
40 "The widget ID isn't valid.",
41 );
42 }
43 let relative: std::path::PathBuf = parts.collect();
44 if relative.as_os_str().is_empty()
45 || relative
46 .components()
47 .any(|part| !matches!(part, Component::Normal(_)))
48 {
49 return error(
50 tauri::http::StatusCode::BAD_REQUEST,
51 "The widget file path isn't valid.",
52 );
53 }
54 let runtime = context.app_handle().state::<Runtime>();
55 let root = runtime.widgets_root.join(widget_id.as_ref());
56 let Ok(root) = root.canonicalize() else {
57 return error(
58 tauri::http::StatusCode::NOT_FOUND,
59 "The widget isn't installed.",
60 );
61 };
62 let file = root.join(relative);
63 let Ok(file) = file.canonicalize() else {
64 return error(
65 tauri::http::StatusCode::NOT_FOUND,
66 "The widget file wasn't found.",
67 );
68 };
69 if !file.starts_with(&root) || !file.is_file() {
70 return error(
71 tauri::http::StatusCode::FORBIDDEN,
72 "The widget file path isn't allowed.",
73 );
74 }
75 let Ok(bytes) = std::fs::read(&file) else {
76 return error(
77 tauri::http::StatusCode::INTERNAL_SERVER_ERROR,
78 "The widget file couldn't be read.",
79 );
80 };
81 let content_type = match file
82 .extension()
83 .and_then(|value| value.to_str())
84 .unwrap_or("")
85 {
86 "html" => "text/html; charset=utf-8",
87 "css" => "text/css; charset=utf-8",
88 "js" | "mjs" => "text/javascript; charset=utf-8",
89 "json" => "application/json; charset=utf-8",
90 "svg" => "image/svg+xml",
91 "png" => "image/png",
92 "jpg" | "jpeg" => "image/jpeg",
93 "gif" => "image/gif",
94 "webp" => "image/webp",
95 "woff" => "font/woff",
96 "woff2" => "font/woff2",
97 _ => "application/octet-stream",
98 };
99 tauri::http::Response::builder()
100 .status(tauri::http::StatusCode::OK)
101 .header("Content-Type", content_type)
102 .header("X-Content-Type-Options", "nosniff")
103 .header("Cache-Control", "no-store")
104 .header("Access-Control-Allow-Origin", "*")
105 .header(
106 "Content-Security-Policy",
107 "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'none'; object-src 'none'; base-uri 'none'; form-action 'none'",
108 )
109 .body(bytes)
110 .unwrap_or_else(|_| tauri::http::Response::new(Vec::new()))
111}
112
113fn error(status: tauri::http::StatusCode, message: &str) -> tauri::http::Response<Vec<u8>> {
115 tauri::http::Response::builder()
116 .status(status)
117 .header("Content-Type", "text/plain; charset=utf-8")
118 .header("X-Content-Type-Options", "nosniff")
119 .body(message.as_bytes().to_vec())
120 .unwrap_or_else(|_| tauri::http::Response::new(Vec::new()))
121}