Skip to content

Commit 9b322da

Browse files
committed
bug #2537 [LiveComponent] Prevent __component property to be serialized when called JSON.stringify() (Kocal)
This PR was merged into the 2.x branch. Discussion ---------- [LiveComponent] Prevent `__component` property to be serialized when called `JSON.stringify()` | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - For new features, provide some code snippets to help understand usage. - Features and deprecations must be submitted against branch main. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Following https://symfony-devs.slack.com/archives/C01FN4EQNLX/p1738238525455439 With some RUM tools that listen user interactions, some of them can `JSON.stringify()` their payload. But if their payload contains an HTML element with the property `__component` (which represents a `Proxy` to the `LiveComponent` instance), then it tries to be serialized and lead to an error by trying to call an action `toJSON` and trigger an XHR request to `/toJSON`: ![image](https://github.com/user-attachments/assets/0538ebe7-8fff-4104-aacd-5954661cea0d) Let's change the property assignment with `Object.defineProperty()`, so the property is not enumarable and so not serialized when `JSON.stringify()` is called: <img width="648" alt="image" src="https://github.com/user-attachments/assets/dc18c06b-0793-424c-b561-b72647ec4d93" /> cc `@jwage` Commits ------- e04f8f0 [LiveComponent] Prevent `__component` property to be serialized when called `JSON.stringify()`
2 parents ec09cdf + e04f8f0 commit 9b322da

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

src/LiveComponent/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 2.23.0
44

55
- Allow configuring the secret used to compute fingerprints and checksums.
6+
- Prevent `__component` property to be serialized when called `JSON.stringify()`
67

78
## 2.22.0
89

src/LiveComponent/assets/dist/live_controller.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -3059,7 +3059,10 @@ class LiveControllerDefault extends Controller {
30593059
const id = this.element.id || null;
30603060
this.component = new Component(this.element, this.nameValue, this.propsValue, this.listenersValue, id, LiveControllerDefault.backendFactory(this), new StimulusElementDriver(this));
30613061
this.proxiedComponent = proxifyComponent(this.component);
3062-
this.element.__component = this.proxiedComponent;
3062+
Object.defineProperty(this.element, '__component', {
3063+
value: this.proxiedComponent,
3064+
writable: true,
3065+
});
30633066
if (this.hasDebounceValue) {
30643067
this.component.defaultDebounce = this.debounceValue;
30653068
}

src/LiveComponent/assets/src/live_controller.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,10 @@ export default class LiveControllerDefault extends Controller<HTMLElement> imple
285285
);
286286
this.proxiedComponent = proxifyComponent(this.component);
287287

288-
// @ts-ignore Adding the dynamic property
289-
this.element.__component = this.proxiedComponent;
288+
Object.defineProperty(this.element, '__component', {
289+
value: this.proxiedComponent,
290+
writable: true,
291+
});
290292

291293
if (this.hasDebounceValue) {
292294
this.component.defaultDebounce = this.debounceValue;

0 commit comments

Comments
 (0)