|
1 | 1 | import enum |
| 2 | +from uuid import UUID |
2 | 3 |
|
3 | 4 | import pytest |
4 | 5 | import sqlalchemy |
| 6 | +from sqlalchemy import Column, MetaData, Table, select |
5 | 7 |
|
6 | 8 | from databricks.sqlalchemy.base import DatabricksDialect |
7 | | -from databricks.sqlalchemy._types import TINYINT, TIMESTAMP, TIMESTAMP_NTZ, DatabricksVariant |
| 9 | +from databricks.sqlalchemy._types import ( |
| 10 | + DatabricksUUID, |
| 11 | + DatabricksVariant, |
| 12 | + TINYINT, |
| 13 | + TIMESTAMP, |
| 14 | + TIMESTAMP_NTZ, |
| 15 | +) |
8 | 16 |
|
9 | 17 |
|
10 | 18 | class DatabricksDataType(enum.Enum): |
@@ -161,3 +169,140 @@ def test_array_string_renders_as_array_of_string(self): |
161 | 169 | self._assert_compiled_value_explicit( |
162 | 170 | sqlalchemy.types.ARRAY(sqlalchemy.types.String), "ARRAY<STRING>" |
163 | 171 | ) |
| 172 | + |
| 173 | + |
| 174 | +class TestDatabricksUUID: |
| 175 | + """Regression coverage for github.com/databricks/databricks-sqlalchemy/issues/50. |
| 176 | +
|
| 177 | + SQLAlchemy's default Uuid renders the 32-char hex form (no dashes) on backends |
| 178 | + without a native UUID type, which breaks equality against UUIDs stored as |
| 179 | + canonical 8-4-4-4-12 strings in Databricks. |
| 180 | + """ |
| 181 | + |
| 182 | + dialect = DatabricksDialect() |
| 183 | + HYPHENATED = "1daa91d7-8d35-4684-86d6-3fa89042c1f4" |
| 184 | + HEX = "1daa91d78d35468486d63fa89042c1f4" |
| 185 | + sample = UUID(HYPHENATED) |
| 186 | + |
| 187 | + def test_bind_processor_preserves_hyphenated_form(self): |
| 188 | + process = DatabricksUUID().bind_processor(self.dialect) |
| 189 | + assert process(self.sample) == self.HYPHENATED |
| 190 | + |
| 191 | + def test_bind_processor_handles_none(self): |
| 192 | + process = DatabricksUUID().bind_processor(self.dialect) |
| 193 | + assert process(None) is None |
| 194 | + |
| 195 | + def test_literal_processor_renders_hyphenated_form(self): |
| 196 | + process = DatabricksUUID().literal_processor(self.dialect) |
| 197 | + assert process(self.sample) == "'%s'" % self.HYPHENATED |
| 198 | + |
| 199 | + def test_literal_processor_handles_none(self): |
| 200 | + process = DatabricksUUID().literal_processor(self.dialect) |
| 201 | + assert process(None) == "NULL" |
| 202 | + |
| 203 | + def test_result_processor_accepts_both_forms(self): |
| 204 | + process = DatabricksUUID().result_processor(self.dialect, None) |
| 205 | + assert process(self.HYPHENATED) == self.sample |
| 206 | + assert process(self.HEX) == self.sample |
| 207 | + assert process(None) is None |
| 208 | + |
| 209 | + def test_dialect_routes_uuid_to_databricks_uuid(self): |
| 210 | + """The colspecs entry is what makes a plain ``Uuid`` column use our impl.""" |
| 211 | + assert self.dialect.colspecs[sqlalchemy.types.Uuid] is DatabricksUUID |
| 212 | + |
| 213 | + def test_uuid_where_clause_renders_with_dashes(self): |
| 214 | + meta = MetaData() |
| 215 | + users = Table( |
| 216 | + "users", meta, Column("id", sqlalchemy.types.Uuid, primary_key=True) |
| 217 | + ) |
| 218 | + stmt = select(users).where(users.c.id == self.sample) |
| 219 | + |
| 220 | + literal_sql = str( |
| 221 | + stmt.compile(dialect=self.dialect, compile_kwargs={"literal_binds": True}) |
| 222 | + ) |
| 223 | + assert "'%s'" % self.HYPHENATED in literal_sql |
| 224 | + assert self.HEX not in literal_sql |
| 225 | + |
| 226 | + def test_uuid_bound_param_wire_value_has_dashes(self): |
| 227 | + meta = MetaData() |
| 228 | + users = Table( |
| 229 | + "users", meta, Column("id", sqlalchemy.types.Uuid, primary_key=True) |
| 230 | + ) |
| 231 | + stmt = select(users).where(users.c.id == self.sample) |
| 232 | + compiled = stmt.compile(dialect=self.dialect) |
| 233 | + |
| 234 | + raw = compiled.construct_params() |
| 235 | + processed = { |
| 236 | + key: ( |
| 237 | + compiled._bind_processors[key](value) |
| 238 | + if key in compiled._bind_processors |
| 239 | + else value |
| 240 | + ) |
| 241 | + for key, value in raw.items() |
| 242 | + } |
| 243 | + assert self.HYPHENATED in processed.values() |
| 244 | + |
| 245 | + def test_bind_processor_normalizes_hex_string_to_canonical(self): |
| 246 | + """A bare 32-char hex string (no dashes) must be coerced to canonical form.""" |
| 247 | + process = DatabricksUUID().bind_processor(self.dialect) |
| 248 | + assert process(self.HEX) == self.HYPHENATED |
| 249 | + |
| 250 | + def test_bind_processor_normalizes_hyphenated_string(self): |
| 251 | + """A canonical hyphenated string passes through unchanged.""" |
| 252 | + process = DatabricksUUID().bind_processor(self.dialect) |
| 253 | + assert process(self.HYPHENATED) == self.HYPHENATED |
| 254 | + |
| 255 | + def test_bind_processor_rejects_non_uuid_string(self): |
| 256 | + """Bad input must raise instead of being silently written to the column.""" |
| 257 | + process = DatabricksUUID().bind_processor(self.dialect) |
| 258 | + with pytest.raises(ValueError): |
| 259 | + process("not-a-uuid") |
| 260 | + |
| 261 | + def test_literal_processor_rejects_injection_attempt(self): |
| 262 | + """An attacker-controlled string must not be allowed to escape the quotes. |
| 263 | +
|
| 264 | + Before the input was normalized through ``UUID(...)``, a string like |
| 265 | + ``abc' OR '1'='1`` would inject directly into ``WHERE id = 'abc' OR |
| 266 | + '1'='1'`` whenever ``literal_binds=True`` was used. |
| 267 | + """ |
| 268 | + process = DatabricksUUID().literal_processor(self.dialect) |
| 269 | + with pytest.raises(ValueError): |
| 270 | + process("abc' OR '1'='1") |
| 271 | + |
| 272 | + def test_literal_processor_rejects_injection_via_uuid_subclass(self): |
| 273 | + """A UUID subclass with a malicious ``__str__`` must not bypass escaping. |
| 274 | +
|
| 275 | + ``_canonical`` reconstructs every UUID through ``UUID(int=value.int)`` |
| 276 | + so a subclass cannot inject SQL via an overridden string conversion. |
| 277 | + """ |
| 278 | + |
| 279 | + class EvilUUID(UUID): |
| 280 | + def __str__(self): # type: ignore[override] |
| 281 | + return "abc' OR '1'='1" |
| 282 | + |
| 283 | + process = DatabricksUUID().literal_processor(self.dialect) |
| 284 | + rendered = process(EvilUUID(self.HYPHENATED)) |
| 285 | + assert rendered == "'%s'" % self.HYPHENATED |
| 286 | + assert "OR" not in rendered |
| 287 | + |
| 288 | + def test_literal_processor_rejects_injection_with_as_uuid_false(self): |
| 289 | + """``Uuid(as_uuid=False)`` shares ``_canonical``; lock in coverage anyway.""" |
| 290 | + process = DatabricksUUID(as_uuid=False).literal_processor(self.dialect) |
| 291 | + with pytest.raises(ValueError): |
| 292 | + process("abc' OR '1'='1") |
| 293 | + |
| 294 | + def test_as_uuid_false_round_trip_normalizes_hex_input(self): |
| 295 | + """``Uuid(as_uuid=False)`` users sometimes pass hex-form strings. |
| 296 | +
|
| 297 | + Pre-fix, the dialect wrote those through unchanged, so a query against |
| 298 | + canonically-stored data returned 0 rows. After the fix, both forms |
| 299 | + normalize to canonical on the wire. |
| 300 | + """ |
| 301 | + type_ = DatabricksUUID(as_uuid=False) |
| 302 | + bind = type_.bind_processor(self.dialect) |
| 303 | + result = type_.result_processor(self.dialect, None) |
| 304 | + |
| 305 | + assert bind(self.HEX) == self.HYPHENATED |
| 306 | + assert bind(self.HYPHENATED) == self.HYPHENATED |
| 307 | + assert result(self.HYPHENATED) == self.HYPHENATED |
| 308 | + assert result(self.HEX) == self.HYPHENATED |
0 commit comments