Skip to content
Open
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
8 changes: 7 additions & 1 deletion flask_admin/contrib/sqla/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,13 @@ def get_one(self, id):
Model id
"""
session = _get_deprecated_session(self.session)
return session.get(self.model, tools.iterdecode(id))
_id = tools.iterdecode(id)
if isinstance(self._primary_key, tuple):
_id = tools.iterdecode(id)
else:
_id = (tools.escape(id),)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why tools.escape?
Looks unnecessary to me

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, what if the actual string-single-PK="1,2", is it possible to navigate /admin/details?id=1,2 without escaping ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, by passing another time iterdecode. Here you don't need to escape, but to undo the escaping needed to build the url.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you mean by build the url ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The url that is linked in a view e.g. the one for editing will be edit/?id=1,2 but 1,2 will be escaped. You can check it by clicking on the pencil icon of a list view

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, yes, it will be escaped, but what if the user wrote edit/?id=1,2 manually in the browser ? then 500 will be returned with error.


return session.get(self.model, _id)

# Error handler
def handle_view_exception(self, exc: Exception) -> bool:
Expand Down
10 changes: 10 additions & 0 deletions flask_admin/tests/sqla/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,16 @@ def test_details_view(app, db, admin, session_or_db):
assert "test2_val_1" in data
assert "test1_val_1" in data

# test single-PK with multiple IDs in query string
rv = client.get(
"/admin/model2/details/?url=%2Fadmin%2Fmodel2%2F&id=1,2",
follow_redirects=True,
)
data = rv.data.decode("utf-8")
assert "String Field" in data
assert "test2_val_1" in data
assert "test1_val_1" in data

# test column_details_list
rv = client.get("/admin/sf_view/details/?url=%2Fadmin%2Fsf_view%2F&id=1")
data = rv.data.decode("utf-8")
Expand Down