Skip to content

Failed to scan resources when exits non ascii characters in resource path on SpringBootVFS #635

Closed
@MisterChangRay

Description

@MisterChangRay

in SpringBootVFS

  @Override
  protected List<String> list(URL url, String path) throws IOException {
    String urlString = url.toString();
    String baseUrlString = urlString.endsWith("/") ? urlString : urlString.concat("/");
    Resource[] resources = resourceResolver.getResources(baseUrlString + "**/*.class");
    return Stream.of(resources).map(resource -> preserveSubpackageName(baseUrlString, resource, path))
        .collect(Collectors.toList());
  }

  private static String preserveSubpackageName(final String baseUrlString, final Resource resource,
      final String rootPath) {
    try {
      return rootPath + (rootPath.endsWith("/") ? "" : "/")
          + resource.getURL().toString().substring(baseUrlString.length());
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }

consider url may be containes none asccii charctor; such as chinese path.
the url.toString() return URLEncode result,
so the baseUrlString.length() should be grater than resource.getURL();

The bug exists in following code:

// if baseUrlString containes Space character, the baseUrlString.length() > resource.getURL().toString().length() is True 
// so there throw StringIndexOutOfBoundsException

 return rootPath + (rootPath.endsWith("/") ? "" : "/")
          + resource.getURL().toString().substring(baseUrlString.length());

微信截图_20220309143843

微信截图_20220309143922

微信截图_20220309154324

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions