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

2.0: Fix #1711 StaticContentHandler fails with encoded URLs #1811

Merged
merged 2 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ boolean doHandle(Http.RequestMethod method, String requestedPath, ServerRequest
LOGGER.finest(() -> "Requested class path resource: " + resource);

// this MUST be done, so we do not escape the bounds of configured directory
String requestedResource = URI.create(resource).normalize().toString();
// We use multi-arg constructor so it performs url encoding
URI myuri = new URI(null, null, resource, null);
barchetta marked this conversation as resolved.
Show resolved Hide resolved
String requestedResource = myuri.normalize().getPath();

if (!requestedResource.equals(root) && !requestedResource.startsWith(rootWithTrailingSlash)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2020 Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@
package io.helidon.webserver;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -218,6 +219,17 @@ public void handleNextOnFalse() throws Exception {
assertThat(handler.counter.get(), is(1));
}

@Test
public void classpathHandleSpaces() throws Exception {
ServerRequest request = mockRequestWithPath("foo/I have spaces.txt");
ServerResponse response = mock(ServerResponse.class);
TestClassPathContentHandler handler = new TestClassPathContentHandler("/root", true);
handler.handle(Http.Method.GET, request, response);
verify(request, never()).next();
assertThat(handler.counter.get(), is(1));
}


static class TestContentHandler extends FileSystemContentHandler {

final AtomicInteger counter = new AtomicInteger(0);
Expand All @@ -242,4 +254,28 @@ boolean doHandle(Http.RequestMethod method, Path path, ServerRequest request, Se
}

}

static class TestClassPathContentHandler extends ClassPathContentHandler {

final AtomicInteger counter = new AtomicInteger(0);
final boolean returnValue;

TestClassPathContentHandler(String welcomeFilename, ContentTypeSelector contentTypeSelector, Path root, boolean returnValue) {
super(welcomeFilename, contentTypeSelector, root.toString(), null);
this.returnValue = returnValue;
}

TestClassPathContentHandler(String path, boolean returnValue) {
this(null, mock(ContentTypeSelector.class), Paths.get(path), returnValue);
}

@Override
boolean doHandle(Http.RequestMethod method, String path, ServerRequest request, ServerResponse response)
throws IOException, URISyntaxException {
super.doHandle(method, path, request, response);
this.counter.incrementAndGet();
return returnValue;
}

}
}