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

Firestore: prevent use of transforms as cursor values. #6706

Merged
merged 1 commit into from
Nov 29, 2018
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
8 changes: 8 additions & 0 deletions firestore/google/cloud/firestore_v1beta1/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

from google.cloud.firestore_v1beta1 import _helpers
from google.cloud.firestore_v1beta1 import document
from google.cloud.firestore_v1beta1 import transforms
from google.cloud.firestore_v1beta1.gapic import enums
from google.cloud.firestore_v1beta1.proto import query_pb2
from google.cloud.firestore_v1beta1.order import Order
Expand All @@ -46,6 +47,7 @@
_BAD_OP_NAN_NULL = (
'Only an equality filter ("==") can be used with None or NaN values')
_BAD_DIR_STRING = 'Invalid direction {!r}. Must be one of {!r} or {!r}.'
_INVALID_CURSOR_TRANSFORM = 'Transforms cannot be used as cursor values.'
_MISSING_ORDER_BY = (
'The "order by" field path {!r} is not present in the cursor data {!r}. '
'All fields sent to ``order_by()`` must be present in the fields '
Expand Down Expand Up @@ -563,6 +565,12 @@ def _normalize_cursor(cursor, orders):
document_fields, order_keys)
raise ValueError(msg)

_transform_bases = (transforms.Sentinel, transforms._ValueList)
for field in document_fields:
if isinstance(field, _transform_bases):
msg = _INVALID_CURSOR_TRANSFORM
raise ValueError(msg)

return document_fields, before

def _to_protobuf(self):
Expand Down
40 changes: 40 additions & 0 deletions firestore/tests/unit/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,46 @@ def test__normalize_cursor_as_dict_mismatched_order(self):
with self.assertRaises(ValueError):
query._normalize_cursor(cursor, query._orders)

def test__normalize_cursor_w_delete(self):
from google.cloud.firestore_v1beta1 import DELETE_FIELD

cursor = ([DELETE_FIELD], True)
query = self._make_one(
mock.sentinel.parent).order_by('b', 'ASCENDING')

with self.assertRaises(ValueError):
query._normalize_cursor(cursor, query._orders)

def test__normalize_cursor_w_server_timestamp(self):
from google.cloud.firestore_v1beta1 import SERVER_TIMESTAMP

cursor = ([SERVER_TIMESTAMP], True)
query = self._make_one(
mock.sentinel.parent).order_by('b', 'ASCENDING')

with self.assertRaises(ValueError):
query._normalize_cursor(cursor, query._orders)

def test__normalize_cursor_w_array_remove(self):
from google.cloud.firestore_v1beta1 import ArrayRemove

cursor = ([ArrayRemove([1, 3, 5])], True)
query = self._make_one(
mock.sentinel.parent).order_by('b', 'ASCENDING')

with self.assertRaises(ValueError):
query._normalize_cursor(cursor, query._orders)

def test__normalize_cursor_w_array_union(self):
from google.cloud.firestore_v1beta1 import ArrayUnion

cursor = ([ArrayUnion([2, 4, 8])], True)
query = self._make_one(
mock.sentinel.parent).order_by('b', 'ASCENDING')

with self.assertRaises(ValueError):
query._normalize_cursor(cursor, query._orders)

def test__normalize_cursor_as_list_hit(self):
cursor = ([1], True)
query = self._make_one(
Expand Down