sefy-core 0.20.0

Core library for sefy: an inconspicuous encrypted secret store in a single file
Documentation
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//! Earlier contents of an item, and what changed between two of them.
//!
//! A version is one state of an item's **contents** — a note's text, a
//! record's fields, a file's bytes. Titles and tags are labels rather than
//! values: renaming an item does not make a version, and bringing back an old
//! password does not bring back the old name with it. "Newest wins" is fine
//! for a label and ruinous for a secret, which is the whole reason this module
//! exists.
//!
//! Every version carries its own identity. Two copies of a vault that parted
//! at version 2 can each go on to write a version numbered 3 with different
//! contents, so neither the item's identity nor the number can tell versions
//! apart; the version's own uuid can, and that is what a merge matches on so
//! that history arriving twice is still kept once. The number, `seq`, orders a
//! line of edits the way they were made, whatever the clocks of the machines
//! that made them said.

use crate::error::{Error, Result};
use crate::exchange::ExportField;
use crate::model::{Field, ItemKind, Payload};
use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64;
use serde::{Deserialize, Serialize};
use zeroize::Zeroizing;

/// One state an item's contents have been in.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Version {
    /// Identity of this state, the same in every vault that holds it.
    pub uuid: String,
    /// Place in the item's line of edits: one more than the version it
    /// replaced.
    pub seq: i64,
    /// When these contents were written, seconds since the Unix epoch.
    pub made_at: i64,
    /// Name of the machine they were written on, when it was known.
    pub device: Option<String>,
    /// Whether this version lost a conflict in a merge and was kept rather
    /// than dropped.
    pub conflict: bool,
    /// Whether these are the item's current contents.
    pub current: bool,
    /// The contents themselves.
    pub payload: Payload,
}

/// What became of one part of an item between two versions.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Change {
    /// The field's name; `text` for a note, `filename` and `contents` for a
    /// file.
    pub name: String,
    /// Whether the value is a secret on either side, and so never shown.
    pub secret: bool,
    /// What happened to it.
    pub kind: ChangeKind,
}

/// How a part of an item differs between two versions.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChangeKind {
    /// Present in both, with the same value and secrecy.
    Same,
    /// Present in both, with a different value or secrecy.
    Changed,
    /// Present only in the older of the two.
    Removed,
    /// Present only in the newer of the two.
    Added,
}

/// Compares two states of an item's contents, part by part.
///
/// Parts come in the newer version's order, followed by whatever only the
/// older one had. Secrecy is reported rather than acted on: this says *that* a
/// password changed, and leaves it to the caller never to print what it was.
pub fn compare(old: &Payload, new: &Payload) -> Vec<Change> {
    let before = parts(old);
    let after = parts(new);

    let mut changes: Vec<Change> = after
        .iter()
        .map(|part| {
            let kind = match before.iter().find(|earlier| earlier.name == part.name) {
                Some(earlier) if earlier.value == part.value && earlier.secret == part.secret => {
                    ChangeKind::Same
                }
                Some(_) => ChangeKind::Changed,
                None => ChangeKind::Added,
            };
            let was_secret = before
                .iter()
                .any(|earlier| earlier.name == part.name && earlier.secret);
            Change {
                name: part.name.to_owned(),
                secret: part.secret || was_secret,
                kind,
            }
        })
        .collect();

    changes.extend(
        before
            .iter()
            .filter(|earlier| !after.iter().any(|part| part.name == earlier.name))
            .map(|earlier| Change {
                name: earlier.name.to_owned(),
                secret: earlier.secret,
                kind: ChangeKind::Removed,
            }),
    );
    changes
}

/// Names of the parts that differ between two versions, in [`compare`] order.
pub fn changed(old: &Payload, new: &Payload) -> Vec<String> {
    compare(old, new)
        .into_iter()
        .filter(|change| change.kind != ChangeKind::Same)
        .map(|change| change.name)
        .collect()
}

/// The text of one part, for the parts that are text.
///
/// `text` of a note, a field by name, the `filename` of a file. A file's
/// `contents` are bytes and have no text to give.
pub fn text_of<'a>(payload: &'a Payload, name: &str) -> Option<&'a str> {
    match payload {
        Payload::Note { text } if name == NOTE_TEXT => Some(text),
        Payload::Fields { .. } => payload.field(name).map(|field| field.value.as_str()),
        Payload::File { filename, .. } if name == FILE_NAME => Some(filename),
        _ => None,
    }
}

/// What a note's body is called when it is compared.
const NOTE_TEXT: &str = "text";
/// What a file's name is called when it is compared.
const FILE_NAME: &str = "filename";
/// What a file's bytes are called when they are compared.
const FILE_CONTENTS: &str = "contents";

