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

fix: remove Lazy annotation from Flow security beans #18463

Merged
merged 5 commits into from
Jan 22, 2024

Conversation

mcollovati
Copy link
Collaborator

Description

For parameters with Lazy annotation, Spring generates a not-serializable proxy. Since some security beans are used inside Flow listeners, they should be fully serializable (or defined transient, if possible).
This change removes the unnecessary Lazy annotaions, moving the lazy evaluation to VaadinWebSecurity.

Fixes #18458

Type of change

  • Bugfix
  • Feature

Checklist

  • I have read the contribution guide: https://vaadin.com/docs/latest/guide/contributing/overview/
  • I have added a description following the guideline.
  • The issue is created in the corresponding repository and I have referenced it.
  • I have added tests to ensure my change is effective and works as intended.
  • New and existing tests are passing locally with my change.
  • I have performed self-review and corrected misspellings.

Additional for Feature type of change

  • Enhancement / new feature was discussed in a corresponding GitHub issue and Acceptance Criteria were created.

For parameters with Lazy annotation, Spring generates a not-serializable proxy.
Since some security beans are used inside Flow listeners, they should be fully
serializable (or defined transient, if possible).
This change removes the unnecessary Lazy annotaions, moving the lazy evaluation
to VaadinWebSecurity.

Fixes #18458
Copy link

github-actions bot commented Jan 15, 2024

Test Results

1 055 files  ± 0  1 055 suites  ±0   1h 13m 58s ⏱️ +19s
6 799 tests + 1  6 754 ✅ + 1  45 💤 ±0  0 ❌ ±0 
7 086 runs   - 14  7 030 ✅  - 14  56 💤 ±0  0 ❌ ±0 

Results for commit 124c117. ± Comparison against base commit 995caf0.

♻️ This comment has been updated with latest results.

@@ -119,7 +119,7 @@ NavigationAccessControlConfigurer navigationAccessControlConfigurerCustomizer()
*/
@Bean
public AnnotatedViewAccessChecker annotatedViewAccessChecker(
@Lazy AccessAnnotationChecker accessAnnotationChecker) {
AccessAnnotationChecker accessAnnotationChecker) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it also make sense to add proxyBeanMethods=false to the @Configuration so that all beans in that class aren't proxied? This would also ensure this isn't reintroduced by accident.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If I recall correctly, proxyBeanMethods=false prevents the configuration class to be proxied, not the exposed beans.
Anyway, it makes sense to set that flag, since we have no direct method calls in SpringSecurityAutoConfiguration

Copy link
Contributor

Choose a reason for hiding this comment

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

I had the same assumption in the past :) until I've read the javadocs of the flag:

Specify whether @bean methods should get proxied in order to enforce bean lifecycle behavior, e.g. to return shared singleton bean instances even in case of direct @bean method calls in user code. This feature requires method interception, implemented through a runtime-generated CGLIB subclass which comes with limitations such as the configuration class and its methods not being allowed to declare final.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I read it as "configuration class is proxied so that bean methods will always return the same instance when called by other methods of the class". My understanding is that when, for example, you call annotatedViewAccessChecker() from another method inside the SpringSecurityAutoConfiguration class, it creates the instance at the first call, and subsequent invocation will return that one instead of a new instance how it would happen if the configuration class is not proxied.
So, SpringSecurityAutoConfiguration methods are proxied, but not their return value.
But I may be wrong. I'll double-check it

Copy link
Contributor

Choose a reason for hiding this comment

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

Good thing to double check! I only remember an old issue where boot also switched all their configuration to false by default, also to increase performance: spring-projects/spring-boot#9068

Copy link
Contributor

Choose a reason for hiding this comment

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

Before this is blocked: don't worry about it and do it later :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added the flag anyway, since it completely makes sense to avoid proxying in this case.
Thanks for pointing out 👍

private <T> void assertObjectIsSerializable(T instance) {
Object deserialized = Assertions.assertDoesNotThrow(() -> {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bs);
Copy link
Contributor

Choose a reason for hiding this comment

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

Intellij like this version better:

Suggested change
ObjectOutputStream out = new ObjectOutputStream(bs);
try (ObjectOutputStream out = new ObjectOutputStream(bs)) {
out.writeObject(instance);
}

Maybe we could use it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, Marco!

czp13
czp13 previously approved these changes Jan 16, 2024
Copy link
Contributor

@czp13 czp13 left a comment

Choose a reason for hiding this comment

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

Thank you for the changes! 🙇 I believe most issues have been addressed, with only a minor code modification suggested. It's a very small change, so I am marking this as approved.

Copy link

sonarcloud bot commented Jan 16, 2024

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@mshabarov mshabarov requested a review from tltv January 22, 2024 12:41
@tltv tltv merged commit 3d2c654 into main Jan 22, 2024
26 checks passed
@tltv tltv deleted the issues/18458_springautoconf_remove_lazy_parameters branch January 22, 2024 14:39
vaadin-bot pushed a commit that referenced this pull request Jan 22, 2024
* fix: remove Lazy annotation from Flow security beans

For parameters with Lazy annotation, Spring generates a not-serializable proxy.
Since some security beans are used inside Flow listeners, they should be fully
serializable (or defined transient, if possible).
This change removes the unnecessary Lazy annotaions, moving the lazy evaluation
to VaadinWebSecurity.

Fixes #18458

* Apply suggestions from code review

Co-authored-by: Peter Czuczor <[email protected]>

* set proxyBeanMethods to false

* use try-with-resource for serialization/deserialization

---------

Co-authored-by: Peter Czuczor <[email protected]>
vaadin-bot added a commit that referenced this pull request Jan 22, 2024
* fix: remove Lazy annotation from Flow security beans

For parameters with Lazy annotation, Spring generates a not-serializable proxy.
Since some security beans are used inside Flow listeners, they should be fully
serializable (or defined transient, if possible).
This change removes the unnecessary Lazy annotaions, moving the lazy evaluation
to VaadinWebSecurity.

Fixes #18458

* Apply suggestions from code review



* set proxyBeanMethods to false

* use try-with-resource for serialization/deserialization

---------

Co-authored-by: Marco Collovati <[email protected]>
Co-authored-by: Peter Czuczor <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

spring-security breaks Vaadin session serialization
5 participants