glycin-core 4.0.0-alpha

Sandboxed image decoding
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#[cfg(feature = "builtin")]
use std::marker::PhantomData;
use std::path::{Path, PathBuf};
use std::sync::Arc;

#[cfg(feature = "gobject")]
use gio::glib;
use gio::prelude::*;

use crate::config::{Config, ImageEditorConfig, ImageLoaderConfig};
#[cfg(feature = "external")]
use crate::dbus::ZbusProxy;
use crate::dbus::{EditorProxy, LoaderProxy};
#[cfg(feature = "external")]
use crate::pool::{PooledProcess, UsageTracker};
use crate::source::SourceTransmission;
use crate::util::RunEnvironment;
use crate::{Error, ErrorKind, MimeType, Pool, config};

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
/// Sandboxing mechanism for image loading and editing
pub enum SandboxMechanism {
    Bwrap,
    FlatpakSpawn,
    NotSandboxed,
}

impl SandboxMechanism {
    pub async fn detect() -> Self {
        match RunEnvironment::cached().await {
            RunEnvironment::FlatpakDevel => Self::NotSandboxed,
            RunEnvironment::Flatpak => Self::FlatpakSpawn,
            RunEnvironment::Host => Self::Bwrap,
            RunEnvironment::HostBwrapSyscallsBlocked => Self::NotSandboxed,
        }
    }

    pub fn into_selector(self) -> SandboxSelector {
        match self {
            Self::Bwrap => SandboxSelector::Bwrap,
            Self::FlatpakSpawn => SandboxSelector::FlatpakSpawn,
            Self::NotSandboxed => SandboxSelector::NotSandboxed,
        }
    }
}

#[derive(Debug, Copy, Clone, Default)]
#[cfg_attr(feature = "gobject", derive(gio::glib::Enum))]
#[cfg_attr(feature = "gobject", enum_type(name = "GlySandboxSelector"))]
#[repr(i32)]
/// Method by which the [`SandboxMechanism`] is selected
pub enum SandboxSelector {
    #[default]
    /// This mode selects `bwrap` outside of Flatpaks and usually
    /// `flatpak-spawn` inside of Flatpaks. The sandbox is disabled
    /// automatically inside of Flatpak development environments. See
    /// details below.
    ///
    /// Inside of Flatpaks, `flatpak-spawn` is used to create the sandbox. This
    /// mechanism starts an installed Flatpak with the same app id. For
    /// development, Flatpak are usually not installed and the sandbox can
    /// therefore not be used. If the sandbox has been started via
    /// `flatpak-builder --run` (i.e. without installed Flatpak) and the app id
    /// ends with `Devel`, the sandbox is disabled.
    Auto,
    Bwrap,
    FlatpakSpawn,
    NotSandboxed,
}

impl SandboxSelector {
    pub async fn determine_sandbox_mechanism(self) -> SandboxMechanism {
        match self {
            Self::Auto => SandboxMechanism::detect().await,
            Self::Bwrap => SandboxMechanism::Bwrap,
            Self::FlatpakSpawn => SandboxMechanism::FlatpakSpawn,
            Self::NotSandboxed => SandboxMechanism::NotSandboxed,
        }
    }
}

#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum ColorState {
    Srgb,
    Cicp(crate::Cicp),
}

/// A version of an input stream that can be sent.
///
/// Using the stream from multiple threads is UB. Therefore the `new` function
/// is unsafe.
#[derive(Debug, Clone)]
pub(crate) struct GInputStreamSend(gio::InputStream);

unsafe impl Send for GInputStreamSend {}
unsafe impl Sync for GInputStreamSend {}

impl GInputStreamSend {
    pub(crate) unsafe fn new(stream: gio::InputStream) -> Self {
        Self(stream)
    }

    pub(crate) fn display(&self) -> String {
        self.0.type_().name().to_string()
    }
}

/// Image source for a loader/editor
#[derive(Debug, Clone)]
pub(crate) enum Source {
    File(gio::File),
    Stream(GInputStreamSend),
    TransferredStream,
}

impl Source {
    pub fn file(&self) -> Option<gio::File> {
        match self {
            Self::File(file) => Some(file.clone()),
            _ => None,
        }
    }

    pub async fn to_stream(&self) -> Result<gio::InputStream, Error> {
        match self {
            Self::File(file) => file
                .read_future(glib::Priority::DEFAULT)
                .await
                .map(|x| x.upcast())
                .map_err(|e| ErrorKind::ImageSource(e).err()),
            Self::Stream(stream) => Ok(stream.0.clone()),
            Self::TransferredStream => Err(ErrorKind::TransferredStream.into()),
        }
    }

    /// Get a [`Source`] for sending to [`GFileWorker`]
    ///
    /// This will remove the stored stream from `self` to avoid it getting used
    /// anywhere else than the [`GFileWorker`] it has been sent to.
    pub fn send(&mut self) -> Self {
        let new = self
            .file()
            .map(Self::File)
            .unwrap_or(Self::TransferredStream);

        std::mem::replace(self, new)
    }