/// One comparable part of an item's contents.
struct Part<'a> {
    name: &'a str,
    value: &'a [u8],
    secret: bool,
}

fn parts(payload: &Payload) -> Vec<Part<'_>> {
    match payload {
        Payload::Note { text } => vec![Part {
            name: NOTE_TEXT,
            value: text.as_bytes(),
            secret: false,
        }],
        Payload::Fields { fields, .. } => fields
            .iter()
            .map(|field| Part {
                name: &field.name,
                value: field.value.as_bytes(),
                secret: field.secret,
            })
            .collect(),
        // Bytes are marked secret so that nobody is tempted to show them as
        // lines: a file is not text, whatever it happens to contain.
        Payload::File { filename, bytes } => vec![
            Part {
                name: FILE_NAME,
                value: filename.as_bytes(),
                secret: false,
            },
            Part {
                name: FILE_CONTENTS,
                value: bytes,
                secret: true,
            },
        ],
        // A kind this build cannot read has no parts it could name.
        Payload::Unknown { .. } => Vec::new(),
    }
}

/// One line of a comparison between two texts.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Line<'a> {
    /// In both, unchanged.
    Same(&'a str),
    /// Only in the older text.
    Removed(&'a str),
    /// Only in the newer text.
    Added(&'a str),
}

/// Beyond this many cells, the comparison stops looking for the shortest
/// answer and says "all of this went, all of that came".
///
/// The table below costs four bytes a cell. Notes worth keeping in a secret
/// store stay far under this; one that does not still gets a correct answer,
/// only a longer one.
const MOST_CELLS: usize = 1_000_000;

/// Compares two texts line by line.
///
/// The longest common subsequence of lines, found over what is left once the
/// shared beginning and end are set aside — which for an edit of a few lines
/// in a long note is almost nothing. Written here rather than taken from a
/// crate: it is thirty lines, and every dependency is something a secret
/// store's users have to trust.
pub fn lines<'a>(old: &'a str, new: &'a str) -> Vec<Line<'a>> {
    let before: Vec<&str> = old.lines().collect();
    let after: Vec<&str> = new.lines().collect();

    let head = before
        .iter()
        .zip(&after)
        .take_while(|(a, b)| a == b)
        .count();
    let tail = before[head..]
        .iter()
        .rev()
        .zip(after[head..].iter().rev())
        .take_while(|(a, b)| a == b)
        .count();
    let a = &before[head..before.len() - tail];
    let b = &after[head..after.len() - tail];

    let mut out: Vec<Line<'a>> = before[..head].iter().map(|line| Line::Same(line)).collect();

    if a.len().saturating_mul(b.len()) > MOST_CELLS {
        out.extend(a.iter().map(|line| Line::Removed(line)));
        out.extend(b.iter().map(|line| Line::Added(line)));
    } else {
        // common[i][j]: how many lines a[i..] and b[j..] share in order.
        let width = b.len() + 1;
        let mut common = vec![0u32; (a.len() + 1) * width];
        for i in (0..a.len()).rev() {
            for j in (0..b.len()).rev() {
                common[i * width + j] = if a[i] == b[j] {
                    common[(i + 1) * width + j + 1] + 1
                } else {
                    common[(i + 1) * width + j].max(common[i * width + j + 1])
                };
            }
        }

        let (mut i, mut j) = (0, 0);
        while i < a.len() && j < b.len() {
            if a[i] == b[j] {
                out.push(Line::Same(a[i]));
                i += 1;
                j += 1;
            } else if common[(i + 1) * width + j] >= common[i * width + j + 1] {
                out.push(Line::Removed(a[i]));
                i += 1;
            } else {
                out.push(Line::Added(b[j]));
                j += 1;
            }
        }
        out.extend(a[i..].iter().map(|line| Line::Removed(line)));
        out.extend(b[j..].iter().map(|line| Line::Added(line)));
    }

    out.extend(
        before[before.len() - tail..]
            .iter()
            .map(|line| Line::Same(line)),
    );
    out
}

/// A version's contents as they are kept in the `versions` table.
///
/// JSON rather than a row per field: an earlier version is only ever read
/// whole, and a frozen copy has no business being queried — or searched,
/// which is where an old password would otherwise turn up.
#[derive(Serialize, Deserialize)]
struct Stored {
    kind: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    text: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    fields: Vec<ExportField>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    filename: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    bytes_base64: Option<String>,
}

/// Renders contents for the `versions` table.
pub(crate) fn encode(payload: &Payload) -> Result<Zeroizing<String>> {
    let mut stored = Stored {
        kind: payload.kind().as_str().to_owned(),
        text: None,
        fields: Vec::new(),
        filename: None,
        bytes_base64: None,
    };
    match payload {
        Payload::Note { text } => stored.text = Some(text.clone()),
        Payload::Fields { fields, .. } => {
            stored.fields = fields
                .iter()
                .map(|field| ExportField {
                    name: field.name.clone(),
                    value: field.value.clone(),
                    secret: field.secret,
                })
                .collect();
        }
        Payload::File { filename, bytes } => {
            stored.filename = Some(filename.clone());
            stored.bytes_base64 = Some(BASE64.encode(bytes));
        }
        // Nothing to keep: this build holds the name of the kind and none of
        // the contents, and a version that claimed otherwise would be empty.
        Payload::Unknown { kind } => {
            return Err(Error::UnknownItemKind {
                id: 0,
                kind: kind.clone(),
            });
        }
    }
    let json = serde_json::to_string(&stored).map_err(|_| Error::UnreadableVersion)?;
    Ok(Zeroizing::new(json))
}

/// Reads contents back out of the `versions` table.
///
/// A kind this build does not know comes back as [`Payload::Unknown`], the
/// same as a current item of that kind: a newer sefy wrote it, and the version
/// is kept rather than refused.
pub(crate) fn decode(json: &str) -> Result<Payload> {
    let stored: Stored = serde_json::from_str(json).map_err(|_| Error::UnreadableVersion)?;
    let kind = ItemKind::parse(&stored.kind);
    Ok(match kind {
        ItemKind::Note => Payload::Note {
            text: stored.text.ok_or(Error::UnreadableVersion)?,
        },
        ItemKind::File => Payload::File {
            filename: stored.filename.ok_or(Error::UnreadableVersion)?,
            bytes: BASE64
                .decode(stored.bytes_base64.ok_or(Error::UnreadableVersion)?)
                .map_err(|_| Error::UnreadableVersion)?,
        },
        ItemKind::Unknown(name) => Payload::Unknown { kind: name },
        kind => {
            let mut fields: Vec<Field> = stored
                .fields
                .into_iter()
                .map(|field| Field {
                    name: field.name,
                    value: field.value,
                    secret: field.secret,
                })
                .collect();
            crate::expiry::upgrade_legacy(&kind, &mut fields);
            Payload::Fields { kind, fields }
        }
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    fn login(password: &str, url: &str) -> Payload {
        Payload::fields(
            ItemKind::Login,
            [
                Field::public("login", "ada"),
                Field::secret("password", password),
                Field::public("url", url),
            ],
        )
    }

    #[test]
    fn every_kind_of_contents_survives_being_kept() {
        let payloads = [
            Payload::Note {
                text: "line one\nline two".to_owned(),
            },
            login("hunter2", "https://example.com"),
            Payload::File {
                filename: "id_ed25519".to_owned(),
                bytes: vec![0, 1, 2, 255],
            },
        ];
        for payload in payloads {
            let json = encode(&payload).unwrap();
            assert_eq!(decode(&json).unwrap(), payload);
        }
    }

    #[test]
    fn a_card_kept_with_its_date_as_printed_reads_with_it_under_the_new_name() {
        // A version written up to 0.14.0 is frozen JSON: it is never rewritten,
        // so a comparison with the current contents would otherwise see the
        // date removed and another added.
        let payload = decode(
            r#"{"kind":"card","fields":[
                {"name":"number","value":"4111","secret":true},
                {"name":"expiry","value":"01/29","secret":false}
            ]}"#,
        )
        .unwrap();
        assert_eq!(payload.field("expires").unwrap().value, "2029-01");
        assert!(payload.field("expiry").is_none());
    }

    #[test]
    fn a_kind_from_a_newer_sefy_is_kept_as_unknown_rather_than_refused() {
        let payload = decode(r#"{"kind":"passport","number":"X"}"#).unwrap();
        assert_eq!(
            payload,
            Payload::Unknown {
                kind: "passport".to_owned()
            }
        );
    }

    #[test]
    fn contents_that_cannot_be_read_are_not_mistaken_for_empty_ones() {
        assert!(matches!(
            decode(r#"{"kind":"note"}"#),
            Err(Error::UnreadableVersion)
        ));
        assert!(matches!(decode("not json"), Err(Error::UnreadableVersion)));
    }

    #[test]
    fn a_comparison_names_what_changed_and_what_did_not() {
        let changes = compare(
            &login("old", "https://example.com"),
            &login("new", "https://example.com"),
        );
        let summary: Vec<(&str, ChangeKind, bool)> = changes
            .iter()
            .map(|change| (change.name.as_str(), change.kind, change.secret))
            .collect();
        assert_eq!(
            summary,
            [
                ("login", ChangeKind::Same, false),
                ("password", ChangeKind::Changed, true),
                ("url", ChangeKind::Same, false),
            ]
        );
    }

    #[test]
    fn fields_that_came_and_went_are_named_as_such() {
        let old = Payload::fields(
            ItemKind::Login,
            [Field::public("login", "ada"), Field::public("notes", "n")],
        );
        let new = Payload::fields(
            ItemKind::Login,
            [Field::public("login", "ada"), Field::secret("pin", "1234")],
        );
        let changes = compare(&old, &new);
        assert_eq!(changes[1].name, "pin");
        assert_eq!(changes[1].kind, ChangeKind::Added);
        assert!(changes[1].secret);
        assert_eq!(changes[2].name, "notes");
        assert_eq!(changes[2].kind, ChangeKind::Removed);
        assert_eq!(changed(&old, &new), ["pin", "notes"]);
    }

    #[test]
    fn a_field_that_was_secret_stays_secret_in_a_comparison() {
        // Made public later, the old value is still a secret: showing it as a
        // line diff would print the very thing the record used to hide.
        let old = Payload::fields(ItemKind::Login, [Field::secret("password", "s3cret")]);
        let new = Payload::fields(ItemKind::Login, [Field::public("password", "s3cret")]);
        let change = &compare(&old, &new)[0];
        assert_eq!(
            change.kind,
            ChangeKind::Changed,
            "secrecy is part of a field"
        );
        assert!(change.secret);
    }

    #[test]
    fn a_file_compares_its_name_and_its_bytes_but_offers_no_bytes_as_text() {
        let old = Payload::File {
            filename: "a.txt".to_owned(),
            bytes: b"one".to_vec(),
        };
        let new = Payload::File {
            filename: "a.txt".to_owned(),
            bytes: b"two".to_vec(),
        };
        let changes = compare(&old, &new);
        assert_eq!(changes[0].kind, ChangeKind::Same);
        assert_eq!(changes[1].name, "contents");
        assert_eq!(changes[1].kind, ChangeKind::Changed);
        assert!(changes[1].secret);
        assert_eq!(text_of(&new, "contents"), None);
        assert_eq!(text_of(&new, "filename"), Some("a.txt"));
    }

    #[test]
    fn a_line_diff_keeps_what_is_shared_and_marks_the_rest() {
        let diff = lines("a\nb\nc\nd", "a\nB\nc\nd\ne");
        assert_eq!(
            diff,
            [
                Line::Same("a"),
                Line::Removed("b"),
                Line::Added("B"),
                Line::Same("c"),
                Line::Same("d"),
                Line::Added("e"),
            ]
        );
    }

    #[test]
    fn a_line_diff_finds_the_longest_shared_run_not_the_first_one() {
        // A greedy walk would pair the first "x" and lose the three lines
        // after it; the table sees that keeping them is the shorter answer.
        let same = |diff: &[Line<'_>]| {
            diff.iter()
                .filter(|line| matches!(line, Line::Same(_)))
                .count()
        };
        let diff = lines("x\na\nb\nc", "a\nb\nc\nx");
        assert_eq!(same(&diff), 3, "{diff:?}");

        // Here the shared run is only reached by stepping over lines of the
        // newer text first; a table that only ever looks one way along the
        // older one never finds it, and reports every line as changed.
        let diff = lines("p\na\nb\nq", "r\ns\na\nb\nt");
        assert_eq!(same(&diff), 2, "{diff:?}");
    }

    #[test]
    fn a_line_diff_of_identical_or_empty_texts_is_all_or_nothing() {
        assert!(
            lines("a\nb", "a\nb")
                .iter()
                .all(|line| matches!(line, Line::Same(_)))
        );
        assert_eq!(lines("", "new"), [Line::Added("new")]);
        assert_eq!(lines("old", ""), [Line::Removed("old")]);
    }

    #[test]
    fn a_line_diff_too_large_to_table_is_still_correct() {
        let old: String = (0..1500).map(|n| format!("old {n}\n")).collect();
        let new: String = (0..1500).map(|n| format!("new {n}\n")).collect();
        let diff = lines(&old, &new);
        let removed = diff
            .iter()
            .filter(|line| matches!(line, Line::Removed(_)))
            .count();
        let added = diff
            .iter()
            .filter(|line| matches!(line, Line::Added(_)))
            .count();
        assert_eq!((removed, added), (1500, 1500));
    }
}