| 110 | } |
| 111 | |
| 112 | pub async fn parse_startup_message(&self, cursor: &mut Cursor<&[u8]>) -> Result<StartupMessage, ParseError> { |
| 113 | let length = cursor.read_u32::<BigEndian>()?; |
| 114 | |
| 115 | if length > self.max_message_size { |
| 116 | return Err(ParseError::MessageTooLarge(length)); |
| 117 | } |
| 118 | |
| 119 | let major = cursor.read_u16::<BigEndian>()? as i32; |
| 120 | let minor = cursor.read_u16::<BigEndian>()? as i32; |
| 121 | let protocol_version = (major << 16) | minor; |
| 122 | |
| 123 | // Validate protocol version |
| 124 | if major != 3 || minor != 0 { |
| 125 | events::protocol_violation(None, &format!("Unsupported protocol version: {}.{}", major, minor)); |
| 126 | return Err(ParseError::ProtocolViolation(format!("Unsupported protocol version: {}.{}", major, minor))); |
| 127 | } |
| 128 | |
| 129 | let mut parameters = HashMap::new(); |
| 130 | let mut param_count = 0; |
| 131 | |
| 132 | // Parse parameter key-value pairs |
| 133 | loop { |
| 134 | param_count += 1; |
| 135 | if param_count > self.max_param_count { |
| 136 | return Err(ParseError::ProtocolViolation("Too many parameters".to_string())); |
| 137 | } |
| 138 | |
| 139 | let key = self.read_cstring(cursor).await?; |
| 140 | if key.is_empty() { |
| 141 | break; // End of parameters |
| 142 | } |
| 143 | |
| 144 | let value = self.read_cstring(cursor).await?; |
| 145 | |
| 146 | // Security: validate parameter names and values |
| 147 | if key.len() > 100 || value.len() > 1000 { |
| 148 | events::protocol_violation(None, "Parameter name or value too long"); |
| 149 | return Err(ParseError::ProtocolViolation("Parameter too long".to_string())); |
| 150 | } |
| 151 | |
| 152 | parameters.insert(key, value); |
| 153 | } |
| 154 | |
| 155 | Ok(StartupMessage { |
| 156 | protocol_version, |
| 157 | parameters, |
| 158 | }) |
| 159 | } |
| 160 | |
| 161 | pub async fn parse_auth_request(&self, cursor: &mut Cursor<&[u8]>) -> Result<AuthenticationRequest, ParseError> { |
| 162 | let auth_type = cursor.read_u32::<BigEndian>()?; |