Skip to main content

coven_storage/
session.rs

1use crate::{CloudProvider, ObjectPath, StorageConfig, StorageError, StorageFailure};
2use coven_crypto::SecretBytes;
3use coven_crypto::SecretText;
4use coven_foundation::id_source::DeviceId;
5use serde::{Deserialize, Serialize};
6use std::ops::Range;
7
8pub(crate) const CLOUDKIT_PART_SIZE: usize = 8 * 1024 * 1024;
9pub(crate) const DROPBOX_PART_SIZE: usize = 8 * 1024 * 1024;
10pub(crate) const ONEDRIVE_PART_SIZE: usize = 24 * 320 * 1024;
11/// The most parts one S3 multipart upload holds.
12pub(crate) const S3_MAX_PARTS: usize = 10_000;
13/// The largest body one S3 request carries: a whole object's PUT, one part,
14/// or the object one `CopyObject` copies.
15pub(crate) const S3_REQUEST_LIMIT: u64 = 5 * 1024 * 1024 * 1024;
16/// S3 part sizes are whole multiples of this.
17const S3_PART_UNIT: u64 = 8 * 1024 * 1024;
18
19/// A provider upload recorded by the caller's operation (§17.9, §11).
20/// The destination retains its original absent/revision condition across retries.
21///
22/// Record it before sending parts and after each call, including failed calls:
23/// a publication conflict invalidates the native session even if abort fails.
24/// Record it, too, after admitting a part that marks its plan
25/// ([`crate::PartPermit::marked`]), before sending that part. After a crash,
26/// call `resume_upload` to discover bytes accepted before a reply was lost. The
27/// bytes of this value include an upload capability: keep them secret. Dropping
28/// a session never aborts it; explicit abort abandons its pending bytes.
29/// On copy profiles the recorded token names the temporary object as well as
30/// identifying publication, and the session is recorded before the native
31/// multipart upload exists: the first part initiates it. Abort deletes it even after native multipart completion; failed
32/// copy or cleanup keeps the recording incomplete so it can be retried.
33/// OneDrive creation returns its planned temporary UUID before any provider request.
34/// After recording it, resume and record the returned native capability before
35/// sending parts. The UUID stays through receipt, copying, verification
36/// and deletion; native transfer completion alone is not publication.
37/// Providers may retain closed session data until their expiry, without publishing it.
38/// When recovery requires complete-byte comparison with a published object,
39/// resumption resets the confirmed offset to zero. Feed the encrypted
40/// bytes through `upload_part` again: they are compared to the published object,
41/// without uploading them. Only a complete match confirms publication.
42#[derive(Clone, Serialize, Deserialize)]
43#[serde(try_from = "RecordedUploadSession")]
44pub struct UploadSession {
45    /// The device whose connection began it: its temporaries and provider
46    /// session sit in that device's reserved folder (D10).
47    pub(crate) device: DeviceId,
48    pub(crate) location: StorageConfig,
49    pub(crate) path: ObjectPath,
50    pub(crate) total: u64,
51    pub(crate) confirmed: u64,
52    pub(crate) part_size: usize,
53    pub(crate) state: SessionState,
54}
55
56// The wire boundary must construct an unchecked recording before validation.
57// Keeping this private makes both Serde and `decode` return validated sessions.
58#[derive(Deserialize)]
59#[serde(deny_unknown_fields)]
60struct RecordedUploadSession {
61    device: DeviceId,
62    location: StorageConfig,
63    path: ObjectPath,
64    total: u64,
65    confirmed: u64,
66    part_size: usize,
67    state: SessionState,
68}
69
70impl TryFrom<RecordedUploadSession> for UploadSession {
71    type Error = StorageError;
72    fn try_from(recorded: RecordedUploadSession) -> Result<Self, Self::Error> {
73        let session = Self {
74            device: recorded.device,
75            location: recorded.location,
76            path: recorded.path,
77            total: recorded.total,
78            confirmed: recorded.confirmed,
79            part_size: recorded.part_size,
80            state: recorded.state,
81        };
82        session.validate()?;
83        Ok(session)
84    }
85}
86
87#[derive(Clone, Serialize, Deserialize)]
88#[serde(deny_unknown_fields)]
89pub(crate) enum SessionState {
90    S3 {
91        id: SecretText,
92        token: SecretText,
93        parts: Vec<S3Part>,
94    },
95    S3Restart {
96        id: SecretText,
97        token: SecretText,
98    },
99    /// A session whose upload name is recorded before the native multipart
100    /// upload is initiated, so no upload exists that its recording does not
101    /// name (Appendix E.10).
102    S3Planned {
103        token: SecretText,
104        started: PlanStart,
105    },
106    Dropbox {
107        id: SecretText,
108    },
109    OneDrive {
110        temporary: uuid::Uuid,
111        upload: OneDriveUpload,
112    },
113    CloudKit {
114        id: SecretText,
115    },
116    VerifyPublished,
117    Complete,
118    /// The deterministic provider's session named before its first part
119    /// starts it, as an S3 multipart upload is (Appendix E.10).
120    #[cfg(any(test, feature = "test-utils"))]
121    MemoryPlanned {
122        started: PlanStart,
123    },
124    #[cfg(any(test, feature = "test-utils"))]
125    Memory {
126        id: u64,
127        complete: bool,
128    },
129    /// The deterministic provider's session whose parts are independent, as
130    /// an S3 multipart upload's are: the indexes, from zero, of the parts it
131    /// confirmed, in order.
132    #[cfg(any(test, feature = "test-utils"))]
133    MemoryParts {
134        id: u64,
135        parts: Vec<u64>,
136        complete: bool,
137    },
138}
139
140/// Whether a planned session's first part may have started its upload
141/// (§17.9, Appendix E.10). The recording keeps one fact, whether the plan is
142/// marked; what this process knows besides lives in memory only.
143#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
144#[serde(from = "bool", into = "bool")]
145pub(crate) enum PlanStart {
146    /// Unmarked: no request that may start the upload was sent, so nothing
147    /// is at the plan's name.
148    Unstarted,
149    /// Marked by the connection's admission of the first part, which has not
150    /// been sent: this process knows nothing is at the name yet.
151    Starting,
152    /// Marked, and the first part may have started an upload at the name: it
153    /// is going out here, or went out, or the recording was restored.
154    MayHaveStarted,
155}
156
157impl From<bool> for PlanStart {
158    fn from(marked: bool) -> Self {
159        if marked {
160            Self::MayHaveStarted
161        } else {
162            Self::Unstarted
163        }
164    }
165}
166
167impl From<PlanStart> for bool {
168    fn from(start: PlanStart) -> Self {
169        start != PlanStart::Unstarted
170    }
171}
172
173/// Whether a planned session's start went out: set where the provider hands
174/// the start's request to the network, past every check made as it goes,
175/// the publication gate's and a provider cooldown's (§17.9, Appendix E.10).
176/// The start's attempts share it, so one that went out before a retry was
177/// refused still counts.
178#[derive(Clone, Default)]
179pub(crate) struct StartSent(std::sync::Arc<std::sync::atomic::AtomicBool>);
180
181impl StartSent {
182    /// The start's request goes out now.
183    pub(crate) fn going(&self) {
184        self.0.store(true, std::sync::atomic::Ordering::SeqCst);
185    }
186    fn went(&self) -> bool {
187        self.0.load(std::sync::atomic::Ordering::SeqCst)
188    }
189}
190
191#[derive(Clone, Serialize, Deserialize)]
192#[serde(deny_unknown_fields)]
193pub(crate) struct S3Part {
194    pub(crate) number: i32,
195    pub(crate) size: u64,
196    pub(crate) etag: String,
197}
198
199/// Native transfer and publication are separate; every phase retains the
200/// temporary's identity until its deletion has succeeded.
201#[derive(Clone, Serialize, Deserialize)]
202#[serde(deny_unknown_fields)]
203pub(crate) enum OneDriveUpload {
204    /// The temporary identity exists before any native request (§20.6).
205    Planned {
206        /// Only this process can know initiation has never been attempted.
207        #[serde(skip)]
208        unstarted: bool,
209    },
210    Receiving {
211        url: SecretText,
212    },
213    Uploaded {
214        item: String,
215    },
216    Copying {
217        monitor: SecretText,
218    },
219    // The copy definitively failed; cleanup may already have removed its source.
220    Refused,
221    VerifyPublished,
222    Published,
223}
224
225impl S3Part {
226    /// Whether this is one of the parts of an object of `total` bytes in
227    /// parts of `part_size`: its number names one, its size is that part's,
228    /// and the provider tagged it.
229    pub(crate) fn fits(&self, total: u64, part_size: usize) -> bool {
230        u64::try_from(self.number)
231            .ok()
232            .and_then(|number| number.checked_sub(1))
233            .and_then(|index| part_span(total, part_size, index))
234            .is_some_and(|span| span.end - span.start == self.size)
235            && !self.etag.is_empty()
236    }
237}
238
239/// The bytes of part `index`, counted from zero, of an object of `total`
240/// bytes in parts of `part_size`; none past its last part.
241pub(crate) fn part_span(total: u64, part_size: usize, index: u64) -> Option<Range<u64>> {
242    let start = index.checked_mul(part_size as u64)?;
243    (start < total).then(|| start..start.saturating_add(part_size as u64).min(total))
244}
245
246/// One part a provider whose parts are independent confirmed, for the
247/// caller to add to its session ([`UploadSession::confirm`]).
248pub struct ConfirmedPart(pub(crate) Confirmation);
249
250impl std::fmt::Debug for ConfirmedPart {
251    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
252        let index = match &self.0 {
253            Confirmation::S3(part) => i64::from(part.number) - 1,
254            #[cfg(any(test, feature = "test-utils"))]
255            Confirmation::Memory { index } => *index as i64,
256        };
257        f.debug_struct("ConfirmedPart").field("index", &index).finish()
258    }
259}
260
261pub(crate) enum Confirmation {
262    S3(S3Part),
263    /// Part `index`, from zero, of a deterministic provider's session.
264    #[cfg(any(test, feature = "test-utils"))]
265    Memory {
266        index: u64,
267    },
268}
269
270impl UploadSession {
271    /// Mark a planned session's plan as its first part is admitted, before
272    /// the part is sent: whether that changed the recording, which the caller
273    /// then saves before sending (§17.9, Appendix E.10).
274    pub(crate) fn mark_start(&mut self) -> bool {
275        let started = match &mut self.state {
276            SessionState::S3Planned { started, .. } => started,
277            #[cfg(any(test, feature = "test-utils"))]
278            SessionState::MemoryPlanned { started } => started,
279            _ => return false,
280        };
281        if *started != PlanStart::Unstarted {
282            return false;
283        }
284        *started = PlanStart::Starting;
285        true
286    }
287    /// Settle a planned session's plan after its start failed. One whose
288    /// request went out may have started an upload, and stays marked; one
289    /// refused before it went out began nothing, so its plan is unmarked
290    /// again, for the caller to save before the failure is reported
291    /// (Appendix E.10).
292    pub(crate) fn start_failed(&mut self, start: &StartSent) {
293        let started = match &mut self.state {
294            SessionState::S3Planned { started, .. } => started,
295            #[cfg(any(test, feature = "test-utils"))]
296            SessionState::MemoryPlanned { started } => started,
297            _ => return,
298        };
299        if !start.went() {
300            *started = PlanStart::Unstarted;
301        }
302    }
303    /// Whether native initiation must follow recording this identity, before
304    /// any part is sent. Resume it and save the returned session first (§20.6).
305    pub fn needs_initiation(&self) -> bool {
306        matches!(self.state, SessionState::OneDrive { upload: OneDriveUpload::Planned { .. }, .. })
307    }
308    /// The device whose connection began this upload.
309    pub fn device(&self) -> DeviceId {
310        self.device
311    }
312    /// The destination encrypted-object path.
313    pub fn path(&self) -> &ObjectPath {
314        &self.path
315    }
316    /// Bytes confirmed by the provider; only these may advance the operation.
317    pub fn confirmed_bytes(&self) -> u64 {
318        self.confirmed
319    }
320    /// Total encrypted bytes promised when the session began.
321    pub fn total_bytes(&self) -> u64 {
322        self.total
323    }
324    /// Maximum part size. After resuming a partial part, send only the bytes
325    /// remaining to the next part boundary (or the end of the object).
326    pub fn part_size(&self) -> usize {
327        self.part_size
328    }
329    /// Whether publication and provider upload cleanup have succeeded.
330    pub fn is_complete(&self) -> bool {
331        match self.state {
332            SessionState::Complete => true,
333            #[cfg(any(test, feature = "test-utils"))]
334            SessionState::Memory { complete, .. } | SessionState::MemoryParts { complete, .. } => {
335                complete
336            }
337            _ => false,
338        }
339    }
340    /// Whether this session's parts are independent: the provider keeps
341    /// each by its index, whatever order they arrive in and however often
342    /// one is sent, so they may go together (§17.9, Appendix E.10). Only an
343    /// S3 multipart upload's are, once its first part has started it.
344    pub fn parts_independent(&self) -> bool {
345        match self.state {
346            SessionState::S3 { .. } => true,
347            #[cfg(any(test, feature = "test-utils"))]
348            SessionState::MemoryParts { complete, .. } => !complete,
349            _ => false,
350        }
351    }
352    /// The bytes of each part still to send, in order: where parts are
353    /// independent, every part the provider hasn't confirmed; elsewhere only
354    /// the next, which goes before any after it. After resuming a partial
355    /// part, the next runs only to its part's end.
356    pub fn parts_to_send(&self) -> Vec<Range<u64>> {
357        if self.is_complete() || self.confirmed >= self.total {
358            return Vec::new();
359        }
360        if let Some(confirmed) = self.confirmed_parts() {
361            return (0..self.total.div_ceil(self.part_size as u64))
362                .filter(|index| confirmed.binary_search(index).is_err())
363                .filter_map(|index| part_span(self.total, self.part_size, index))
364                .collect();
365        }
366        let next = (self.confirmed / self.part_size as u64 + 1) * self.part_size as u64;
367        std::iter::once(self.confirmed..next.min(self.total)).collect()
368    }
369    /// The indexes, from zero, of an independent session's confirmed parts,
370    /// in order.
371    fn confirmed_parts(&self) -> Option<Vec<u64>> {
372        match &self.state {
373            SessionState::S3 { parts, .. } => Some(
374                parts
375                    .iter()
376                    .filter_map(|part| u64::try_from(part.number).ok()?.checked_sub(1))
377                    .collect(),
378            ),
379            #[cfg(any(test, feature = "test-utils"))]
380            SessionState::MemoryParts { parts, complete: false, .. } => Some(parts.clone()),
381            _ => None,
382        }
383    }
384    /// Add a part the provider confirmed to an independent session. A part
385    /// that isn't one of the session's, or one it holds already, is refused
386    /// and changes nothing.
387    pub fn confirm(&mut self, part: ConfirmedPart) -> Result<(), StorageError> {
388        let (total, part_size) = (self.total, self.part_size);
389        let size = match (&mut self.state, part.0) {
390            (SessionState::S3 { parts, .. }, Confirmation::S3(part)) => {
391                if !part.fits(total, part_size) {
392                    return Err(StorageFailure::Protocol
393                        .with_source("S3 confirmed a part the session doesn't have"));
394                }
395                let at = parts
396                    .binary_search_by_key(&part.number, |held| held.number)
397                    .err()
398                    .ok_or(StorageFailure::InvalidPart)?;
399                let size = part.size;
400                parts.insert(at, part);
401                size
402            }
403            #[cfg(any(test, feature = "test-utils"))]
404            (
405                SessionState::MemoryParts { parts, complete: false, .. },
406                Confirmation::Memory { index },
407            ) => {
408                let span = part_span(total, part_size, index).ok_or(StorageFailure::Protocol)?;
409                let at = parts.binary_search(&index).err().ok_or(StorageFailure::InvalidPart)?;
410                parts.insert(at, index);
411                span.end - span.start
412            }
413            _ => return Err(StorageFailure::SessionMismatch.into()),
414        };
415        self.confirmed += size;
416        Ok(())
417    }
418    /// Encode the complete recorded state without printing upload credentials.
419    pub fn encode(&self) -> Result<SecretBytes, StorageError> {
420        self.validate()?;
421        crate::secret_json::encode(self)
422    }
423    /// Read a recording only if its provider, destination, state and progress agree.
424    /// Validation covers the location, state variant, nonempty identifiers, part
425    /// counts and sizes, and confirmed byte offset.
426    /// The adapter also checks that it names the adapter's location before any request.
427    pub fn decode(bytes: &[u8]) -> Result<Self, StorageError> {
428        let value: RecordedUploadSession = crate::secret_json::decode(bytes)?;
429        value.try_into()
430    }
431    fn validate(&self) -> Result<(), StorageError> {
432        self.location.validate()?;
433        // The floor pointer is one small record replaced in one request; no
434        // session targets it.
435        if self.path.is_replaceable() {
436            return Err(StorageFailure::InvalidPath.into());
437        }
438        if self.part_size == 0
439            || self.total == 0
440            || self.confirmed > self.total
441            || (self.is_complete() && self.confirmed != self.total)
442        {
443            return Err(StorageFailure::InvalidPart.into());
444        }
445        let provider = self.location.provider();
446        match &self.state {
447            SessionState::S3 { id, token, parts } if provider == CloudProvider::S3 => {
448                nonempty(id)?;
449                nonempty(token)?;
450                if parts.len() > S3_MAX_PARTS {
451                    return Err(StorageFailure::InvalidPart.into());
452                }
453                // Each confirmed part once, in number order, whichever
454                // arrived first.
455                let mut confirmed = 0u64;
456                let mut previous = 0;
457                for part in parts {
458                    if part.number <= previous || !part.fits(self.total, self.part_size) {
459                        return Err(StorageFailure::InvalidPart.into());
460                    }
461                    previous = part.number;
462                    confirmed += part.size;
463                }
464                if confirmed != self.confirmed {
465                    return Err(StorageFailure::InvalidPart.into());
466                }
467            }
468            SessionState::S3Restart { id, token } if provider == CloudProvider::S3 => {
469                nonempty(id)?;
470                nonempty(token)?;
471                if self.confirmed != 0 {
472                    return Err(StorageFailure::InvalidPart.into());
473                }
474            }
475            SessionState::S3Planned { token, .. } if provider == CloudProvider::S3 => {
476                nonempty(token)?;
477                if self.confirmed != 0 {
478                    return Err(StorageFailure::InvalidPart.into());
479                }
480            }
481            SessionState::Dropbox { id } if provider == CloudProvider::Dropbox => nonempty(id)?,
482            SessionState::OneDrive { upload, .. } if provider == CloudProvider::OneDrive => {
483                match upload {
484                    OneDriveUpload::Planned { .. } => {
485                        if self.confirmed != 0 {
486                            return Err(StorageFailure::InvalidPart.into());
487                        }
488                    }
489                    OneDriveUpload::Receiving { url } => {
490                        transfer_url(url)?;
491                        if self.confirmed == self.total {
492                            return Err(StorageFailure::InvalidPart.into());
493                        }
494                    }
495                    OneDriveUpload::Uploaded { item } => {
496                        if item.is_empty() || self.confirmed != self.total {
497                            return Err(StorageFailure::InvalidPart.into());
498                        }
499                    }
500                    OneDriveUpload::Copying { monitor } => {
501                        transfer_url(monitor)?;
502                        if self.confirmed != self.total {
503                            return Err(StorageFailure::InvalidPart.into());
504                        }
505                    }
506                    OneDriveUpload::Published | OneDriveUpload::Refused
507                        if self.confirmed != self.total =>
508                    {
509                        return Err(StorageFailure::InvalidPart.into());
510                    }
511                    OneDriveUpload::VerifyPublished if self.confirmed == self.total => {
512                        return Err(StorageFailure::InvalidPart.into());
513                    }
514                    OneDriveUpload::VerifyPublished
515                    | OneDriveUpload::Published
516                    | OneDriveUpload::Refused => {}
517                }
518            }
519            SessionState::CloudKit { id } if provider == CloudProvider::CloudKit => {
520                check_cloudkit_id(id.as_str())?
521            }
522            SessionState::VerifyPublished if provider == CloudProvider::Dropbox => {
523                if self.confirmed == self.total {
524                    return Err(StorageFailure::InvalidPart.into());
525                }
526            }
527            SessionState::Complete => {}
528            #[cfg(any(test, feature = "test-utils"))]
529            SessionState::MemoryPlanned { .. } => {
530                return if self.confirmed == 0 {
531                    Ok(())
532                } else {
533                    Err(StorageFailure::InvalidPart.into())
534                };
535            }
536            #[cfg(any(test, feature = "test-utils"))]
537            SessionState::Memory { id, .. } => {
538                return if *id != 0 { Ok(()) } else { Err(StorageFailure::InvalidPart.into()) };
539            }
540            #[cfg(any(test, feature = "test-utils"))]
541            SessionState::MemoryParts { id, parts, complete } => {
542                let mut confirmed = 0u64;
543                for (at, index) in parts.iter().enumerate() {
544                    let span = part_span(self.total, self.part_size, *index)
545                        .ok_or(StorageFailure::InvalidPart)?;
546                    if at > 0 && parts[at - 1] >= *index {
547                        return Err(StorageFailure::InvalidPart.into());
548                    }
549                    confirmed += span.end - span.start;
550                }
551                let held = if *complete { self.total } else { confirmed };
552                return if *id != 0 && held == self.confirmed {
553                    Ok(())
554                } else {
555                    Err(StorageFailure::InvalidPart.into())
556                };
557            }
558            _ => return Err(StorageFailure::SessionMismatch.into()),
559        }
560        let valid_size = match provider {
561            CloudProvider::S3 => self.part_size == s3_part_size(self.total)?,
562            CloudProvider::Dropbox => self.part_size == DROPBOX_PART_SIZE,
563            CloudProvider::OneDrive => self.part_size == ONEDRIVE_PART_SIZE,
564            CloudProvider::CloudKit => self.part_size == CLOUDKIT_PART_SIZE,
565        };
566        if !valid_size {
567            return Err(StorageFailure::InvalidPart.into());
568        }
569        Ok(())
570    }
571    pub(crate) fn check(&self, location: &StorageConfig) -> Result<(), StorageError> {
572        self.validate()?;
573        if !self.location.same_location(location) {
574            return Err(StorageFailure::SessionMismatch.into());
575        }
576        Ok(())
577    }
578    /// Where a part of `len` bytes starting `offset` bytes in ends, if it is
579    /// one this session may send now: where parts are independent, a whole
580    /// part the provider hasn't confirmed; elsewhere the next, from the
581    /// confirmed bytes to at most its part's end.
582    pub(crate) fn part_end(&self, offset: u64, len: usize) -> Result<u64, StorageError> {
583        let end = offset.checked_add(len as u64).ok_or(StorageFailure::InvalidPart)?;
584        if self.is_complete() || len == 0 {
585            return Err(StorageFailure::InvalidPart.into());
586        }
587        if let Some(confirmed) = self.confirmed_parts() {
588            let index = offset / self.part_size as u64;
589            let whole = offset.is_multiple_of(self.part_size as u64)
590                && part_span(self.total, self.part_size, index).is_some_and(|span| span.end == end);
591            if !whole || confirmed.binary_search(&index).is_ok() {
592                return Err(StorageFailure::InvalidPart.into());
593            }
594            return Ok(end);
595        }
596        if offset != self.confirmed
597            || len > self.part_size
598            || end > self.total
599            || (end != self.total && end % self.part_size as u64 != 0)
600        {
601            return Err(StorageFailure::InvalidPart.into());
602        }
603        Ok(end)
604    }
605    /// The bytes `len` bytes sent as the session's next part cover, if they
606    /// fit it: the first part still to send.
607    pub(crate) fn next_part(&self, len: usize) -> Result<Range<u64>, StorageError> {
608        let offset = self
609            .parts_to_send()
610            .first()
611            .map(|part| part.start)
612            .ok_or(StorageFailure::InvalidPart)?;
613        Ok(offset..self.part_end(offset, len)?)
614    }
615}
616
617pub(crate) fn s3_part_size(total: u64) -> Result<usize, StorageError> {
618    let parts = S3_MAX_PARTS as u64;
619    if total == 0 || total > parts * S3_REQUEST_LIMIT {
620        return Err(StorageFailure::InvalidPart.into());
621    }
622    usize::try_from(total.div_ceil(parts).div_ceil(S3_PART_UNIT) * S3_PART_UNIT)
623        .map_err(|error| StorageFailure::InvalidPart.with_source(error))
624}
625pub(crate) fn check_cloudkit_id(value: &str) -> Result<(), StorageError> {
626    coven_foundation::id_source::canonical_uuid(value)
627        .map_err(|error| StorageFailure::SessionMismatch.with_source(error))?;
628    Ok(())
629}
630fn nonempty(id: &SecretText) -> Result<(), StorageError> {
631    if id.as_str().is_empty() {
632        return Err(StorageFailure::SessionMismatch.into());
633    }
634    Ok(())
635}
636fn transfer_url(value: &SecretText) -> Result<(), StorageError> {
637    let url = url::Url::parse(value.as_str())
638        .map_err(|error| StorageFailure::SessionMismatch.with_source(error))?;
639    if !crate::web_url::is_web_url(&url) || url.fragment().is_some() {
640        return Err(StorageFailure::SessionMismatch.into());
641    }
642    Ok(())
643}
644
645#[cfg(test)]
646#[path = "session_tests.rs"]
647mod tests;