Skip to content
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

Live component force post requests #1218

Merged
merged 1 commit into from
Jan 21, 2024

Conversation

hepisec
Copy link
Contributor

@hepisec hepisec commented Oct 21, 2023

Q A
Bug fix? no
New feature? yes
Tickets Fix #1208
License MIT

When your component model may contain sensitive data, you probably don't want your data being transferred via GET requests, because then the data might leak to browser history, server logs etc.

With this PR you can add forcePost: true to #[AsLiveComponent] and your component will always use POST requests to talk to the backend.

#[AsLiveComponent(forcePost: true)]
class MySensitiveComponent
{
// ...
}

@weaverryan
Copy link
Member

Thanks for getting this rolling - that's awesome! Might it make sense to have the setting not as an attribute but on the #[AsLiveComponent] attribute? If you have a component that requires POST for some reason, then it will ALWAYS need to be POSTed - it shouldn't need to have a data-force-post passed each time.

@hepisec
Copy link
Contributor Author

hepisec commented Oct 21, 2023

I tried that first as this was your first suggestion. But I was not able to pass the property to the frontend yet. Would LiveComponentHydrator be the right place to do so?

@weaverryan
Copy link
Member

But I was not able to pass the property to the frontend yet. Would LiveComponentHydrator be the right place to do so?

Try https://github.com/symfony/ux/blob/2.x/src/LiveComponent/src/Util/LiveControllerAttributesCreator.php

If we added a new forcePost: true option to AsLiveComponent, we should be able to get it in there. Let me know if you need help - I can dig in a bit further. Ultimately, we'll add an attribute that would map to a new "value" in the Live stimulus controller. Then the JS part shouldn't be too much different :)

@smnandre
Copy link
Member

Should we make that a default ?

I agree with @hepisec, and i'm even ready to say that any data sent to the server should be in POST...

@hepisec
Copy link
Contributor Author

hepisec commented Oct 22, 2023

I've added support for #[AsLiveComponent(forcePost: true)] to the PR. I leave the decision for the default value up to you. I'd like to see the security considerations covered in the documentation.

@hepisec hepisec requested a review from smnandre October 24, 2023 08:31
@smnandre
Copy link
Member

Is there something checking the request is sent in POST in the backend side ?

I think somehow the LiveComponentSubscriber should be updated no ?

@hepisec
Copy link
Contributor Author

hepisec commented Oct 25, 2023

I've added a check for forcePost in LiveComponentSubscriber::onKernelController.

@weaverryan
Copy link
Member

After talking with a few people, I think we should reverse your PR. We should make components use POST by default, with an option to opt into GET. The reason is the one given in your description. And, more generally, if you think of a live component re-render as a type of "form submit" (and, if the user is changing a prop value... then they likely are filling out a form in one way or another), then form submits should almost always be POST (Symfony forms, for example, default to POST).

Could you change the option from forcePost to method and default it to post? We could throw an exception in the attribute's constructor if some value other than get or post` were passed.

@hepisec
Copy link
Contributor Author

hepisec commented Nov 7, 2023

I'll update the PR in the next days to have POST by default, as suggested by @weaverryan

Copy link
Contributor

@WebMamba WebMamba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for your work @hepisec! 🧡

src/LiveComponent/assets/src/Backend/Backend.ts Outdated Show resolved Hide resolved
src/LiveComponent/src/Attribute/AsLiveComponent.php Outdated Show resolved Hide resolved
@hepisec
Copy link
Contributor Author

hepisec commented Nov 13, 2023

I've finished the work on this issue. As POST is now the new default method and GET is only allowed when #[AsLiveComponent(method: 'get')] is set, I had to update several tests / fixtures to work with the new default method.

Thanks everyone so far, I'm looking forward to your feedback.

@hepisec hepisec requested a review from weaverryan November 14, 2023 22:08
Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor notes - this is looking good. Thanks for the patience on the slow review - but this is a very important PR!

@weaverryan
Copy link
Member

Friendly ping to you @hepisec :). Let us know if you have time to do the final steps! Thanks!

@hepisec
Copy link
Contributor Author

hepisec commented Dec 23, 2023

I'm starting right now.

@hepisec
Copy link
Contributor Author

hepisec commented Dec 23, 2023

It should be done now. The failed tests seem to be related to TwigComponent and are 8.2 only.

I wish you all happy holidays.

@weaverryan
Copy link
Member

Hey @hepisec!

Sorry for the delay - this is ready to merge. Can you rebase this please?

Thanks!

@hepisec
Copy link
Contributor Author

hepisec commented Jan 18, 2024

Rebase done. Looking forward to my first finished contribution to symfony/ux :-)

@weaverryan weaverryan force-pushed the live-component-force-post-requests branch from 5bafb88 to 4310601 Compare January 21, 2024 21:49
@weaverryan
Copy link
Member

Thank you 1000 for this @hepisec!

@weaverryan weaverryan merged commit f63cb59 into symfony:2.x Jan 21, 2024
6 checks passed
@hepisec hepisec deleted the live-component-force-post-requests branch January 22, 2024 12:33
weaverryan added a commit that referenced this pull request Feb 1, 2024
…r (daFish)

This PR was merged into the 2.x branch.

Discussion
----------

fix: use method from metadata for live component test helper

| 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

After #1218 has been released as part of 2.14.0 my tests fail. This change passes the actual method to the requests made by the test helper.

Commits
-------

d84f3eb fix: use method from metadata for live component test helper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[LiveComponent] Force POST method when model is changed
4 participants