Skip to content

create "check_query_filter" for checking that parameters have secure fields. #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/spaceone/core/service/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"change_timestamp_value",
"change_date_value",
"change_timestamp_filter",
"check_query_filter",
]


Expand Down Expand Up @@ -420,3 +421,49 @@ def _convert_date_from_string(date_str, key, date_format) -> date:
return datetime.strptime(date_str, date_format).date()
except Exception as e:
raise ERROR_INVALID_PARAMETER_TYPE(key=key, type=date_format)


def check_query_filter(keywords=None) -> callable:
if keywords is None:
keywords = []

def wrapper(func):
@functools.wraps(func)
def wrapped_func(cls, params):
query = params.get("query", {})
if "filter" in query:
for filters in query["filter"]:
key = filters.get("key", filters.get("k"))
if key in keywords:
raise ERROR_INVALID_PARAMETER(
key=key, reason="Include secure parameter"
)

if "group_by" in query:
for group_bys in query["group_by"]:
key = group_bys.get("key", group_bys.get("k"))
if key in keywords:
raise ERROR_INVALID_PARAMETER(
key=key, reason="Include secure parameter"
)

if "fields" in query:
value = query["fields"].get("value", query["fields"].get("v"))
key = value.get("key", value.get("k"))
if key in keywords:
raise ERROR_INVALID_PARAMETER(
key=key, reason="Include secure parameter"
)

if "distinct" in query:
key = query["distinct"]
if key in keywords:
raise ERROR_INVALID_PARAMETER(
key=key, reason="Include secure parameter"
)

return func(cls, params)

return wrapped_func

return wrapper
Loading