@@ -44,7 +44,9 @@ use crate::{
4444 utils:: json:: { convert_array_to_object, flatten:: convert_to_array} ,
4545} ;
4646
47- const IGNORE_HEADERS : [ & str ; 1 ] = [ STREAM_NAME_HEADER_KEY ] ;
47+ const IGNORE_HEADERS : [ & str ; 2 ] = [ STREAM_NAME_HEADER_KEY , LOG_SOURCE_KEY ] ;
48+ const MAX_CUSTOM_FIELDS : usize = 10 ;
49+ const MAX_FIELD_VALUE_LENGTH : usize = 100 ;
4850
4951pub async fn flatten_and_push_logs (
5052 json : Value ,
@@ -160,12 +162,31 @@ pub fn get_custom_fields_from_header(req: HttpRequest) -> HashMap<String, String
160162
161163 // Iterate through headers and add custom fields
162164 for ( header_name, header_value) in req. headers ( ) . iter ( ) {
165+ // Check if we've reached the maximum number of custom fields
166+ if p_custom_fields. len ( ) >= MAX_CUSTOM_FIELDS {
167+ warn ! (
168+ "Maximum number of custom fields ({}) reached, ignoring remaining headers" ,
169+ MAX_CUSTOM_FIELDS
170+ ) ;
171+ break ;
172+ }
173+
163174 let header_name = header_name. as_str ( ) ;
164175 if header_name. starts_with ( "x-p-" ) && !IGNORE_HEADERS . contains ( & header_name) {
165176 if let Ok ( value) = header_value. to_str ( ) {
166177 let key = header_name. trim_start_matches ( "x-p-" ) ;
167178 if !key. is_empty ( ) {
168- p_custom_fields. insert ( key. to_string ( ) , value. to_string ( ) ) ;
179+ // Truncate value if it exceeds the maximum length
180+ let truncated_value = if value. len ( ) > MAX_FIELD_VALUE_LENGTH {
181+ warn ! (
182+ "Header value for '{}' exceeds maximum length, truncating" ,
183+ header_name
184+ ) ;
185+ & value[ ..MAX_FIELD_VALUE_LENGTH ]
186+ } else {
187+ value
188+ } ;
189+ p_custom_fields. insert ( key. to_string ( ) , truncated_value. to_string ( ) ) ;
169190 } else {
170191 warn ! (
171192 "Ignoring header with empty key after prefix: {}" ,
0 commit comments