Your code is well-organized for processing keyboard and mouse events using chromedp. Since you're dealing with user-input events in JSON format from an external source, security checks are essential to prevent unexpected or malicious inputs from interfering with Chrome's behavior. Here are some recommendations for security checks and improvements:
For example:
DispatchKeyEventParams: Validate fields like Type, Modifiers, Key, etc., to ensure they are within the expected range or type. DispatchMouseEventParams: Check X, Y coordinates and event Type to ensure they align with your application's requirements. Using a JSON schema validator can help automate this, but a manual check for critical fields is also viable.
Limit Event Rate If the input source can flood your application with a large volume of events, it might overwhelm your application or Chrome. Consider rate-limiting or debouncing these events to reduce excessive, rapid events. Implement a mechanism to limit the number of events processed per second or per client session.
Restrict Allowed Key Combinations For keyboard events, you might want to restrict certain key combinations or sequences that could be harmful. For example:
Disallow System Keys: Prevent triggering keys like F12, Alt+Tab, or Ctrl+Alt+Delete that might interfere with Chrome’s operation or the host system. Block Keys with Side Effects: Prevent actions that could change Chrome’s state, like Ctrl+W (close tab), Ctrl+N (new window), etc., unless these are explicitly required for your use case. You can filter keys by validating keyevt.Key, keyevt.Modifiers, and keyevt.Type fields, and reject combinations that could pose issues.
Coordinate Bounds Check for Mouse Events For DispatchMouseEventParams, ensure that the X and Y values are within the bounds of the intended viewport. Large values, either accidentally or maliciously, could cause erratic behavior or even unintended scrolling.
Limit Event Types for Both Keyboard and Mouse Ensure that only supported event types are allowed to go through. For example:
For keyevt.Type, allow only legitimate types like "keyDown", "keyUp", or "char". For ptrevt.Type, allow only types like "mousePressed", "mouseReleased", "mouseMoved", etc. Rejecting unsupported or unusual event types can reduce potential abuse.
Example: Implement an API token or other identity checks when clients connect.