Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 40 additions & 44 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
resolver = "2"
members = [
"src-tauri",
"crates/codex-archive",
"crates/codex-mac-engine",
"crates/codex-theme-engine",
"crates/codex-win-engine",
Expand Down Expand Up @@ -35,4 +36,4 @@ tokio = "1"
url = "2.5"
uuid = { version = "1", features = ["v4"] }
windows-sys = "0.61.2"
zip = { version = "8", default-features = false, features = ["deflate"] }
zip = { version = "9.0.5", default-features = false, features = ["deflate"] }
9 changes: 9 additions & 0 deletions crates/codex-archive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "codex-archive"
version = "0.1.0"
edition.workspace = true
license.workspace = true
description = "ZIP filename decoding and relative paths shared by the package readers."

[dependencies]
zip.workspace = true
139 changes: 139 additions & 0 deletions crates/codex-archive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
//! ZIP entry names must honor the language flag, not UTF-8 autodetection.

use std::{borrow::Cow, io::Read, path::PathBuf, str::Utf8Error};

// Unicode mappings for CP437 bytes 0x80..=0xff. ASCII bytes map unchanged.
const CP437_HIGH: [char; 128] = [
'Ç', 'ü', 'é', 'â', 'ä', 'à', 'å', 'ç', 'ê', 'ë', 'è', 'ï', 'î', 'ì', 'Ä', 'Å', 'É', 'æ', 'Æ',
'ô', 'ö', 'ò', 'û', 'ù', 'ÿ', 'Ö', 'Ü', '¢', '£', '¥', '₧', 'ƒ', 'á', 'í', 'ó', 'ú', 'ñ', 'Ñ',
'ª', 'º', '¿', '⌐', '¬', '½', '¼', '¡', '«', '»', '░', '▒', '▓', '│', '┤', '╡', '╢', '╖', '╕',
'╣', '║', '╗', '╝', '╜', '╛', '┐', '└', '┴', '┬', '├', '─', '┼', '╞', '╟', '╚', '╔', '╩', '╦',
'╠', '═', '╬', '╧', '╨', '╤', '╥', '╙', '╘', '╒', '╓', '╫', '╪', '┘', '┌', '█', '▄', '▌', '▐',
'▀', 'α', 'ß', 'Γ', 'π', 'Σ', 'σ', 'µ', 'τ', 'Φ', 'Θ', 'Ω', 'δ', '∞', 'φ', 'ε', '∩', '≡', '±',
'≥', '≤', '⌠', '⌡', '÷', '≈', '°', '∙', '·', '√', 'ⁿ', '²', '■', '\u{a0}',
];

/// Decode the effective name using bit 11 (UTF-8) or CP437.
///
/// ZIP 9 applies valid UnicodePath extra fields, including their CRC and UTF-8
/// checks, to name_raw() and sets bit 11 before exposing the entry. Using these
/// effective bytes and flags preserves that override without guessing encodings.
/// Malformed names explicitly marked UTF-8 are rejected, not decoded as CP437.
pub fn entry_name<'a, R: Read>(
entry: &'a zip::read::ZipFile<'_, R>,
) -> Result<Cow<'a, str>, Utf8Error> {
let raw = entry.name_raw();
if entry.flags().as_u16() & (1 << 11) != 0 || raw.is_ascii() {
return std::str::from_utf8(raw).map(Cow::Borrowed);
}
Ok(Cow::Owned(
raw.iter()
.map(|&byte| {
if byte < 0x80 {
char::from(byte)
} else {
CP437_HIGH[usize::from(byte - 0x80)]
}
})
.collect(),
))
}

/// Validate the decoded name as a relative path on both Windows and Unix.
/// Normalize separators and internal `.`/`..` without allowing root escape.
/// Callers must use this returned path for subsequent checks and extraction.
pub fn enclosed_path(name: &str) -> Option<PathBuf> {
if name.starts_with(['/', '\\']) || name.contains(['\0', ':']) {
return None;
}
let mut path = PathBuf::new();
for component in name.split(['/', '\\']) {
match component {
"" | "." => {}
".." if !path.pop() => return None,
".." => {}
part => path.push(part),
}
}
(!path.as_os_str().is_empty()).then_some(path)
}

#[cfg(test)]
#[path = "../../test-support/zip.rs"]
mod zip_test;

#[cfg(test)]
mod tests {
use super::*;
use crate::zip_test::{archive, unicode_path_extra, Entry};
use std::io::Cursor;

#[test]
fn language_flag_disambiguates_the_same_raw_name() {
for (flags, expected) in [(0, "├⌐.webp"), (1 << 11, "é.webp")] {
let mut entry = Entry::new(b"\xc3\xa9.webp", b"data");
entry.flags = flags;
let mut zip = zip::ZipArchive::new(Cursor::new(archive(&[entry]))).unwrap();
let file = zip.by_index(0).unwrap();
assert_eq!(file.flags().as_u16(), flags);
assert_eq!(entry_name(&file).unwrap(), expected);
}
}

#[test]
fn unicode_path_requires_valid_crc_and_utf8() {
let raw = b"\xc3\xa9.webp";
for (unicode, valid_crc, expected) in [
("资源.webp".as_bytes(), true, "资源.webp"),
("资源.webp".as_bytes(), false, "├⌐.webp"),
(b"\xff.webp".as_slice(), true, "é.webp"),
] {
let extra = unicode_path_extra(raw, unicode, valid_crc);
let mut entry = Entry::new(raw, b"data");
entry.extra = &extra;
let mut zip = zip::ZipArchive::new(Cursor::new(archive(&[entry]))).unwrap();
assert_eq!(entry_name(&zip.by_index(0).unwrap()).unwrap(), expected);
}
}

#[test]
fn invalid_utf8_is_only_allowed_without_the_utf8_flag() {
for flags in [0, 1 << 11] {
let mut entry = Entry::new(b"caf\x82.bin", b"data");
entry.flags = flags;
let mut zip = zip::ZipArchive::new(Cursor::new(archive(&[entry]))).unwrap();
let file = zip.by_index(0).unwrap();
if flags == 0 {
assert_eq!(entry_name(&file).unwrap(), "café.bin");
} else {
assert!(entry_name(&file).is_err());
}
}
}

#[test]
fn decoded_paths_cannot_escape_on_either_platform() {
for name in [
"",
".",
"..",
"../evil",
"a/../../evil",
r"a\..\..\evil",
"/absolute",
r"\absolute",
r"C:\evil",
"C:evil",
r"\\server\share",
"a\0b",
] {
assert!(enclosed_path(name).is_none(), "{name:?}");
}
for name in ["assets/é.webp", r"assets\é.webp", "assets/x/../é.webp"] {
assert_eq!(
enclosed_path(name).unwrap(),
PathBuf::from("assets").join("é.webp")
);
}
}
}
1 change: 1 addition & 0 deletions crates/codex-theme-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ description = "Asset-based UI theme engine for the Codex desktop app: loopback C

[dependencies]
base64.workspace = true
codex-archive = { path = "../codex-archive" }
futures-util = { version = "0.3", default-features = false, features = ["sink", "std"] }
log.workspace = true
serde.workspace = true
Expand Down
Loading
Loading