-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
acl.rs
288 lines (250 loc) · 9.47 KB
/
acl.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use std::{
collections::{BTreeMap, BTreeSet},
fs::{copy, create_dir_all, read_to_string, write},
path::PathBuf,
};
use anyhow::{Context, Result};
use schemars::{
schema::{
ArrayValidation, InstanceType, Metadata, ObjectValidation, RootSchema, Schema, SchemaObject,
SubschemaValidation,
},
schema_for,
};
use tauri_utils::{
acl::{build::CapabilityFile, capability::Capability, plugin::Manifest},
platform::Target,
};
const CAPABILITIES_SCHEMA_FILE_NAME: &str = "schema.json";
/// Path of the folder where schemas are saved.
const CAPABILITIES_SCHEMA_FOLDER_PATH: &str = "capabilities/schemas";
const CAPABILITIES_FILE_NAME: &str = "capabilities.json";
const PLUGIN_MANIFESTS_FILE_NAME: &str = "plugin-manifests.json";
fn capabilities_schema(plugin_manifests: &BTreeMap<String, Manifest>) -> RootSchema {
let mut schema = schema_for!(CapabilityFile);
fn schema_from(plugin: &str, id: &str, description: Option<&str>) -> Schema {
Schema::Object(SchemaObject {
metadata: Some(Box::new(Metadata {
description: description
.as_ref()
.map(|d| format!("{plugin}:{id} -> {d}")),
..Default::default()
})),
instance_type: Some(InstanceType::String.into()),
enum_values: Some(vec![serde_json::Value::String(format!("{plugin}:{id}"))]),
..Default::default()
})
}
let mut permission_schemas = Vec::new();
for (plugin, manifest) in plugin_manifests {
for (set_id, set) in &manifest.permission_sets {
permission_schemas.push(schema_from(plugin, set_id, Some(&set.description)));
}
if let Some(default) = &manifest.default_permission {
permission_schemas.push(schema_from(
plugin,
"default",
Some(default.description.as_ref()),
));
}
for (permission_id, permission) in &manifest.permissions {
permission_schemas.push(schema_from(
plugin,
permission_id,
permission.description.as_deref(),
));
}
}
if let Some(Schema::Object(obj)) = schema.definitions.get_mut("Identifier") {
obj.object = None;
obj.instance_type = None;
obj.metadata.as_mut().map(|metadata| {
metadata
.description
.replace("Permission identifier".to_string());
metadata
});
obj.subschemas.replace(Box::new(SubschemaValidation {
one_of: Some(permission_schemas),
..Default::default()
}));
}
if let Some(Schema::Object(obj)) = schema.definitions.get_mut("PermissionEntry") {
let permission_entry_any_of_schemas = obj.subschemas().any_of.as_mut().unwrap();
if let Schema::Object(mut scope_extended_schema_obj) =
permission_entry_any_of_schemas.remove(permission_entry_any_of_schemas.len() - 1)
{
let mut global_scope_one_of = Vec::new();
for (plugin, manifest) in plugin_manifests {
if let Some(global_scope_schema) = &manifest.global_scope_schema {
let global_scope_schema_def: Schema = serde_json::from_value(global_scope_schema.clone())
.unwrap_or_else(|e| panic!("invalid JSON schema for plugin {plugin}: {e}"));
let global_scope_schema = Schema::Object(SchemaObject {
array: Some(Box::new(ArrayValidation {
items: Some(global_scope_schema_def.into()),
..Default::default()
})),
..Default::default()
});
let mut required = BTreeSet::new();
required.insert("identifier".to_string());
let mut object = ObjectValidation {
required,
..Default::default()
};
let mut permission_schemas = Vec::new();
if let Some(default) = &manifest.default_permission {
permission_schemas.push(schema_from(plugin, "default", Some(&default.description)));
}
for set in manifest.permission_sets.values() {
permission_schemas.push(schema_from(plugin, &set.identifier, Some(&set.description)));
}
for permission in manifest.permissions.values() {
permission_schemas.push(schema_from(
plugin,
&permission.identifier,
permission.description.as_deref(),
));
}
let identifier_schema = Schema::Object(SchemaObject {
subschemas: Some(Box::new(SubschemaValidation {
one_of: Some(permission_schemas),
..Default::default()
})),
..Default::default()
});
object
.properties
.insert("identifier".to_string(), identifier_schema);
object
.properties
.insert("allow".to_string(), global_scope_schema.clone());
object
.properties
.insert("deny".to_string(), global_scope_schema);
global_scope_one_of.push(Schema::Object(SchemaObject {
instance_type: Some(InstanceType::Object.into()),
object: Some(Box::new(object)),
..Default::default()
}));
}
}
if !global_scope_one_of.is_empty() {
scope_extended_schema_obj.object = None;
scope_extended_schema_obj
.subschemas
.replace(Box::new(SubschemaValidation {
one_of: Some(global_scope_one_of),
..Default::default()
}));
permission_entry_any_of_schemas.push(scope_extended_schema_obj.into());
};
}
}
schema
}
pub fn generate_schema(
plugin_manifests: &BTreeMap<String, Manifest>,
target: Target,
) -> Result<()> {
let schema = capabilities_schema(plugin_manifests);
let schema_str = serde_json::to_string_pretty(&schema).unwrap();
let out_dir = PathBuf::from(CAPABILITIES_SCHEMA_FOLDER_PATH);
create_dir_all(&out_dir).context("unable to create schema output directory")?;
let schema_path = out_dir.join(format!("{target}-{CAPABILITIES_SCHEMA_FILE_NAME}"));
if schema_str != read_to_string(&schema_path).unwrap_or_default() {
write(&schema_path, "{schema_str}")?;
copy(
schema_path,
out_dir.join(format!(
"{}-{CAPABILITIES_SCHEMA_FILE_NAME}",
if target.is_desktop() {
"desktop"
} else {
"mobile"
}
)),
)?;
}
Ok(())
}
pub fn save_capabilities(capabilities: &BTreeMap<String, Capability>) -> Result<PathBuf> {
let capabilities_path =
PathBuf::from(CAPABILITIES_SCHEMA_FOLDER_PATH).join(CAPABILITIES_FILE_NAME);
let capabilities_json = serde_json::to_string(&capabilities)?;
if capabilities_json != read_to_string(&capabilities_path).unwrap_or_default() {
std::fs::write(&capabilities_path, capabilities_json)?;
}
Ok(capabilities_path)
}
pub fn save_plugin_manifests(plugin_manifests: &BTreeMap<String, Manifest>) -> Result<PathBuf> {
let plugin_manifests_path =
PathBuf::from(CAPABILITIES_SCHEMA_FOLDER_PATH).join(PLUGIN_MANIFESTS_FILE_NAME);
let plugin_manifests_json = serde_json::to_string(&plugin_manifests)?;
if plugin_manifests_json != read_to_string(&plugin_manifests_path).unwrap_or_default() {
std::fs::write(&plugin_manifests_path, plugin_manifests_json)?;
}
Ok(plugin_manifests_path)
}
pub fn get_plugin_manifests() -> Result<BTreeMap<String, Manifest>> {
let permission_map =
tauri_utils::acl::build::read_permissions().context("failed to read plugin permissions")?;
let mut global_scope_map = tauri_utils::acl::build::read_global_scope_schemas()
.context("failed to read global scope schemas")?;
let mut processed = BTreeMap::new();
for (plugin_name, permission_files) in permission_map {
let manifest = Manifest::new(permission_files, global_scope_map.remove(&plugin_name));
processed.insert(plugin_name, manifest);
}
Ok(processed)
}
pub fn validate_capabilities(
plugin_manifests: &BTreeMap<String, Manifest>,
capabilities: &BTreeMap<String, Capability>,
) -> Result<()> {
let target = tauri_utils::platform::Target::from_triple(&std::env::var("TARGET").unwrap());
for capability in capabilities.values() {
if !capability.platforms.contains(&target) {
continue;
}
for permission_entry in &capability.permissions {
let permission_id = permission_entry.identifier();
if let Some((plugin_name, permission_name)) = permission_id.get().split_once(':') {
let permission_exists = plugin_manifests
.get(plugin_name)
.map(|manifest| {
if permission_name == "default" {
manifest.default_permission.is_some()
} else {
manifest.permissions.contains_key(permission_name)
|| manifest.permission_sets.contains_key(permission_name)
}
})
.unwrap_or(false);
if !permission_exists {
let mut available_permissions = Vec::new();
for (plugin, manifest) in plugin_manifests {
if manifest.default_permission.is_some() {
available_permissions.push(format!("{plugin}:default"));
}
for p in manifest.permissions.keys() {
available_permissions.push(format!("{plugin}:{p}"));
}
for p in manifest.permission_sets.keys() {
available_permissions.push(format!("{plugin}:{p}"));
}
}
anyhow::bail!(
"Permission {} not found, expected one of {}",
permission_id.get(),
available_permissions.join(", ")
);
}
}
}
}
Ok(())
}