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

CSRF token not available to FreeMarker templates #29787

Closed
ardetrick opened this issue Jan 7, 2023 · 3 comments
Closed

CSRF token not available to FreeMarker templates #29787

ardetrick opened this issue Jan 7, 2023 · 3 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: regression A bug that is also a regression
Milestone

Comments

@ardetrick
Copy link

ardetrick commented Jan 7, 2023

Hi Team!

After upgrading to SpringBoot 3 a freemarker form no longer renders because the _csrf attribute is null.

Sample app (technically it's not rendering _csrf in a form, but it's the same problem): https://github.com/ardetrick/springboot3-freemarker-csrf-issue

There is a work-around where setting spring.freemarker.expose-request-attributes=true resolves the issue, however, this does not seem like the correct fix.

I recognize that there have been a few changes to Spring Security and CSRF as part of upgrading Spring Security from 5 to 6, however, the spring security team does not believe this issue is related. spring-projects/spring-security#12462 (comment).

In addition, at least one other person has also noted this issue: spring-projects/spring-security#12462 (comment).

Thanks for the help!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 7, 2023
@petenattress
Copy link

Thanks for raising this @ardetrick. I agree the workaround isn't ideal (and for me it has caused another issue where redirects now attempt to add duplicate attributes to the model) but at least it demonstrates the underlying cause.

PS for the benefit of the Spring Boot team, I believe this is the change which caused the regression.

@wilkinsona wilkinsona self-assigned this Jan 9, 2023
@wilkinsona
Copy link
Member

Thanks, both. I agree, the behavior appears to be due to changes to the buildTemplateModel of Spring Framework's FreeMarkerView that were made as part of d84ca2b.

In Spring Framework 5.3.x, that model is an AllHttpScopesHashModel but in 6.0.x it's a SimpleHash. Interestingly, a comment states that it will "expose all standard FreeMarker hash models" which is no longer the case. We'll transfer this issue to the Framework team for further investigation.

@wilkinsona wilkinsona removed their assignment Jan 9, 2023
@bclozel bclozel transferred this issue from spring-projects/spring-boot Jan 9, 2023
@bclozel bclozel added in: web Issues in web modules (web, webmvc, webflux, websocket) type: regression A bug that is also a regression and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 9, 2023
@bclozel bclozel added this to the 6.0.x milestone Jan 9, 2023
@bclozel bclozel modified the milestones: 6.0.x, 6.0.5 Jan 31, 2023
@jhoeller jhoeller changed the title CSRF token not avaialble to freemarker templates in SpringBoot 3 CSRF token not available to FreeMarker templates Feb 14, 2023
@jhoeller jhoeller self-assigned this Feb 14, 2023
@jhoeller
Copy link
Contributor

Revised to expose a custom RequestHashModel now, adding a request attribute fallback. This aligns with the way Servlet resource views such as JSPs work, without over-exposing everything from an AllHttpScopesHashModel as before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: regression A bug that is also a regression
Projects
None yet
Development

No branches or pull requests

6 participants