-
Notifications
You must be signed in to change notification settings - Fork 41.3k
Document how to access the H2 Console in a secured web application #29932
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
Conversation
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.
Thanks for the PR. I've added a couple comments that are more directed to the team as I'd like to get more feedback on this. I've flagged the issue accordingly.
spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/sql.adoc
Show resolved
Hide resolved
==== Configuring Spring Security for H2 Console | ||
H2 Console uses frames and, as it's intended for development only, does not implement CSRF protection measures. If your application uses Spring Security, you need to configure it accordingly. | ||
|
||
For example, Spring Security will ignore the console if the following `WebSecurityCustomizer` is exposed: |
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.
I think we need to be more explicit than "ignoring" and mention anybody could access it.
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.
I agree. In the context of CSRF, it would be better to just disable Security's protection rather than ignoring entirely. This is what the old (1.x) auto-configuration for the console used to do:
Lines 97 to 113 in 10a5cef
@Override | |
public void configure(HttpSecurity http) throws Exception { | |
String path = this.console.getPath(); | |
String antPattern = (path.endsWith("/") ? path + "**" : path + "/**"); | |
HttpSecurity h2Console = http.antMatcher(antPattern); | |
h2Console.csrf().disable(); | |
h2Console.httpBasic(); | |
h2Console.headers().frameOptions().sameOrigin(); | |
String[] roles = this.security.getUser().getRole().toArray(new String[0]); | |
SecurityAuthorizeMode mode = this.security.getBasic().getAuthorizeMode(); | |
if (mode == null || mode == SecurityAuthorizeMode.ROLE) { | |
http.authorizeRequests().anyRequest().hasAnyRole(roles); | |
} | |
else if (mode == SecurityAuthorizeMode.AUTHENTICATED) { | |
http.authorizeRequests().anyRequest().authenticated(); | |
} | |
} |
@snicoll @wilkinsona Thanks for the feedback! I'll rewrite the PR to show how a dedicated |
I'm not sure that we should go that far. The security for the console may need to be quite complex, and that'll depend on the user's security configuration which is an unknown to us. I think we should avoid prescribing a particular approach and just point out any common pitfalls such as the situation with CSRF protection. |
I removed the code snippet and added some explanations on what needs to be done together with a warning not to do it in production. |
@hpoettker Your code snippet in 1573e32 has changed my mind. I was concerned about providing something that users would copy and paste and try to use as-is. I think your snippet is informative without making that too much of a risk. Sorry to be a pain, but would you mind reinstating it? |
This reverts commit 2b9b148.
@wilkinsona I reverted the last commit. Quick thing and no pain at all. 😄 I fully agree that one needs to be careful with publishing code snippets in the reference documentation that have the potential to create security risks if used lightheartedly. The code snippet makes it pretty clear that it is not supposed to be used in production code. However, if just the bean method is copied verbatim and the comment asking for authorization to be configured is ignored, then the H2 console is again unprotected as in the first draft with the |
The code snippet that I came up with that is the most safe to be copied and pasted is
But this code snippet is quite dense and may confuse more people than it helps. |
WebSecurityCustomizer
for H2 Console
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
SecurityFilterChain h2ConsoleSecurityFilterChain(HttpSecurity http) throws Exception {
return http.requestMatcher(PathRequest.toH2Console())
.csrf(AbstractHttpConfigurer::disable)
.headers(headers -> headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin))
.build();
} |
Hi @linghengqian, thanks for joining the discussion! |
The Spring Security docs seem to use the lambda-style API, such as in their example for disabling CSRF. I think it makes sense to align with that. I've opened #30432. |
Resolves #28268.
The
WebSecurityCustomizer
is a bit blunt. But for a development setup, a properSecurityFilterChain
seems overly ceremonial, in my opinion. 😄