@Override @Nullable public Resource resolveResource( @Nullable HttpServletRequest request, String requestPath, List<? extends Resource> locations) { return (this.resolver != null && this.nextChain != null ? this.resolver.resolveResource(request, requestPath, locations, this.nextChain) : null); }
@Override @Nullable public String resolveUrlPath(String resourcePath, List<? extends Resource> locations) { return (this.resolver != null && this.nextChain != null ? this.resolver.resolveUrlPath(resourcePath, locations, this.nextChain) : null); }
@Override @Nullable public String resolveUrlPath(String resourcePath, List<? extends Resource> locations) { return (this.resolver != null && this.nextChain != null ? this.resolver.resolveUrlPath(resourcePath, locations, this.nextChain) : null); }
@Override @Nullable public Resource resolveResource( @Nullable HttpServletRequest request, String requestPath, List<? extends Resource> locations) { return (this.resolver != null && this.nextChain != null ? this.resolver.resolveResource(request, requestPath, locations, this.nextChain) : null); }
@Override public String resolveUrlPath(String resourcePath, List<? extends Resource> locations) { ResourceResolver resolver = getNext(); if (resolver == null) { return null; } try { String returnPath = resolver.resolveUrlPath(resourcePath, locations, this); if (LOG.isTraceEnabled()) { LOG.trace("The return path for " + resourcePath + " from resolver " + resolver + " is " + returnPath); } return returnPath; } finally { this.index--; } }
@Override public Resource resolveResource(HttpServletRequest request, String requestPath, List<? extends Resource> locations) { ResourceResolver resolver = getNext(); if (resolver == null) { return null; } try { return resolver.resolveResource(request, requestPath, locations, this); } finally { this.index--; } }
@Test // SPR-16296 public void getForLookupPathShouldNotFailIfPathContainsDoubleSlashes() { // given ResourceResolver mockResourceResolver = mock(ResourceResolver.class); when(mockResourceResolver.resolveUrlPath(any(), any(), any())).thenReturn("some-path"); ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler(); handler.getResourceResolvers().add(mockResourceResolver); ResourceUrlProvider provider = new ResourceUrlProvider(); provider.getHandlerMap().put("/some-pattern/**", handler); // when String lookupForPath = provider.getForLookupPath("/some-pattern/some-lib//some-resource"); // then assertEquals("/some-pattern/some-path", lookupForPath); }
@Test public void testInvalidPath() throws Exception { // Use mock ResourceResolver: i.e. we're only testing upfront validations... Resource resource = mock(Resource.class); when(resource.getFilename()).thenThrow(new AssertionError("Resource should not be resolved")); when(resource.getInputStream()).thenThrow(new AssertionError("Resource should not be resolved")); ResourceResolver resolver = mock(ResourceResolver.class); when(resolver.resolveResource(any(), any(), any(), any())).thenReturn(resource); ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler(); handler.setLocations(Collections.singletonList(new ClassPathResource("test/", getClass()))); handler.setResourceResolvers(Collections.singletonList(resolver)); handler.setServletContext(new TestServletContext()); handler.afterPropertiesSet(); testInvalidPath("../testsecret/secret.txt", handler); testInvalidPath("test/../../testsecret/secret.txt", handler); testInvalidPath(":/../../testsecret/secret.txt", handler); Resource location = new UrlResource(getClass().getResource("./test/")); this.handler.setLocations(Collections.singletonList(location)); Resource secretResource = new UrlResource(getClass().getResource("testsecret/secret.txt")); String secretPath = secretResource.getURL().getPath(); testInvalidPath("file:" + secretPath, handler); testInvalidPath("/file:" + secretPath, handler); testInvalidPath("url:" + secretPath, handler); testInvalidPath("/url:" + secretPath, handler); testInvalidPath("/../.." + secretPath, handler); testInvalidPath("/%2E%2E/testsecret/secret.txt", handler); testInvalidPath("/%2E%2E/testsecret/secret.txt", handler); testInvalidPath("%2F%2F%2E%2E%2F%2F%2E%2E" + secretPath, handler); }
@Override public String resolveUrlPath(String resourcePath, List<? extends Resource> locations) { ResourceResolver resolver = getNext(); if (resolver == null) { return null; } try { return resolver.resolveUrlPath(resourcePath, locations, this); } finally { this.index--; } }
@Override public Resource resolveResource(HttpServletRequest request, String requestPath, List<? extends Resource> locations) { ResourceResolver resolver = getNext(); if (resolver == null) { return null; } try { return resolver.resolveResource(request, requestPath, locations, this); } finally { this.index--; } }