Skip to content

Commit

Permalink
fix broken ipv4 regex
Browse files Browse the repository at this point in the history
  • Loading branch information
hirusha-adi committed Oct 24, 2024
1 parent 03f9a98 commit 436fe32
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions near/utils/input_sanitization.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ def check_input(input_str: str) -> bool:
Check if input string contains any potentially harmful commands.
Args:
input_str: The string to check.
input_str (str): The string to check.
Returns:
True if the string is safe,
False if it contains a harmful command.
bool:
True if the string is safe,
False if it contains a harmful command.
"""

for keyword in privilege_escalation_keywords:
Expand All @@ -30,11 +31,12 @@ def is_base64(input_str: str) -> bool:
Based on: https://stackoverflow.com/questions/475074/regex-to-parse-or-validate-base64-data
Args:
input_str: The string to check.
input_str (str): The string to check.
Returns:
True if the string is a valid base64 string,
False if it is not.
bool:
True if the string is a valid base64 string,
False if it is not.
"""

pattern = r'^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$'
Expand All @@ -49,14 +51,15 @@ def is_ipaddr(input_str: str) -> bool:
IPv6: https://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses
Args:
input_str: The string to check.
input_str (str): The string to check.
Returns:
True if the string is a valid IP address,
False if it is not.
bool:
True if the string is a valid IP address,
False if it is not.
"""

ipv4 = r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
ipv4 = r'^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$'
ipv6 = r'(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))'
return (re.match(ipv4, input_str) or re.match(ipv6, input_str)) is not None

Expand All @@ -66,11 +69,12 @@ def is_text_only(input_str: str) -> bool:
Check if the input string contains only alphabetic characters.
Args:
input_str: The string to check.
input_str (str): The string to check.
Returns:
True if the string contains only alphabetic characters,
False otherwise.
bool:
True if the string contains only alphabetic characters,
False otherwise.
"""

pattern = r'^[a-zA-Z]+$'
Expand All @@ -85,11 +89,12 @@ def is_instagram_username(input_str: str) -> bool:
- Must only contain alphanumeric characters
Args:
input_str: The string to check.
input_str (str): The string to check.
Returns:
True if the string is a valid Instagram username,
False otherwise.
bool:
True if the string is a valid Instagram username,
False otherwise.
"""

pattern = r'^[a-zA-Z0-9]{1,29}$'
Expand All @@ -110,8 +115,9 @@ def color(hex: str) -> int:
hex (str): The string to convert.
Returns:
int: The integer value of the given hex string.
If the conversion fails, return the default color red (int: 0xFF0000).
int:
The integer value of the given hex string.
If the conversion fails, return the default color red (int: 0xFF0000).
"""
try:
return int(hex, 16)
Expand Down

0 comments on commit 436fe32

Please sign in to comment.