Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hotfix][Connector-V2][SFTP] Add quote to sftp file names with wildcard characters #8501

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from

Conversation

e-mhui
Copy link

@e-mhui e-mhui commented Jan 11, 2025

[Hotfix][Connector-V2][SFTP] Add quote to sftp file names with wildcard characters

Purpose of this pull request

Fix #8500.

Does this PR introduce any user-facing change?

No.

How was this patch tested?

Check list

@corgy-w
Copy link
Contributor

corgy-w commented Jan 11, 2025

@e-mhui Thank you for your contribution. Please open ci according to the guidance and add the tests corresponding to the modified parts.

@e-mhui
Copy link
Author

e-mhui commented Jan 11, 2025

@e-mhui Thank you for your contribution. Please open ci according to the guidance and add the tests corresponding to the modified parts.

Ok, I'll add the test later.

@github-actions github-actions bot added the e2e label Jan 12, 2025
Copy link
Member

@Hisoka-X Hisoka-X left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @e-mhui , does other file system will face same problems?

@e-mhui
Copy link
Author

e-mhui commented Jan 15, 2025

Hi @e-mhui , does other file system will face same problems?

@Hisoka-X Other file systems will not have this problem. This is because the code in jsch uses get to retrieve the file stream, which calls the isUnique method to check if the file is unique. However, file names with wildcards will match multiple files, causing is not unique error. If we already know the file exists, we can add escape characters to avoid this problem.

https://github.com/rtyley/jsch/blob/96b0558b66ec8982e708e46b6e5a254a3650d01b/src/com/jcraft/jsch/ChannelSftp.java#L864

public InputStream get(String src, final SftpProgressMonitor monitor, final long skip) throws SftpException{

    try{
      ((MyPipedInputStream)io_in).updateReadSide();

      src=remoteAbsolutePath(src);
      src=isUnique(src);
     //........
   }
}

  /**
   * This method will check if the given string can be expanded to the
   * unique string.  If it can be expanded to mutiple files, SftpException
   * will be thrown.
   * @return the returned string is unquoted.
   */
  private String isUnique(String path) throws SftpException, Exception{
    Vector v=glob_remote(path);
    if(v.size()!=1){
      throw new SftpException(SSH_FX_FAILURE, path+" is not unique: "+v.toString());
    }
    return (String)(v.elementAt(0));
  }

Comment on lines +158 to +179
private String quote(String path) {
byte[] _path = path.getBytes(StandardCharsets.UTF_8);
int count = 0;
for (int i = 0; i < _path.length; i++) {
byte b = _path[i];
if (b == '\\' || b == '?' || b == '*') {
count++;
}
}
if (count == 0) {
return path;
}
byte[] _path2 = new byte[_path.length + count];
for (int i = 0, j = 0; i < _path.length; i++) {
byte b = _path[i];
if (b == '\\' || b == '?' || b == '*') {
_path2[j++] = '\\';
}
_path2[j++] = b;
}
return new String(_path2, 0, _path2.length, StandardCharsets.UTF_8);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a UT for this method.

rules {
row_rules = [
{
rule_type = MAX_ROW
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rule_type = MAX_ROW
rule_type = MIN_ROW

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants