-
-
Notifications
You must be signed in to change notification settings - Fork 709
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5915 from thc202/value-provider
Move `ValueGenerator` from core and rename it
- Loading branch information
Showing
48 changed files
with
513 additions
and
385 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
addOns/commonlib/src/main/java/org/zaproxy/addon/commonlib/DefaultValueProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* | ||
* Zed Attack Proxy (ZAP) and its related class files. | ||
* | ||
* ZAP is an HTTP/HTTPS proxy for assessing web application security. | ||
* | ||
* Copyright 2024 The ZAP Development Team | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.zaproxy.addon.commonlib; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Map; | ||
import net.htmlparser.jericho.FormControlType; | ||
import org.apache.commons.httpclient.URI; | ||
|
||
/** | ||
* Default implementation of the {@link ValueProvider}. | ||
* | ||
* <p>Generally it should not be used directly it should be used the one from {@link | ||
* ExtensionCommonlib#getValueProvider()}. | ||
* | ||
* @since 1.29.0 | ||
*/ | ||
public class DefaultValueProvider implements ValueProvider { | ||
|
||
private static final String ATTR_TYPE = "type"; | ||
private static final String DEFAULT_NUMBER_VALUE = "1"; | ||
private static final String DEFAULT_TEXT_VALUE = | ||
org.parosproxy.paros.Constant.PROGRAM_NAME_SHORT; | ||
private static final String DEFAULT_PASS_VALUE = DEFAULT_TEXT_VALUE; | ||
private static final String DEFAULT_FILE_VALUE = "test_file.txt"; | ||
private static final String DEFAULT_EMPTY_VALUE = ""; | ||
|
||
private Date defaultDate; | ||
|
||
/** | ||
* Gets the default {@code Date}, to be used for default values of date fields. | ||
* | ||
* @return the date, never {@code null}. | ||
* @see #setDefaultDate(Date) | ||
*/ | ||
public Date getDefaultDate() { | ||
if (defaultDate == null) { | ||
return new Date(); | ||
} | ||
return defaultDate; | ||
} | ||
|
||
public void setDefaultDate(Date date) { | ||
this.defaultDate = date; | ||
} | ||
|
||
/** | ||
* Generates accurate field values for following types: | ||
* | ||
* <ul> | ||
* <li>Text/Password/Search - DEFAULT_TEXT_VALUE | ||
* <li>number/range - if min is defined, then use min. If max is defined use max, otherwise | ||
* DEFAULT_NUMBER_VALUE | ||
* <li>url - http://www.example.com | ||
* <li>email - [email protected] | ||
* <li>color - #ffffff | ||
* <li>tel - 9999999999 | ||
* <li>date/datetime/time/month/week/datetime-local - current date in the proper format | ||
* <li>file - DEFAULT_FILE_VALUE | ||
* </ul> | ||
* | ||
* @return the default String value for each control type | ||
*/ | ||
@Override | ||
public String getValue( | ||
URI uri, | ||
String url, | ||
String fieldId, | ||
String defaultValue, | ||
List<String> definedValues, | ||
Map<String, String> envAttributes, | ||
Map<String, String> fieldAttributes) { | ||
|
||
// If there is a default value provided, return it | ||
if (!defaultValue.isEmpty()) { | ||
return defaultValue; | ||
} | ||
|
||
if (fieldAttributes.get("Control Type").equalsIgnoreCase(FormControlType.TEXT.toString())) { | ||
// Converted FormControlType to String to allow for case insensitive comparison | ||
// If the control type was reduced to a TEXT type by the Jericho library, check the | ||
// HTML5 type and use proper values | ||
String type = fieldAttributes.get(ATTR_TYPE); | ||
if (type == null || type.equalsIgnoreCase("text")) { | ||
return DEFAULT_TEXT_VALUE; | ||
} | ||
if (type.equalsIgnoreCase("number") || type.equalsIgnoreCase("range")) { | ||
String min = fieldAttributes.get("min"); | ||
if (min != null) { | ||
return min; | ||
} | ||
String max = fieldAttributes.get("max"); | ||
if (max != null) { | ||
return max; | ||
} | ||
return DEFAULT_NUMBER_VALUE; | ||
} | ||
if (type.equalsIgnoreCase("url")) { | ||
return "http://www.example.com"; | ||
} | ||
if (type.equalsIgnoreCase("email")) { | ||
return "[email protected]"; | ||
} | ||
if (type.equalsIgnoreCase("color")) { | ||
return "#ffffff"; | ||
} | ||
if (type.equalsIgnoreCase("tel")) { | ||
return "9999999999"; | ||
} | ||
if (type.equalsIgnoreCase("datetime")) { | ||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); | ||
return format.format(getDefaultDate()); | ||
} | ||
if (type.equalsIgnoreCase("datetime-local")) { | ||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); | ||
return format.format(getDefaultDate()); | ||
} | ||
if (type.equalsIgnoreCase("date")) { | ||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); | ||
return format.format(getDefaultDate()); | ||
} | ||
if (type.equalsIgnoreCase("time")) { | ||
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); | ||
return format.format(getDefaultDate()); | ||
} | ||
if (type.equalsIgnoreCase("month")) { | ||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); | ||
return format.format(getDefaultDate()); | ||
} | ||
if (type.equalsIgnoreCase("week")) { | ||
SimpleDateFormat format = new SimpleDateFormat("yyyy-'W'ww"); | ||
return format.format(getDefaultDate()); | ||
} | ||
} else if (fieldAttributes | ||
.get("Control Type") | ||
.equalsIgnoreCase(FormControlType.PASSWORD.toString())) { | ||
return DEFAULT_PASS_VALUE; | ||
} else if (fieldAttributes | ||
.get("Control Type") | ||
.equalsIgnoreCase(FormControlType.FILE.toString())) { | ||
return DEFAULT_FILE_VALUE; | ||
} | ||
return DEFAULT_EMPTY_VALUE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
addOns/commonlib/src/main/java/org/zaproxy/addon/commonlib/ValueProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Zed Attack Proxy (ZAP) and its related class files. | ||
* | ||
* ZAP is an HTTP/HTTPS proxy for assessing web application security. | ||
* | ||
* Copyright 2024 The ZAP Development Team | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.zaproxy.addon.commonlib; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import org.apache.commons.httpclient.URI; | ||
|
||
/** | ||
* A provider of values. | ||
* | ||
* @since 1.29.0 | ||
* @see ExtensionCommonlib#getValueProvider() | ||
*/ | ||
public interface ValueProvider { | ||
|
||
/** | ||
* Gets a value for the given field name. | ||
* | ||
* @param uri the uri | ||
* @param url the resolved URL | ||
* @param fieldId the name associated with the current field | ||
* @param defaultValue the value of 'value attribute' if it has one | ||
* @param definedValues the predefined values for the field, if present | ||
* @param envAttributes all attributes of the current form | ||
* @param fieldAttributes all attributes of the current field | ||
*/ | ||
String getValue( | ||
URI uri, | ||
String url, | ||
String fieldId, | ||
String defaultValue, | ||
List<String> definedValues, | ||
Map<String, String> envAttributes, | ||
Map<String, String> fieldAttributes); | ||
} |
Oops, something went wrong.