[ 
https://issues.apache.org/jira/browse/VFS-840?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17883403#comment-17883403
 ] 

Matthew Bellew edited comment on VFS-840 at 9/20/24 8:24 PM:
-------------------------------------------------------------

I've just hit this, in trying to sort this out I discovered various JDK and VFS 
inconsistencies (alluded to above).  In trying to make the simplest repro I 
found 5 chars that are poison to LocalFIle.getPath().  They are ?[]{}

This is a challenge for adopting VFS because I'd have to somehow enforce that 
across the entire codebase no one uses FileObject.getPath() and always use 
FileObject.getName().

 
{quote}java.util.List<String> bad = java.util.Arrays.asList(
    "file:///%3F",    //  /?        this fails differently than the others 
(getURI() is OK)
    "file:///%5B",    //  /[
    "file:///%5D",    //  /]
    "file:///%7B",    //  /{
    "file:///%7D"     //  /}
);

for (String uriString : bad)
{
    // TRY ENCODED
    out.println(uriString);
    try
    {
        FileObject fo = VFS.getManager().resolveFile(uriString);
        fo.getName().getPathDecoded();
        fo.getURI();
        fo.getPath();
    }
    catch (Exception x)
    {
        out.println(x.getClass().getName() + " " + x.getMessage());
    }
    // TRY DECODED
    out.println(URI.create(uriString).getPath());
    try

    {
        FileObject fo = 
VFS.getManager().resolveFile(URI.create(uriString).getPath());
        fo.getName().getPathDecoded();
        fo.getURI();
        fo.getPath();
    }
    catch (Exception x)
    {
        out.println(x.getClass().getName() + " " + x.getMessage());
    }
}
{quote}


was (Author: mbellew):
I've just hit this, in trying to sort this out I discovered various JDK and VFS 
inconsistencies (alluded to above).  In trying to make the simplest repro I 
found 5 chars that are poison to LocalFIle.getPath().  They are ?[]{}

This is a challenge for adopting VFS because I'd have to somehow enforce that 
across the entire codebase no one uses FileObject.getPath() and always use 
FileObject.getName().

 
{quote}java.util.List<String> bad = java.util.Arrays.asList(
    "file:///%3F",    //  /?        this fails differently than the others 
(getURI() is OK)
    "file:///%5B",    //  /[
    "file:///%5D",    //  /]
    "file:///%7B",    //  /{
    "file:///%7D"     //  /}
);

for (String uriString : bad)
{
    // TRY ENCODED
    out.println(uriString);
    try
    {
        FileObject fo = VFS.getManager().resolveFile(uriString);
        fo.getName().getPathDecoded();
        fo.getURI();
        fo.getPath();
    }
    catch (Exception x)
    {
        out.println(x.getClass().getName() + " " + x.getMessage());
    }
    // TRY DECODED
    out.println(URI.create(uriString).getPath());
    try

    {
        FileObject fo = 
VFS.getManager().resolveFile(URI.create(uriString).getPath());
        fo.getName().getPathDecoded();
        fo.getURI();
        fo.getPath();
    }
    catch (Exception x)
    {
        out.println(x.getClass().getName() + " " + x.getMessage());
    }
}
{quote}

> IllegalArgumentException when using special characters in filename eg [ or ] 
> and calling FileObject.getPath method
> ------------------------------------------------------------------------------------------------------------------
>
>                 Key: VFS-840
>                 URL: https://issues.apache.org/jira/browse/VFS-840
>             Project: Commons VFS
>          Issue Type: Bug
>    Affects Versions: 2.9.0
>         Environment: Windows 10
> Java 17
> Apache VFS 2.9.0
>            Reporter: Usman Ashraf Bajwah
>            Priority: Major
>
> When special characters ([ or ] for example there might be others also) are 
> used in filename FileObject resolve it alright but calling its getPath method 
> fails with following exception.
>  
> +java.lang.IllegalArgumentException+{color:#ff0000}: Illegal character in 
> path at index 17: file:///D:/citrus[1].jpg{color}
> {color:#ff0000} at 
> java.base/java.net.URI.create({color}+URI.java:906+{color:#ff0000}){color}
> {color:#ff0000} at 
> org.apache.commons.vfs2.FileObject.getURI({color}+FileObject.java:310+{color:#ff0000}){color}
> {color:#ff0000} at 
> org.apache.commons.vfs2.FileObject.getPath({color}+FileObject.java:320+{color:#ff0000}){color}
> {color:#ff0000} at 
> com.gallerysystems.tms.common.util.FileUtil.main({color}+FileUtil.java:859+{color:#ff0000}){color}
> {color:#ff0000}Caused by: 
> {color}+java.net.URISyntaxException+{color:#ff0000}: Illegal character in 
> path at index 17: file:///D:/citrus[1].jpg{color}
> {color:#ff0000} at 
> java.base/java.net.URI$Parser.fail({color}+URI.java:2974+{color:#ff0000}){color}
> {color:#ff0000} at 
> java.base/java.net.URI$Parser.checkChars({color}+URI.java:3145+{color:#ff0000}){color}
> {color:#ff0000} at 
> java.base/java.net.URI$Parser.parseHierarchical({color}+URI.java:3227+{color:#ff0000}){color}
> {color:#ff0000} at 
> java.base/java.net.URI$Parser.parse({color}+URI.java:3175+{color:#ff0000}){color}
> {color:#ff0000} at 
> java.base/java.net.URI.<init>({color}+URI.java:623+{color:#ff0000}){color}
> {color:#ff0000} at 
> java.base/java.net.URI.create({color}+URI.java:904+{color:#ff0000}){color}
> {color:#ff0000} ... 3 more{color}
>  
> Following is the code to reproduce the issue.
> {color:#7f0055}try{color}{color:#000000} {{color}
> {color:#000000} String {color}{color:#6a3e3e}fileName{color}{color:#000000} = 
> {color}{color:#2a00ff}"D:\\citrus[1].jpg"{color}{color:#000000};{color}
> {color:#000000} FileSystemManager 
> {color}{color:#6a3e3e}fsManager{color}{color:#000000} = 
> VFS.{color}{color:#000000}getManager{color}{color:#000000}();{color}
> {color:#000000} FileObject 
> {color}{color:#6a3e3e}fileObject{color}{color:#000000} = 
> {color}{color:#6a3e3e}fsManager{color}{color:#000000}.resolveFile({color}{color:#6a3e3e}fileName{color}{color:#000000});{color}
> {color:#000000} 
> System.{color}{color:#0000c0}out{color}{color:#000000}.println({color}{color:#6a3e3e}fileObject{color}{color:#000000}.getPath());{color}
> {color:#000000} }{color}{color:#7f0055}catch{color}{color:#000000}(Exception 
> {color}{color:#6a3e3e}ex{color}{color:#000000}) {{color}
> {color:#000000} 
> {color}{color:#6a3e3e}ex{color}{color:#000000}.printStackTrace();{color}
> {color:#000000} }{color}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to