mirror of
https://github.com/marcoallegretti/WEFT_OS.git
synced 2026-11-22 08:13:59 +00:00
fix(appd): keep the relay's platform attribute on the function and bound frames exactly
The frame limit constant was inserted between spawn_ipc_relay's doc comment and its cfg(unix) attribute, so the attribute applied to the constant instead. A non-Unix build would have had two definitions of the function. The size is now checked before the line ending, so a frame one byte over the limit is refused even when its line break arrives in the same read. A message that is not UTF-8 ends the relay, as it did before, rather than reaching the page with replacement characters. The test now also covers a message at exactly the limit ending in CRLF.
This commit is contained in:
parent
2cd7ee735b
commit
8c476fdf15
1 changed files with 31 additions and 13 deletions
|
|
@ -33,14 +33,15 @@ impl Drop for IpcRelay {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The longest message from a component, its line break included: the
|
||||||
|
/// runtime refuses longer ones, so a longer line ends the relay.
|
||||||
|
#[cfg(unix)]
|
||||||
|
const MAX_RELAY_FRAME: usize = 64 * 1024 + 2;
|
||||||
|
|
||||||
/// Listens on `socket_path` for the session's component and relays
|
/// Listens on `socket_path` for the session's component and relays
|
||||||
/// newline-delimited messages between it and the returned sender (towards
|
/// newline-delimited messages between it and the returned sender (towards
|
||||||
/// the component) and the broadcast channel (from the component).
|
/// the component) and the broadcast channel (from the component).
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
/// The longest message from a component, its line break included: the
|
|
||||||
/// runtime refuses longer ones, so a longer line ends the relay.
|
|
||||||
const MAX_RELAY_FRAME: usize = 64 * 1024 + 2;
|
|
||||||
|
|
||||||
pub(crate) fn spawn_ipc_relay(
|
pub(crate) fn spawn_ipc_relay(
|
||||||
session_id: u64,
|
session_id: u64,
|
||||||
socket_path: PathBuf,
|
socket_path: PathBuf,
|
||||||
|
|
@ -79,15 +80,18 @@ pub(crate) fn spawn_ipc_relay(
|
||||||
n = limited.read_until(b'\n', &mut frame) => {
|
n = limited.read_until(b'\n', &mut frame) => {
|
||||||
match n {
|
match n {
|
||||||
Ok(0) | Err(_) => break,
|
Ok(0) | Err(_) => break,
|
||||||
Ok(_) if !frame.ends_with(b"\n") => {
|
Ok(_) if frame.len() > MAX_RELAY_FRAME => {
|
||||||
if frame.len() > MAX_RELAY_FRAME {
|
tracing::warn!(session_id, "IPC relay: message over the size limit");
|
||||||
tracing::warn!(session_id, "IPC relay: message over the size limit");
|
break;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// The rest of the message is still to come.
|
||||||
|
Ok(_) if !frame.ends_with(b"\n") => {}
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
let line = std::mem::take(&mut frame);
|
let line = std::mem::take(&mut frame);
|
||||||
let text = String::from_utf8_lossy(&line);
|
let Ok(text) = String::from_utf8(line) else {
|
||||||
|
tracing::warn!(session_id, "IPC relay: message is not UTF-8");
|
||||||
|
break;
|
||||||
|
};
|
||||||
let payload = text.trim_end_matches(['\n', '\r']).to_owned();
|
let payload = text.trim_end_matches(['\n', '\r']).to_owned();
|
||||||
let _ = broadcast.send(Response::IpcMessage { session_id, payload });
|
let _ = broadcast.send(Response::IpcMessage { session_id, payload });
|
||||||
}
|
}
|
||||||
|
|
@ -611,11 +615,25 @@ mod tests {
|
||||||
"{message:?}"
|
"{message:?}"
|
||||||
);
|
);
|
||||||
|
|
||||||
// A line longer than any message ends the relay.
|
// A message at the limit, ending in a carriage return and line
|
||||||
component
|
// feed, arrives whole.
|
||||||
.write_all(&vec![b'x'; super::MAX_RELAY_FRAME + 1])
|
let mut at_limit = vec![b'y'; super::MAX_RELAY_FRAME - 2];
|
||||||
|
at_limit.extend_from_slice(b"\r\n");
|
||||||
|
component.write_all(&at_limit).await.unwrap();
|
||||||
|
let message = tokio::time::timeout(std::time::Duration::from_secs(5), messages.recv())
|
||||||
.await
|
.await
|
||||||
|
.unwrap()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
matches!(&message, crate::ipc::Response::IpcMessage { payload, .. } if payload.len() == super::MAX_RELAY_FRAME - 2),
|
||||||
|
"a message at the limit did not arrive whole"
|
||||||
|
);
|
||||||
|
|
||||||
|
// A line longer than any message ends the relay, even when its line
|
||||||
|
// break comes in the same read.
|
||||||
|
let mut over = vec![b'x'; super::MAX_RELAY_FRAME];
|
||||||
|
over.push(b'\n');
|
||||||
|
component.write_all(&over).await.unwrap();
|
||||||
tokio::time::timeout(std::time::Duration::from_secs(5), relay.ended())
|
tokio::time::timeout(std::time::Duration::from_secs(5), relay.ended())
|
||||||
.await
|
.await
|
||||||
.expect("the relay did not end on an overlong line");
|
.expect("the relay did not end on an overlong line");
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue