Skip to content

Commit

Permalink
Warn on unnecessary argument encoding, Closes #850
Browse files Browse the repository at this point in the history
  • Loading branch information
jshcodes committed Aug 25, 2023
1 parent 7db1ad6 commit e1a88d0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
32 changes: 29 additions & 3 deletions src/falconpy/_util/_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
NoContentWarning,
PayloadValidationError,
InvalidBaseURL,
SSLDisabledWarning
SSLDisabledWarning,
UnnecessaryEncodingUsed
)
from .._result import Result
urllib3.disable_warnings(InsecureRequestWarning)
Expand Down Expand Up @@ -524,7 +525,13 @@ def get_default(types: list, position: int) -> Union[list, str, int, dict, bool]
return retval


def args_to_params(payload: dict, passed_arguments: dict, endpoints: list, epname: str) -> dict:
def args_to_params(payload: dict,
passed_arguments: dict,
endpoints: list,
epname: str,
log_utl: Logger = None,
pyth: bool = None
) -> dict:
"""Query String parameter abstraction handler.
This function reviews arguments passed to the function against arguments accepted by the
Expand All @@ -544,6 +551,8 @@ def args_to_params(payload: dict, passed_arguments: dict, endpoints: list, epnam
passed_arguments -- Keywords provided to the calling method.
endpoints -- List of API endpoints available to the calling method.
epname -- Operation ID to be retrieved from the endpoints list.
log_utl -- Logger used when warnings are issued.
pyth -- Boolean indicating if pythonic responses are enabled.
Returns: dictionary representing QueryString parameters.
"""
Expand All @@ -559,6 +568,17 @@ def args_to_params(payload: dict, passed_arguments: dict, endpoints: list, epnam
if argument["type"] == "array":
if isinstance(passed_arguments[arg_name], (str)):
passed_arguments[arg_name] = passed_arguments[arg_name].split(",")
# Check for unnecessarily URLEncoded strings by finding an encoded ":", Issue #850
if isinstance(passed_arguments[arg_name], str):
if "%3A" in passed_arguments[arg_name]:
msg = " ".join([arg_name,
"argument contains potentially urlencoded string of",
f"'{passed_arguments[arg_name]}'."
])
if pyth:
warn(msg, UnnecessaryEncodingUsed, stacklevel=5)
else:
log_utl.warning(msg)
# More data type validation can go here
payload[arg_name] = passed_arguments[arg_name]
except IndexError:
Expand Down Expand Up @@ -638,7 +658,13 @@ def process_service_request(calling_object, # pylint: disable=R0914 # (19/15)
passed_params = kwargs.get("params", None)
parameter_payload = None
if passed_keywords or passed_params:
parameter_payload = args_to_params(passed_params, passed_keywords, endpoints, operation_id)
parameter_payload = args_to_params(passed_params,
passed_keywords,
endpoints,
operation_id,
calling_object.log,
kwargs.get("pythonic", None)
)
expand_result = passed_keywords.get("expand_result", False) if passed_keywords else kwargs.get("expand_result", False)
new_keywords = {
"caller": calling_object,
Expand Down
8 changes: 7 additions & 1 deletion src/falconpy/_util/_uber.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,13 @@ def uber_request_keywords(caller,
"endpoint": tgt,
"body": kwa.get("body", return_preferred_default(oper)),
"data": kwa.get("data", return_preferred_default(oper)),
"params": args_to_params(kwa.get("parameters", {}), kwa, caller.commands, oper),
"params": args_to_params(kwa.get("parameters", {}),
kwa,
caller.commands,
oper,
caller.log,
caller.pythonic
),
"headers": create_uber_header_payload(caller.auth_headers, kwa),
"files": kwa.get("files", return_preferred_default(oper, "list")),
"verify": caller.ssl_verify,
Expand Down

0 comments on commit e1a88d0

Please sign in to comment.