-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUseHTTPSWhenPossibleRule.java
More file actions
76 lines (60 loc) · 2.2 KB
/
UseHTTPSWhenPossibleRule.java
File metadata and controls
76 lines (60 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.sunsecure;
import java.util.regex.Pattern;
import net.sourceforge.pmd.*;
import net.sourceforge.pmd.lang.java.ast.ASTLiteral;
import net.sourceforge.pmd.lang.java.rule.regex.RegexHelper;
import net.sourceforge.pmd.lang.rule.properties.StringProperty;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import java.net.*;
import java.io.*;
import java.net.MalformedURLException;
import java.security.cert.Certificate;
import javax.net.ssl.HttpsURLConnection;
/**
* This class is meant to match literals that contain 'http://' calls and then checks if a valid
* https version exists. If so, the class throws a violation to show that there is an easy-to-fix
* securtity violation
*
* @author Michael Wolf wolfmd@mail.uc.edu, based off Literal Rule by Romain PELISSE, belaran@gmail.com
*/
public class UseHTTPSWhenPossibleRule extends AbstractJavaRule {
/**
* This method checks if the URL starts with http://, if so it tries to ping the
* https version, throwing a violation if it succeeds.
*/
@Override
public Object visit(ASTLiteral node, Object data) {
String image = node.getImage();
int response = 0;
if ( image != null && image.length() > 0 && image.contains("http://") ) {
String new_image = image.replace("http","https");
new_image = new_image.replace("\"","");
URL url = null;
URI uri = null;
try {
url = new URL(new_image.toString());
uri = url.toURI();
try {
url = uri.toURL();
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
response = urlConnection.getResponseCode();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();}
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
if (response == 200) {
addViolation(data, node);
}
}
return data;
}
}