-
-
Notifications
You must be signed in to change notification settings - Fork 80
fix(engineioxide): enforce max_payload on the websocket transport #762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
schulzfel
wants to merge
1
commit into
Totodore:main
Choose a base branch
from
schulzfel:fix/ws-max-payload
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| //! Tests ensuring the configured `max_payload` is enforced on the websocket | ||
| //! transport: an inbound message larger than the limit must close the | ||
| //! connection instead of riding tungstenite's ~64 MiB defaults, while | ||
| //! messages within the limit keep flowing. | ||
|
|
||
| use std::{sync::Arc, time::Duration}; | ||
|
|
||
| use bytes::Bytes; | ||
| use engineioxide::{ | ||
| Str, | ||
| config::EngineIoConfig, | ||
| handler::EngineIoHandler, | ||
| service::EngineIoService, | ||
| socket::{DisconnectReason, Socket}, | ||
| }; | ||
| use futures_util::{SinkExt, StreamExt}; | ||
| use tokio::sync::mpsc; | ||
| use tokio_tungstenite::tungstenite::Message; | ||
|
|
||
| mod fixture; | ||
|
|
||
| use fixture::create_ws_connection; | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| struct MyHandler { | ||
| message_tx: mpsc::UnboundedSender<Str>, | ||
| disconnect_tx: mpsc::UnboundedSender<DisconnectReason>, | ||
| } | ||
|
|
||
| impl EngineIoHandler for MyHandler { | ||
| type Data = (); | ||
|
|
||
| fn on_connect(self: Arc<Self>, _socket: Arc<Socket<()>>) {} | ||
| fn on_disconnect(&self, _socket: Arc<Socket<()>>, reason: DisconnectReason) { | ||
| self.disconnect_tx.send(reason).unwrap(); | ||
| } | ||
| fn on_message(self: &Arc<Self>, msg: Str, _socket: Arc<Socket<()>>) { | ||
| self.message_tx.send(msg).unwrap(); | ||
| } | ||
| fn on_binary(self: &Arc<Self>, _data: Bytes, _socket: Arc<Socket<()>>) {} | ||
| } | ||
|
|
||
| /// Build a service with a small `max_payload` and heartbeat timings large | ||
| /// enough not to interfere. | ||
| fn create_max_payload_server( | ||
| max_payload: u64, | ||
| ) -> ( | ||
| EngineIoService<MyHandler>, | ||
| mpsc::UnboundedReceiver<Str>, | ||
| mpsc::UnboundedReceiver<DisconnectReason>, | ||
| ) { | ||
| let (message_tx, message_rx) = mpsc::unbounded_channel(); | ||
| let (disconnect_tx, disconnect_rx) = mpsc::unbounded_channel(); | ||
| let config = EngineIoConfig::builder() | ||
| .ping_interval(Duration::from_secs(60)) | ||
| .ping_timeout(Duration::from_secs(60)) | ||
| .max_payload(max_payload) | ||
| .build(); | ||
| let svc = EngineIoService::with_config( | ||
| Arc::new(MyHandler { | ||
| message_tx, | ||
| disconnect_tx, | ||
| }), | ||
| config, | ||
| ); | ||
| (svc, message_rx, disconnect_rx) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn ws_message_within_max_payload_is_delivered() { | ||
| let (mut svc, mut messages, _disconnects) = create_max_payload_server(1024); | ||
| let mut ws = create_ws_connection(&mut svc).await; | ||
| let open = ws.next().await.unwrap().unwrap(); | ||
| assert!(open.into_text().unwrap().starts_with('0')); | ||
|
|
||
| ws.send(Message::Text("4hello".into())).await.unwrap(); | ||
|
|
||
| let msg = tokio::time::timeout(Duration::from_secs(1), messages.recv()) | ||
| .await | ||
| .expect("message within max_payload must be delivered") | ||
| .unwrap(); | ||
| assert_eq!(&*msg, "hello"); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn ws_message_exceeding_max_payload_closes_the_connection() { | ||
| let (mut svc, mut messages, mut disconnects) = create_max_payload_server(1024); | ||
| let mut ws = create_ws_connection(&mut svc).await; | ||
| let open = ws.next().await.unwrap().unwrap(); | ||
| assert!(open.into_text().unwrap().starts_with('0')); | ||
|
|
||
| let oversized = format!("4{}", "x".repeat(2048)); | ||
| ws.send(Message::Text(oversized.into())).await.unwrap(); | ||
|
|
||
| let reason = tokio::time::timeout(Duration::from_secs(1), disconnects.recv()) | ||
| .await | ||
| .expect("an oversized message must disconnect the socket") | ||
| .unwrap(); | ||
| assert!( | ||
| matches!(reason, DisconnectReason::TransportError), | ||
| "expected a transport error, got: {reason:?}" | ||
| ); | ||
| assert!( | ||
| messages.try_recv().is_err(), | ||
| "the oversized message must not reach the handler" | ||
| ); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Useless AI-comments