-
Notifications
You must be signed in to change notification settings - Fork 9
feat(html): add serializableEvent sanitizer and tests for event serialization #1430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(html): add serializableEvent sanitizer and tests for event serialization #1430
Conversation
…lization - Introduce `serializableEvent` in `render.ts` to sanitize DOM events, making them serializable by extracting a safe subset of properties. - Add allow-lists for event and event target properties to control what gets serialized. - Update `render.test.ts` to include comprehensive tests for `serializableEvent`, covering Event, KeyboardEvent, MouseEvent, InputEvent (with target value), and CustomEvent (with detail). - Ensure all serialized events are plain objects suitable for structured cloning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Adds a serializer for DOM events in render.ts and validates it with new tests.
- Implements
serializableEventto extract an allow-listed subset of event and target properties and ensure plain-object serialization. - Defines property allow-lists (
allowListedEventPropertiesandallowListedEventTargetProperties) and makessanitizeEventdefault to useserializableEvent. - Adds comprehensive tests in
render.test.tscovering generic Event, KeyboardEvent, MouseEvent, InputEvent (with target value), and CustomEvent (with detail).
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/html/src/render.ts | Introduced serializableEvent, allow-lists for event/target properties, and set it as the default sanitizer. |
| packages/html/test/render.test.ts | Imported and tested serializableEvent, added global event constructors, and helper to verify plain-object serialization. |
Comments suppressed due to low confidence (2)
packages/html/src/render.ts:361
- The generic
<T>return type may be misleading since the function always returns a plain object. Consider specifying a concrete return type likeRecord<string, any>or defining a dedicated serializable event interface.
export function serializableEvent<T>(event: Event): T {
packages/html/src/render.ts:320
- [nitpick] To improve clarity, consider renaming
allowListedEventPropertiesandallowListedEventTargetPropertiestoallowedEventPropertiesandallowedEventTargetProperties, respectively, so the naming more directly reflects their purpose.
const allowListedEventProperties = [
| targetObject[property] = event.target?.[property as keyof EventTarget]; | ||
| } | ||
| if (Object.keys(targetObject).length > 0) eventObject.target = targetObject; |
Copilot
AI
Jul 17, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider checking for at least one non-undefined property in targetObject before assigning it to eventObject.target, so you don’t include an empty target object for events without relevant target properties.
| targetObject[property] = event.target?.[property as keyof EventTarget]; | |
| } | |
| if (Object.keys(targetObject).length > 0) eventObject.target = targetObject; | |
| const value = event.target?.[property as keyof EventTarget]; | |
| if (value !== undefined) { | |
| targetObject[property] = value; | |
| } | |
| } | |
| if (Object.keys(targetObject).length > 0) { | |
| eventObject.target = targetObject; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cubic reviewed 2 files and found no issues. Review PR in cubic.dev.
| if (Object.keys(targetObject).length > 0) eventObject.target = targetObject; | ||
|
|
||
| if ((event as CustomEvent).detail !== undefined) { | ||
| eventObject.detail = (event as CustomEvent).detail; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
detail could contain anything, but the serialization cycle should at least sever e.g. element references
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added comment. my understanding is that only our own custom elements can trigger those, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, or at least no CustomEvents fired from default browser interaction
- verify that not allow listed fields are actually filtered
serializableEventinrender.tsto sanitize DOM events, making them serializable by extracting a safe subset of properties.render.test.tsto include comprehensive tests forserializableEvent, covering Event, KeyboardEvent, MouseEvent, InputEvent (with target value), and CustomEvent (with detail).Summary by cubic
Added a serializableEvent sanitizer to extract a safe, serializable subset of DOM event properties, and added tests to ensure correct event serialization.