mirror of
https://github.com/marcoallegretti/WEFT_OS.git
synced 2026-11-22 08:13:59 +00:00
fix(runtime): keep helper error messages, and send notification bodies as plain text
Discarding wl-copy's output also lost its error message, such as a missing Wayland connection. Its standard error is now kept and read only when it fails, which is always before it leaves its selection server. Other fixes: - An output over the limit is reported as such, instead of as the helper dying of the broken pipe. - The clipboard is read as any text type, since the content is checked as UTF-8 anyway. - Notification bodies are sent with markup escaped, since notification servers may render links and images. - A failed wait for a helper kills and reaps it.
This commit is contained in:
parent
0c9b2c2b0d
commit
6055e0b0a0
2 changed files with 53 additions and 22 deletions
|
|
@ -24,9 +24,11 @@ enum Output {
|
||||||
/// Read: up to the limit from standard output, and standard error for
|
/// Read: up to the limit from standard output, and standard error for
|
||||||
/// the failure message.
|
/// the failure message.
|
||||||
Captured,
|
Captured,
|
||||||
/// Discarded, for a helper that leaves a process running which would
|
/// Standard output discarded and standard error read only when the
|
||||||
/// keep the streams open, as `wl-copy` does to serve the selection.
|
/// helper fails, for a helper that leaves a process running which
|
||||||
Discarded,
|
/// keeps its streams open after succeeding, as `wl-copy` does to
|
||||||
|
/// serve the selection; it fails, if at all, before that.
|
||||||
|
ErrorsOnly,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Runs `command` with `input` on its standard input and returns at most
|
/// Runs `command` with `input` on its standard input and returns at most
|
||||||
|
|
@ -38,18 +40,17 @@ fn run(
|
||||||
output: Output,
|
output: Output,
|
||||||
) -> Result<Vec<u8>, String> {
|
) -> Result<Vec<u8>, String> {
|
||||||
let program = command.get_program().to_string_lossy().into_owned();
|
let program = command.get_program().to_string_lossy().into_owned();
|
||||||
let stream = || match output {
|
|
||||||
Output::Captured => Stdio::piped(),
|
|
||||||
Output::Discarded => Stdio::null(),
|
|
||||||
};
|
|
||||||
let mut child = command
|
let mut child = command
|
||||||
.stdin(if input.is_some() {
|
.stdin(if input.is_some() {
|
||||||
Stdio::piped()
|
Stdio::piped()
|
||||||
} else {
|
} else {
|
||||||
Stdio::null()
|
Stdio::null()
|
||||||
})
|
})
|
||||||
.stdout(stream())
|
.stdout(match output {
|
||||||
.stderr(stream())
|
Output::Captured => Stdio::piped(),
|
||||||
|
Output::ErrorsOnly => Stdio::null(),
|
||||||
|
})
|
||||||
|
.stderr(Stdio::piped())
|
||||||
.spawn()
|
.spawn()
|
||||||
.map_err(|e| format!("cannot run {program}: {e}"))?;
|
.map_err(|e| format!("cannot run {program}: {e}"))?;
|
||||||
if let (Some(mut stdin), Some(input)) = (child.stdin.take(), input) {
|
if let (Some(mut stdin), Some(input)) = (child.stdin.take(), input) {
|
||||||
|
|
@ -93,10 +94,19 @@ fn run(
|
||||||
HELPER_TIMEOUT.as_secs()
|
HELPER_TIMEOUT.as_secs()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
Err(e) => return Err(e.to_string()),
|
Err(e) => {
|
||||||
|
let _ = child.kill();
|
||||||
|
let _ = child.wait();
|
||||||
|
return Err(e.to_string());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let output = stdout.join().unwrap_or_default();
|
let output = stdout.join().unwrap_or_default();
|
||||||
|
// Checked first: a helper whose output is cut off at the limit may die
|
||||||
|
// of the broken pipe.
|
||||||
|
if output.len() > max_output {
|
||||||
|
return Err(format!("{program} returned more than {max_output} bytes"));
|
||||||
|
}
|
||||||
if !status.success() {
|
if !status.success() {
|
||||||
let message = String::from_utf8_lossy(&stderr.join().unwrap_or_default())
|
let message = String::from_utf8_lossy(&stderr.join().unwrap_or_default())
|
||||||
.trim()
|
.trim()
|
||||||
|
|
@ -104,18 +114,16 @@ fn run(
|
||||||
return Err(if message.is_empty() {
|
return Err(if message.is_empty() {
|
||||||
format!("{program} exited with {status}")
|
format!("{program} exited with {status}")
|
||||||
} else {
|
} else {
|
||||||
message
|
format!("{program}: {message}")
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if output.len() > max_output {
|
|
||||||
return Err(format!("{program} returned more than {max_output} bytes"));
|
|
||||||
}
|
|
||||||
Ok(output)
|
Ok(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clipboard_read() -> Result<String, String> {
|
pub fn clipboard_read() -> Result<String, String> {
|
||||||
let mut command = Command::new("wl-paste");
|
let mut command = Command::new("wl-paste");
|
||||||
command.args(["--no-newline", "--type", "text/plain;charset=utf-8"]);
|
// Any text type; the content must still be UTF-8.
|
||||||
|
command.args(["--no-newline", "--type", "text"]);
|
||||||
let data = run(command, None, MAX_CLIPBOARD, Output::Captured)?;
|
let data = run(command, None, MAX_CLIPBOARD, Output::Captured)?;
|
||||||
String::from_utf8(data).map_err(|_| "the clipboard does not hold UTF-8 text".to_owned())
|
String::from_utf8(data).map_err(|_| "the clipboard does not hold UTF-8 text".to_owned())
|
||||||
}
|
}
|
||||||
|
|
@ -130,7 +138,7 @@ pub fn clipboard_write(text: &str) -> Result<(), String> {
|
||||||
command,
|
command,
|
||||||
Some(text.as_bytes().to_vec()),
|
Some(text.as_bytes().to_vec()),
|
||||||
0,
|
0,
|
||||||
Output::Discarded,
|
Output::ErrorsOnly,
|
||||||
)
|
)
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
@ -162,6 +170,12 @@ fn is_icon_name(icon: &str) -> bool {
|
||||||
.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.'))
|
.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.'))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn escape_markup(text: &str) -> String {
|
||||||
|
text.replace('&', "&")
|
||||||
|
.replace('<', "<")
|
||||||
|
.replace('>', ">")
|
||||||
|
}
|
||||||
|
|
||||||
/// Sends a notification attributed to `app_id`.
|
/// Sends a notification attributed to `app_id`.
|
||||||
pub fn notify(app_id: &str, title: &str, body: &str, icon: Option<&str>) -> Result<(), String> {
|
pub fn notify(app_id: &str, title: &str, body: &str, icon: Option<&str>) -> Result<(), String> {
|
||||||
check_notification(title, body, icon)?;
|
check_notification(title, body, icon)?;
|
||||||
|
|
@ -170,7 +184,9 @@ pub fn notify(app_id: &str, title: &str, body: &str, icon: Option<&str>) -> Resu
|
||||||
if let Some(icon) = icon {
|
if let Some(icon) = icon {
|
||||||
command.arg(format!("--icon={icon}"));
|
command.arg(format!("--icon={icon}"));
|
||||||
}
|
}
|
||||||
command.arg("--").arg(title).arg(body);
|
// Notification servers may render markup in the body, including links
|
||||||
|
// and images, so it is sent as plain text.
|
||||||
|
command.arg("--").arg(title).arg(escape_markup(body));
|
||||||
run(command, None, 0, Output::Captured).map(|_| ())
|
run(command, None, 0, Output::Captured).map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -178,6 +194,14 @@ pub fn notify(app_id: &str, title: &str, body: &str, icon: Option<&str>) -> Resu
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn notification_bodies_are_plain_text() {
|
||||||
|
assert_eq!(
|
||||||
|
escape_markup("<a href=\"x\">a & b</a>"),
|
||||||
|
"<a href=\"x\">a & b</a>"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn notifications_are_bounded_and_icons_are_theme_names() {
|
fn notifications_are_bounded_and_icons_are_theme_names() {
|
||||||
assert!(check_notification("t", "b", None).is_ok());
|
assert!(check_notification("t", "b", None).is_ok());
|
||||||
|
|
@ -206,15 +230,15 @@ mod tests {
|
||||||
assert!(started.elapsed() < HELPER_TIMEOUT + Duration::from_secs(2));
|
assert!(started.elapsed() < HELPER_TIMEOUT + Duration::from_secs(2));
|
||||||
|
|
||||||
let mut chatty = Command::new("head");
|
let mut chatty = Command::new("head");
|
||||||
chatty.args(["-c", "2000", "/dev/zero"]);
|
// Far more than a pipe holds, so the cut-off kills the writer.
|
||||||
|
chatty.args(["-c", "10000000", "/dev/zero"]);
|
||||||
assert!(
|
assert!(
|
||||||
run(chatty, None, 1000, Output::Captured)
|
run(chatty, None, 1000, Output::Captured)
|
||||||
.unwrap_err()
|
.unwrap_err()
|
||||||
.contains("more than 1000 bytes")
|
.contains("more than 1000 bytes")
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut echo = Command::new("cat");
|
let echo = Command::new("cat");
|
||||||
echo.stdin(Stdio::piped());
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
run(echo, Some(b"text".to_vec()), 100, Output::Captured).unwrap(),
|
run(echo, Some(b"text".to_vec()), 100, Output::Captured).unwrap(),
|
||||||
b"text"
|
b"text"
|
||||||
|
|
@ -225,9 +249,16 @@ mod tests {
|
||||||
let mut forking = Command::new("sh");
|
let mut forking = Command::new("sh");
|
||||||
forking.args(["-c", "sleep 30 & exit 0"]);
|
forking.args(["-c", "sleep 30 & exit 0"]);
|
||||||
let started = Instant::now();
|
let started = Instant::now();
|
||||||
assert!(run(forking, None, 0, Output::Discarded).is_ok());
|
assert!(run(forking, None, 0, Output::ErrorsOnly).is_ok());
|
||||||
assert!(started.elapsed() < Duration::from_secs(2));
|
assert!(started.elapsed() < Duration::from_secs(2));
|
||||||
|
|
||||||
|
let mut failing = Command::new("sh");
|
||||||
|
failing.args(["-c", "echo no display >&2; exit 1"]);
|
||||||
|
assert_eq!(
|
||||||
|
run(failing, None, 0, Output::ErrorsOnly).unwrap_err(),
|
||||||
|
"sh: no display"
|
||||||
|
);
|
||||||
|
|
||||||
let failing = Command::new("false");
|
let failing = Command::new("false");
|
||||||
assert!(
|
assert!(
|
||||||
run(failing, None, 0, Output::Captured)
|
run(failing, None, 0, Output::Captured)
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,7 @@ Each capability-controlled host import (fetch, notifications, clipboard) checks
|
||||||
| `sys:clipboard:read` / `sys:clipboard:write` | `weft:app/clipboard#read` / `#write` |
|
| `sys:clipboard:read` / `sys:clipboard:write` | `weft:app/clipboard#read` / `#write` |
|
||||||
| `hw:gpu:compute` / `hw:gpu:render` | Not supported; the launch fails |
|
| `hw:gpu:compute` / `hw:gpu:render` | Not supported; the launch fails |
|
||||||
|
|
||||||
Clipboard and notifications run `wl-paste`, `wl-copy` and `notify-send` from the runtime's environment, each bounded to 5 seconds, after which the helper is killed and the call fails. Clipboard text is UTF-8 and at most 1 MiB in either direction. A notification's title is at most 256 bytes and its body 4096; its icon must be an icon theme name, not a path or URL; and it is sent with the app's ID as the application name, so an app cannot pass a notification off as the system's. Where a helper is not installed the call fails with that error.
|
Clipboard and notifications run `wl-paste`, `wl-copy` and `notify-send` from the runtime's environment, each bounded to 5 seconds, after which the helper is killed and the call fails. Clipboard text is UTF-8, of any text type, and at most 1 MiB in either direction. A notification's title is at most 256 bytes and its body 4096, sent as plain text with markup escaped; its icon must be an icon theme name, not a path or URL; and it is sent with the app's ID as the application name, so an app cannot pass a notification off as the system's. Where a helper is not installed the call fails with that error.
|
||||||
|
|
||||||
Fetch does not follow redirects: a redirect is returned to the component as a response, so every destination it requests is checked against its grants. HTTP error statuses are responses too; only policy and transport failures are errors. Only the methods GET, HEAD, POST, PUT, PATCH, DELETE and OPTIONS are accepted, and a component cannot set `Host`, `Content-Length`, `Transfer-Encoding` or other hop-by-hop headers. Connecting times out after 10 seconds and the whole request after 30 seconds, not counting DNS resolution; response bodies are limited to 16 MiB. Fetch requires a runtime built with the `net-fetch` feature.
|
Fetch does not follow redirects: a redirect is returned to the component as a response, so every destination it requests is checked against its grants. HTTP error statuses are responses too; only policy and transport failures are errors. Only the methods GET, HEAD, POST, PUT, PATCH, DELETE and OPTIONS are accepted, and a component cannot set `Host`, `Content-Length`, `Transfer-Encoding` or other hop-by-hop headers. Connecting times out after 10 seconds and the whole request after 30 seconds, not counting DNS resolution; response bodies are limited to 16 MiB. Fetch requires a runtime built with the `net-fetch` feature.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue