Skip to content
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

fix(MVC): discard excluded filters from query #1862

Merged
merged 5 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
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
55 changes: 55 additions & 0 deletions flask_appbuilder/tests/test_urltools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import os

from flask import Flask
from flask_appbuilder import AppBuilder, SQLA
from flask_appbuilder.models.sqla.interface import SQLAInterface
from flask_appbuilder.tests.sqla.models import Model1
from flask_appbuilder.urltools import get_filter_args

from .base import FABTestCase


class FlaskTestCase(FABTestCase):
def setUp(self):
self.app = Flask(__name__)
self.basedir = os.path.abspath(os.path.dirname(__file__))
self.app.config.from_object("flask_appbuilder.tests.config_api")

self.db = SQLA(self.app)
self.appbuilder = AppBuilder(self.app, self.db.session)

def test_get_filter_args_allow_one(self):
datamodel = SQLAInterface(Model1)
with self.appbuilder.get_app.test_request_context(
"/users/list?_flt_1_field_string=a"
):
filters = datamodel.get_filters(["field_string", "field_integer"])
get_filter_args(filters)
assert filters.values == [["a"]]

def test_get_filter_args_allow_multiple(self):
datamodel = SQLAInterface(Model1)
with self.appbuilder.get_app.test_request_context(
"/users/list?_flt_1_field_string=a&_flt_1_field_integer=2"
):
filters = datamodel.get_filters(["field_string", "field_integer"])
get_filter_args(filters)
assert filters.values in ([["a"], ["2"]], [["2"], ["a"]])

def test_get_filter_args_disallow(self):
datamodel = SQLAInterface(Model1)
with self.appbuilder.get_app.test_request_context(
"/users/list?_flt_1_field_float=1.0"
):
filters = datamodel.get_filters(["field_string", "field_integer"])
get_filter_args(filters)
assert filters.values == []

def test_get_filter_args_invalid_index(self):
datamodel = SQLAInterface(Model1)
with self.appbuilder.get_app.test_request_context(
"/users/list?_flt_a_field_string=a"
):
filters = datamodel.get_filters(["field_string", "field_integer"])
get_filter_args(filters)
assert filters.values == []
12 changes: 11 additions & 1 deletion flask_appbuilder/urltools.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import logging
import re

from flask import request

log = logging.getLogger(__name__)


class Stack(object):
"""
Expand Down Expand Up @@ -96,7 +99,14 @@ def get_filter_args(filters):
request_args = set(request.args)
for arg in request_args:
re_match = re.findall(r"_flt_(\d)_(.*)", arg)
if not re_match:
continue
filter_index = int(re_match[0][0])
filter_column = re_match[0][1]
if filter_column not in filters.get_search_filters().keys():
log.warning("Filter column not allowed")
continue
if re_match:
filters.add_filter_index(
re_match[0][1], int(re_match[0][0]), request.args.getlist(arg)
filter_column, filter_index, request.args.getlist(arg)
)