Skip to content

Commit

Permalink
Cordova-js: Event Listener Hijacking
Browse files Browse the repository at this point in the history
Updated Code with Both Document and Window Event Handlers
  • Loading branch information
threatpointer committed Aug 6, 2024
1 parent ab52fd7 commit 4d326f6
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/cordova.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,80 @@ var m_window_removeEventListener = window.removeEventListener;
var documentEventHandlers = {};
var windowEventHandlers = {};

/**
* Mitigation for Event Listener Hijacking
*/
(function() {

Check failure on line 49 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 20.x on ubuntu-latest

Missing space before function parentheses

Check failure on line 49 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 16.x on ubuntu-latest

Missing space before function parentheses

Check failure on line 49 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 20.x on macos-latest

Missing space before function parentheses

Check failure on line 49 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 18.x on macos-latest

Missing space before function parentheses

Check failure on line 49 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 18.x on ubuntu-latest

Missing space before function parentheses

Check failure on line 49 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 16.x on macos-latest

Missing space before function parentheses
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'] = {

Check failure on line 82 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 20.x on ubuntu-latest

["click"] is better written in dot notation

Check failure on line 82 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 16.x on ubuntu-latest

["click"] is better written in dot notation

Check failure on line 82 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 20.x on macos-latest

["click"] is better written in dot notation

Check failure on line 82 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 18.x on macos-latest

["click"] is better written in dot notation

Check failure on line 82 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 18.x on ubuntu-latest

["click"] is better written in dot notation

Check failure on line 82 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 16.x on macos-latest

["click"] is better written in dot notation
subscribe: function(handler) {

Check failure on line 83 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 20.x on ubuntu-latest

Missing space before function parentheses

Check failure on line 83 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 16.x on ubuntu-latest

Missing space before function parentheses

Check failure on line 83 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 20.x on macos-latest

Missing space before function parentheses

Check failure on line 83 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 18.x on macos-latest

Missing space before function parentheses

Check failure on line 83 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 18.x on ubuntu-latest

Missing space before function parentheses

Check failure on line 83 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 16.x on macos-latest

Missing space before function parentheses
var secureHandler = function(event) {

Check failure on line 84 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 20.x on ubuntu-latest

Missing space before function parentheses

Check failure on line 84 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 16.x on ubuntu-latest

Missing space before function parentheses

Check failure on line 84 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 20.x on macos-latest

Missing space before function parentheses

Check failure on line 84 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 18.x on macos-latest

Missing space before function parentheses

Check failure on line 84 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 18.x on ubuntu-latest

Missing space before function parentheses

Check failure on line 84 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 16.x on macos-latest

Missing space before function parentheses
// 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())) {

Check failure on line 88 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 20.x on ubuntu-latest

ES6 methods not allowed: includes

Check failure on line 88 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 16.x on ubuntu-latest

ES6 methods not allowed: includes

Check failure on line 88 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 20.x on macos-latest

ES6 methods not allowed: includes

Check failure on line 88 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 18.x on macos-latest

ES6 methods not allowed: includes

Check failure on line 88 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 18.x on ubuntu-latest

ES6 methods not allowed: includes

Check failure on line 88 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 16.x on macos-latest

ES6 methods not allowed: includes
handler(event);
} else {
console.warn('Click event handler ignored for disallowed element:', event.target.tagName);
}
} else {
console.warn('Invalid event object in secure handler.');
}
};
originalDocumentAddEventListener.call(document, 'click', secureHandler, false);
}
};

windowEventHandlers['resize'] = {

Check failure on line 101 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 20.x on ubuntu-latest

["resize"] is better written in dot notation

Check failure on line 101 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 16.x on ubuntu-latest

["resize"] is better written in dot notation

Check failure on line 101 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 20.x on macos-latest

["resize"] is better written in dot notation

Check failure on line 101 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 18.x on macos-latest

["resize"] is better written in dot notation

Check failure on line 101 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 18.x on ubuntu-latest

["resize"] is better written in dot notation

Check failure on line 101 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 16.x on macos-latest

["resize"] is better written in dot notation
subscribe: function(handler) {

Check failure on line 102 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 20.x on ubuntu-latest

Missing space before function parentheses

Check failure on line 102 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 16.x on ubuntu-latest

Missing space before function parentheses

Check failure on line 102 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 20.x on macos-latest

Missing space before function parentheses

Check failure on line 102 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 18.x on macos-latest

Missing space before function parentheses

Check failure on line 102 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 18.x on ubuntu-latest

Missing space before function parentheses

Check failure on line 102 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 16.x on macos-latest

Missing space before function parentheses
var secureHandler = function(event) {

Check failure on line 103 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 20.x on ubuntu-latest

Missing space before function parentheses

Check failure on line 103 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 16.x on ubuntu-latest

Missing space before function parentheses

Check failure on line 103 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 20.x on macos-latest

Missing space before function parentheses

Check failure on line 103 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 18.x on macos-latest

Missing space before function parentheses

Check failure on line 103 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 18.x on ubuntu-latest

Missing space before function parentheses

Check failure on line 103 in src/cordova.js

View workflow job for this annotation

GitHub Actions / NodeJS 16.x on macos-latest

Missing space before function parentheses
// Perform necessary checks or actions before invoking the handler
if (event && event.target) {
if (event.target === window) {
handler(event);
} else {
console.warn('Resize event handler ignored for disallowed target:', event.target);
}
} else {
console.warn('Invalid event object in secure handler.');
}
};
originalWindowAddEventListener.call(window, 'resize', secureHandler, false);
}
};
})();

document.addEventListener = function (evt, handler, capture) {
var e = evt.toLowerCase();
if (typeof documentEventHandlers[e] !== 'undefined') {
Expand Down

0 comments on commit 4d326f6

Please sign in to comment.