breautek commented on code in PR #276: URL: https://github.com/apache/cordova-js/pull/276#discussion_r1705948690
########## src/cordova.js: ########## @@ -43,6 +43,80 @@ var m_window_removeEventListener = window.removeEventListener; var documentEventHandlers = {}; var windowEventHandlers = {}; +/** + * Mitigation for Event Listener Hijacking + */ +(function() { + var originalDocumentAddEventListener = document.addEventListener; + var originalWindowAddEventListener = window.addEventListener; + var documentEventHandlers = {}; + var windowEventHandlers = {}; + + document.addEventListener = function (evt, handler, capture) { + var e = evt.toLowerCase(); + if (typeof documentEventHandlers[e] !== 'undefined') { + if (typeof documentEventHandlers[e].subscribe === 'function') { + documentEventHandlers[e].subscribe(handler); + } else { + console.warn('No subscribe function defined for event:', e); + } + } else { + originalDocumentAddEventListener.call(document, evt, handler, capture); + } + }; + + window.addEventListener = function (evt, handler, capture) { + var e = evt.toLowerCase(); + if (typeof windowEventHandlers[e] !== 'undefined') { + if (typeof windowEventHandlers[e].subscribe === 'function') { + windowEventHandlers[e].subscribe(handler); + } else { + console.warn('No subscribe function defined for event:', e); + } + } else { + originalWindowAddEventListener.call(window, evt, handler, capture); + } + }; + + // Securely define your event handlers + documentEventHandlers['click'] = { + subscribe: function(handler) { + var secureHandler = function(event) { + // Perform necessary checks or actions before invoking the handler + if (event && event.target) { + var allowedElements = ['button', 'a', 'div']; + if (allowedElements.includes(event.target.tagName.toLowerCase())) { Review Comment: The standard browser behaviour allows click events on all tags. Users can even create custom tags which is common in some frameworks, so we cannot realistically know ahead of time which is acceptable. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org For additional commands, e-mail: issues-h...@cordova.apache.org