Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

security: bound preview reads and note embed expansion

RoversX903a10d2732e
diff --git a/wezterm-gui/Cargo.toml b/wezterm-gui/Cargo.toml
index d026d975..c25a375e 100644
--- a/wezterm-gui/Cargo.toml
+++ b/wezterm-gui/Cargo.toml
@@ -146,6 +146,8 @@ window.workspace = true
shared_library.workspace = true
winapi = { workspace=true, features = [
"winuser",
+ "winbase",
+ "winnt",
"consoleapi",
"handleapi",
"fileapi",
diff --git a/wezterm-gui/src/bounded_file.rs b/wezterm-gui/src/bounded_file.rs
new file mode 100644
index 00000000..3d17e9c9
--- /dev/null
+++ b/wezterm-gui/src/bounded_file.rs
@@ -0,0 +1,255 @@
+//! Bounded reads of untrusted preview content, including special-file rejection.
+use anyhow::{bail, Context, Result};
+use std::fs::{File, OpenOptions};
+use std::io::Read;
+use std::path::{Component, Path};
+
+#[derive(Debug)]
+pub(crate) struct ByteBudgetExceeded;
+impl std::fmt::Display for ByteBudgetExceeded {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.write_str("preview source exceeds the byte budget")
+ }
+}
+impl std::error::Error for ByteBudgetExceeded {}
+
+fn options() -> OpenOptions {
+ let mut options = OpenOptions::new();
+ options.read(true);
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::OpenOptionsExt;
+ // Opening a FIFO must not block before we can inspect the handle.
+ options.custom_flags(libc::O_NONBLOCK);
+ }
+ options
+}
+
+/// Account payload bytes even if a later read fails. Opening/stat failures
+/// consume no payload; callers separately budget attempts.
+struct CountedReader<'a, R> {
+ inner: R,
+ consumed: &'a mut usize,
+}
+impl<R: Read> Read for CountedReader<'_, R> {
+ fn read(&mut self, buffer: &mut [u8]) -> std::io::Result<usize> {
+ let read = self.inner.read(buffer)?;
+ *self.consumed = self.consumed.saturating_add(read);
+ Ok(read)
+ }
+}
+
+fn read_handle(file: File, limit: usize, consumed: &mut usize) -> Result<Vec<u8>> {
+ let metadata = file.metadata()?;
+ if !metadata.is_file() {
+ bail!("preview source is not a regular file");
+ }
+ if metadata.len() > limit as u64 {
+ return Err(ByteBudgetExceeded.into());
+ }
+ let mut bytes = Vec::with_capacity((metadata.len() as usize).min(limit));
+ CountedReader {
+ inner: file,
+ consumed,
+ }
+ .take(limit as u64 + 1)
+ .read_to_end(&mut bytes)?;
+ if bytes.len() > limit {
+ return Err(ByteBudgetExceeded.into());
+ }
+ Ok(bytes)
+}
+
+pub(crate) fn read_regular(path: &Path, limit: usize) -> Result<Vec<u8>> {
+ read_handle(
+ options()
+ .open(path)
+ .with_context(|| format!("open {}", path.display()))?,
+ limit,
+ &mut 0,
+ )
+}
+
+/// Resolve from an opened root without following descendant symlinks. Vault
+/// indexing also excludes symlinks; a replacement between indexing and reading
+/// must not turn a note into a read outside the vault.
+pub(crate) fn read_in_root(
+ root: &Path,
+ relative: &Path,
+ limit: usize,
+ consumed: &mut usize,
+) -> Result<Vec<u8>> {
+ let parts: Vec<_> = relative
+ .components()
+ .filter(|c| *c != Component::CurDir)
+ .collect();
+ if parts.is_empty() || parts.iter().any(|c| !matches!(c, Component::Normal(_))) {
+ bail!("invalid embedded note path");
+ }
+ #[cfg(unix)]
+ {
+ use std::ffi::CString;
+ use std::os::fd::{AsRawFd, FromRawFd};
+ use std::os::unix::ffi::OsStrExt;
+ let mut directory = File::open(root)?;
+ for (index, component) in parts.iter().enumerate() {
+ let name = CString::new(component.as_os_str().as_bytes())?;
+ let last = index + 1 == parts.len();
+ let flags = libc::O_RDONLY
+ | libc::O_CLOEXEC
+ | libc::O_NOFOLLOW
+ | libc::O_NONBLOCK
+ | if last { 0 } else { libc::O_DIRECTORY };
+ // SAFETY: the directory fd and NUL-terminated component live through
+ // openat; each returned descriptor is immediately owned by File.
+ let fd = unsafe { libc::openat(directory.as_raw_fd(), name.as_ptr(), flags) };
+ if fd < 0 {
+ return Err(std::io::Error::last_os_error().into());
+ }
+ let file = unsafe { File::from_raw_fd(fd) };
+ if last {
+ return read_handle(file, limit, consumed);
+ }
+ directory = file;
+ }
+ }
+ #[cfg(windows)]
+ {
+ use std::os::windows::fs::{MetadataExt, OpenOptionsExt};
+ use std::os::windows::{ffi::OsStringExt, io::AsRawHandle};
+ use winapi::um::fileapi::GetFinalPathNameByHandleW;
+ fn opened_path(file: &File) -> Result<std::path::PathBuf> {
+ let mut buffer = vec![0u16; 512];
+ loop {
+ // Query the opened object, not a path that could be swapped
+ // after validation. The handle and output buffer remain live.
+ let length = unsafe {
+ GetFinalPathNameByHandleW(
+ file.as_raw_handle() as _,
+ buffer.as_mut_ptr(),
+ buffer.len() as u32,
+ 0,
+ )
+ };
+ if length == 0 {
+ return Err(std::io::Error::last_os_error().into());
+ }
+ if (length as usize) < buffer.len() {
+ buffer.truncate(length as usize);
+ return Ok(std::ffi::OsString::from_wide(&buffer).into());
+ }
+ buffer.resize(length as usize + 1, 0);
+ }
+ }
+ use winapi::um::winbase::{FILE_FLAG_BACKUP_SEMANTICS, FILE_FLAG_OPEN_REPARSE_POINT};
+ use winapi::um::winnt::{FILE_ATTRIBUTE_REPARSE_POINT, FILE_SHARE_READ, FILE_SHARE_WRITE};
+ let mut path = root.canonicalize()?;
+ // Omit FILE_SHARE_DELETE: held directories cannot be renamed/replaced
+ // while resolving the next component. Inspect reparse points themselves.
+ let open = |path: &Path| -> Result<File> {
+ let file = OpenOptions::new()
+ .read(true)
+ .share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE)
+ .custom_flags(FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT)
+ .open(path)?;
+ if file.metadata()?.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT != 0 {
+ bail!("embedded note path contains a reparse point");
+ }
+ Ok(file)
+ };
+ let mut held = vec![open(&path)?];
+ let opened_root = opened_path(&held[0])?;
+ for (index, component) in parts.iter().enumerate() {
+ path.push(component.as_os_str());
+ let file = open(&path)?;
+ // FILE_SHARE_DELETE protects renames, but cannot by itself stop
+ // another writer setting a junction on an existing directory.
+ if !opened_path(&file)?.starts_with(&opened_root) {
+ bail!("embedded note escaped its root");
+ }
+ if index + 1 == parts.len() {
+ return read_handle(file, limit, consumed);
+ }
+ if !file.metadata()?.is_dir() {
+ bail!("embedded note parent is not a directory");
+ }
+ held.push(file);
+ }
+ }
+ bail!("unsupported embedded note path")
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ #[test]
+ fn regular_files_respect_the_exact_budget() {
+ let dir = tempfile::tempdir().unwrap();
+ let path = dir.path().join("note.md");
+ std::fs::write(&path, b"abcd").unwrap();
+ assert_eq!(read_regular(&path, 4).unwrap(), b"abcd");
+ assert!(read_regular(&path, 3).is_err());
+ assert!(read_regular(dir.path(), 100).is_err());
+ assert_eq!(
+ read_in_root(dir.path(), Path::new("note.md"), 4, &mut 0).unwrap(),
+ b"abcd"
+ );
+ assert!(read_in_root(dir.path(), &path, 4, &mut 0).is_err());
+ assert!(read_in_root(dir.path(), Path::new("../note.md"), 4, &mut 0).is_err());
+ }
+ #[test]
+ fn actual_bytes_are_charged_and_preflight_errors_are_free() {
+ let dir = tempfile::tempdir().unwrap();
+ std::fs::write(dir.path().join("note.md"), b"abcd").unwrap();
+ for path in ["missing.md", "..", "note.md"] {
+ let mut consumed = 0;
+ assert!(read_in_root(dir.path(), Path::new(path), 3, &mut consumed).is_err());
+ assert_eq!(consumed, 0);
+ }
+ let mut consumed = 0;
+ assert_eq!(
+ read_in_root(dir.path(), Path::new("note.md"), 4, &mut consumed).unwrap(),
+ b"abcd"
+ );
+ assert_eq!(consumed, 4);
+ struct FailAfterFirst(bool);
+ impl Read for FailAfterFirst {
+ fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
+ if self.0 {
+ return Err(std::io::Error::other("test partial read failure"));
+ }
+ self.0 = true;
+ buf[0] = b'x';
+ Ok(1)
+ }
+ }
+ let mut consumed = 0;
+ let mut bytes = Vec::new();
+ assert!(CountedReader {
+ inner: FailAfterFirst(false),
+ consumed: &mut consumed
+ }
+ .take(16)
+ .read_to_end(&mut bytes)
+ .is_err());
+ assert_eq!(consumed, 1);
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn devices_fifos_and_swapped_directory_symlinks_are_rejected() {
+ use std::os::unix::{ffi::OsStrExt, fs::symlink};
+ let dir = tempfile::tempdir().unwrap();
+ let fifo = dir.path().join("fifo.png");
+ let cpath = std::ffi::CString::new(fifo.as_os_str().as_bytes()).unwrap();
+ assert_eq!(unsafe { libc::mkfifo(cpath.as_ptr(), 0o600) }, 0);
+ assert!(read_regular(&fifo, 32).is_err());
+ let device = dir.path().join("device.png");
+ symlink("/dev/zero", &device).unwrap();
+ assert!(read_regular(&device, 32).is_err());
+ let outside = tempfile::tempdir().unwrap();
+ std::fs::write(outside.path().join("note.md"), "outside").unwrap();
+ symlink(outside.path(), dir.path().join("swapped")).unwrap();
+ assert!(read_in_root(dir.path(), Path::new("swapped/note.md"), 100, &mut 0).is_err());
+ }
+}
diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rs
index 8b2455d6..7e25a1be 100644
--- a/wezterm-gui/src/main.rs
+++ b/wezterm-gui/src/main.rs
@@ -37,6 +37,7 @@ use wezterm_toast_notification::*;
mod agent_status;
mod bottom_quotes;
+mod bounded_file;
mod colorease;
mod commands;
mod customglyph;
diff --git a/wezterm-gui/src/markdown_editor/host.rs b/wezterm-gui/src/markdown_editor/host.rs
index a8c2b1f4..8c8f8406 100644
--- a/wezterm-gui/src/markdown_editor/host.rs
+++ b/wezterm-gui/src/markdown_editor/host.rs
@@ -400,30 +400,34 @@ impl NoteHostState {
projection_stage = Some(crate::input_diagnostics::StageTimer::begin(
"note_projection",
));
- let cached_links = self
- .projection
- .objects
- .iter()
- .filter_map(|object| match object {
- ProjectedObject::WikiLink {
- target,
- embed,
+ // Borrow the old cache instead of deep-cloning all payloads before
+ // admission. Keep the first occurrence: a later duplicate may be
+ // only an exhaustion placeholder from the previous shared budget.
+ let mut cached_links = HashMap::new();
+ for object in &self.projection.objects {
+ if let ProjectedObject::WikiLink {
+ target,
+ embed,
+ resolved_path,
+ ambiguous_paths,
+ rendered_lines,
+ embed_truncation,
+ ..
+ } = object
+ {
+ cached_links.entry((target.as_str(), *embed)).or_insert((
resolved_path,
ambiguous_paths,
rendered_lines,
- ..
- } => Some((
- (target.clone(), *embed),
- (
- resolved_path.clone(),
- ambiguous_paths.clone(),
- rendered_lines.clone(),
- ),
- )),
- _ => None,
- })
- .collect::<HashMap<_, _>>();
+ embed_truncation,
+ ));
+ }
+ }
+ let mut embed_budget = super::projection::EmbedBudget::default();
let mut projection = MarkdownProjection::parse(source);
+ // Reallocate shared budgets only when link targets/order/count change.
+ // Plain edits must not make a deterministic truncation rescan the vault.
+ let links_changed = !self.projection.link_targets().eq(projection.link_targets());
// Never walk the vault (or read embedded notes) on the UI thread:
// reuse the previous projection's resolutions for immediate paint
// and let the background parse worker do the authoritative lookup.
@@ -436,18 +440,27 @@ impl NoteHostState {
resolved_path,
ambiguous_paths,
rendered_lines,
+ embed_truncation,
..
} = object
else {
continue;
};
any_links = true;
- if let Some((cached_path, cached_ambiguous_paths, cached_lines)) =
- cached_links.get(&(target.clone(), *embed))
+ if let Some((
+ cached_path,
+ cached_ambiguous_paths,
+ cached_lines,
+ cached_truncation,
+ )) = cached_links.get(&(target.as_str(), *embed))
{
- *resolved_path = cached_path.clone();
- *ambiguous_paths = cached_ambiguous_paths.clone();
- *rendered_lines = cached_lines.clone();
+ *resolved_path = (*cached_path).clone();
+ *ambiguous_paths = (*cached_ambiguous_paths).clone();
+ if *embed {
+ let preview = embed_budget.copy_cached(cached_lines, **cached_truncation);
+ *rendered_lines = preview.lines;
+ *embed_truncation = preview.truncation;
+ }
} else {
links_missing_resolution = true;
}
@@ -455,9 +468,11 @@ impl NoteHostState {
// Published projections used to re-resolve on every reparse, which
// is what picks up created/renamed targets and edited embeds; keep
// that freshness, just on the worker instead of the UI thread.
- // Live/Frozen keep the old cache-authoritative behavior and only
- // owe a round trip for links the cache has never seen.
- self.links_resolution_pending = links_missing_resolution
+ // Live/Frozen re-resolve on structural edits, not truncation markers.
+ // Preserve outstanding work across edits while an old worker runs.
+ self.links_resolution_pending = self.links_resolution_pending
+ || links_changed
+ || links_missing_resolution
|| (any_links && matches!(self.display_source, NoteDisplaySource::Published));
self.projection = projection;
self.reveal_key_cache = None;
@@ -1213,6 +1228,195 @@ mod state_tests {
assert!(host.background_parse_request().is_none());
}
+ #[test]
+ fn edited_duplicate_embeds_cannot_amplify_cached_payload_or_lose_first_content() {
+ for payload_bytes in [16, 128 * 1024] {
+ let vault = tempfile::tempdir().unwrap();
+ std::fs::write(vault.path().join("big.md"), "x".repeat(payload_bytes)).unwrap();
+ let mut host = host_with_source("![[big]]\n");
+ host.display_source = NoteDisplaySource::Live;
+ host.projection
+ .resolve_vault_links(vault.path(), "Inbox.md");
+ let session = host.session.as_ref().unwrap().clone();
+ let mut view = EditorViewState::default();
+ let end = session.lock().source().len();
+ session.lock().set_caret(&mut view, end, false);
+ assert!(session
+ .lock()
+ .insert_text(&mut view, &"![[big]]\n".repeat(299)));
+ host.refresh_projection();
+ let embedded: Vec<_> = host
+ .projection
+ .objects
+ .iter()
+ .filter_map(|object| match object {
+ ProjectedObject::WikiLink {
+ rendered_lines,
+ embed_truncation,
+ ..
+ } => {
+ let end = rendered_lines.len() - usize::from(embed_truncation.is_some());
+ Some(&rendered_lines[..end])
+ }
+ _ => None,
+ })
+ .collect();
+ let content: Vec<_> = embedded.iter().flat_map(|lines| lines.iter()).collect();
+ assert!(content.len() <= 256 * 200);
+ assert!(content.iter().map(|line| line.len()).sum::<usize>() <= 4 * 1024 * 1024);
+ assert_eq!(embedded[0][0].len(), payload_bytes);
+ assert!(host.links_resolution_pending);
+ // A second edit must not let a later placeholder overwrite the
+ // first occurrence's real cache value for this same target.
+ assert!(session.lock().insert_text(&mut view, "\n"));
+ host.refresh_projection();
+ let first = host
+ .projection
+ .objects
+ .iter()
+ .find_map(|object| match object {
+ ProjectedObject::WikiLink { rendered_lines, .. } => Some(rendered_lines),
+ _ => None,
+ })
+ .unwrap();
+ assert_eq!(first[0].len(), payload_bytes);
+ }
+ }
+
+ fn resolve_requested_links(host: &mut NoteHostState, vault: &std::path::Path) {
+ let (revision, source, mode, caret, _) = host
+ .background_parse_request()
+ .expect("one resolution request");
+ let mut projection = MarkdownProjection::parse(&source);
+ projection.resolve_vault_links(vault, "Inbox.md");
+ let visual = build_visual_document(&source, &projection, mode, caret);
+ assert!(host.apply_background_parse(revision, mode, caret, projection, visual));
+ assert!(host.background_parse_request().is_none());
+ }
+
+ #[test]
+ fn ordinary_edits_do_not_reindex_truncated_or_literal_notice_embeds() {
+ for contents in [
+ "line\n".repeat(201),
+ "… (embed budget reached)\n".into(),
+ "x".repeat(1024 * 1024 + 1),
+ ] {
+ let vault = tempfile::tempdir().unwrap();
+ std::fs::write(vault.path().join("note.md"), &contents).unwrap();
+ let mut host = host_with_source("![[note]]\n");
+ host.display_source = NoteDisplaySource::Live;
+ resolve_requested_links(&mut host, vault.path());
+ let original: Vec<_> = host
+ .projection
+ .objects
+ .iter()
+ .filter_map(|object| match object {
+ ProjectedObject::WikiLink {
+ rendered_lines,
+ embed_truncation,
+ ..
+ } => Some((rendered_lines.clone(), *embed_truncation)),
+ _ => None,
+ })
+ .collect();
+ if contents.starts_with('…') {
+ assert_eq!(
+ original[0].1, None,
+ "literal text is not truncation metadata"
+ );
+ } else {
+ assert!(original[0].1.is_some());
+ }
+ let session = host.session.as_ref().unwrap().clone();
+ let mut view = EditorViewState::default();
+ for text in ["a", "b", "c"] {
+ let end = session.lock().source().len();
+ session.lock().set_caret(&mut view, end, false);
+ assert!(session.lock().insert_text(&mut view, text));
+ host.refresh_projection();
+ assert!(!host.links_resolution_pending);
+ assert!(
+ host.background_parse_request().is_none(),
+ "plain typing must not schedule vault indexing"
+ );
+ let current: Vec<_> = host
+ .projection
+ .objects
+ .iter()
+ .filter_map(|object| match object {
+ ProjectedObject::WikiLink {
+ rendered_lines,
+ embed_truncation,
+ ..
+ } => Some((rendered_lines.clone(), *embed_truncation)),
+ _ => None,
+ })
+ .collect();
+ assert_eq!(current, original);
+ }
+ }
+ }
+
+ #[test]
+ fn removing_embeds_restores_budgeted_content_despite_an_inflight_edit() {
+ let vault = tempfile::tempdir().unwrap();
+ std::fs::write(vault.path().join("big.md"), "x".repeat(1024 * 1024)).unwrap();
+ std::fs::write(vault.path().join("tail.md"), "Hello\n").unwrap();
+ let mut host = host_with_source(&("![[big]]\n".repeat(4) + "![[tail]]\n"));
+ host.display_source = NoteDisplaySource::Live;
+ resolve_requested_links(&mut host, vault.path());
+ let tail = host
+ .projection
+ .objects
+ .iter()
+ .find_map(|object| match object {
+ ProjectedObject::WikiLink {
+ target,
+ embed_truncation,
+ ..
+ } if target == "tail" => Some(*embed_truncation),
+ _ => None,
+ })
+ .unwrap();
+ assert_eq!(tail, Some(super::super::projection::EmbedTruncation::Bytes));
+ let session = host.session.as_ref().unwrap().clone();
+ let mut view = EditorViewState::default();
+ session.lock().select_all(&mut view);
+ assert!(session.lock().insert_text(&mut view, "![[tail]]\n"));
+ host.refresh_projection();
+ let (revision, source, mode, caret, _) =
+ host.background_parse_request().expect("structure changed");
+ assert!(session.lock().insert_text(&mut view, "plain edit"));
+ host.refresh_projection();
+ assert!(
+ host.links_resolution_pending,
+ "the outstanding resolution must survive another edit"
+ );
+ let projection = MarkdownProjection::parse(&source);
+ let visual = build_visual_document(&source, &projection, mode, caret);
+ assert!(!host.apply_background_parse(revision, mode, caret, projection, visual));
+ resolve_requested_links(&mut host, vault.path());
+ let tail = host
+ .projection
+ .objects
+ .iter()
+ .find_map(|object| match object {
+ ProjectedObject::WikiLink {
+ target,
+ rendered_lines,
+ embed_truncation,
+ ..
+ } if target == "tail" => Some((rendered_lines, embed_truncation)),
+ _ => None,
+ })
+ .unwrap();
+ assert_eq!(tail.0, &vec!["Hello".to_string()]);
+ assert_eq!(*tail.1, None);
+ assert!(session.lock().insert_text(&mut view, " more text"));
+ host.refresh_projection();
+ assert!(host.background_parse_request().is_none());
+ }
+
#[test]
fn published_host_resolves_links_for_the_displayed_snapshot_not_the_live_one() {
let mut host = host_with_source("See [[Other]]");
diff --git a/wezterm-gui/src/markdown_editor/projection.rs b/wezterm-gui/src/markdown_editor/projection.rs
index 1d84e483..d98e5f80 100644
--- a/wezterm-gui/src/markdown_editor/projection.rs
+++ b/wezterm-gui/src/markdown_editor/projection.rs
@@ -147,6 +147,7 @@ pub(crate) enum ProjectedObject {
resolved_path: Option<String>,
ambiguous_paths: Vec<String>,
rendered_lines: Vec<String>,
+ embed_truncation: Option<EmbedTruncation>,
},
Callout {
source: Range<usize>,
@@ -557,6 +558,7 @@ impl MarkdownProjection {
resolved_path: None,
ambiguous_paths: vec![],
rendered_lines: vec![],
+ embed_truncation: None,
});
}
for (range, kind, title, body) in parse_callouts(source, &self.syntax) {
@@ -582,6 +584,13 @@ impl MarkdownProjection {
self.text = project_tags(project_plain_urls(std::mem::take(&mut self.text)));
}
+ pub(super) fn link_targets(&self) -> impl Iterator<Item = (&str, bool)> {
+ self.objects.iter().filter_map(|object| match object {
+ ProjectedObject::WikiLink { target, embed, .. } => Some((target.as_str(), *embed)),
+ _ => None,
+ })
+ }
+
pub(crate) fn resolve_vault_links(&mut self, vault_root: &Path, current_note: &str) {
if !self
.objects
@@ -598,6 +607,7 @@ impl MarkdownProjection {
}
};
let index = VaultLinkIndex::new(paths);
+ let mut embed_budget = EmbedBudget::default();
let current_directory = Path::new(current_note)
.parent()
.unwrap_or_else(|| Path::new(""));
@@ -608,6 +618,7 @@ impl MarkdownProjection {
resolved_path,
ambiguous_paths,
rendered_lines,
+ embed_truncation,
..
} = object
else {
@@ -617,11 +628,20 @@ impl MarkdownProjection {
*resolved_path = resolution.path;
*ambiguous_paths = resolution.ambiguous_paths;
rendered_lines.clear();
+ *embed_truncation = None;
if *embed {
if let Some(path) = resolved_path.as_deref() {
let mut visiting = HashSet::new();
- *rendered_lines =
- render_embedded_note(vault_root, path, &index, 0, &mut visiting);
+ let preview = render_embedded_note(
+ vault_root,
+ path,
+ &index,
+ 0,
+ &mut visiting,
+ &mut embed_budget,
+ );
+ *rendered_lines = preview.lines;
+ *embed_truncation = preview.truncation;
}
}
}
@@ -1212,84 +1232,233 @@ impl VaultLinkIndex {
}
}
+const EMBED_BUDGET_NOTICE: &str = "… (embed budget reached)";
+const MAX_EMBED_CONTENT_LINES: usize = 200;
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub(crate) enum EmbedTruncation {
+ Lines,
+ Bytes,
+ Visits,
+}
+
+#[derive(Default)]
+pub(super) struct EmbedPreview {
+ pub(super) lines: Vec<String>,
+ pub(super) truncation: Option<EmbedTruncation>,
+}
+impl EmbedPreview {
+ fn truncate(&mut self, reason: EmbedTruncation) {
+ self.truncation.get_or_insert(reason);
+ }
+
+ fn finish(mut self) -> Self {
+ // A notice is presentation, not one of the 200 admitted content rows.
+ // Its presence is recorded separately; literal note text is never a flag.
+ if self.truncation.is_some() {
+ self.lines.push(EMBED_BUDGET_NOTICE.into());
+ }
+ self
+ }
+}
+
+/// Payload and file-visit budgets are shared by the whole projection pass.
+/// Each top-level embed separately admits 200 content rows, including children.
+pub(super) struct EmbedBudget {
+ bytes: usize,
+ visits: usize,
+}
+impl Default for EmbedBudget {
+ fn default() -> Self {
+ Self {
+ bytes: 4 * 1024 * 1024,
+ visits: 256,
+ }
+ }
+}
+impl EmbedBudget {
+ /// Admit payload bytes before cloning, including when many embeds share a
+ /// cached target. This runs only on projection changes, not unchanged paints.
+ pub(super) fn copy_cached(
+ &mut self,
+ cached: &[String],
+ truncation: Option<EmbedTruncation>,
+ ) -> EmbedPreview {
+ let mut preview = EmbedPreview::default();
+ if self.visits == 0 {
+ preview.truncate(EmbedTruncation::Visits);
+ return preview.finish();
+ }
+ if self.bytes == 0 {
+ preview.truncate(EmbedTruncation::Bytes);
+ return preview.finish();
+ }
+ self.visits -= 1;
+ // The final generated notice is excluded using metadata, not its text.
+ let content_len = cached
+ .len()
+ .saturating_sub(usize::from(truncation.is_some()));
+ for text in &cached[..content_len] {
+ if preview.lines.len() == MAX_EMBED_CONTENT_LINES {
+ preview.truncate(EmbedTruncation::Lines);
+ break;
+ }
+ if text.len() > self.bytes {
+ preview.truncate(EmbedTruncation::Bytes);
+ break;
+ }
+ self.bytes -= text.len();
+ preview.lines.push(text.clone());
+ }
+ if preview.truncation.is_none() {
+ preview.truncation = truncation;
+ }
+ preview.finish()
+ }
+}
+
fn render_embedded_note(
vault_root: &Path,
relative_path: &str,
index: &VaultLinkIndex,
depth: usize,
visiting: &mut HashSet<String>,
-) -> Vec<String> {
+ budget: &mut EmbedBudget,
+) -> EmbedPreview {
+ let mut preview = EmbedPreview::default();
+ render_embedded_note_into(
+ vault_root,
+ relative_path,
+ index,
+ depth,
+ visiting,
+ budget,
+ &mut preview,
+ );
+ preview.finish()
+}
+
+fn render_embedded_note_into(
+ vault_root: &Path,
+ relative_path: &str,
+ index: &VaultLinkIndex,
+ depth: usize,
+ visiting: &mut HashSet<String>,
+ budget: &mut EmbedBudget,
+ preview: &mut EmbedPreview,
+) {
+ if preview.lines.len() == MAX_EMBED_CONTENT_LINES {
+ preview.truncate(EmbedTruncation::Lines);
+ return;
+ }
+ if budget.visits == 0 {
+ preview.truncate(EmbedTruncation::Visits);
+ return;
+ }
+ if budget.bytes == 0 {
+ preview.truncate(EmbedTruncation::Bytes);
+ return;
+ }
+ budget.visits -= 1;
if Path::new(relative_path)
.extension()
- .and_then(|extension| extension.to_str())
- .is_some_and(|extension| !extension.eq_ignore_ascii_case("md"))
+ .and_then(|e| e.to_str())
+ .is_some_and(|e| !e.eq_ignore_ascii_case("md"))
{
- return vec![format!("Attachment: {relative_path}")];
+ preview.lines.push(format!("Attachment: {relative_path}"));
+ return;
}
if depth >= 3 {
- return vec!["…".to_string()];
- }
- if !visiting.insert(relative_path.to_ascii_lowercase()) {
- return vec![format!("↻ {relative_path} (cycle)")];
- }
- let result = std::fs::read_to_string(vault_root.join(relative_path))
- .map(|source| {
- let frontmatter = parse_frontmatter(&source).map(|(range, _)| range);
- let current_directory = Path::new(relative_path)
- .parent()
- .unwrap_or_else(|| Path::new(""));
- let mut lines = Vec::new();
- let mut source_offset = 0usize;
- let mut in_fence = false;
- for line in source.split_inclusive('\n') {
- if lines.len() >= 200 {
- lines.push("…".to_string());
- break;
- }
- let line_end = source_offset + line.len();
- if frontmatter
- .as_ref()
- .is_some_and(|range| source_offset < range.end && range.start < line_end)
- {
- source_offset = line_end;
- continue;
- }
- let trimmed = line.trim_end_matches(['\r', '\n']).trim();
- if trimmed.starts_with("```") || trimmed.starts_with("~~~") {
- in_fence = !in_fence;
- source_offset = line_end;
- continue;
- }
- if let Some(target) = trimmed
- .strip_prefix("![[")
- .and_then(|value| value.strip_suffix("]]"))
- {
- if let Some(path) = index.resolve(current_directory, target).path {
- lines.extend(render_embedded_note(
- vault_root,
- &path,
- index,
- depth + 1,
- visiting,
- ));
- } else {
- lines.push(format!("▧ {target}"));
- }
- } else if in_fence {
- lines.push(line.trim_end_matches(['\r', '\n']).to_string());
- } else {
- lines.push(clean_embedded_markdown_line(trimmed));
- }
+ preview.lines.push("…".into());
+ return;
+ }
+ let key = relative_path.to_ascii_lowercase();
+ if !visiting.insert(key.clone()) {
+ preview.lines.push(format!("↻ {relative_path} (cycle)"));
+ return;
+ }
+ let allowance = budget.bytes.min(1024 * 1024);
+ let mut consumed = 0;
+ let bytes = crate::bounded_file::read_in_root(
+ vault_root,
+ Path::new(relative_path),
+ allowance,
+ &mut consumed,
+ );
+ budget.bytes = budget.bytes.saturating_sub(consumed);
+ let source = match bytes {
+ Ok(bytes) => String::from_utf8(bytes).ok(),
+ Err(error) if error.is::<crate::bounded_file::ByteBudgetExceeded>() => {
+ visiting.remove(&key);
+ preview.truncate(EmbedTruncation::Bytes);
+ return;
+ }
+ Err(_) => None,
+ };
+ if let Some(source) = source {
+ let frontmatter = parse_frontmatter(&source).map(|(range, _)| range);
+ let current_directory = Path::new(relative_path)
+ .parent()
+ .unwrap_or_else(|| Path::new(""));
+ let first_line = preview.lines.len();
+ let mut source_offset = 0usize;
+ let mut in_fence = false;
+ for line in source.split_inclusive('\n') {
+ let line_end = source_offset + line.len();
+ if frontmatter
+ .as_ref()
+ .is_some_and(|range| source_offset < range.end && range.start < line_end)
+ {
+ source_offset = line_end;
+ continue;
+ }
+ let trimmed = line.trim_end_matches(['\r', '\n']).trim();
+ if trimmed.starts_with("```") || trimmed.starts_with("~~~") {
+ in_fence = !in_fence;
source_offset = line_end;
+ continue;
}
- while lines.last().is_some_and(|line| line.is_empty()) {
- lines.pop();
+ if preview.lines.len() == MAX_EMBED_CONTENT_LINES {
+ preview.truncate(EmbedTruncation::Lines);
+ break;
}
- lines
- })
- .unwrap_or_else(|_| vec![format!("Missing embed: {relative_path}")]);
- visiting.remove(&relative_path.to_ascii_lowercase());
- result
+ if let Some(target) = trimmed
+ .strip_prefix("![[")
+ .and_then(|v| v.strip_suffix("]]"))
+ {
+ if let Some(path) = index.resolve(current_directory, target).path {
+ render_embedded_note_into(
+ vault_root,
+ &path,
+ index,
+ depth + 1,
+ visiting,
+ budget,
+ preview,
+ );
+ } else {
+ preview.lines.push(format!("▧ {target}"));
+ }
+ } else if in_fence {
+ preview
+ .lines
+ .push(line.trim_end_matches(['\r', '\n']).to_string());
+ } else {
+ preview.lines.push(clean_embedded_markdown_line(trimmed));
+ }
+ source_offset = line_end;
+ }
+ while preview.lines.len() > first_line
+ && preview.lines.last().is_some_and(|line| line.is_empty())
+ {
+ preview.lines.pop();
+ }
+ } else {
+ preview
+ .lines
+ .push(format!("Unable to read embed: {relative_path}"));
+ }
+ visiting.remove(&key);
}
fn clean_embedded_markdown_line(line: &str) -> String {
@@ -1741,3 +1910,222 @@ mod tests {
);
}
}
+
+#[cfg(test)]
+mod embed_budget_tests {
+ use super::*;
+ #[test]
+ fn repeated_empty_children_and_multiple_roots_share_a_budget() {
+ let dir = tempfile::tempdir().unwrap();
+ std::fs::write(dir.path().join("leaf.md"), "").unwrap();
+ std::fs::write(dir.path().join("middle.md"), "![[leaf]]\n".repeat(100)).unwrap();
+ std::fs::write(dir.path().join("root.md"), "![[middle]]\n".repeat(100)).unwrap();
+ let index =
+ VaultLinkIndex::new(vec!["leaf.md".into(), "middle.md".into(), "root.md".into()]);
+ let mut budget = EmbedBudget::default();
+ let mut visiting = HashSet::new();
+ let _ = render_embedded_note(dir.path(), "root.md", &index, 0, &mut visiting, &mut budget);
+ assert_eq!(budget.visits, 0);
+ let bytes = budget.bytes;
+ let _ = render_embedded_note(dir.path(), "root.md", &index, 0, &mut visiting, &mut budget);
+ assert_eq!(budget.bytes, bytes);
+ assert!(visiting.is_empty());
+ }
+ #[test]
+ fn each_top_level_embed_keeps_200_content_lines_and_its_own_notice() {
+ let dir = tempfile::tempdir().unwrap();
+ std::fs::write(dir.path().join("first.md"), "line\n".repeat(201)).unwrap();
+ std::fs::write(dir.path().join("second.md"), "Hello\n").unwrap();
+ let mut projection = MarkdownProjection::parse("![[first]]\n![[second]]\n");
+ projection.resolve_vault_links(dir.path(), "Home.md");
+ let rows: Vec<_> = projection
+ .objects
+ .iter()
+ .filter_map(|object| match object {
+ ProjectedObject::WikiLink { rendered_lines, .. } => Some(rendered_lines),
+ _ => None,
+ })
+ .collect();
+ assert_eq!(rows[0].len(), 201);
+ assert!(rows[0][..200].iter().all(|line| line == "line"));
+ assert_eq!(rows[0].last().unwrap(), EMBED_BUDGET_NOTICE);
+ assert_eq!(rows[1], &vec!["Hello".to_string()]);
+ }
+
+ #[test]
+ fn byte_cap_notices_preserve_unread_budget_for_smaller_embeds() {
+ let dir = tempfile::tempdir().unwrap();
+ std::fs::write(dir.path().join("large.md"), "Hello\n").unwrap();
+ std::fs::write(dir.path().join("small.md"), "x\n").unwrap();
+ let index = VaultLinkIndex::new(vec!["large.md".into(), "small.md".into()]);
+ let mut budget = EmbedBudget {
+ bytes: 5,
+ ..Default::default()
+ };
+ let mut visiting = HashSet::new();
+ let too_large = render_embedded_note(
+ dir.path(),
+ "large.md",
+ &index,
+ 0,
+ &mut visiting,
+ &mut budget,
+ );
+ assert_eq!(too_large.lines, vec![EMBED_BUDGET_NOTICE]);
+ assert_eq!(too_large.truncation, Some(EmbedTruncation::Bytes));
+ assert_eq!(budget.bytes, 5);
+ assert!(visiting.is_empty());
+ assert_eq!(
+ render_embedded_note(
+ dir.path(),
+ "small.md",
+ &index,
+ 0,
+ &mut visiting,
+ &mut budget
+ )
+ .lines,
+ vec!["x"]
+ );
+ assert_eq!(budget.bytes, 3);
+ assert_eq!(budget.visits, 254);
+ // Cache reuse must charge the same content before cloning and must not
+ // reserve a whole file's allowance on rejection.
+ let copied = budget.copy_cached(&["abc".into()], None);
+ assert_eq!(copied.lines, vec!["abc"]);
+ assert_eq!(budget.bytes, 0);
+ let exhausted = budget.copy_cached(&["x".into()], None);
+ assert_eq!(exhausted.truncation, Some(EmbedTruncation::Bytes));
+ }
+
+ #[test]
+ fn missing_embeds_spend_visits_but_not_unread_bytes() {
+ let dir = tempfile::tempdir().unwrap();
+ std::fs::write(dir.path().join("good.md"), "Hello\n").unwrap();
+ std::fs::write(dir.path().join("invalid.md"), [0xff, 0xfe]).unwrap();
+ let index = VaultLinkIndex::new(vec!["good.md".into(), "invalid.md".into()]);
+ let mut budget = EmbedBudget::default();
+ let bytes = budget.bytes;
+ for _ in 0..4 {
+ let missing = render_embedded_note(
+ dir.path(),
+ "missing.md",
+ &index,
+ 0,
+ &mut HashSet::new(),
+ &mut budget,
+ );
+ assert_eq!(missing.truncation, None);
+ assert_eq!(missing.lines, vec!["Unable to read embed: missing.md"]);
+ }
+ assert_eq!(budget.bytes, bytes);
+ assert_eq!(budget.visits, 252);
+ assert_eq!(
+ render_embedded_note(
+ dir.path(),
+ "good.md",
+ &index,
+ 0,
+ &mut HashSet::new(),
+ &mut budget
+ )
+ .lines,
+ vec!["Hello"]
+ );
+ assert_eq!(budget.bytes, bytes - 6);
+ render_embedded_note(
+ dir.path(),
+ "invalid.md",
+ &index,
+ 0,
+ &mut HashSet::new(),
+ &mut budget,
+ );
+ assert_eq!(
+ budget.bytes,
+ bytes - 8,
+ "invalid UTF-8 still consumed payload"
+ );
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn backslash_filename_cannot_turn_a_vault_embed_into_an_absolute_read() {
+ let vault = tempfile::tempdir().unwrap();
+ let outside = tempfile::tempdir().unwrap();
+ let secret = outside.path().join("outside.md");
+ std::fs::write(&secret, "PRIVATE_TEST_MARKER").unwrap();
+ let alias = secret.to_string_lossy().replace('/', "\\");
+ std::fs::write(vault.path().join(alias), "decoy").unwrap();
+ let mut projection = MarkdownProjection::parse("![[outside]]");
+ projection.resolve_vault_links(vault.path(), "Home.md");
+ let rendered: Vec<_> = projection
+ .objects
+ .iter()
+ .filter_map(|object| match object {
+ ProjectedObject::WikiLink { rendered_lines, .. } => Some(rendered_lines),
+ _ => None,
+ })
+ .flatten()
+ .collect();
+ assert!(rendered
+ .iter()
+ .any(|line| line.contains("Unable to read embed")));
+ assert!(!rendered
+ .iter()
+ .any(|line| line.contains("PRIVATE_TEST_MARKER")));
+ }
+
+ #[test]
+ fn normal_nested_notes_render_but_absolute_aliases_cannot_read_outside() {
+ let dir = tempfile::tempdir().unwrap();
+ std::fs::write(dir.path().join("child.md"), "# Hello\n").unwrap();
+ std::fs::write(dir.path().join("root.md"), "![[child]]\n").unwrap();
+ let outside = tempfile::tempdir().unwrap();
+ let secret = outside.path().join("outside.md");
+ std::fs::write(&secret, "PRIVATE_TEST_MARKER").unwrap();
+ let index = VaultLinkIndex::new(vec![
+ "child.md".into(),
+ "root.md".into(),
+ secret.to_string_lossy().into_owned(),
+ ]);
+ let mut budget = EmbedBudget::default();
+ assert_eq!(
+ render_embedded_note(
+ dir.path(),
+ "root.md",
+ &index,
+ 0,
+ &mut HashSet::new(),
+ &mut budget
+ )
+ .lines,
+ vec!["Hello"]
+ );
+ std::fs::write(dir.path().join("child.md"), "nested\n".repeat(201)).unwrap();
+ let nested = render_embedded_note(
+ dir.path(),
+ "root.md",
+ &index,
+ 0,
+ &mut HashSet::new(),
+ &mut budget,
+ );
+ assert_eq!(nested.lines.len(), 201);
+ assert!(nested.lines[..200].iter().all(|line| line == "nested"));
+ assert_eq!(nested.truncation, Some(EmbedTruncation::Lines));
+ let path = index.resolve(Path::new(""), "outside").path.unwrap();
+ let result = render_embedded_note(
+ dir.path(),
+ &path,
+ &index,
+ 0,
+ &mut HashSet::new(),
+ &mut budget,
+ );
+ assert!(!result
+ .lines
+ .iter()
+ .any(|s| s.contains("PRIVATE_TEST_MARKER")));
+ }
+}
diff --git a/wezterm-gui/src/termwindow/ui/right_sidebar.rs b/wezterm-gui/src/termwindow/ui/right_sidebar.rs
index 098e0259..38842c96 100644
--- a/wezterm-gui/src/termwindow/ui/right_sidebar.rs
+++ b/wezterm-gui/src/termwindow/ui/right_sidebar.rs
@@ -18622,15 +18622,8 @@ fn remote_preview_from_bytes(
}
fn load_file_preview_image(path: &Path) -> anyhow::Result<RightSidebarFilePreviewImage> {
- let metadata = fs::metadata(path).with_context(|| format!("stat {}", path.display()))?;
- if metadata.len() > FILE_PREVIEW_IMAGE_MAX_BYTES as u64 {
- anyhow::bail!(
- "image is larger than {} MiB",
- FILE_PREVIEW_IMAGE_MAX_BYTES / 1024 / 1024
- );
- }
-
- let bytes = fs::read(path).with_context(|| format!("read {}", path.display()))?;
+ let bytes = crate::bounded_file::read_regular(path, FILE_PREVIEW_IMAGE_MAX_BYTES)?;
+ let encoded_bytes = bytes.len();
let image_data = ImageDataType::EncodedFile(bytes);
// `dimensions()` only reads the header, so this is cheap and lets us reject
// decompression bombs *before* `cached_image` decodes the full RGBA bitmap.
@@ -18645,7 +18638,7 @@ fn load_file_preview_image(path: &Path) -> anyhow::Result<RightSidebarFilePrevie
data: Arc::new(ImageData::with_data(image_data)),
width,
height,
- encoded_bytes: metadata.len() as usize,
+ encoded_bytes,
})
}