Skip to content

WebUtils#extractFilenameFromUrlPath IndexOutOfBoundsException [SPR-5112] #9785

@spring-projects-issues

Description

@spring-projects-issues

Zach Legein opened SPR-5112 and commented

A url like this will cause an error:

http://www.website.com/test.html?file=/path/to/file

My suggestion would be to do something like this:

    int begin = urlPath.indexOf("://");
    if (begin == -1) {
        return null;
    }

    String body = urlPath.substring(begin + "://".length(), urlPath.length());
    if (!body.contains("/")) {
        return null;
    }

    int end = urlPath.indexOf('?');
    if (end == -1) {
        end = urlPath.length();
    }

    String url = urlPath.substring(0, end);
    begin = url.lastIndexOf("/") + 1;
    String filename = url.substring(begin, url.length());
    int dotIndex = filename.lastIndexOf('.');
    if (dotIndex != -1) {
        filename = filename.substring(0, dotIndex);
    }
    return filename;

Affects: 2.5.5

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions