From 548ff67a10b8310d3ef2841ea5c508df8ab4df0f Mon Sep 17 00:00:00 2001 From: clover caruso Date: Mon, 19 Oct 2026 12:44:16 -0700 Subject: [PATCH] fix: a section written anew names its pages' metadata for their new identities A section given, changed or relieved of a password, or moved or copied into another notebook, is written anew under new object space identities. OneNote 2010 names a page's metadata in the section's page list after the page's object space; the rewrite renamed the spaces but kept each metadata object's old name, so OneNote, finding no metadata under the name it expected, appended a revision holding a second copy of it on first open. The metadata now takes the name of its page's new space, and a cold open leaves a moved or copied file as it was. OneNote still adds properties to pages Snowbound made in a protected section when it first shows them, which is separate. - OneNote no longer adds a second copy of each page's details to a section Snowbound moved, copied or changed the password of Refs #105 Assisted-by: claude-opus-5.5 --- crates/onestore/src/protected/mod.rs | 8 ++ crates/onestore/tests/rewrite.rs | 132 +++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 crates/onestore/tests/rewrite.rs diff --git a/crates/onestore/src/protected/mod.rs b/crates/onestore/src/protected/mod.rs index b12c129c34b1204b5646360fe25f50e0752e6ec2..ecbda3e86c8a374d3ffd057235a10c62978a58d6 100644 --- a/crates/onestore/src/protected/mod.rs +++ b/crates/onestore/src/protected/mod.rs @@ -357,6 +357,14 @@ pub fn rekey(image: &[u8], key: Option<&Key>, new: Option<&Key>) -> Result = (fresh.iter()) + .map(|(old, new)| { + let named = |guid: &[u8; 16]| crate::pages::metadata_id(ExGuid { guid: *guid, n: 1 }); + (named(old).guid, named(new).guid) + }) + .collect(); + fresh.extend(derived); // OneNote gives the payloads new identities too. let mut files = BTreeMap::new(); for guid in crate::write::declared_payloads(&store) { diff --git a/crates/onestore/tests/rewrite.rs b/crates/onestore/tests/rewrite.rs new file mode 100644 index 0000000000000000000000000000000000000000..0136a86bd7244c1c6da9ccb564ec1b289a4cbd0e --- /dev/null +++ b/crates/onestore/tests/rewrite.rs @@ -0,0 +1,132 @@ +//! A section written anew under new identities (`protected::rekey`), as a password, a move or +//! a copy into another notebook writes one: every page, version and conflict page comes along, +//! each under the identities OneNote 2010 derives from its own. + +use onestore::{ + Arena, ExGuid, RevisionIndex, Section, Store, + document::{Document, Kind}, + page::Page, + protected::rekey, +}; + +fn read(path: &str) -> Vec { + std::fs::read(format!( + "{}/../../corpus/{path}", + env!("CARGO_MANIFEST_DIR") + )) + .unwrap() +} + +/// A page as a reader sees it: its title, its versions' authors and its conflict pages' +/// titles and users. +type Seen = (String, Vec>, Vec<(String, String)>); + +/// What a reader sees of a section's pages, in order. +fn seen(image: &[u8]) -> Vec { + let arena = Arena::default(); + let mut section = Section::open(&arena, image.to_vec()).unwrap(); + let versions = section.versions().unwrap(); + let conflicts = section.conflicts().unwrap(); + let mut seen = Vec::new(); + for (space, title, _) in section.pages().unwrap() { + let page: Page = section.page(space).unwrap(); + assert_eq!(page.title, title); + let versions: Vec> = (versions.iter()) + .filter(|(listed, _)| *listed == space) + .flat_map(|(_, versions)| versions) + .map(|version| { + section.version(space, version.context).unwrap(); + version.author.clone() + }) + .collect(); + let conflicts = (conflicts.iter()) + .filter(|(listed, _)| *listed == space) + .flat_map(|(_, conflicts)| conflicts) + .map(|conflict| { + section.page(conflict.space).unwrap(); + (conflict.title.clone(), conflict.user.clone()) + }) + .collect(); + seen.push((title, versions, conflicts)); + } + seen +} + +/// The identity OneNote 2010 gives the metadata of the page in object space `space`. +fn metadata(space: ExGuid) -> ExGuid { + let salt = [ + 0x31, 0xc0, 0xa8, 0x22, 0, 0x36, 0xee, 0x42, 0xb7, 0x14, 0xd7, 0xac, 0xda, 0x24, 0x35, 0xe8, + ]; + let mut guid = space.guid; + guid.iter_mut() + .zip(salt) + .for_each(|(byte, salt)| *byte ^= salt); + ExGuid { guid, n: 1 } +} + +/// The page metadata the section's page list holds, each with the page space it names. +fn listed(image: &[u8]) -> Vec<(ExGuid, ExGuid)> { + let store = Store::parse(image).unwrap(); + let index = RevisionIndex::parse(&store).unwrap(); + let document = Document::parse(&index).unwrap(); + let revision = document.active(document.root).unwrap(); + let spaces: Vec = index.spaces.keys().copied().collect(); + (revision.nodes.iter()) + .filter(|(_, node)| matches!(node.kind, Kind::Metadata { .. })) + .map(|(id, _)| { + let space = spaces.iter().find(|space| metadata(**space) == *id); + (*id, *space.unwrap_or(&ExGuid::default())) + }) + .collect() +} + +fn rewritten_whole(path: &str) { + rewritten(path, read(path)); +} + +fn rewritten(path: &str, image: Vec) { + let rewritten = rekey(&image, None, None).unwrap(); + let before = seen(&image); + assert!(!before.is_empty()); + assert_eq!(seen(&rewritten), before, "{path}"); + let store = |image: &[u8]| Store::parse(image).unwrap().header.file_id; + assert_ne!(store(&rewritten), store(&image)); + for image in [&image, &rewritten] { + let listed = listed(image); + assert!(!listed.is_empty(), "{path}"); + for (id, space) in listed { + assert_ne!( + space, + ExGuid::default(), + "{path}: metadata {id} names no page" + ); + } + } +} + +#[test] +fn versions_come_along() { + rewritten_whole("page-versions/native/step-02/notebook/History.one"); +} + +#[test] +fn conflict_pages_come_along() { + rewritten_whole("conflict-page/native-three/notebook/synthetic.one"); +} + +#[test] +fn a_section_snowbound_made_comes_along() { + let mut image = onestore::create_empty_section("Made.one", None).unwrap(); + let page = onestore::PageCreation::new(None, Some("Made"), "Author").unwrap(); + let edit = onestore::op::Edit { + at: 134_040_000_000_000_000, + ops: vec![onestore::op::Op::Section(onestore::op::SectionOp::Create( + page, + ))], + }; + let arena = Arena::default(); + let mut section = Section::open(&arena, image.clone()).unwrap(); + section.apply("Author", &edit).unwrap(); + section.seal().unwrap().unwrap().apply(&mut image).unwrap(); + rewritten("Made.one", image); +} -- 2.54.1