Kaynağa Gözat

Limit size of documents to 100 KB

Eric Zhang 4 yıl önce
ebeveyn
işleme
9a0692f9ff
2 değiştirilmiş dosya ile 42 ekleme ve 0 silme
  1. 6 0
      rustpad-server/src/rustpad.rs
  2. 36 0
      rustpad-server/tests/stress.rs

+ 6 - 0
rustpad-server/src/rustpad.rs

@@ -240,6 +240,12 @@ impl Rustpad {
         for history_op in &state.operations[revision..] {
             operation = operation.transform(&history_op.operation)?.0;
         }
+        if operation.target_len() > 100000 {
+            bail!(
+                "target length {} is greater than 100 KB maximum",
+                operation.target_len()
+            );
+        }
         let new_text = operation.apply(&state.text)?;
         let mut state = RwLockUpgradableReadGuard::upgrade(state);
         state.operations.push(UserOperation { id, operation });

+ 36 - 0
rustpad-server/tests/stress.rs

@@ -70,3 +70,39 @@ async fn test_lost_wakeups() -> Result<()> {
 
     Ok(())
 }
+
+#[tokio::test]
+async fn test_large_document() -> Result<()> {
+    pretty_env_logger::try_init().ok();
+    let filter = server();
+
+    expect_text(&filter, "stress", "").await;
+
+    let mut client = connect(&filter, "stress").await?;
+    let msg = client.recv().await?;
+    assert_eq!(msg, json!({ "Identity": 0 }));
+
+    let mut operation = OperationSeq::default();
+    operation.insert(&"a".repeat(5000));
+    let msg = json!({
+        "Edit": {
+            "revision": 0,
+            "operation": operation
+        }
+    });
+    client.send(&msg).await;
+    client.recv().await?;
+
+    let mut operation = OperationSeq::default();
+    operation.insert(&"a".repeat(500000));
+    let msg = json!({
+        "Edit": {
+            "revision": 0,
+            "operation": operation
+        }
+    });
+    client.send(&msg).await;
+    client.recv_closed().await?;
+
+    Ok(())
+}