Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(ipp): preserve printer group metadata (#8828)

Roni Balthazar3a870ca3ce41
diff --git a/contributors/people.toml b/contributors/people.toml
index 4be7a8803..2e2f94518 100644
--- a/contributors/people.toml
+++ b/contributors/people.toml
@@ -252,6 +252,9 @@ sync_github_name = true
[people.benandez]
real_name = "Martin Ball"
+[people.ronibalthazar]
+sync_github_name = true
+
[people.batuhangozegu]
real_name = "Batuhan Gözegü"
sync_github_name = false
diff --git a/crates/ipp/src/lib.rs b/crates/ipp/src/lib.rs
index bbf6efffe..7fc653286 100644
--- a/crates/ipp/src/lib.rs
+++ b/crates/ipp/src/lib.rs
@@ -323,56 +323,31 @@ pub struct Reply {
pub attributes: Vec<Attribute>,
}
+/// The attribute range for one group delimiter in a parsed message. Ranges are half-open and
+/// include empty groups, so repeated group tags remain distinct.
+#[derive(Debug, Clone, Copy)]
+struct GroupRange {
+ tag: u8,
+ start: usize,
+ end: usize,
+}
+
+/// Parsed message details kept private so the public flat `Reply` API stays source-compatible.
+struct ParsedMessage {
+ reply: Reply,
+ end: usize,
+ groups: Vec<GroupRange>,
+}
+
impl Reply {
/// Parses an IPP message (RFC 8010 §3): header, then groups of attributes until the end tag.
pub fn parse(b: &[u8]) -> Result<Reply> {
- Self::parse_at(b).map(|(r, _)| r)
+ parse_message(b).map(|parsed| parsed.reply)
}
/// Parses a message and returns where its attributes end (document data may follow).
fn parse_at(b: &[u8]) -> Result<(Reply, usize)> {
- let bad = |m: &str| Error::Protocol(m.to_string());
- if b.len() < 9 {
- return Err(bad("reply shorter than an IPP header"));
- }
- let status = u16::from_be_bytes([b[2], b[3]]);
- let request_id = u32::from_be_bytes([b[4], b[5], b[6], b[7]]);
- let mut pos = 8;
- let mut group = 0u8;
- let mut attributes: Vec<Attribute> = Vec::new();
- loop {
- let tag = *b.get(pos).ok_or_else(|| bad("reply ends before the end-of-attributes tag"))?;
- pos += 1;
- if tag == TAG_END {
- return Ok((Reply { status, request_id, attributes }, pos));
- }
- if tag < 0x10 {
- group = tag;
- continue;
- }
- let name_len =
- usize::from(u16::from_be_bytes([*b.get(pos).ok_or_else(|| bad("cut attribute"))?, *b.get(pos + 1).ok_or_else(|| bad("cut attribute"))?]));
- pos += 2;
- let name = b.get(pos..pos + name_len).ok_or_else(|| bad("cut attribute name"))?;
- pos += name_len;
- let value_len =
- usize::from(u16::from_be_bytes([*b.get(pos).ok_or_else(|| bad("cut attribute"))?, *b.get(pos + 1).ok_or_else(|| bad("cut attribute"))?]));
- pos += 2;
- let value = b.get(pos..pos + value_len).ok_or_else(|| bad("cut attribute value"))?.to_vec();
- pos += value_len;
- if name_len == 0 {
- // An additional value of the previous attribute.
- match attributes.last_mut() {
- Some(last) if last.group == group => last.values.push(value),
- _ => return Err(bad("an additional value with no attribute before it")),
- }
- } else {
- attributes.push(Attribute { group, tag, name: String::from_utf8_lossy(name).into_owned(), values: vec![value] });
- }
- if attributes.len() > MAX_ATTRIBUTES {
- return Err(bad("too many attributes"));
- }
- }
+ parse_message(b).map(|parsed| (parsed.reply, parsed.end))
}
/// The server's own message for an error status, or a name for the code.
@@ -394,8 +369,72 @@ impl Reply {
}
}
-/// POSTs `body` (IPP, with any document data after it) to `path` and returns the reply.
-fn post(endpoint: &Endpoint, path: &str, body: &[u8]) -> Result<Reply> {
+/// Parses one IPP message while preserving each group instance for private consumers that need
+/// object boundaries. The public reply remains a flat list of attributes.
+fn parse_message(b: &[u8]) -> Result<ParsedMessage> {
+ let bad = |m: &str| Error::Protocol(m.to_string());
+ if b.len() < 9 {
+ return Err(bad("reply shorter than an IPP header"));
+ }
+ let status = u16::from_be_bytes([b[2], b[3]]);
+ let request_id = u32::from_be_bytes([b[4], b[5], b[6], b[7]]);
+ let mut pos = 8;
+ let mut group = 0u8;
+ let mut active_group = None;
+ let mut last_attribute_group = None;
+ let mut attributes: Vec<Attribute> = Vec::new();
+ let mut groups: Vec<GroupRange> = Vec::new();
+ loop {
+ let tag = *b.get(pos).ok_or_else(|| bad("reply ends before the end-of-attributes tag"))?;
+ pos += 1;
+ if tag == TAG_END {
+ if let Some(last) = groups.last_mut() {
+ last.end = attributes.len();
+ }
+ return Ok(ParsedMessage { reply: Reply { status, request_id, attributes }, end: pos, groups });
+ }
+ if tag < 0x10 {
+ if let Some(last) = groups.last_mut() {
+ last.end = attributes.len();
+ }
+ if groups.len() >= MAX_ATTRIBUTES {
+ return Err(bad("too many attribute groups"));
+ }
+ group = tag;
+ let group_index = groups.len();
+ groups.push(GroupRange { tag, start: attributes.len(), end: attributes.len() });
+ active_group = Some(group_index);
+ continue;
+ }
+ let name_len =
+ usize::from(u16::from_be_bytes([*b.get(pos).ok_or_else(|| bad("cut attribute"))?, *b.get(pos + 1).ok_or_else(|| bad("cut attribute"))?]));
+ pos += 2;
+ let name = b.get(pos..pos + name_len).ok_or_else(|| bad("cut attribute name"))?;
+ pos += name_len;
+ let value_len =
+ usize::from(u16::from_be_bytes([*b.get(pos).ok_or_else(|| bad("cut attribute"))?, *b.get(pos + 1).ok_or_else(|| bad("cut attribute"))?]));
+ pos += 2;
+ let value = b.get(pos..pos + value_len).ok_or_else(|| bad("cut attribute value"))?.to_vec();
+ pos += value_len;
+ if name_len == 0 {
+ // Additional values can continue an attribute only within this exact group instance.
+ match attributes.last_mut() {
+ Some(last) if last_attribute_group == active_group => last.values.push(value),
+ _ => return Err(bad("an additional value with no attribute before it")),
+ }
+ } else {
+ attributes.push(Attribute { group, tag, name: String::from_utf8_lossy(name).into_owned(), values: vec![value] });
+ last_attribute_group = active_group;
+ }
+ if attributes.len() > MAX_ATTRIBUTES {
+ return Err(bad("too many attributes"));
+ }
+ }
+}
+
+/// POSTs `body` (IPP, with any document data after it) to `path` and returns the parsed reply with
+/// its private group boundaries.
+fn post(endpoint: &Endpoint, path: &str, body: &[u8]) -> Result<ParsedMessage> {
let mut stream = endpoint.connect()?;
let head = format!(
"POST {path} HTTP/1.1\r\nHost: {}\r\nContent-Type: application/ipp\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
@@ -417,9 +456,9 @@ fn post(endpoint: &Endpoint, path: &str, body: &[u8]) -> Result<Reply> {
}));
}
let body = if headers.iter().any(|(k, v)| k == "transfer-encoding" && v.contains("chunked")) { dechunk(body)? } else { body.to_vec() };
- let reply = Reply::parse(&body)?;
- if reply.status >= 0x0400 {
- return Err(Error::Status(reply.status, reply.status_message()));
+ let reply = parse_message(&body)?;
+ if reply.reply.status >= 0x0400 {
+ return Err(Error::Status(reply.reply.status, reply.reply.status_message()));
}
Ok(reply)
}
@@ -563,41 +602,35 @@ pub fn printers(endpoint: &Endpoint) -> Result<Vec<Printer>> {
fn queues(endpoint: &Endpoint, op: u16, is_class: bool) -> Result<Vec<Printer>> {
let mut r = Request::new(op, 1);
r.attr_many(TAG_KEYWORD, "requested-attributes", &["printer-name", "printer-info", "printer-state", "printer-is-accepting-jobs"]);
- let reply = match post(endpoint, "/", &r.end()) {
+ let parsed = match post(endpoint, "/", &r.end()) {
Ok(reply) => reply,
// CUPS answers "not found" ("No destinations added.") when no printer is set up.
Err(Error::Status(0x0406, _)) => return Ok(Vec::new()),
Err(e) => return Err(e),
};
let mut out = Vec::new();
- let mut current: Option<Printer> = None;
- for a in reply.attributes.iter().filter(|a| a.group == TAG_PRINTER_GROUP) {
- match a.name.as_str() {
- "printer-name" => {
- if let Some(p) = current.take() {
- out.push(p);
- }
- current = Some(Printer { name: a.text(), info: String::new(), state: 3, accepting_jobs: true, is_default: false, is_class });
- }
- "printer-info" => {
- if let Some(p) = &mut current {
- p.info = a.text();
- }
- }
- "printer-state" => {
- if let Some(p) = &mut current {
- p.state = a.int().unwrap_or(3).clamp(0, 5) as u32;
- }
- }
- "printer-is-accepting-jobs" => {
- if let Some(p) = &mut current {
- p.accepting_jobs = a.values.first().is_none_or(|v| v.first() != Some(&0));
- }
+ let attributes = &parsed.reply.attributes;
+ for group in parsed.groups.iter().filter(|group| group.tag == TAG_PRINTER_GROUP) {
+ let Some(group_attributes) = attributes.get(group.start..group.end) else {
+ return Err(Error::Protocol("invalid printer attribute group range".into()));
+ };
+ let mut name = None;
+ let mut info = String::new();
+ let mut state = 3;
+ let mut accepting_jobs = true;
+ for a in group_attributes {
+ match a.name.as_str() {
+ "printer-name" if name.is_none() => name = Some(a.text()),
+ "printer-info" => info = a.text(),
+ "printer-state" => state = a.int().unwrap_or(3).clamp(0, 5) as u32,
+ "printer-is-accepting-jobs" => accepting_jobs = a.values.first().is_none_or(|v| v.first() != Some(&0)),
+ _ => {}
}
- _ => {}
+ }
+ if let Some(name) = name.filter(|name| !name.is_empty()) {
+ out.push(Printer { name, info, state, accepting_jobs, is_default: false, is_class });
}
}
- out.extend(current);
Ok(out)
}
@@ -606,7 +639,7 @@ pub fn default_printer(endpoint: &Endpoint) -> Result<Option<String>> {
let mut r = Request::new(OP_CUPS_GET_DEFAULT, 2);
r.attr_many(TAG_KEYWORD, "requested-attributes", &["printer-name"]);
match post(endpoint, "/", &r.end()) {
- Ok(reply) => Ok(reply.attributes.iter().find(|a| a.name == "printer-name").map(Attribute::text).filter(|n| !n.is_empty())),
+ Ok(reply) => Ok(reply.reply.attributes.iter().find(|a| a.name == "printer-name").map(Attribute::text).filter(|n| !n.is_empty())),
// CUPS answers "not found" when no default is set.
Err(Error::Status(0x0406, _)) => Ok(None),
Err(e) => Err(e),
@@ -642,7 +675,7 @@ pub fn print_job(endpoint: &Endpoint, job: &Job<'_>) -> Result<u32> {
}
let mut body = r.end();
body.extend_from_slice(job.document);
- let reply = post(endpoint, &path, &body)?;
+ let reply = post(endpoint, &path, &body)?.reply;
reply
.attributes
.iter()
diff --git a/crates/ipp/src/tests.rs b/crates/ipp/src/tests.rs
index 518c9dfa3..14ddf4384 100644
--- a/crates/ipp/src/tests.rs
+++ b/crates/ipp/src/tests.rs
@@ -137,6 +137,161 @@ fn printers_are_listed_with_state_and_default() {
}
}
+#[test]
+fn printer_attributes_before_name_are_preserved() {
+ let state = 5u32.to_be_bytes();
+ let base: [Attr<'_>; 4] = [
+ (TAG_TEXT, "printer-info", &[b"Studio Printer"]),
+ (TAG_ENUM, "printer-state", &[&state]),
+ (TAG_BOOLEAN, "printer-is-accepting-jobs", &[b"\0"]),
+ (TAG_NAME, "printer-name", &[b"studio"]),
+ ];
+ let body = ipp(0, &[ok_header(), (TAG_PRINTER_GROUP, &base)]);
+ for chunked in [false, true] {
+ let (ep, _) = fake(body.clone(), chunked, 200);
+ let list = printers(&ep).unwrap();
+ assert_eq!(list.len(), 1, "chunked {chunked}");
+ assert_eq!((list[0].name.as_str(), list[0].info.as_str(), list[0].state, list[0].accepting_jobs), ("studio", "Studio Printer", 5, false));
+ assert!(!list[0].is_class);
+ }
+}
+
+#[test]
+fn printer_groups_do_not_share_metadata() {
+ let state_a = 4u32.to_be_bytes();
+ let state_b = 5u32.to_be_bytes();
+ let body = ipp(
+ 0,
+ &[
+ ok_header(),
+ (
+ TAG_PRINTER_GROUP,
+ &[(TAG_NAME, "printer-name", &[b"first"]), (TAG_TEXT, "printer-info", &[b"First printer"]), (TAG_ENUM, "printer-state", &[&state_a])],
+ ),
+ (
+ TAG_PRINTER_GROUP,
+ &[(TAG_TEXT, "printer-info", &[b"Second printer"]), (TAG_ENUM, "printer-state", &[&state_b]), (TAG_NAME, "printer-name", &[b"second"])],
+ ),
+ ],
+ );
+ let (ep, _) = fake(body, false, 200);
+ let list = queues(&ep, OP_CUPS_GET_PRINTERS, false).unwrap();
+ assert_eq!(list.len(), 2);
+ assert_eq!((list[0].name.as_str(), list[0].info.as_str(), list[0].state), ("first", "First printer", 4));
+ assert_eq!((list[1].name.as_str(), list[1].info.as_str(), list[1].state), ("second", "Second printer", 5));
+}
+
+#[test]
+fn printer_attribute_order_does_not_change_results() {
+ let state = 5u32.to_be_bytes();
+ let fields: [Attr<'_>; 4] = [
+ (TAG_TEXT, "printer-info", &[b"Studio Printer"]),
+ (TAG_ENUM, "printer-state", &[&state]),
+ (TAG_BOOLEAN, "printer-is-accepting-jobs", &[b"\0"]),
+ (TAG_NAME, "printer-name", &[b"studio"]),
+ ];
+ let expected = Printer { name: "studio".into(), info: "Studio Printer".into(), state: 5, accepting_jobs: false, is_default: false, is_class: false };
+ let mut permutations = 0;
+ for first in 0..4 {
+ for second in 0..4 {
+ for third in 0..4 {
+ for fourth in 0..4 {
+ let order = [first, second, third, fourth];
+ if order[0] == order[1]
+ || order[0] == order[2]
+ || order[0] == order[3]
+ || order[1] == order[2]
+ || order[1] == order[3]
+ || order[2] == order[3]
+ {
+ continue;
+ }
+ permutations += 1;
+ let ordered = order.iter().map(|index| fields[*index]).collect::<Vec<_>>();
+ let body = ipp(0, &[ok_header(), (TAG_PRINTER_GROUP, &ordered)]);
+ let (ep, _) = fake(body, false, 200);
+ assert_eq!(queues(&ep, OP_CUPS_GET_PRINTERS, false).unwrap(), vec![expected.clone()], "order {order:?}");
+ }
+ }
+ }
+ }
+ assert_eq!(permutations, 24);
+}
+
+#[test]
+fn class_attributes_before_name_are_preserved() {
+ let state = 5u32.to_be_bytes();
+ let body = ipp(
+ 0,
+ &[
+ ok_header(),
+ (
+ TAG_PRINTER_GROUP,
+ &[
+ (TAG_BOOLEAN, "printer-is-accepting-jobs", &[b"\0"]),
+ (TAG_TEXT, "printer-info", &[b"Shared studio"]),
+ (TAG_ENUM, "printer-state", &[&state]),
+ (TAG_NAME, "printer-name", &[b"studio-class"]),
+ ],
+ ),
+ ],
+ );
+ let (ep, rx) = fake(body, true, 200);
+ let classes = queues(&ep, OP_CUPS_GET_CLASSES, true).unwrap();
+ assert_eq!(rx.recv().unwrap().0, OP_CUPS_GET_CLASSES);
+ assert_eq!(classes.len(), 1);
+ assert_eq!(
+ (classes[0].name.as_str(), classes[0].info.as_str(), classes[0].state, classes[0].accepting_jobs, classes[0].is_class),
+ ("studio-class", "Shared studio", 5, false, true)
+ );
+}
+
+#[test]
+fn empty_or_unnamed_groups_do_not_leak_metadata() {
+ let body = ipp(
+ 0,
+ &[
+ ok_header(),
+ (TAG_PRINTER_GROUP, &[(TAG_NAME, "printer-name", &[b"named"])]),
+ (TAG_PRINTER_GROUP, &[]),
+ (TAG_PRINTER_GROUP, &[(TAG_TEXT, "printer-info", &[b"orphan metadata"])]),
+ (TAG_PRINTER_GROUP, &[(TAG_NAME, "printer-name", &[b"next"]), (TAG_ENUM, "printer-state", &[&3u32.to_be_bytes()])]),
+ ],
+ );
+ let (ep, _) = fake(body, false, 200);
+ let list = queues(&ep, OP_CUPS_GET_PRINTERS, false).unwrap();
+ assert_eq!(list.len(), 2);
+ assert_eq!((list[0].name.as_str(), list[0].info.as_str()), ("named", ""));
+ assert_eq!((list[1].name.as_str(), list[1].info.as_str()), ("next", ""));
+}
+
+#[test]
+fn additional_values_do_not_cross_group_boundaries() {
+ let valid = ipp(0, &[ok_header(), (TAG_PRINTER_GROUP, &[(TAG_KEYWORD, "supported", &[b"one", b"two"])])]);
+ let reply = Reply::parse(&valid).unwrap();
+ assert_eq!(reply.attributes.iter().find(|a| a.name == "supported").map(|a| a.values.as_slice()), Some(&[b"one".to_vec(), b"two".to_vec()][..]));
+
+ let mut body = vec![2, 0, 0, 0, 0, 0, 0, 1, TAG_PRINTER_GROUP];
+ body.extend_from_slice(&[TAG_NAME, 0, 12]);
+ body.extend_from_slice(b"printer-name");
+ body.extend_from_slice(&[0, 5]);
+ body.extend_from_slice(b"first");
+ body.extend_from_slice(&[TAG_TEXT, 0, 12]);
+ body.extend_from_slice(b"printer-info");
+ body.extend_from_slice(&[0, 6]);
+ body.extend_from_slice(b"before");
+ body.push(TAG_PRINTER_GROUP);
+ body.extend_from_slice(&[TAG_TEXT, 0, 0, 0, 5]);
+ body.extend_from_slice(b"after");
+ body.extend_from_slice(&[TAG_NAME, 0, 12]);
+ body.extend_from_slice(b"printer-name");
+ body.extend_from_slice(&[0, 6]);
+ body.extend_from_slice(b"second");
+ body.push(TAG_END);
+ let (ep, _) = fake(body, true, 200);
+ assert!(matches!(queues(&ep, OP_CUPS_GET_PRINTERS, false), Err(Error::Protocol(_))));
+}
+
#[test]
fn default_printer_is_read_and_absence_is_not_an_error() {
let (ep, rx) = fake(ipp(0, &[ok_header(), (TAG_PRINTER_GROUP, &[(TAG_NAME, "printer-name", &[b"Office"])])]), false, 200);