Skip to content

Commit 3606a3e

Browse files
cite-readerrotty
authored andcommitted
Fix denial-of-service in Message::gets
`Message::gets` now returns None on invalid UTF-8 instead of panicking. The doc for zmq_msg_gets says that "both the property argument and the value shall be NULL-terminated UTF8-strings", but in practice libzmq neither confirms nor enforces this. A buggy or malicious peer can send arbitrary binary garbage, which will be faithfully returned verbatim from zmq_msg_gets. Converting the Result from str::from_utf8 to an Option via .ok() instead of unwrapping saves us from a crash without a client-breaking type change.
1 parent 00a8d7e commit 3606a3e

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/message.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ impl Message {
124124
if value.is_null() {
125125
None
126126
} else {
127-
Some(unsafe { str::from_utf8(ffi::CStr::from_ptr(value).to_bytes()).unwrap() })
127+
// Note: libzmq` does not do UTF-8 validation, even though its doc
128+
// suggest that UTF-8 "shall" be used here. Maybe we should changge
129+
// the API to return a bytes slice.
130+
str::from_utf8(unsafe { ffi::CStr::from_ptr(value) }.to_bytes()).ok()
128131
}
129132
}
130133
}

0 commit comments

Comments
 (0)