diff options
author | Matt Mastracci <matthew@mastracci.com> | 2023-06-08 12:29:26 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-08 18:29:26 +0000 |
commit | 1d4c66308c00d56910cf60a4f7b269381c898e38 (patch) | |
tree | d82cef6fb130778eb83f65a56534190b62d197dd /ext/websocket/lib.rs | |
parent | db9482d6880739f4333ce80d9e40f856a0ebefa7 (diff) |
fix(ext/websocket): Close socket on bad string data (#19424)
Diffstat (limited to 'ext/websocket/lib.rs')
-rw-r--r-- | ext/websocket/lib.rs | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/ext/websocket/lib.rs b/ext/websocket/lib.rs index 1df71abaa..bcbec2c5e 100644 --- a/ext/websocket/lib.rs +++ b/ext/websocket/lib.rs @@ -325,6 +325,7 @@ pub struct ServerWebSocket { errored: Cell<bool>, closed: Cell<bool>, buffer: Cell<Option<Vec<u8>>>, + string: Cell<Option<String>>, ws: AsyncRefCell<FragmentCollector<WebSocketStream>>, tx_lock: AsyncRefCell<()>, } @@ -337,6 +338,7 @@ impl ServerWebSocket { errored: Cell::new(false), closed: Cell::new(false), buffer: Cell::new(None), + string: Cell::new(None), ws: AsyncRefCell::new(FragmentCollector::new(ws)), tx_lock: AsyncRefCell::new(()), } @@ -535,8 +537,7 @@ pub fn op_ws_get_buffer_as_string( rid: ResourceId, ) -> String { let resource = state.resource_table.get::<ServerWebSocket>(rid).unwrap(); - // TODO(mmastrac): We won't panic on a bad string, but we return an empty one. - String::from_utf8(resource.buffer.take().unwrap()).unwrap_or_default() + resource.string.take().unwrap() } #[op] @@ -585,10 +586,16 @@ pub async fn op_ws_next_event( }; break match val.opcode { - OpCode::Text => { - resource.buffer.set(Some(val.payload)); - MessageKind::Text as u16 - } + OpCode::Text => match String::from_utf8(val.payload) { + Ok(s) => { + resource.string.set(Some(s)); + MessageKind::Text as u16 + } + Err(_) => { + resource.set_error(Some("Invalid string data".into())); + MessageKind::Error as u16 + } + }, OpCode::Binary => { resource.buffer.set(Some(val.payload)); MessageKind::Binary as u16 |