Repository navigation
Expand file tree
/
Copy pathimport.rs
More file actions
302 lines (277 loc) · 11.9 KB
/
Copy pathimport.rs
File metadata and controls
302 lines (277 loc) · 11.9 KB
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
//! `.codexskin` import: a theme package zipped with `theme.json` at the
//! archive root. Extraction is paranoid (zip-slip, size/entry caps), the
//! extracted tree must pass the full `load_theme` validation, and the final
//! placement into `<themes_root>/<id>` is transactional — an existing install
//! of the same id is swapped out and restored on any failure.
use std::io::Read;
use std::path::{Component, Path, PathBuf};
use crate::theme::{load_theme, summarize, ThemeSummary};
use crate::{Result, ThemeEngineError};
/// Caps chosen far above any real package (biggest studio theme ≈ 3 MB
/// zipped, ~40 entries) but low enough to shrug off a zip bomb.
const MAX_ARCHIVE_BYTES: u64 = 50 * 1024 * 1024;
const MAX_UNPACKED_BYTES: u64 = 100 * 1024 * 1024;
const MAX_ENTRIES: usize = 500;
fn err(message: impl Into<String>) -> ThemeEngineError {
ThemeEngineError::Theme(message.into())
}
/// A zip entry name is accepted only as a plain relative path — no roots, no
/// parent hops, no drive prefixes (`ZipFile::enclosed_name` plus belt and
/// braces for portability).
fn safe_entry_path(name: &str) -> Option<PathBuf> {
let path = Path::new(name);
if path
.components()
.all(|c| matches!(c, Component::Normal(_)))
{
Some(path.to_path_buf())
} else {
None
}
}
/// Import a `.codexskin` archive into `themes_root`, returning the installed
/// theme's summary. The archive must carry `theme.json` at its root.
pub fn import_codexskin(archive_path: &Path, themes_root: &Path) -> Result<ThemeSummary> {
let archive_meta = std::fs::metadata(archive_path)
.map_err(|e| err(format!("无法读取主题包: {e}")))?;
if archive_meta.len() > MAX_ARCHIVE_BYTES {
return Err(err("主题包超过大小上限 (50MB)"));
}
let file =
std::fs::File::open(archive_path).map_err(|e| err(format!("无法打开主题包: {e}")))?;
let mut archive = zip::ZipArchive::new(file)
.map_err(|e| err(format!("主题包不是有效的 zip: {e}")))?;
if archive.len() > MAX_ENTRIES {
return Err(err("主题包条目数超过上限"));
}
if archive.by_name("theme.json").is_err() {
return Err(err(
"主题包缺少根级 theme.json(.codexskin 的内容必须位于压缩包根目录)",
));
}
std::fs::create_dir_all(themes_root)
.map_err(|e| err(format!("无法创建主题目录: {e}")))?;
let nonce = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);
let staging = themes_root.join(format!(".import-{}-{nonce}", std::process::id()));
let outcome = extract_and_place(&mut archive, &staging, themes_root);
let _ = std::fs::remove_dir_all(&staging);
outcome
}
fn extract_and_place(
archive: &mut zip::ZipArchive<std::fs::File>,
staging: &Path,
themes_root: &Path,
) -> Result<ThemeSummary> {
std::fs::create_dir_all(staging).map_err(|e| err(format!("创建临时目录失败: {e}")))?;
let mut unpacked: u64 = 0;
for index in 0..archive.len() {
let entry = archive
.by_index(index)
.map_err(|e| err(format!("读取压缩包条目失败: {e}")))?;
let entry_name = entry
.name()
.map_err(|e| err(format!("读取压缩包条目名称失败: {e}")))?;
let Some(relative) = entry
.enclosed_name()
.and_then(|p| safe_entry_path(p.to_string_lossy().as_ref()))
else {
return Err(err(format!("主题包含非法路径条目: {entry_name}")));
};
if entry.is_dir() {
std::fs::create_dir_all(staging.join(&relative))
.map_err(|e| err(format!("解压目录失败: {e}")))?;
continue;
}
unpacked = unpacked.saturating_add(entry.size());
if unpacked > MAX_UNPACKED_BYTES {
return Err(err("主题包解压后超过大小上限"));
}
let target = staging.join(&relative);
if let Some(parent) = target.parent() {
std::fs::create_dir_all(parent).map_err(|e| err(format!("解压目录失败: {e}")))?;
}
let mut contents = Vec::with_capacity(entry.size().min(8 * 1024 * 1024) as usize);
entry
.take(MAX_UNPACKED_BYTES)
.read_to_end(&mut contents)
.map_err(|e| err(format!("解压 {} 失败: {e}", relative.display())))?;
std::fs::write(&target, contents)
.map_err(|e| err(format!("写入 {} 失败: {e}", relative.display())))?;
}
// Full package validation before anything touches the live gallery.
let theme = load_theme(staging)?;
let id = theme.config.id.clone();
// Transactional placement: park any existing install of this id, move the
// new tree in, drop the parked copy only on success.
let destination = themes_root.join(&id);
let parked = themes_root.join(format!(".replaced-{id}-{}", std::process::id()));
let had_previous = destination.exists();
if had_previous {
std::fs::rename(&destination, &parked)
.map_err(|e| err(format!("暂存旧版本失败: {e}")))?;
}
match std::fs::rename(staging, &destination) {
Ok(()) => {
if had_previous {
let _ = std::fs::remove_dir_all(&parked);
}
let installed = load_theme(&destination)?;
Ok(summarize(installed))
}
Err(error) => {
if had_previous {
let _ = std::fs::rename(&parked, &destination);
}
Err(err(format!("安装主题失败: {error}")))
}
}
}
#[cfg(test)]
mod tests {
use std::io::Write;
use super::*;
fn write_zip(path: &Path, entries: &[(&str, &[u8])]) {
let file = std::fs::File::create(path).unwrap();
let mut writer = zip::ZipWriter::new(file);
let options = zip::write::SimpleFileOptions::default()
.compression_method(zip::CompressionMethod::Deflated);
for (name, bytes) in entries {
writer.start_file(*name, options).unwrap();
writer.write_all(bytes).unwrap();
}
writer.finish().unwrap();
}
fn minimal_entries(id: &str) -> Vec<(String, Vec<u8>)> {
vec![
(
"theme.json".to_string(),
format!(
r##"{{"schemaVersion":2,"id":"{id}","name":"T","version":"1.0.0",
"colors":{{"accent":"#abc"}},
"previews":["previews/home.webp"]}}"##
)
.into_bytes(),
),
("theme.css".to_string(), b"html.codex-theme-studio {}\n".to_vec()),
("previews/home.webp".to_string(), vec![0x52, 0x49, 0x46, 0x46, 1, 2]),
]
}
#[test]
fn imports_a_valid_codexskin_and_replaces_existing() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("themes");
let skin = tmp.path().join("pkg.codexskin");
let entries = minimal_entries("import-me");
let refs: Vec<(&str, &[u8])> =
entries.iter().map(|(n, b)| (n.as_str(), b.as_slice())).collect();
write_zip(&skin, &refs);
let summary = import_codexskin(&skin, &root).unwrap();
assert_eq!(summary.id, "import-me");
assert_eq!(summary.meta.version.as_deref(), Some("1.0.0"));
assert!(summary.preview.as_ref().unwrap().ends_with("previews/home.webp"));
assert!(root.join("import-me/theme.json").is_file());
// Re-import (upgrade path) replaces in place and leaves no debris.
let summary2 = import_codexskin(&skin, &root).unwrap();
assert_eq!(summary2.id, "import-me");
let leftovers: Vec<_> = std::fs::read_dir(&root)
.unwrap()
.flatten()
.filter(|e| e.file_name().to_string_lossy().starts_with('.'))
.collect();
assert!(leftovers.is_empty(), "staging/parked dirs must be cleaned");
}
#[test]
fn rejects_zip_slip_and_missing_manifest() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("themes");
let evil = tmp.path().join("evil.codexskin");
write_zip(
&evil,
&[
("theme.json", br#"{"schemaVersion":2,"id":"evil"}"# as &[u8]),
("../escape.txt", b"boom"),
],
);
assert!(import_codexskin(&evil, &root).is_err());
assert!(!tmp.path().join("escape.txt").exists());
let hollow = tmp.path().join("hollow.codexskin");
write_zip(&hollow, &[("readme.md", b"hi" as &[u8])]);
let error = import_codexskin(&hollow, &root).unwrap_err().to_string();
assert!(error.contains("theme.json"), "{error}");
}
#[test]
fn imports_legacy_cp437_entry_names() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("themes");
let skin = tmp.path().join("cp437-name.codexskin");
let mut entries = minimal_entries("cp437-name");
entries.push(("cafe.bin".to_string(), b"legacy filename".to_vec()));
let refs: Vec<(&str, &[u8])> =
entries.iter().map(|(n, b)| (n.as_str(), b.as_slice())).collect();
write_zip(&skin, &refs);
let mut bytes = std::fs::read(&skin).unwrap();
let name = b"cafe.bin";
let mut replaced = 0;
for index in 0..=bytes.len() - name.len() {
if bytes[index..index + name.len()] == *name {
bytes[index + 3] = 0x82; // CP437 encodes é as one byte.
replaced += 1;
}
}
assert_eq!(replaced, 2, "update local and central directory names");
std::fs::write(&skin, bytes).unwrap();
assert_eq!(import_codexskin(&skin, &root).unwrap().id, "cp437-name");
assert_eq!(
std::fs::read(root.join("cp437-name/café.bin")).unwrap(),
b"legacy filename"
);
}
#[test]
fn rejects_invalid_package_content() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("themes");
let bad = tmp.path().join("bad.codexskin");
// Valid zip, but schemaVersion is wrong — must fail load_theme and
// leave the gallery untouched.
write_zip(
&bad,
&[("theme.json", br#"{"schemaVersion":1,"id":"bad"}"# as &[u8])],
);
assert!(import_codexskin(&bad, &root).is_err());
assert!(!root.join("bad").exists());
}
#[test]
fn imports_a_codexskin_with_motion() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("themes");
let skin = tmp.path().join("motion.codexskin");
// A package carrying an mp4 motion asset (plus its required static intro
// fallback) must import without any extra rejection path.
write_zip(
&skin,
&[
(
"theme.json",
br#"{"schemaVersion":2,"id":"motion-skin","name":"M","version":"1.0.0",
"assets":{"intro":"assets/intro.webp"},
"motionAssets":{"intro-video":"assets/intro-video.mp4"},
"previews":["previews/home.webp"]}"# as &[u8],
),
("theme.css", b"html.codex-theme-studio {}\n"),
("assets/intro.webp", b"RIFF\x01\x02"),
("assets/intro-video.mp4", b"\x00\x00\x00\x18ftypisom"),
("previews/home.webp", b"RIFF\x01\x02"),
],
);
let summary = import_codexskin(&skin, &root).unwrap();
assert_eq!(summary.id, "motion-skin");
// The installed package validates and exposes the motion asset with its
// video mime — ready for the payload's dedicated motion data-URL map.
let installed = crate::theme::load_theme(&root.join("motion-skin")).unwrap();
assert!(installed.motion_assets.contains_key("intro-video"));
assert_eq!(installed.motion_assets["intro-video"].mime, "video/mp4");
}
}