    pub fn display(&self) -> String {
        match self {
            Self::File(file) => {
                if let Some(path) = file.path() {
                    path.display().to_string()
                } else {
                    file.uri().to_string()
                }
            }
            Self::Stream(stream) => {
                format!("Stream({})", stream.display())
            }
            Self::TransferredStream => String::from("TransferredStream"),
        }
    }
}

#[derive(Debug)]
pub(crate) struct ProcessorContext<T: GetConfig, S> {
    pub mime_type: MimeType,
    pub sandbox_mechanism: SandboxMechanism,
    pub config_entry: T,
    pub g_file_worker: S,
    pub base_dir: Option<PathBuf>,
}

pub trait GetConfig {
    fn config_entry<'a>(config: &'a Config, mime_type: &'a MimeType) -> Result<&'a Self, Error>;
    fn expose_base_dir(&self) -> bool;
    fn guess_mime_type(config: &Config, path: Option<&Path>, head: &[u8]) -> Option<MimeType>;
}

impl GetConfig for ImageLoaderConfig {
    fn config_entry<'a>(
        config: &'a Config,
        mime_type: &'a MimeType,
    ) -> Result<&'a ImageLoaderConfig, Error> {
        config.loader(mime_type)
    }

    fn expose_base_dir(&self) -> bool {
        self.expose_base_dir
    }

    fn guess_mime_type(config: &Config, path: Option<&Path>, head: &[u8]) -> Option<MimeType> {
        Config::guess_mime_type(config, path, head, false)
    }
}

impl GetConfig for ImageEditorConfig {
    fn config_entry<'a>(
        config: &'a Config,
        mime_type: &'a MimeType,
    ) -> Result<&'a ImageEditorConfig, Error> {
        config.editor(mime_type)
    }

    fn expose_base_dir(&self) -> bool {
        self.expose_base_dir
    }

    fn guess_mime_type(config: &Config, path: Option<&Path>, head: &[u8]) -> Option<MimeType> {
        Config::guess_mime_type(config, path, head, true)
    }
}

impl<T: GetConfig + Clone> ProcessorContext<T, SourceTransmission> {
    /// Determines mime-type, relevant config entry, and sandboxing mode
    ///
    /// Also spawns the file worker since we need to read from the file for
    /// detecting the mime type
    pub(crate) async fn new(
        source: Source,
        use_expose_base_dir: bool,
        sandbox_selector: &SandboxSelector,
    ) -> Result<ProcessorContext<T, SourceTransmission>, Error> {
        let file = source.file();

        let source_transmission = SourceTransmission::init(source).await?;
        let config = config::Config::cached().await;

        let mime_type = T::guess_mime_type(
            &config,
            source_transmission.file().and_then(|x| x.path()).as_deref(),
            source_transmission.first_bytes(),
        );

        let mime_type = if let Some(mime_type) = mime_type {
            mime_type
        } else {
            guess_mime_type(
                source_transmission.file(),
                source_transmission.first_bytes(),
            )
            .await?
        };

        let config_entry = T::config_entry(&config, &mime_type)?.clone();

        let base_dir = if use_expose_base_dir && config_entry.expose_base_dir() {
            file.and_then(|x| x.parent()).and_then(|x| x.path())
        } else {
            None
        };

        let sandbox_mechanism = sandbox_selector.determine_sandbox_mechanism().await;

        Ok(ProcessorContext {
            config_entry,
            base_dir,
            mime_type,
            sandbox_mechanism,
            g_file_worker: source_transmission,
        })
    }
}

impl<T: GetConfig + Clone> ProcessorContext<T, ()> {
    pub async fn new_sourceless(
        mime_type: MimeType,
        sandbox_selector: &SandboxSelector,
    ) -> Result<ProcessorContext<T, ()>, Error> {
        let config = Config::cached().await;
        let config_entry = T::config_entry(&config, &mime_type)?.clone();
        let sandbox_mechanism = sandbox_selector.determine_sandbox_mechanism().await;

        Ok(Self {
            mime_type,
            base_dir: None,
            config_entry,
            sandbox_mechanism,
            g_file_worker: (),
        })
    }
}

impl<S> ProcessorContext<ImageLoaderConfig, S> {
    pub async fn loader(
        self,
        pool: Arc<Pool>,
        cancellable: &gio::Cancellable,
    ) -> Result<Processor<LoaderProxy<'static>, S>, Error> {
        match self.config_entry.processor {
            #[cfg(feature = "external")]
            config::Processor::Binary(_) => self
                .spin_up_loader(pool, cancellable)
                .await
                .map(Processor::Binary),
            #[cfg(feature = "builtin")]
            config::Processor::Builtin(builtin) => Ok(Processor::Builtin(BuiltinProcessor {
                builtin,
                source_transmission: self.g_file_worker,
                mime_type: self.mime_type,
                _phantom_data: Default::default(),
            })),
        }
    }

    #[cfg(feature = "external")]
    async fn spin_up_loader(
        self,
        pool: Arc<Pool>,
        cancellable: &gio::Cancellable,
    ) -> Result<ExternalProcessor<LoaderProxy<'static>, S>, Error> {
        let (process, usage_tracker) = pool
            .clone()
            .get_loader(
                self.config_entry,
                self.sandbox_mechanism,
                self.base_dir,
                cancellable,
            )
            .await?;

        Ok(ExternalProcessor {
            process,
            usage_tracker,
            source_transmission: self.g_file_worker,
            mime_type: self.mime_type,
            sandbox_mechanism: self.sandbox_mechanism,
        })
    }
}

