Skip to content

Commit 5b199c9

Browse files
committed
scan rules: Clean code tweaks
- Add static modifier where applicable. - CHANGELOG > Add maintenance note (if there wasn't already one present). - pscanrules > Made resource message methods private again where example alerts have been implemented, or removed them where there was only a single usage (inlining the Contstant resource message usage).
1 parent 2e960a0 commit 5b199c9

File tree

98 files changed

+245
-520
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+245
-520
lines changed

addOns/ascanrules/CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ All notable changes to this add-on will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
55

66
## Unreleased
7-
7+
### Changed
8+
- Maintenance changes.
89

910
## [67] - 2024-07-22
1011

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/BufferOverflowScanRule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public int getWascId() {
169169
return 7;
170170
}
171171

172-
private String randomCharacterString(int length) {
172+
private static String randomCharacterString(int length) {
173173
StringBuilder sb1 = new StringBuilder(length + 1);
174174
int counter = 0;
175175
int character = 0;

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/CommandInjectionScanRule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ public int getRisk() {
366366
return Alert.RISK_HIGH;
367367
}
368368

369-
private String getOtherInfo(TestType testType, String testValue) {
369+
private static String getOtherInfo(TestType testType, String testValue) {
370370
return Constant.messages.getString(
371371
MESSAGE_PREFIX + "otherinfo." + testType.getNameKey(), testValue);
372372
}

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/DirectoryBrowsingScanRule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public String getReference() {
9595
return Constant.messages.getString(MESSAGE_PREFIX + "refs");
9696
}
9797

98-
private void checkIfDirectory(HttpMessage msg) throws URIException {
98+
private static void checkIfDirectory(HttpMessage msg) throws URIException {
9999

100100
URI uri = msg.getRequestHeader().getURI();
101101
uri.setQuery(null);

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/ExternalRedirectScanRule.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ private static boolean isRedirectHost(String value, boolean escaped) throws URIE
342342
* @param msg the current message where reflected redirection should be check into
343343
* @return get back the redirection type if exists
344344
*/
345-
private int isRedirected(String payload, HttpMessage msg) {
345+
private static int isRedirected(String payload, HttpMessage msg) {
346346

347347
// (1) Check if redirection by "Location" header
348348
// http://en.wikipedia.org/wiki/HTTP_location
@@ -471,7 +471,7 @@ private static boolean isRedirectPresent(Pattern pattern, String value) {
471471
* @param type the redirection type
472472
* @return a string representing the reason of this redirection
473473
*/
474-
private String getRedirectionReason(int type) {
474+
private static String getRedirectionReason(int type) {
475475
switch (type) {
476476
case REDIRECT_LOCATION_HEADER:
477477
return Constant.messages.getString(MESSAGE_PREFIX + "reason.location.header");

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/FormatStringScanRule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public String getReference() {
105105
return Constant.messages.getString(MESSAGE_PREFIX + "refs");
106106
}
107107

108-
private String getError(char c) {
108+
private static String getError(char c) {
109109
return Constant.messages.getString(MESSAGE_PREFIX + "error" + c);
110110
}
111111

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/PaddingOracleScanRule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ private String getEmptyValueResponse(String paramName) throws IOException {
267267
* @param value the value that need to be checked
268268
* @return true if it seems to be encrypted
269269
*/
270-
private boolean isEncrypted(byte[] value) {
270+
private static boolean isEncrypted(byte[] value) {
271271

272272
// Make sure we have a reasonable sized string
273273
// (encrypted strings tend to be long, and short strings tend to break our numbers)

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/PathTraversalScanRule.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ private boolean sendAndCheckPayload(
608608
return false;
609609
}
610610

611-
private String getContentsToMatch(HttpMessage message) {
611+
private static String getContentsToMatch(HttpMessage message) {
612612
return message.getResponseHeader().isHtml()
613613
? StringEscapeUtils.unescapeHtml4(message.getResponseBody().toString())
614614
: message.getResponseHeader().toString() + message.getResponseBody().toString();
@@ -700,7 +700,7 @@ public String match(String contents) {
700700
return matchWinDirectories(contents);
701701
}
702702

703-
private String matchNixDirectories(String contents) {
703+
private static String matchNixDirectories(String contents) {
704704
Pattern procPattern =
705705
Pattern.compile("(?:^|\\W)proc(?:\\W|$)", Pattern.CASE_INSENSITIVE);
706706
Pattern etcPattern = Pattern.compile("(?:^|\\W)etc(?:\\W|$)", Pattern.CASE_INSENSITIVE);
@@ -727,7 +727,7 @@ private String matchNixDirectories(String contents) {
727727
return null;
728728
}
729729

730-
private String matchWinDirectories(String contents) {
730+
private static String matchWinDirectories(String contents) {
731731
if (contents.contains("Windows")
732732
&& Pattern.compile("Program\\sFiles").matcher(contents).find()) {
733733
return "Windows";

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/SourceCodeDisclosureWebInfScanRule.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ private HttpMessage createHttpMessage(URI uri) throws HttpMalformedHeaderExcepti
277277
* @return
278278
* @throws URIException
279279
*/
280-
private URI getClassURI(URI hostURI, String classname) throws URIException {
280+
private static URI getClassURI(URI hostURI, String classname) throws URIException {
281281
return new URI(
282282
hostURI.getScheme()
283283
+ "://"
@@ -288,7 +288,7 @@ private URI getClassURI(URI hostURI, String classname) throws URIException {
288288
false);
289289
}
290290

291-
private URI getPropsFileURI(URI hostURI, String propsfilename) throws URIException {
291+
private static URI getPropsFileURI(URI hostURI, String propsfilename) throws URIException {
292292
return new URI(
293293
hostURI.getScheme()
294294
+ "://"

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/Spring4ShellScanRule.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ public String getDescription() {
7676
return Constant.messages.getString("ascanrules.spring4shell.desc");
7777
}
7878

79-
private boolean is400Response(HttpMessage msg) {
79+
private static boolean is400Response(HttpMessage msg) {
8080
return !msg.getResponseHeader().isEmpty() && msg.getResponseHeader().getStatusCode() == 400;
8181
}
8282

83-
private void setGetPayload(HttpMessage msg, String payload) throws URIException {
83+
private static void setGetPayload(HttpMessage msg, String payload) throws URIException {
8484
msg.getRequestHeader().setMethod("GET");
8585
URI uri = msg.getRequestHeader().getURI();
8686
String query = uri.getEscapedQuery();
@@ -92,7 +92,7 @@ private void setGetPayload(HttpMessage msg, String payload) throws URIException
9292
uri.setEscapedQuery(query);
9393
}
9494

95-
private void setPostPayload(HttpMessage msg, String payload) {
95+
private static void setPostPayload(HttpMessage msg, String payload) {
9696
msg.getRequestHeader().setMethod("POST");
9797
String body = msg.getRequestBody().toString();
9898
if (body.isEmpty()

addOns/ascanrules/src/test/java/org/zaproxy/zap/extension/ascanrules/ExternalRedirectScanRuleUnitTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ private enum PayloadHandling {
8080
CONCAT_PATH
8181
};
8282

83-
private NanoServerHandler createHttpRedirectHandler(String path, String header) {
83+
private static NanoServerHandler createHttpRedirectHandler(String path, String header) {
8484
return createHttpRedirectHandler(path, header, PayloadHandling.NEITHER);
8585
}
8686

87-
private NanoServerHandler createHttpRedirectHandler(
87+
private static NanoServerHandler createHttpRedirectHandler(
8888
String path, String header, PayloadHandling payloadHandling) {
8989
return new NanoServerHandler(path) {
9090
@Override

addOns/ascanrules/src/test/java/org/zaproxy/zap/extension/ascanrules/HiddenFilesScanRuleUnitTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void checkNoPathsHaveLeadingSlash() {
110110
}
111111
}
112112

113-
private void assertNoLeadingSlash(String message, String path) {
113+
private static void assertNoLeadingSlash(String message, String path) {
114114
assertThat(message.replace(REPLACE_TOKEN, path), !path.startsWith("/"), is(true));
115115
}
116116

addOns/ascanrules/src/test/java/org/zaproxy/zap/extension/ascanrules/XxeScanRuleUnitTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ void shouldAlertOnlyIfCertainTagValuesArePresent()
314314
assertThat(alert.getConfidence(), equalTo(Alert.CONFIDENCE_MEDIUM));
315315
}
316316

317-
private NanoServerHandler createNanoHandler(
317+
private static NanoServerHandler createNanoHandler(
318318
String path, NanoHTTPD.Response.IStatus status, String responseBody) {
319319
return new NanoServerHandler(path) {
320320
@Override

addOns/ascanrulesAlpha/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66
## Unreleased
77
### Changed
88
- Update minimum ZAP version to 2.15.0.
9+
- Maintenance changes.
910

1011
### Fixed
1112
- Alert text for various rules has been updated to more consistently use periods and spaces in a uniform manner.

addOns/ascanrulesAlpha/src/main/java/org/zaproxy/zap/extension/ascanrulesAlpha/ExampleFileActiveScanRule.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ public String getDescription() {
8080
return Constant.messages.getString(MESSAGE_PREFIX + "desc");
8181
}
8282

83-
private String getOtherInfo() {
84-
return Constant.messages.getString(MESSAGE_PREFIX + "other");
85-
}
86-
8783
@Override
8884
public String getSolution() {
8985
return Constant.messages.getString(MESSAGE_PREFIX + "soln");
@@ -159,7 +155,7 @@ public void scan(HttpMessage msg, String param, String value) {
159155
.setConfidence(Alert.CONFIDENCE_MEDIUM)
160156
.setParam(param)
161157
.setAttack(attack)
162-
.setOtherInfo(getOtherInfo())
158+
.setOtherInfo(Constant.messages.getString(MESSAGE_PREFIX + "other"))
163159
.setEvidence(evidence)
164160
.setMessage(testMsg)
165161
.raise();
@@ -194,7 +190,7 @@ private String doesResponseContainString(HttpBody body, String str) {
194190
return null;
195191
}
196192

197-
private List<String> loadFile(String file) {
193+
private static List<String> loadFile(String file) {
198194
/*
199195
* ZAP will have already extracted the file from the add-on and put it underneath the 'ZAP home' directory
200196
*/

addOns/ascanrulesBeta/src/main/java/org/zaproxy/zap/extension/ascanrulesBeta/BackupFileDisclosureScanRule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ public List<Alert> getExampleAlerts() {
426426
.build());
427427
}
428428

429-
private boolean isEmptyResponse(byte[] response) {
429+
private static boolean isEmptyResponse(byte[] response) {
430430
return response.length == 0;
431431
}
432432

addOns/ascanrulesBeta/src/main/java/org/zaproxy/zap/extension/ascanrulesBeta/HttpParameterPollutionScanRule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public TreeSet<HtmlParameter> getParams(Source s, List<Element> inputTags) {
242242
* @param url found in the body of the targeted page
243243
* @return a hashmap of the query string
244244
*/
245-
private Map<String, List<String>> getUrlParameters(String url) {
245+
private static Map<String, List<String>> getUrlParameters(String url) {
246246
Map<String, List<String>> params = new HashMap<>();
247247

248248
if (url != null) {

addOns/ascanrulesBeta/src/main/java/org/zaproxy/zap/extension/ascanrulesBeta/IntegerOverflowScanRule.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public String getReference() {
8585
return Constant.messages.getString(MESSAGE_PREFIX + "refs");
8686
}
8787

88-
private String getError(char c) {
88+
private static String getError(char c) {
8989
return Constant.messages.getString(MESSAGE_PREFIX + "error" + c);
9090
}
9191

@@ -145,7 +145,7 @@ public Map<String, String> getAlertTags() {
145145
return ALERT_TAGS;
146146
}
147147

148-
private String randomIntegerString(int length) {
148+
private static String randomIntegerString(int length) {
149149

150150
int numbercounter = 0;
151151
int character = 0;
@@ -169,7 +169,7 @@ private String randomIntegerString(int length) {
169169
return sb1.toString();
170170
}
171171

172-
private String singleString(int length, char c) // Single Character String
172+
private static String singleString(int length, char c) // Single Character String
173173
{
174174

175175
int numbercounter = 0;
@@ -241,7 +241,7 @@ private AlertBuilder buildAlert(
241241
.setUri(url)
242242
.setParam(param)
243243
.setAttack(attack)
244-
.setOtherInfo(this.getError(type))
244+
.setOtherInfo(IntegerOverflowScanRule.getError(type))
245245
.setEvidence(evidence);
246246
}
247247
}

addOns/ascanrulesBeta/src/main/java/org/zaproxy/zap/extension/ascanrulesBeta/ProxyDisclosureScanRule.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ public void scan() {
752752
Constant.messages.getString(
753753
MESSAGE_PREFIX + "desc",
754754
step2numberOfNodes - 1 + silentProxySet.size()))
755-
.setAttack(getAttack())
755+
.setAttack(Constant.messages.getString(MESSAGE_PREFIX + "attack"))
756756
.setOtherInfo(extraInfo)
757757
.setMessage(getBaseMsg())
758758
.raise();
@@ -765,18 +765,14 @@ public void scan() {
765765
}
766766
}
767767

768-
private String getPath(URI uri) {
768+
private static String getPath(URI uri) {
769769
String path = uri.getEscapedPath();
770770
if (path != null) {
771771
return path;
772772
}
773773
return "/";
774774
}
775775

776-
private String getAttack() {
777-
return Constant.messages.getString(MESSAGE_PREFIX + "attack");
778-
}
779-
780776
@Override
781777
public int getRisk() {
782778
return Alert.RISK_MEDIUM;

addOns/ascanrulesBeta/src/main/java/org/zaproxy/zap/extension/ascanrulesBeta/RelativePathConfusionScanRule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ private AlertBuilder buildAlert(String attack, String otherInfo, String evidence
645645
.setEvidence(evidence);
646646
}
647647

648-
private Matcher matchStyles(String body) {
648+
private static Matcher matchStyles(String body) {
649649
// remove all " and ' for proper matching url('somefile.png')
650650
String styleBody = body.replaceAll("['\"]", "");
651651
return STYLE_URL_LOAD.matcher(styleBody);

addOns/ascanrulesBeta/src/main/java/org/zaproxy/zap/extension/ascanrulesBeta/SessionFixationScanRule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,7 @@ private static void logSessionFixation(
13171317
* @param cookieName
13181318
* @return the HtmlParameter representing the cookie, or null if no matching cookie was found
13191319
*/
1320-
private HtmlParameter getResponseCookie(HttpMessage message, String cookieName) {
1320+
private static HtmlParameter getResponseCookie(HttpMessage message, String cookieName) {
13211321
TreeSet<HtmlParameter> cookieBackParams = message.getResponseHeader().getCookieParams();
13221322
if (cookieBackParams.isEmpty()) {
13231323
// no cookies

addOns/ascanrulesBeta/src/main/java/org/zaproxy/zap/extension/ascanrulesBeta/SlackerCookieScanRule.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ private AlertBuilder createAlert(int risk, String otherInfo) {
212212
return newAlert().setRisk(risk).setConfidence(Alert.CONFIDENCE_LOW).setOtherInfo(otherInfo);
213213
}
214214

215-
private StringBuilder createOtherInfoText(
215+
private static StringBuilder createOtherInfoText(
216216
Set<String> cookiesThatMakeADifference, Set<String> cookiesThatDoNOTMakeADifference) {
217217

218218
StringBuilder otherInfoBuff =
@@ -228,15 +228,15 @@ private StringBuilder createOtherInfoText(
228228
return otherInfoBuff;
229229
}
230230

231-
private void listCookies(Set<String> cookieSet, StringBuilder otherInfoBuff) {
231+
private static void listCookies(Set<String> cookieSet, StringBuilder otherInfoBuff) {
232232
Iterator<String> itYes = cookieSet.iterator();
233233
while (itYes.hasNext()) {
234234
formatCookiesList(otherInfoBuff, itYes);
235235
}
236236
otherInfoBuff.append(getEOL());
237237
}
238238

239-
private int calculateRisk(
239+
private static int calculateRisk(
240240
Set<String> cookiesThatDoNOTMakeADifference, StringBuilder otherInfoBuff) {
241241
int riskLevel = Alert.RISK_INFO;
242242
for (String cookie : cookiesThatDoNOTMakeADifference) {
@@ -252,35 +252,36 @@ private int calculateRisk(
252252
return riskLevel;
253253
}
254254

255-
private String getSessionDestroyedText(String cookie) {
255+
private static String getSessionDestroyedText(String cookie) {
256256
return Constant.messages.getString("ascanbeta.cookieslack.session.destroyed", cookie);
257257
}
258258

259-
private String getAffectResponseYes() {
259+
private static String getAffectResponseYes() {
260260
return Constant.messages.getString("ascanbeta.cookieslack.affect.response.yes");
261261
}
262262

263-
private String getAffectResponseNo() {
263+
private static String getAffectResponseNo() {
264264
return Constant.messages.getString("ascanbeta.cookieslack.affect.response.no");
265265
}
266266

267-
private String getSeparator() {
267+
private static String getSeparator() {
268268
return Constant.messages.getString("ascanbeta.cookieslack.separator");
269269
}
270270

271-
private String getEOL() {
271+
private static String getEOL() {
272272
return Constant.messages.getString("ascanbeta.cookieslack.endline");
273273
}
274274

275-
private void formatCookiesList(StringBuilder otherInfoBuff, Iterator<String> cookieIterator) {
275+
private static void formatCookiesList(
276+
StringBuilder otherInfoBuff, Iterator<String> cookieIterator) {
276277

277278
otherInfoBuff.append(cookieIterator.next());
278279
if (cookieIterator.hasNext()) {
279280
otherInfoBuff.append(getSeparator());
280281
}
281282
}
282283

283-
private String getSessionCookieWarning(String cookie) {
284+
private static String getSessionCookieWarning(String cookie) {
284285
return Constant.messages.getString("ascanbeta.cookieslack.session.warning", cookie);
285286
}
286287

addOns/ascanrulesBeta/src/main/java/org/zaproxy/zap/extension/ascanrulesBeta/SourceCodeDisclosureFileInclusionScanRule.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ private boolean isEmptyOrTooSimilar(HttpMessage msg, int matchPercentage) {
448448
* @param fileExtension
449449
* @return
450450
*/
451-
private boolean dataMatchesExtension(byte[] data, String fileExtension) {
451+
private static boolean dataMatchesExtension(byte[] data, String fileExtension) {
452452
if (fileExtension != null) {
453453
if (fileExtension.equals("JSP")) {
454454
if (PATTERN_JSP.matcher(new String(data)).find()) return true;
@@ -502,7 +502,7 @@ public Map<String, String> getAlertTags() {
502502
* @param b
503503
* @return
504504
*/
505-
private int calcLengthMatchPercentage(int a, int b) {
505+
private static int calcLengthMatchPercentage(int a, int b) {
506506
if (a == 0 && b == 0) return 100;
507507
if (a == 0 || b == 0) return 0;
508508

0 commit comments

Comments
 (0)