Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit 00a9f88

Browse files
authored
Merge pull request #1 from genexuslabs/issue#80271_extWhitelisting
Adds Sftp Whitelisting
2 parents 461d41e + d4c5691 commit 00a9f88

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

GeneXusSftp/src/main/java/com/genexus/sftp/SftpClient.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.genexus.sftp;
22

33
import com.genexus.commons.sftp.SftpClientObject;
4+
import com.genexus.securityapicommons.utils.ExtensionsWhiteList;
45
import com.genexus.securityapicommons.utils.SecurityUtils;
56
import com.jcraft.jsch.ChannelSftp;
67
import com.jcraft.jsch.JSch;
@@ -12,6 +13,7 @@ public class SftpClient extends SftpClientObject {
1213

1314
private ChannelSftp channel;
1415
private Session session;
16+
private ExtensionsWhiteList whiteList;
1517

1618
public SftpClient() {
1719
super();
@@ -54,10 +56,17 @@ public boolean connect(SftpOptions options) {
5456
this.error.setError("SF004", e.getMessage() + e.getStackTrace());
5557
return false;
5658
}
59+
this.whiteList = options.getWhiteList();
5760
return true;
5861
}
5962

6063
public boolean put(String localPath, String remoteDir) {
64+
if (this.whiteList != null) {
65+
if (!this.whiteList.isValid(localPath)) {
66+
this.error.setError("WL001", "Invalid file extension");
67+
return false;
68+
}
69+
}
6170
if (this.channel == null) {
6271
this.error.setError("SF005", "The channel is invalid, reconect");
6372
return false;
@@ -72,6 +81,12 @@ public boolean put(String localPath, String remoteDir) {
7281
}
7382

7483
public boolean get(String remoteFilePath, String localDir) {
84+
if (this.whiteList != null) {
85+
if (!this.whiteList.isValid(remoteFilePath)) {
86+
this.error.setError("WL002", "Invalid file extension");
87+
return false;
88+
}
89+
}
7590
if (this.channel == null) {
7691
this.error.setError("SF007", "The channel is invalid, reconect");
7792
return false;

GeneXusSftp/src/main/java/com/genexus/sftp/SftpOptions.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.genexus.sftp;
22

33
import com.genexus.securityapicommons.commons.SecurityAPIObject;
4+
import com.genexus.securityapicommons.utils.ExtensionsWhiteList;
45
import com.genexus.securityapicommons.utils.SecurityUtils;
56

67
public class SftpOptions extends SecurityAPIObject {
@@ -13,6 +14,7 @@ public class SftpOptions extends SecurityAPIObject {
1314
private String keyPassword;
1415
private boolean allowHostKeyChecking;
1516
private String knownHostsPath;
17+
private ExtensionsWhiteList whiteList;
1618

1719
public SftpOptions() {
1820
this.host = "";
@@ -23,6 +25,7 @@ public SftpOptions() {
2325
this.keyPassword = "";
2426
this.allowHostKeyChecking = true;
2527
this.knownHostsPath = "";
28+
this.whiteList = null;
2629
}
2730

2831
public void setUser(String value) {
@@ -100,4 +103,12 @@ public void setKnownHostsPath(String value) {
100103
this.knownHostsPath = value.trim();
101104
}
102105
}
106+
107+
public void setWhiteList(ExtensionsWhiteList value) {
108+
this.whiteList = value;
109+
}
110+
111+
public ExtensionsWhiteList getWhiteList() {
112+
return this.whiteList;
113+
}
103114
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.genexus.securityapicommons.utils;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class ExtensionsWhiteList {
7+
8+
private List<String> whitelist;
9+
10+
public ExtensionsWhiteList() {
11+
this.whitelist = new ArrayList<String>();
12+
}
13+
14+
public void setExtension(String value) {
15+
if (value.charAt(0) != '.') {
16+
value = "." + value;
17+
}
18+
this.whitelist.add(value);
19+
}
20+
21+
public boolean isValid(String path) {
22+
if (!isValidName(path)) {
23+
return false;
24+
}
25+
String ext = SecurityUtils.getFileExtension(path);
26+
for (int i = 0; i <= this.whitelist.size(); i++) {
27+
if (SecurityUtils.compareStrings(ext, this.whitelist.get(i))) {
28+
return true;
29+
}
30+
}
31+
return false;
32+
}
33+
34+
public boolean isEmpty() {
35+
if (this.whitelist.size() == 0) {
36+
return true;
37+
}
38+
return false;
39+
}
40+
41+
private boolean isValidName(String path) {
42+
int counter = 0;
43+
int i = 0;
44+
while (i < path.length() && counter <= 2) {
45+
if (path.charAt(i) == '.') {
46+
counter++;
47+
}
48+
i++;
49+
}
50+
if (counter >= 2) {
51+
return false;
52+
}
53+
return true;
54+
}
55+
}

0 commit comments

Comments
 (0)