Skip to content

Commit 95b3c29

Browse files
vaadin-botArtur-
andauthored
fix: Return 404 for /VAADIN/ urls which do not map to a file (#12952) (#12972)
Never serve the index.html page for URLs inside /VAADIN/ Co-authored-by: Artur <artur@vaadin.com>
1 parent 2ae3e1a commit 95b3c29

6 files changed

Lines changed: 56 additions & 10 deletions

File tree

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,12 @@ protected boolean canHandleRequest(VaadinRequest request) {
493493
if (request.getHeader(SERVICE_WORKER_HEADER) != null) {
494494
return false;
495495
}
496+
497+
if (isVaadinStaticFileRequest(request)) {
498+
// Do not allow routes inside /VAADIN/
499+
return false;
500+
}
501+
496502
return super.canHandleRequest(request);
497503
}
498504

@@ -525,6 +531,26 @@ public static boolean isFrameworkInternalRequest(VaadinRequest request) {
525531
return false;
526532
}
527533

534+
/**
535+
* Checks whether the request is a request for /VAADIN/*.
536+
* <p>
537+
* Warning: This assumes that the VaadinRequest is targeted for a
538+
* VaadinServlet and does no further checks to validate this.
539+
* <p>
540+
* This is public only so that
541+
* {@link com.vaadin.flow.server.communication.IndexHtmlRequestHandler} can
542+
* access it. If you are not IndexHtmlRequestHandler, go away.
543+
*
544+
* @param request
545+
* the request
546+
* @return {@code true} if the request is for /VAADIN/*, {@code false}
547+
* otherwise
548+
*/
549+
public static boolean isVaadinStaticFileRequest(VaadinRequest request) {
550+
return request.getPathInfo() != null
551+
&& request.getPathInfo().startsWith("/" + VAADIN_MAPPING);
552+
}
553+
528554
@Override
529555
public boolean synchronizedHandleRequest(VaadinSession session,
530556
VaadinRequest request, VaadinResponse response) throws IOException {

‎flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ private void includeInitialUidl(JsonObject initialJson,
256256
protected boolean canHandleRequest(VaadinRequest request) {
257257
return isRequestForHtml(request)
258258
&& !BootstrapHandler.isFrameworkInternalRequest(request)
259+
&& !BootstrapHandler.isVaadinStaticFileRequest(request)
259260
&& request.getService().getBootstrapUrlPredicate()
260261
.isValidUrl(request);
261262
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,19 @@ public void internal_request_no_bootstrap_page() {
15561556
Assert.assertFalse(bootstrapHandler.canHandleRequest(request));
15571557
}
15581558

1559+
@Test
1560+
public void vaadin_request_no_bootstrap_page() {
1561+
BootstrapHandler bootstrapHandler = new BootstrapHandler();
1562+
VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
1563+
Mockito.when(request.getPathInfo()).thenReturn("/VAADIN/hello.js");
1564+
Assert.assertFalse(bootstrapHandler.canHandleRequest(request));
1565+
1566+
Mockito.when(request.getPathInfo())
1567+
.thenReturn("/VAADIN/bundle/notfound");
1568+
Assert.assertFalse(bootstrapHandler.canHandleRequest(request));
1569+
1570+
}
1571+
15591572
@Test
15601573
public void serviceWorkerRequest_canNotHandleRequest() {
15611574
BootstrapHandler bootstrapHandler = new BootstrapHandler();

‎flow-server/src/test/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandlerTest.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,12 @@ public void canHandleRequest_doNotHandle_imageRequest() {
280280
createRequestWithDestination("/", "image", "no-cors")));
281281
}
282282

283+
@Test
284+
public void canHandleRequest_doNotHandle_vaadinStaticResources() {
285+
Assert.assertFalse(indexHtmlRequestHandler.canHandleRequest(
286+
createRequestWithDestination("/VAADIN/foo.js", null, null)));
287+
}
288+
283289
@Test
284290
public void canHandleRequest_handle_serviceWorkerDocumentRequest() {
285291
Assert.assertTrue(indexHtmlRequestHandler.canHandleRequest(

‎flow-tests/test-frontend/vite-basics/package.json‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "no-name",
33
"license": "UNLICENSED",
44
"dependencies": {
5-
"@polymer/polymer": "3.2.0",
5+
"@polymer/polymer": "3.4.1",
66
"@vaadin/common-frontend": "0.0.17",
77
"@vaadin/flow-frontend": "./target/flow-frontend",
88
"@vaadin/router": "1.7.4",
@@ -21,16 +21,16 @@
2121
"glob": "7.1.6",
2222
"mkdirp": "1.0.4",
2323
"rollup-plugin-brotli": "3.1.0",
24-
"typescript": "4.4.3",
25-
"vite": "v2.7.0",
24+
"typescript": "4.5.3",
25+
"vite": "v2.8.0-beta.5",
2626
"vite-plugin-checker": "0.3.4",
27-
"workbox-build": "6.4.1",
27+
"workbox-build": "6.4.2",
2828
"workbox-core": "6.4.2",
2929
"workbox-precaching": "6.4.2"
3030
},
3131
"vaadin": {
3232
"dependencies": {
33-
"@polymer/polymer": "3.2.0",
33+
"@polymer/polymer": "3.4.1",
3434
"@vaadin/common-frontend": "0.0.17",
3535
"@vaadin/router": "1.7.4",
3636
"@vaadin/vaadin-lumo-styles": "22.0.0-alpha9",
@@ -41,13 +41,13 @@
4141
"glob": "7.1.6",
4242
"mkdirp": "1.0.4",
4343
"rollup-plugin-brotli": "3.1.0",
44-
"typescript": "4.4.3",
45-
"vite": "v2.7.0",
44+
"typescript": "4.5.3",
45+
"vite": "v2.8.0-beta.5",
4646
"vite-plugin-checker": "0.3.4",
47-
"workbox-build": "6.4.1",
47+
"workbox-build": "6.4.2",
4848
"workbox-core": "6.4.2",
4949
"workbox-precaching": "6.4.2"
5050
},
51-
"hash": "5ce564d422b308509737fbbd7a036ad80c191e574bc151193226da9131b5fcb5"
51+
"hash": "ebde6e0910bd4ca3eebbc855a4b06a9aa2f8621fe76bd2b4d0ef2e7bd203b822"
5252
}
5353
}

‎flow-tests/test-frontend/vite-basics/src/test/java/com/vaadin/viteapp/FileAccessIT.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void expectedFoldersAccessible() throws Exception {
1818
*/
1919
assertAllowed("target/flow-frontend/Flow.js");
2020
assertAllowed("target/frontend/generated-flow-imports.js");
21-
assertAllowed("frontend/index.ts");
21+
assertAllowed("frontend/jsonloader.js");
2222
}
2323

2424
private void assertAllowed(String fileInProject) throws IOException {

0 commit comments

Comments
 (0)