0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-10 14:17:49 -04:00

fix(ext/websocket): Close socket on bad string data (#19424)

This commit is contained in:
Matt Mastracci 2023-06-08 12:29:26 -06:00 committed by Bartek Iwańczuk
parent 270ac0775a
commit eb28a37cf8
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750

View file

@ -325,6 +325,7 @@ pub struct ServerWebSocket {
errored: Cell<bool>, errored: Cell<bool>,
closed: Cell<bool>, closed: Cell<bool>,
buffer: Cell<Option<Vec<u8>>>, buffer: Cell<Option<Vec<u8>>>,
string: Cell<Option<String>>,
ws: AsyncRefCell<FragmentCollector<WebSocketStream>>, ws: AsyncRefCell<FragmentCollector<WebSocketStream>>,
tx_lock: AsyncRefCell<()>, tx_lock: AsyncRefCell<()>,
} }
@ -337,6 +338,7 @@ impl ServerWebSocket {
errored: Cell::new(false), errored: Cell::new(false),
closed: Cell::new(false), closed: Cell::new(false),
buffer: Cell::new(None), buffer: Cell::new(None),
string: Cell::new(None),
ws: AsyncRefCell::new(FragmentCollector::new(ws)), ws: AsyncRefCell::new(FragmentCollector::new(ws)),
tx_lock: AsyncRefCell::new(()), tx_lock: AsyncRefCell::new(()),
} }
@ -535,8 +537,7 @@ pub fn op_ws_get_buffer_as_string(
rid: ResourceId, rid: ResourceId,
) -> String { ) -> String {
let resource = state.resource_table.get::<ServerWebSocket>(rid).unwrap(); 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. resource.string.take().unwrap()
String::from_utf8(resource.buffer.take().unwrap()).unwrap_or_default()
} }
#[op] #[op]
@ -585,10 +586,16 @@ pub async fn op_ws_next_event(
}; };
break match val.opcode { break match val.opcode {
OpCode::Text => { OpCode::Text => match String::from_utf8(val.payload) {
resource.buffer.set(Some(val.payload)); Ok(s) => {
resource.string.set(Some(s));
MessageKind::Text as u16 MessageKind::Text as u16
} }
Err(_) => {
resource.set_error(Some("Invalid string data".into()));
MessageKind::Error as u16
}
},
OpCode::Binary => { OpCode::Binary => {
resource.buffer.set(Some(val.payload)); resource.buffer.set(Some(val.payload));
MessageKind::Binary as u16 MessageKind::Binary as u16