Repository navigation
fix(thirdparty): address the #268 review of Third Party form login (#249) #270
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bf1555b
fix(thirdparty): address the #268 review of Third Party form login (#…
dfcoffin fc34638
fix(thirdparty): block the H2 console instead of opening it to TP adm…
dfcoffin 07cc992
fix(thirdparty): give TP administrators the H2 console, localhost only
dfcoffin 2421ecd
fix(thirdparty): development-only H2 console chain with a Sec-Fetch-S…
dfcoffin 5d3c9af
fix(thirdparty): address the #270 review (console entry rule, console…
dfcoffin 57010b6
fix(thirdparty): stop console actions being replayed through sign-in;…
dfcoffin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...n/java/org/greenbuttonalliance/espi/thirdparty/config/H2ConsoleSecurityConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /* | ||
| * | ||
| * Copyright (c) 2025-2026 Green Button Alliance, Inc. | ||
| * | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
|
|
||
| package org.greenbuttonalliance.espi.thirdparty.config; | ||
|
|
||
| import org.greenbuttonalliance.espi.thirdparty.security.SameOriginFetchAuthorizationManager; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.core.annotation.Order; | ||
| import org.springframework.security.authorization.AuthorityAuthorizationManager; | ||
| import org.springframework.security.authorization.AuthorizationManagers; | ||
| import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
| import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; | ||
| import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer; | ||
| import org.springframework.security.web.SecurityFilterChain; | ||
| import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint; | ||
| import org.springframework.security.web.savedrequest.HttpSessionRequestCache; | ||
| import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher; | ||
|
|
||
| /** | ||
| * Security for the H2 web console, a development-only diagnostic tool. Loaded only where the | ||
| * console is enabled ({@code spring.h2.console.enabled=true}: the {@code local}, {@code dev} and | ||
| * {@code h2} profiles). Everywhere else this configuration does not exist and the main chain in | ||
| * {@link SecurityConfiguration} denies {@code /h2-console/**}. | ||
| * | ||
| * <p>The console runs arbitrary SQL, and H2's {@code CREATE ALIAS} runs Java code. Its own forms | ||
| * carry no CSRF token, so CSRF tokens are off for this chain and it is guarded instead by:</p> | ||
| * <ul> | ||
| * <li>H2 itself answering localhost only ({@code web-allow-others: false} on every profile that | ||
| * enables the console);</li> | ||
| * <li>a signed-in TP administrator ({@code ROLE_CUSTODIAN}; the session from the TP's form login | ||
| * applies here too);</li> | ||
| * <li>{@link SameOriginFetchAuthorizationManager}: the browser must report the request as | ||
| * same-origin; a user-initiated request ({@code Sec-Fetch-Site: none}, e.g. a bookmark or a | ||
| * link clicked in another application) may only open the console's entry page. This stops | ||
| * cross-site pages, pages on other {@code localhost} ports (which {@code SameSite=Lax} cookies | ||
| * do not stop) and links carrying a console action from driving it with the developer's | ||
| * session.</li> | ||
| * </ul> | ||
| */ | ||
| @Configuration | ||
| @ConditionalOnProperty(name = "spring.h2.console.enabled", havingValue = "true") | ||
| public class H2ConsoleSecurityConfiguration { | ||
|
|
||
| /** | ||
| * Ahead of the main chain, which has no request matcher and would otherwise take every request. | ||
| * The console's path comes from {@code spring.h2.console.path} (Spring Boot's default | ||
| * {@code /h2-console}), the same property the console itself is served from, so the rules follow | ||
| * the console if the path changes. | ||
| */ | ||
| @Bean | ||
| @Order(0) | ||
| public SecurityFilterChain h2ConsoleSecurityFilterChain(HttpSecurity http, | ||
| @Value("${spring.h2.console.path:/h2-console}") String configuredPath) throws Exception { | ||
| String path = basePath(configuredPath); | ||
| SameOriginFetchAuthorizationManager sameOrigin = new SameOriginFetchAuthorizationManager(path); | ||
|
|
||
| // Not signed in: go to the TP login page. Only the console's entry page is saved for the | ||
| // redirect back after sign-in. A saved action URL (e.g. an attacker's login.do?url=...) would | ||
| // otherwise be replayed by that redirect, which the browser reports as same-origin. | ||
| HttpSessionRequestCache entryPageOnly = new HttpSessionRequestCache(); | ||
| entryPageOnly.setRequestMatcher(sameOrigin::isEntryPage); | ||
| // No "?continue" marker on the replay: it must stay a parameterless GET of the entry page. | ||
| entryPageOnly.setMatchingRequestParameterName(null); | ||
|
|
||
| return http | ||
| .securityMatcher(PathPatternRequestMatcher.withDefaults().matcher(path + "/**")) | ||
| .authorizeHttpRequests(authz -> authz | ||
| .anyRequest().access(AuthorizationManagers.allOf( | ||
| AuthorityAuthorizationManager.hasRole("CUSTODIAN"), | ||
| sameOrigin))) | ||
| .requestCache(cache -> cache.requestCache(entryPageOnly)) | ||
| .exceptionHandling(e -> e.authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login"))) | ||
| .csrf(AbstractHttpConfigurer::disable) | ||
| // The console is built from frames on its own origin. | ||
| .headers(headers -> headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin)) | ||
| .build(); | ||
| } | ||
|
|
||
| /** | ||
| * {@code spring.h2.console.path} without trailing slashes. Spring Boot accepts {@code /h2-console/}; | ||
| * appending {@code /**} to that as configured would give {@code /h2-console//**}, which matches | ||
| * nothing, so the console would fall through to the main chain's {@code authenticated()}. Used by | ||
| * both this chain and {@link SecurityConfiguration}'s rule that closes the console. | ||
| */ | ||
| static String basePath(String configuredPath) { | ||
| String path = configuredPath == null ? "" : configuredPath.trim(); | ||
| while (path.length() > 1 && path.endsWith("/")) { | ||
| path = path.substring(0, path.length() - 1); | ||
| } | ||
| if (path.isEmpty() || path.equals("/") || !path.startsWith("/")) { | ||
| throw new IllegalStateException("spring.h2.console.path must be a path below '/': '" + configuredPath + "'"); | ||
| } | ||
| return path; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.