@@ -62,6 +62,14 @@ function connectionLostDetection(type) {
6262 }
6363}
6464
65+ // Utility function to sanitize strings for safe logging
66+ function sanitizeForLog ( input ) {
67+ if ( typeof input !== 'string' ) return '' ;
68+ return input
69+ . replace ( / [ \n \r \t ] / g, ' ' ) // Replace newlines, carriage returns, and tabs with space
70+ . replace ( / [ \u001b \u009b ] [ [ ( ) # ; ? ] * (?: [ 0 - 9 ] { 1 , 4 } (?: ; [ 0 - 9 ] { 0 , 4 } ) * ) ? [ 0 - 9 A - O R Z c f - n q r y = > < ] / g, '' ) ; // Remove ANSI escape codes
71+ }
72+
6573setInterval ( connectionLostDetection , 3000 ) ;
6674
6775// init websocket to hyperion and bind socket events to jquery events of $(hyperion) object
@@ -116,7 +124,8 @@ function initWebSocket() {
116124 const success = response . success ;
117125 const cmd = response . command ;
118126 const tan = response . tan
119- if ( success || typeof ( success ) == "undefined" ) {
127+
128+ if ( success || typeof ( success ) === "undefined" ) {
120129 $ ( window . hyperion ) . trigger ( { type : "cmd-" + cmd , response : response } ) ;
121130 }
122131 else
@@ -127,37 +136,49 @@ function initWebSocket() {
127136 window . location . reload ( ) ;
128137 } else {
129138 const errorData = Array . isArray ( response . errorData ) ? response . errorData : [ ] ;
130- console . log ( "[window.websocket::onmessage] " , error , ", Description:" , errorData ) ;
139+
140+ const safeError = sanitizeForLog ( error ) ;
141+ const safeErrorData = errorData . map ( ( item ) => sanitizeForLog ( item . description || "" ) ) ;
142+
143+ console . log ( "[window.websocket::onmessage] " , safeError , ", Description:" , safeErrorData ) ;
144+
131145 $ ( window . hyperion ) . trigger ( {
132146 type : "error" ,
133147 reason : {
134148 cmd : cmd ,
135- message : error ,
136- details : errorData . map ( ( item ) => item . description || "" )
149+ message : safeError ,
150+ details : safeErrorData
137151 }
138152 } ) ;
139153 }
140154 }
141155 }
142156 catch ( exception_error ) {
143- console . log ( "[window.websocket::onmessage] " , exception_error ) ;
157+ const safeExceptionMessage = sanitizeForLog ( exception_error . message || 'Unknown error' ) ;
158+ const safeExceptionStack = sanitizeForLog ( exception_error . stack || '' ) ;
159+
160+ console . log ( "[window.websocket::onmessage] " , safeExceptionMessage ) ;
161+
144162 $ ( window . hyperion ) . trigger ( {
145163 type : "error" ,
146164 reason : {
147- message : $ . i18n ( "ws_processing_exception" ) + ": " + exception_error . message ,
148- details : [ exception_error . stack ]
165+ message : $ . i18n ( "ws_processing_exception" ) + ": " + safeExceptionMessage ,
166+ details : [ safeExceptionStack ]
149167 }
150168 } ) ;
151169 }
152170 } ;
153171
154172 window . websocket . onerror = function ( error ) {
155- console . log ( "[window.websocket::onerror] " , error ) ;
173+ const safeError = sanitizeForLog ( error ?. message || String ( error ) ) ;
174+
175+ console . log ( "[window.websocket::onerror] " , safeError ) ;
176+
156177 $ ( window . hyperion ) . trigger ( {
157178 type : "error" ,
158179 reason : {
159- message : $ . i18n ( "ws_error_occured" ) ,
160- details : [ error ]
180+ message : $ . i18n ( "ws_error_occured" ) ,
181+ details : [ safeError ]
161182 }
162183 } ) ;
163184 } ;
0 commit comments