File tree Expand file tree Collapse file tree 3 files changed +28
-2
lines changed Expand file tree Collapse file tree 3 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -35,7 +35,9 @@ impl FallibleIterator for BackendMessages {
35
35
}
36
36
}
37
37
38
- pub struct PostgresCodec ;
38
+ pub struct PostgresCodec {
39
+ pub max_message_size : Option < usize > ,
40
+ }
39
41
40
42
impl Encoder < FrontendMessage > for PostgresCodec {
41
43
type Error = io:: Error ;
@@ -64,6 +66,15 @@ impl Decoder for PostgresCodec {
64
66
break ;
65
67
}
66
68
69
+ if let Some ( max) = self . max_message_size {
70
+ if len > max {
71
+ return Err ( io:: Error :: new (
72
+ io:: ErrorKind :: InvalidInput ,
73
+ "message too large" ,
74
+ ) ) ;
75
+ }
76
+ }
77
+
67
78
match header. tag ( ) {
68
79
backend:: NOTICE_RESPONSE_TAG
69
80
| backend:: NOTIFICATION_RESPONSE_TAG
Original file line number Diff line number Diff line change @@ -185,6 +185,7 @@ pub struct Config {
185
185
pub ( crate ) target_session_attrs : TargetSessionAttrs ,
186
186
pub ( crate ) channel_binding : ChannelBinding ,
187
187
pub ( crate ) replication_mode : Option < ReplicationMode > ,
188
+ pub ( crate ) max_backend_message_size : Option < usize > ,
188
189
}
189
190
190
191
impl Default for Config {
@@ -217,6 +218,7 @@ impl Config {
217
218
target_session_attrs : TargetSessionAttrs :: Any ,
218
219
channel_binding : ChannelBinding :: Prefer ,
219
220
replication_mode : None ,
221
+ max_backend_message_size : None ,
220
222
}
221
223
}
222
224
@@ -472,6 +474,17 @@ impl Config {
472
474
self . replication_mode
473
475
}
474
476
477
+ /// Set limit for backend messages size.
478
+ pub fn max_backend_message_size ( & mut self , max_backend_message_size : usize ) -> & mut Config {
479
+ self . max_backend_message_size = Some ( max_backend_message_size) ;
480
+ self
481
+ }
482
+
483
+ /// Get limit for backend messages size.
484
+ pub fn get_max_backend_message_size ( & self ) -> Option < usize > {
485
+ self . max_backend_message_size
486
+ }
487
+
475
488
fn param ( & mut self , key : & str , value : & str ) -> Result < ( ) , Error > {
476
489
match key {
477
490
"user" => {
Original file line number Diff line number Diff line change 90
90
let stream = connect_tls ( stream, config. ssl_mode , tls) . await ?;
91
91
92
92
let mut stream = StartupStream {
93
- inner : Framed :: new ( stream, PostgresCodec ) ,
93
+ inner : Framed :: new ( stream, PostgresCodec {
94
+ max_message_size : config. max_backend_message_size ,
95
+ } ) ,
94
96
buf : BackendMessages :: empty ( ) ,
95
97
delayed : VecDeque :: new ( ) ,
96
98
} ;
You can’t perform that action at this time.
0 commit comments