impl<S> ProcessorContext<ImageEditorConfig, S> {
    pub async fn editor(
        self,
        pool: Arc<Pool>,
        cancellable: &gio::Cancellable,
    ) -> Result<Processor<EditorProxy<'static>, S>, Error> {
        match self.config_entry.processor {
            #[cfg(feature = "external")]
            config::Processor::Binary(_) => self
                .spin_up_editor(pool, cancellable)
                .await
                .map(Processor::Binary),
            #[cfg(feature = "builtin")]
            config::Processor::Builtin(builtin) => Ok(Processor::Builtin(BuiltinProcessor {
                builtin,
                source_transmission: self.g_file_worker,
                mime_type: self.mime_type,
                _phantom_data: Default::default(),
            })),
        }
    }

    #[cfg(feature = "external")]
    async fn spin_up_editor(
        self,
        pool: Arc<Pool>,
        cancellable: &gio::Cancellable,
    ) -> Result<ExternalProcessor<EditorProxy<'static>, S>, Error> {
        let (process, usage_tracker) = pool
            .clone()
            .get_editor(
                self.config_entry,
                self.sandbox_mechanism,
                self.base_dir,
                cancellable,
            )
            .await?;

        Ok(ExternalProcessor {
            process,
            usage_tracker,
            source_transmission: self.g_file_worker,
            mime_type: self.mime_type,
            sandbox_mechanism: self.sandbox_mechanism,
        })
    }
}
#[cfg(feature = "external")]
pub trait DBusProxy: ZbusProxy<'static> + 'static {}
#[cfg(not(feature = "external"))]
pub trait DBusProxy: 'static {}

impl DBusProxy for LoaderProxy<'static> {}
impl DBusProxy for EditorProxy<'static> {}

//impl DBusProxy for () {}

pub(crate) enum Processor<P: DBusProxy, S> {
    #[cfg(feature = "external")]
    Binary(ExternalProcessor<P, S>),
    #[cfg(feature = "builtin")]
    Builtin(BuiltinProcessor<P, S>),
}

#[cfg(feature = "external")]
pub(crate) struct ExternalProcessor<P: DBusProxy, S> {
    pub process: Arc<PooledProcess<P>>,
    pub source_transmission: S,
    pub mime_type: MimeType,
    pub sandbox_mechanism: SandboxMechanism,
    pub usage_tracker: Arc<UsageTracker>,
}

#[cfg(feature = "builtin")]
pub(crate) struct BuiltinProcessor<T, S> {
    pub builtin: config::BuiltinProcessor,
    pub mime_type: MimeType,
    pub source_transmission: S,
    _phantom_data: PhantomData<T>,
}

#[cfg(feature = "external")]
impl<P: DBusProxy, S> ExternalProcessor<P, S> {
    pub fn use_process(&self) -> Arc<crate::dbus::RemoteProcess<P>> {
        self.process.use_()
    }
}

pub(crate) async fn guess_mime_type(
    file: Option<&gio::File>,
    head: &[u8],
) -> Result<MimeType, Error> {
    fn guess_mime_type_(
        filename: Option<PathBuf>,
        data: &[u8],
    ) -> (Result<glib::GString, Error>, bool) {
        let (content_type, unsure) = gio::content_type_guess(filename, data);

        let mime_type = gio::content_type_get_mime_type(&content_type)
            .ok_or_else(|| ErrorKind::UnknownContentType(content_type.to_string()).into());

        (mime_type, unsure)
    }

    let (mime_type, unsure) = guess_mime_type_(None, head);

    // Prefer file extension for TIFF since it can be a RAW format as well
    let is_tiff = mime_type.clone().ok() == Some("image/tiff".into());

    // Prefer file extension for XML since long comment between `<?xml` and `<svg>`
    // can falsely guess XML instead of SVG
    let is_xml = mime_type.clone().ok() == Some("application/xml".into());

    // Prefer file extension for gzip since it might be an SVGZ
    let is_gzip = mime_type.clone().ok() == Some("application/gzip".into());

    // Prefer file extension for text since it might be an XBM
    let is_text = mime_type.clone().ok() == Some("text/plain".into());

    let mime_type = if (unsure || is_tiff || is_xml || is_gzip || is_text)
        && let Some(filename) = file.and_then(|x| x.basename())
    {
        guess_mime_type_(Some(filename), head).0?
    } else {
        mime_type?
    };

    tracing::trace!("Mimetype is: '{mime_type}'");

    Ok(MimeType::new(mime_type.to_string()))
}