Skip to content

Commit ce93b5f

Browse files
vaadin-botArtur-
andauthored
fix: Allow access to /favicon even when the vaadin mapping is /something (#16375) (#16381)
Allow accessing the error page in security tests so that the 404 is sent to the browser Fixes #16371 Co-authored-by: Artur <artur@vaadin.com>
1 parent 11d3f31 commit ce93b5f

6 files changed

Lines changed: 44 additions & 8 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/server/HandlerHelper.java‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ public String getIdentifier() {
108108
}
109109
}
110110

111+
private static final String[] publicResourcesRoot;
111112
private static final String[] publicResources;
112113
static {
113114
List<String> resources = new ArrayList<>();
114-
resources.add("/favicon.ico");
115115
resources.add("/" + PwaConfiguration.DEFAULT_PATH);
116116
resources.add("/" + FrontendUtils.SERVICE_WORKER_SRC_JS);
117117
resources.add(PwaHandler.SW_RUNTIME_PRECACHE_PATH);
@@ -122,6 +122,11 @@ public String getIdentifier() {
122122
resources.addAll(getIconVariants(PwaConfiguration.DEFAULT_ICON));
123123
publicResources = resources.toArray(new String[resources.size()]);
124124

125+
// These are always in the root of the app, not inside any url mapping
126+
List<String> rootResources = new ArrayList<>();
127+
rootResources.add("/favicon.ico");
128+
publicResourcesRoot = rootResources
129+
.toArray(new String[rootResources.size()]);
125130
}
126131

127132
private HandlerHelper() {
@@ -451,11 +456,25 @@ public static boolean isPathUnsafe(String path) {
451456
* URLs matching these patterns should be publicly available for
452457
* applications to work. Can be used for defining a bypass for rules in e.g.
453458
* Spring Security.
459+
* <p>
460+
* These paths are relative to a potential Vaadin mapping
454461
*/
455462
public static String[] getPublicResources() {
456463
return publicResources;
457464
}
458465

466+
/**
467+
* URLs matching these patterns should be publicly available for
468+
* applications to work. Can be used for defining a bypass for rules in e.g.
469+
* Spring Security.
470+
* <p>
471+
* These URLs are always relative to the root path and independent of any
472+
* Vaadin mapping
473+
*/
474+
public static String[] getPublicResourcesRoot() {
475+
return publicResourcesRoot;
476+
}
477+
459478
private static List<String> getIconVariants(String iconPath) {
460479
return PwaRegistry.getIconTemplates(iconPath).stream()
461480
.map(PwaIcon::getRelHref).collect(Collectors.toList());

‎flow-server/src/test/java/com/vaadin/flow/server/HandlerHelperTest.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ private VaadinServletRequest createVaadinRequest(String requestPath,
331331
@Test
332332
public void publicResources() {
333333
Set<String> expected = new HashSet<>();
334-
expected.add("/favicon.ico");
335334
expected.add("/manifest.webmanifest");
336335
expected.add("/sw.js");
337336
expected.add("/sw-runtime-resources-precache.js");
@@ -376,5 +375,11 @@ public void publicResources() {
376375
Set<String> actual = new HashSet<>();
377376
Collections.addAll(actual, HandlerHelper.getPublicResources());
378377
Assert.assertEquals(expected, actual);
378+
379+
Set<String> expectedRoot = Set.of("/favicon.ico");
380+
381+
Set<String> actualRoot = new HashSet<>();
382+
Collections.addAll(actualRoot, HandlerHelper.getPublicResourcesRoot());
383+
Assert.assertEquals(expectedRoot, actualRoot);
379384
}
380385
}

‎flow-test-util/src/main/java/com/vaadin/flow/testutil/TestBenchHelpers.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,8 @@ protected void checkLogsForErrors(
342342
.contains(WEB_SOCKET_CONNECTION_ERROR_PREFIX)
343343
&& !acceptableMessagePredicate
344344
.test(logEntry.getMessage())) {
345-
throw new AssertionError(String.format(
346-
"Received error message in browser log console right after opening the page, message: %s",
347-
logEntry));
345+
throw new AssertionError(String
346+
.format("Error message in browser log: %s", logEntry));
348347
} else {
349348
LoggerFactory.getLogger(TestBenchHelpers.class.getName()).warn(
350349
"This message in browser log console may be a potential error: '{}'",

‎flow-tests/vaadin-spring-tests/test-spring-security-flow/src/main/java/com/vaadin/flow/spring/flowsecurity/SecurityConfig.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public void configure(HttpSecurity http) throws Exception {
6363
http.authorizeHttpRequests()
6464
.requestMatchers(new AntPathRequestMatcher("/public/**"))
6565
.permitAll();
66+
http.authorizeHttpRequests()
67+
.requestMatchers(new AntPathRequestMatcher("/error"))
68+
.permitAll();
69+
6670
super.configure(http);
6771
if (getLogoutSuccessUrl().equals("/")) {
6872
// Test the default url with empty context path

‎flow-tests/vaadin-spring-tests/test-spring-security-flow/src/test/java/com/vaadin/flow/spring/flowsecurity/AbstractIT.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ private void checkForBrowserErrors() {
4646
checkLogsForErrors(msg -> msg.contains(
4747
"admin-only/secret.txt - Failed to load resource: the server responded with a status of 403")
4848
|| msg.contains(
49-
"admin-only/secret.txt?continue - Failed to load resource: the server responded with a status of 403"));
49+
"admin-only/secret.txt?continue - Failed to load resource: the server responded with a status of 403")
50+
|| (msg.contains("X-Atmosphere-Transport=close")
51+
&& msg.contains(
52+
"Failed to load resource: the server responded with a status of 403")));
5053
}
5154

5255
/**

‎vaadin-spring/src/main/java/com/vaadin/flow/spring/security/VaadinWebSecurity.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
import java.io.IOException;
1919
import java.lang.reflect.Field;
20+
import java.util.Arrays;
2021
import java.util.LinkedHashMap;
22+
import java.util.List;
2123
import java.util.Objects;
2224
import java.util.Optional;
2325
import java.util.function.Consumer;
@@ -304,9 +306,13 @@ public static RequestMatcher getDefaultWebSecurityIgnoreMatcher(
304306
String urlMapping) {
305307
Objects.requireNonNull(urlMapping,
306308
"Vaadin servlet url mapping is required");
307-
return new OrRequestMatcher(Stream
309+
Stream<String> mappingRelativePaths = Stream
308310
.of(HandlerHelper.getPublicResources())
309-
.map(path -> RequestUtil.applyUrlMapping(urlMapping, path))
311+
.map(path -> RequestUtil.applyUrlMapping(urlMapping, path));
312+
Stream<String> rootPaths = Stream
313+
.of(HandlerHelper.getPublicResourcesRoot());
314+
return new OrRequestMatcher(Stream
315+
.concat(mappingRelativePaths, rootPaths)
310316
.map(AntPathRequestMatcher::new).collect(Collectors.toList()));
311317
}
312318

0 commit comments

Comments
 (0)