Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-16296

ResourceUrlProvider.getForLookupPath fails if path contains double slashes

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 5.0.2
    • Fix Version/s: 5.0.3
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      If the ResourceUrlProvider.getLookupForPath(String) method is called with a path containing double slashes, a StringIndexOutOfBoundsException is thrown. This situation may arise, for example, if a third-party library is sloppily programmed and contains references to resources with double slashes. As developers may not be able to modify such libraries, the ResourceUrlProvider should be able to handle such requests.

      Below is a test case illustrating the issue.

      import org.junit.Test;
      import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
      import org.springframework.web.servlet.resource.ResourceResolver;
      import org.springframework.web.servlet.resource.ResourceUrlProvider;
      
      import static org.junit.Assert.assertEquals;
      import static org.mockito.ArgumentMatchers.any;
      import static org.mockito.Mockito.mock;
      import static org.mockito.Mockito.when;
      
      public class ResourceUrlProviderTest {
      
          @Test
          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);
          }
      }
      

        Attachments

          Activity

            People

            • Assignee:
              rstoya05-aop Rossen Stoyanchev
              Reporter:
              freitagm Michael Freitag
              Last updater:
              Spring Issuemaster
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                35 weeks, 5 days ago