diff --git a/.github/workflows/actions.lock b/.github/workflows/actions.lock new file mode 100644 index 000000000..6917bde73 --- /dev/null +++ b/.github/workflows/actions.lock @@ -0,0 +1,29 @@ +# This file is machine-generated by `gh actions-lock`. +# Do not edit by hand; run `gh actions-lock` to update. +# Docs: https://gh.io/actions-lockfile +version: 'v0.0.3' +workflows: + '.github/workflows/ci.yml': [] + '.github/workflows/freebsd.yml': [] + '.github/workflows/house-rules.yml': [] + '.github/workflows/lockfile.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/nix.yml': [] + '.github/workflows/packaging-lint.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/release.yml': [] + '.github/workflows/sonarcloud.yml': [] + '.github/workflows/windows-arm64.yml': [] +dependencies: + 'actions/checkout@v7.0.1': + ref: 'v7.0.1' + commit: 'sha1-3d3c42e5aac5ba805825da76410c181273ba90b1' + owner_id: 44036562 + repo_id: 197814629 + 'step-security/harden-runner@v2.22.4': + ref: 'v2.22.4' + commit: 'sha1-1d88384fd173af6ecb012dfd0264a306b711241a' + owner_id: 88700172 + repo_id: 422287306 diff --git a/.github/workflows/lockfile.yml b/.github/workflows/lockfile.yml new file mode 100644 index 000000000..e630c9a8f --- /dev/null +++ b/.github/workflows/lockfile.yml @@ -0,0 +1,34 @@ +name: Lockfile + +# Fails when .github/workflows/actions.lock is out of sync with the workflows, or when a locked pin +# no longer verifies upstream, so a moved tag cannot slip a different commit into a run. +# Docs: https://gh.io/actions-lockfile +on: + pull_request: + push: + branches: [main] + workflow_dispatch: + +permissions: + contents: read + +env: + GH_TOKEN: ${{ github.token }} + +jobs: + lockfile: + name: Lockfile is in sync + runs-on: ubuntu-24.04 + timeout-minutes: 10 + steps: + - name: Harden runner + uses: step-security/harden-runner@v2.22.4 + with: + egress-policy: audit + - uses: actions/checkout@v7.0.1 + with: + persist-credentials: false + - name: Install gh-actions-lock + run: gh extension install github/gh-actions-lock --pin v0.4.1 + - name: Verify the lockfile + run: gh actions-lock --verify diff --git a/.github/workflows/packaging-lint.yml b/.github/workflows/packaging-lint.yml index 0a91cc050..f442059bb 100644 --- a/.github/workflows/packaging-lint.yml +++ b/.github/workflows/packaging-lint.yml @@ -30,10 +30,10 @@ jobs: runs-on: windows-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: Install the pinned packaging toolchain diff --git a/AGENTS.md b/AGENTS.md index e63e92c7c..cc4ea91e5 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -59,7 +59,7 @@ inspection. It runs natively on macOS, Windows, Linux and FreeBSD, and in the br Siblings: `../vectorcraft` (Illustrator-class), `../photocraft` (Photoshop-class), `../designcraft` (InDesign-class) and the other Crafting Apps; same conventions. -Repos don't share code. +Share reusable code through craft-libs; do not depend on sibling application crates. ## Start every session here 1. Read [`ROADMAP.md`](ROADMAP.md) (stage, headline numbers, estimates) and @@ -182,3 +182,7 @@ commit. Grade by behaviour against the incumbent, not by whether a menu item exi - Workflows in `.github/workflows/` call the shared workflows in [`storytold/craft-common-actions`](https://github.com/storytold/craft-common-actions) at `@main`, with their inputs and secrets. No other jobs: if no shared workflow does what's needed, it doesn't go here. - Don't add or change CI in a feature or fix PR. If the existing workflows can't build or test a change, link an existing craft-common-actions issue or PR, or open a new issue, or a PR only for a small fix that is safe for every app and does not increase CI time. - Some workflow files here still run their own jobs. Don't add to them. Moving a job into craft-common-actions is its own task, never part of another PR, and the job stays here until a shared workflow runs it. + +## Shared Craft libraries + +Before adding or changing reusable file-format, codec, protocol or UI code, check [craft-libs](https://github.com/storytold/craft-libs) and existing contributor work; reuse its public APIs or extend the appropriate shared library instead of adding an app-local duplicate, keeping app-specific policy and model mapping local and preserving compatibility, security limits and regression coverage. diff --git a/Cargo.lock b/Cargo.lock index 8adab0b43..77abcfa01 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1016,7 +1016,7 @@ name = "craft-fig" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=d13ac58a15c45d618f25910772502e38c4ce1d4c#d13ac58a15c45d618f25910772502e38c4ce1d4c" dependencies = [ - "craft-figma-rest", + "craft-figma-rest 0.1.0 (git+https://github.com/storytold/craft-libs?rev=d13ac58a15c45d618f25910772502e38c4ce1d4c)", "craft-kiwi", "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=d13ac58a15c45d618f25910772502e38c4ce1d4c)", "flate2", @@ -1048,6 +1048,16 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-figma-rest" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=ff142b3ad58aa7306676749428f4f2f9b277b5dd#ff142b3ad58aa7306676749428f4f2f9b277b5dd" +dependencies = [ + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-font-catalog" version = "0.1.0" @@ -1164,6 +1174,18 @@ dependencies = [ "toml_edit", ] +[[package]] +name = "craft-sketch" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=ff142b3ad58aa7306676749428f4f2f9b277b5dd#ff142b3ad58aa7306676749428f4f2f9b277b5dd" +dependencies = [ + "craft-figma-rest 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ff142b3ad58aa7306676749428f4f2f9b277b5dd)", + "craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ff142b3ad58aa7306676749428f4f2f9b277b5dd)", + "kurbo", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-ui" version = "0.1.0" @@ -1286,6 +1308,16 @@ dependencies = [ "zip 9.1.1", ] +[[package]] +name = "craft-zip" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=ff142b3ad58aa7306676749428f4f2f9b277b5dd#ff142b3ad58aa7306676749428f4f2f9b277b5dd" +dependencies = [ + "flate2", + "thiserror 2.0.21", + "zip 9.1.1", +] + [[package]] name = "crc32fast" version = "1.5.3" @@ -2037,8 +2069,9 @@ version = "0.18.1" dependencies = [ "craft-fig", "craft-figcraft-format", - "craft-figma-rest", + "craft-figma-rest 0.1.0 (git+https://github.com/storytold/craft-libs?rev=d13ac58a15c45d618f25910772502e38c4ce1d4c)", "craft-penpot", + "craft-sketch", "figcraft-doc", "figcraft-layout", "figcraft-pathops", diff --git a/Cargo.toml b/Cargo.toml index ac3aedee2..dd33c406c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,6 +64,7 @@ craft-penpot = { git = "https://github.com/storytold/craft-libs", rev = "8418f9a # The design-file formats (craft-libs): the .fig container and its kiwi encoding, the mapping of # a .fig message onto the REST file JSON, and that JSON's shared helpers. These crates share # types, so they stay on one rev; they share none with the crates above. +craft-sketch = { git = "https://github.com/storytold/craft-libs", rev = "ff142b3ad58aa7306676749428f4f2f9b277b5dd" } craft-fig = { git = "https://github.com/storytold/craft-libs", rev = "d13ac58a15c45d618f25910772502e38c4ce1d4c" } craft-figcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "976eb51c59edb0a37f24105850f52d921c8518ee" } craft-figma-rest = { git = "https://github.com/storytold/craft-libs", rev = "d13ac58a15c45d618f25910772502e38c4ce1d4c" } diff --git a/crates/format/Cargo.toml b/crates/format/Cargo.toml index 46869824c..d988a7269 100644 --- a/crates/format/Cargo.toml +++ b/crates/format/Cargo.toml @@ -21,6 +21,7 @@ sha1 = { workspace = true } sha2 = { workspace = true } image = { workspace = true } craft-penpot = { workspace = true } +craft-sketch = { workspace = true } craft-fig = { workspace = true } craft-figcraft-format = { workspace = true } craft-figma-rest = { workspace = true } diff --git a/crates/format/src/sketch.rs b/crates/format/src/sketch.rs index 182447015..2389813bb 100644 --- a/crates/format/src/sketch.rs +++ b/crates/format/src/sketch.rs @@ -43,48 +43,15 @@ mod tree_tests; use std::collections::BTreeSet; use figcraft_doc::Document; -use serde_json::{Value, json}; +use serde_json::Value; + +// Container parsing is shared; FigCraft document projection remains in this module. +pub(crate) use craft_sketch::Archive; use crate::FormatError; -/// The oldest Sketch whose files open: from 48 on, text is plain JSON too. -pub const MIN_SKETCH: u32 = 48; -/// The newest `meta.json` format version checked against real files (Sketch 83); newer files -/// open with a warning. -pub const NEWEST_TESTED_VERSION: u64 = 139; -/// What opening a file saved by an older Sketch says. -pub const TOO_OLD: &str = "saved by a Sketch older than 48; open it in a newer Sketch and save it again"; -/// Largest JSON member read (a real page of a few thousand layers takes 33 MB). -const MAX_JSON_MEMBER: u64 = 512 * 1024 * 1024; -/// Most bytes a member may unpack to, per compressed byte (real JSON packs 5–20 times and images -/// barely; a zero-filled member a thousand times), with a floor for small members. -const MAX_RATIO: u64 = 100; -const MIN_ALLOWANCE: u64 = 16 * 1024 * 1024; -/// Most JSON read from one file in all. -#[cfg(not(target_pointer_width = "32"))] -const MAX_JSON_TOTAL: u64 = 2 * 1024 * 1024 * 1024; -#[cfg(target_pointer_width = "32")] -const MAX_JSON_TOTAL: u64 = 512 * 1024 * 1024; -/// Largest image member read. -const MAX_IMAGE: u64 = figcraft_doc::ImageData::MAX_BYTES; -/// Most image bytes read from one file in all. -#[cfg(not(target_pointer_width = "32"))] -const MAX_IMAGE_TOTAL: u64 = 1024 * 1024 * 1024; -#[cfg(target_pointer_width = "32")] -const MAX_IMAGE_TOTAL: u64 = 256 * 1024 * 1024; -/// Most pages read from one file. -const MAX_PAGES: usize = 1000; - -/// Whether `bytes` look like a `.sketch` file: a zip holding `document.json` and `meta.json`, or -/// the SQLite database Sketch wrote before version 43 (so that it gets the "too old" message). -pub fn is_sketch(bytes: &[u8]) -> bool { - bytes.starts_with(b"SQLite format 3\0") - || (bytes.starts_with(b"PK") - && zip::ZipArchive::new(std::io::Cursor::new(bytes)).is_ok_and(|z| { - let names: BTreeSet<&str> = z.file_names().collect(); - names.contains("document.json") && names.contains("meta.json") - })) -} +pub use craft_sketch::{MIN_SKETCH, NEWEST_TESTED_VERSION, TOO_OLD, is_sketch}; +const MAX_JSON_MEMBER: u64 = craft_sketch::MAX_JSON_MEMBER; /// Finds the installed face a PostScript name belongs to: its family and style. pub type FontLookup = Box Option<(String, String)>>; @@ -133,7 +100,12 @@ fn import_parts( if bytes.starts_with(b"SQLite format 3\0") { return Err(FormatError::Io(TOO_OLD.into())); } - let archive = Archive::open(bytes, max_member)?; + let archive = + Archive::open_with(bytes, craft_sketch::Limits { max_page: max_member, max_image: figcraft_doc::ImageData::MAX_BYTES, ..Default::default() }) + .map_err(|error| match error { + craft_sketch::Error::TooOld => FormatError::Io(TOO_OLD.into()), + craft_sketch::Error::Corrupt(message) => FormatError::Corrupt(format!("Sketch file: {message}")), + })?; let map = tree::to_rest(&archive, name, max_nodes, fonts); if map.too_old { return Err(FormatError::Io(TOO_OLD.into())); @@ -172,135 +144,3 @@ fn embedded_fonts(document: &Value) -> Option { let list: Vec<&str> = families.iter().map(String::as_str).collect(); Some(format!("the file has embedded fonts FigCraft doesn't use ({}); install them to see the text as designed", list.join(", "))) } - -fn corrupt(msg: impl Into) -> FormatError { - FormatError::Corrupt(format!("Sketch file: {}", msg.into())) -} - -/// The parts of a `.sketch` archive: the document, the pages in document order and the images. -pub(crate) struct Archive<'b> { - pub document: Value, - /// `meta.json`'s format version (0 when missing). - pub version: u64, - /// `(id, page JSON)` in the document's order. - pub pages: Vec<(String, Value)>, - /// Problems met while reading (pages skipped). - pub warnings: Vec, - zip: zip::ZipArchive>, - /// Image bytes read so far. - image_total: std::cell::Cell, -} - -impl<'b> Archive<'b> { - fn open(bytes: &'b [u8], max_member: u64) -> Result { - let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).map_err(|e| corrupt(e.to_string()))?; - let mut total = 0u64; - let meta = json_member(&mut z, "meta.json", MAX_JSON_MEMBER, &mut total)?; - if !meta.is_object() { - return Err(corrupt("meta.json is not an object")); - } - if app_major(&meta).is_some_and(|v| v < MIN_SKETCH) { - return Err(FormatError::Io(TOO_OLD.into())); - } - let mut warnings = Vec::new(); - if meta.get("version").and_then(Value::as_u64).is_some_and(|v| v > NEWEST_TESTED_VERSION) { - warnings.push("the file was saved by a newer Sketch than the import was checked with; some layers may differ".to_string()); - } - // A document.json that can't be read (nested too deep, damaged) loses its shared styles and - // library copies, not the pages: they are then taken in archive order. - let document = match json_member(&mut z, "document.json", MAX_JSON_MEMBER, &mut total) { - Ok(d) if d.is_object() => d, - other => { - let why = other.err().map_or_else(|| "not an object".to_string(), |e| e.to_string()); - warnings.push(format!( - "the document's shared styles and library symbols were skipped: {}", - why.trim_start_matches("not a valid FigCraft file: ") - )); - let pages: Vec = z - .file_names() - .filter_map(|n| n.strip_prefix("pages/")?.strip_suffix(".json")) - .map(|id| json!({"_ref": format!("pages/{id}")})) - .collect(); - json!({"pages": pages}) - } - }; - let refs = document.get("pages").and_then(Value::as_array).map(Vec::as_slice).unwrap_or_default(); - let mut pages = Vec::new(); - let mut seen = std::collections::HashSet::new(); - for r in refs.iter().take(MAX_PAGES) { - let Some(id) = r.get("_ref").and_then(Value::as_str).and_then(|s| s.strip_prefix("pages/")).filter(|id| valid_id(id)) else { - warnings.push("pages the document lists without a valid reference were skipped".into()); - continue; - }; - // A page listed twice is read once: a repeated reference must not repeat the unpacking. - if !seen.insert(id) { - continue; - } - match json_member(&mut z, &format!("pages/{id}.json"), max_member, &mut total) { - Ok(page) if page.is_object() => pages.push((id.to_string(), page)), - Ok(_) => warnings.push("pages that aren't JSON objects were skipped".into()), - Err(e) => warnings.push(format!("a page was skipped: {}", e.to_string().trim_start_matches("not a valid FigCraft file: "))), - } - } - if refs.len() > MAX_PAGES { - warnings.push(format!("the file has more than {MAX_PAGES} pages; the rest were skipped")); - } - let version = meta.get("version").and_then(Value::as_u64).unwrap_or(0); - Ok(Archive { document, version, pages, warnings, zip: z, image_total: std::cell::Cell::new(0) }) - } - - /// The bytes of the image a layer references (`images/`). - pub(crate) fn image(&self, r: &str) -> Option> { - let name = r.strip_prefix("images/").unwrap_or(r); - if !valid_id(name.split('.').next().unwrap_or("")) || name.split('.').count() > 2 { - return None; - } - // A clone shares the parsed directory and the bytes; the resolver only has shared access. - let mut z = self.zip.clone(); - let mut total = self.image_total.get(); - let data = capped_member(&mut z, &format!("images/{name}"), MAX_IMAGE, &mut total, MAX_IMAGE_TOTAL).ok(); - self.image_total.set(total); - data - } -} - -/// Member `name`, refused before unpacking when it would be larger than `limit`, than -/// [`MAX_RATIO`] times what it takes in the archive (past a floor), or than what is left of -/// `max_total` (`total`: read so far, counted up). -fn capped_member( - z: &mut zip::ZipArchive>, - name: &str, - limit: u64, - total: &mut u64, - max_total: u64, -) -> Result, FormatError> { - let (size, packed) = { - let f = z.by_name(name).map_err(|e| corrupt(format!("{name}: {e}")))?; - (f.size(), f.compressed_size()) - }; - let allowed = limit.min(packed.saturating_mul(MAX_RATIO).max(MIN_ALLOWANCE)).min(max_total.saturating_sub(*total)); - if size > allowed { - return Err(corrupt(format!("{name} is too large"))); - } - let data = crate::read_member(z, name, allowed); - // A failed read (a bad checksum at the end, say) unpacked all the same: charged in full, so - // a page list naming the member again can't repeat it within the budget. - *total += data.as_ref().map_or(size, |d| d.len() as u64); - data -} - -/// JSON member `name`, read by [`capped_member`] within the file's [`MAX_JSON_TOTAL`]. -fn json_member(z: &mut zip::ZipArchive>, name: &str, limit: u64, total: &mut u64) -> Result { - let data = capped_member(z, name, limit, total, MAX_JSON_TOTAL)?; - serde_json::from_slice(&data).map_err(|e| corrupt(format!("{name}: {e}"))) -} - -/// The major version of the Sketch that saved the file (`meta.json` `appVersion`, "50.2" → 50). -fn app_major(meta: &Value) -> Option { - meta.get("appVersion").and_then(Value::as_str)?.split('.').next()?.trim().parse().ok() -} - -/// Ids name archive members: letters, digits, `-` and `_` only, never a path. -fn valid_id(id: &str) -> bool { - !id.is_empty() && id.len() <= 128 && id.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_') -} diff --git a/crates/format/src/sketch/tests.rs b/crates/format/src/sketch/tests.rs index d8ace8be9..784c542bd 100644 --- a/crates/format/src/sketch/tests.rs +++ b/crates/format/src/sketch/tests.rs @@ -206,3 +206,21 @@ fn members_over_budget_are_skipped() { assert!(warnings.iter().any(|w| w.contains("too large")), "{warnings:?}"); assert!(!page_names(&doc).contains(&"Big".to_string())); } + +/// The shared container refuses ambiguous member names instead of selecting one silently. +#[test] +fn duplicate_archive_members_are_refused() { + let mut z = sketch_zip("50.2", 103, document(&["A", "B"]), &[page("A", "Alpha", vec![]), page("B", "Beta", vec![])], &[]); + let from = b"pages/B.json"; + let to = b"pages/A.json"; + let mut replaced = 0; + for i in 0..z.len().saturating_sub(from.len()).saturating_add(1) { + if z.get(i..i + from.len()) == Some(from.as_slice()) { + z[i..i + from.len()].copy_from_slice(to); + replaced += 1; + } + } + assert_eq!(replaced, 2, "local and central directory names changed"); + let error = import(&z, "ambiguous").unwrap_err().to_string(); + assert!(error.contains("two entries of the archive share a name"), "{error}"); +} diff --git a/sonar-project.properties b/sonar-project.properties index 7bd9e8ce6..8c36accaf 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -6,6 +6,14 @@ sonar.exclusions=craft-fonts/** # S4790 (weak hash): a .fig file names each image by the SHA-1 of its bytes (20 bytes, the only # length readers accept). It is an identifier the format fixes, not a security choice. -sonar.issue.ignore.multicriteria=fig_image_sha1 +sonar.issue.ignore.multicriteria=fig_image_sha1,gha-lockfile sonar.issue.ignore.multicriteria.fig_image_sha1.ruleKey=rust:S4790 sonar.issue.ignore.multicriteria.fig_image_sha1.resourceKey=crates/format/src/fig/export.rs + +# githubactions:S7637 asks for every action to be pinned to a commit SHA. A workflow covered by +# .github/workflows/actions.lock runs the commit the lockfile records, so its tag refs are pinned +# and the lockfile covers the actions those actions use too. Sonar does not read lockfiles yet, so +# the rule flags the tag refs anyway: +# https://community.sonarsource.com/t/githubactions-s7637-false-positives-for-actions-pinned-through-native-actions-lock/190814 +sonar.issue.ignore.multicriteria.gha-lockfile.ruleKey=githubactions:S7637 +sonar.issue.ignore.multicriteria.gha-lockfile.resourceKey=.github/workflows/**