|
| 1 | +# Copyright 2024 Google LLC All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import base64 |
| 16 | +import datetime |
| 17 | +import gzip |
| 18 | +import json |
| 19 | +import unittest |
| 20 | + |
| 21 | +from google.cloud.spanner_dbapi import partition_helper |
| 22 | +from google.cloud.spanner_v1 import BatchTransactionId |
| 23 | +from google.cloud.spanner_v1.types import ExecuteSqlRequest |
| 24 | + |
| 25 | + |
| 26 | +class TestPartitionHelper(unittest.TestCase): |
| 27 | + def test_encode_and_decode_success_query(self): |
| 28 | + btid = BatchTransactionId( |
| 29 | + transaction_id=b"test-txn-123", |
| 30 | + session_id="session-xyz", |
| 31 | + read_timestamp=datetime.datetime( |
| 32 | + 2024, 5, 10, 12, 34, 56, tzinfo=datetime.timezone.utc |
| 33 | + ), |
| 34 | + ) |
| 35 | + |
| 36 | + query_options = ExecuteSqlRequest.QueryOptions( |
| 37 | + optimizer_version="2", |
| 38 | + optimizer_statistics_package="package-abc", |
| 39 | + ) |
| 40 | + |
| 41 | + partition_result = { |
| 42 | + "partition": b"partition-token-456", |
| 43 | + "query": { |
| 44 | + "sql": "SELECT * FROM users WHERE age > %s", |
| 45 | + "params": {"age": 21}, |
| 46 | + "query_options": query_options, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + encoded = partition_helper.encode_to_string(btid, partition_result) |
| 51 | + self.assertIsInstance(encoded, str) |
| 52 | + |
| 53 | + decoded = partition_helper.decode_from_string(encoded) |
| 54 | + self.assertIsInstance(decoded, partition_helper.PartitionId) |
| 55 | + |
| 56 | + # Verify BatchTransactionId |
| 57 | + self.assertEqual( |
| 58 | + decoded.batch_transaction_id.transaction_id, btid.transaction_id |
| 59 | + ) |
| 60 | + self.assertEqual(decoded.batch_transaction_id.session_id, btid.session_id) |
| 61 | + self.assertEqual( |
| 62 | + decoded.batch_transaction_id.read_timestamp, btid.read_timestamp |
| 63 | + ) |
| 64 | + |
| 65 | + # Verify partition result |
| 66 | + self.assertEqual(decoded.partition_result["partition"], b"partition-token-456") |
| 67 | + self.assertEqual( |
| 68 | + decoded.partition_result["query"]["sql"], |
| 69 | + "SELECT * FROM users WHERE age > %s", |
| 70 | + ) |
| 71 | + self.assertEqual(decoded.partition_result["query"]["params"], {"age": 21}) |
| 72 | + |
| 73 | + # Verify query options (restored to object) |
| 74 | + opts_obj = decoded.partition_result["query"]["query_options"] |
| 75 | + self.assertEqual(opts_obj.optimizer_version, "2") |
| 76 | + self.assertEqual(opts_obj.optimizer_statistics_package, "package-abc") |
| 77 | + |
| 78 | + def test_encode_and_decode_success_read(self): |
| 79 | + btid = BatchTransactionId( |
| 80 | + transaction_id=b"test-txn-456", |
| 81 | + session_id="session-abc", |
| 82 | + read_timestamp=None, |
| 83 | + ) |
| 84 | + |
| 85 | + partition_result = { |
| 86 | + "partition": b"partition-token-789", |
| 87 | + "read": { |
| 88 | + "table": "users", |
| 89 | + "columns": ["name", "age"], |
| 90 | + "keyset": {"keys": [[1], [2]]}, |
| 91 | + }, |
| 92 | + } |
| 93 | + |
| 94 | + encoded = partition_helper.encode_to_string(btid, partition_result) |
| 95 | + decoded = partition_helper.decode_from_string(encoded) |
| 96 | + |
| 97 | + self.assertEqual( |
| 98 | + decoded.batch_transaction_id.transaction_id, btid.transaction_id |
| 99 | + ) |
| 100 | + self.assertEqual(decoded.batch_transaction_id.session_id, btid.session_id) |
| 101 | + self.assertIsNone(decoded.batch_transaction_id.read_timestamp) |
| 102 | + |
| 103 | + self.assertEqual(decoded.partition_result["partition"], b"partition-token-789") |
| 104 | + self.assertEqual(decoded.partition_result["read"]["table"], "users") |
| 105 | + self.assertEqual(decoded.partition_result["read"]["columns"], ["name", "age"]) |
| 106 | + self.assertEqual( |
| 107 | + decoded.partition_result["read"]["keyset"], {"keys": [[1], [2]]} |
| 108 | + ) |
| 109 | + |
| 110 | + def test_insecure_deserialization_failure(self): |
| 111 | + # Malicious payload that attempts to execute pickle.loads under old code |
| 112 | + # (Here, we'll just pass invalid JSON wrapped in gzip + base64, or a pickle payload, |
| 113 | + # and make sure it does NOT get deserialized or execute anything, but raises an error gracefully) |
| 114 | + |
| 115 | + # A valid pickle payload for some simple object, base64 encoded and compressed |
| 116 | + import pickle |
| 117 | + |
| 118 | + pickle_bytes = pickle.dumps({"test": "payload"}) |
| 119 | + gzip_bytes = gzip.compress(pickle_bytes) |
| 120 | + encoded_pickle = base64.b64encode(gzip_bytes).decode("utf-8") |
| 121 | + |
| 122 | + # Since we now use json.loads, a pickle payload will fail to decode as UTF-8 / JSON |
| 123 | + with self.assertRaises((json.JSONDecodeError, UnicodeDecodeError)): |
| 124 | + partition_helper.decode_from_string(encoded_pickle) |
0 commit comments