Skip to content

Commit 7d1ec84

Browse files
committed
ehnance script
1 parent 2a149cf commit 7d1ec84

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

scripts/identify-unrestricted-string-types-from-xsd-schema.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
STRICT_RESTRICTIONS_ACCEPTED = ["enumeration", "pattern"]
1818
STRICT_RESTRICTIONS_MAX_STRING_LENTGTH_ACCEPTED_AS_VALID_RESTRICTION = 20
1919
XSD_NS_PREFIX_IDENTIFICATION_REGEX = r'xmlns:([a-zA-Z0-9\-_]+)="http:\/\/www\.w3\.org\/2001\/XMLSchema"'
20+
DEBUG_MODE = False
2021

2122

2223
def local_name(name, delimiter="}"):
@@ -61,8 +62,8 @@ def has_restrictions(node, xsd_namespaces, xsd_namespace_prefix):
6162
for restriction_child_element in restriction_element:
6263
lname = local_name(restriction_child_element.tag)
6364
# For maxLength extract the constraints.
64-
# Indeed if only 10 characters is allowed then it make
65-
# harder to submit a malicious content
65+
# Indeed if only 'STRICT_RESTRICTIONS_MAX_STRING_LENTGTH_ACCEPTED_AS_VALID_RESTRICTION' characters
66+
# is allowed then it make harder to submit a malicious content
6667
if lname == "maxLength":
6768
lname += " " + restriction_child_element.get("value")
6869
restriction_tags.append(lname)
@@ -86,6 +87,8 @@ def find_unrestricted_string_types(xsd_file, xsd_namespaces, xsd_namespace_prefi
8687
unrestricted_string_types.append(type_name)
8788
elif restrictions[0] and use_strict_mode and not has_strict_restrictions(restrictions[2]) and type_name not in unrestricted_string_types:
8889
unrestricted_string_types.append(type_name)
90+
if DEBUG_MODE:
91+
print(f"[{colored('DEBUG', 'cyan')}] {type_name} => {restrictions}")
8992
return unrestricted_string_types
9093

9194

@@ -94,7 +97,9 @@ def find_unrestricted_string_types(xsd_file, xsd_namespaces, xsd_namespace_prefi
9497
parser.add_argument("--file", action="store", dest="xsd_file_path", help="XSD file to analyze.", required=False)
9598
parser.add_argument("--folder", action="store", dest="xsd_files_base_folder", help="Folder containing a collection of XSD files to analyze.", required=False)
9699
parser.add_argument("--strict", action="store_true", dest="use_strict_mode", help=f"Only consider 'enumeration', 'pattern' and 'maxLength <= {STRICT_RESTRICTIONS_MAX_STRING_LENTGTH_ACCEPTED_AS_VALID_RESTRICTION}' as accepted restrictions.", required=False, default=False)
100+
parser.add_argument("--debug", action="store_true", dest="use_debug_mode", help="Print verbose information during some phases for debugging purpose.", required=False, default=False)
97101
args = parser.parse_args()
102+
DEBUG_MODE = args.use_debug_mode
98103
if args.xsd_file_path is None and args.xsd_files_base_folder is None:
99104
print("At least one processing mode must be specified!")
100105
sys.exit(-1)

0 commit comments

Comments
 (0)