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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
- Fixed a bug in `DataFrameReader.jdbc` where a `query` ending with `;` or with a `--` comment failed with a syntax error.
- `DataFrameReader.jdbc` now supports partitioning on timestamp columns, including Oracle `DATE` columns, which the Oracle JDBC driver reports as timestamps.
- `DataFrameReader.jdbc` now drops its schema-inference UDTF after use instead of keeping it until the session ends, and creates its UDTFs as scoped temporary objects when `Session` uses scoped temporary objects.
- Changed the type mapping of `DataFrameReader.jdbc` (Private Preview):
- Postgres `boolean` and SQL Server `bit` columns are read as `BooleanType` instead of strings. Postgres `bit(n)` and MySQL `BIT`, `BOOLEAN` and `TINYINT(1)` columns are still read as strings, because MySQL reports `TINYINT(1)` like `BIT(1)` although it can hold any small integer.
- Oracle `TIMESTAMP WITH TIME ZONE`, SQL Server `datetimeoffset` and Postgres `timestamptz` are read as `TimestampType(TimestampTimeZone.TZ)`, and Oracle `TIMESTAMP WITH LOCAL TIME ZONE` as `TimestampType(TimestampTimeZone.LTZ)`. Previously the Oracle and SQL Server types were read as `VariantType` strings, and Postgres `timestamptz` lost its time zone and was shifted to the time zone of the UDTF.
- Types without a mapping (e.g. Oracle `INTERVAL`, Postgres `xml`, `timetz`) are read as `StringType` instead of `VariantType`, as in `DataFrameReader.dbapi`.

### Snowpark Local Testing Updates

Expand Down
97 changes: 69 additions & 28 deletions src/snowflake/snowpark/_internal/data_source/jdbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
BooleanType,
ArrayType,
StructField,
VariantType,
TimestampType,
TimestampTimeZone,
IntegerType,
Expand Down Expand Up @@ -143,6 +142,16 @@ class JDBCType(Enum):

PARTITION_TABLE_COLUMN_NAME = "partition"

# How the read UDTF fetches a column. getString() is used by default; time-zone
# aware values need an explicit Java type because getString() renders them in a
# driver-specific format (e.g. "-8:00", region names) that Snowflake cannot cast.
STRING_READER = "string"
OFFSET_DATETIME_READER = "offset_datetime"
INSTANT_READER = "instant"

TIMESTAMP_TZ_JAVA_TYPES = {"oracle.sql.TIMESTAMPTZ", "microsoft.sql.DateTimeOffset"}
TIMESTAMP_LTZ_JAVA_TYPES = {"oracle.sql.TIMESTAMPLTZ"}


def to_java_string_literal(value: object) -> str:
"""Return ``value`` as a double-quoted Java string literal.
Expand Down Expand Up @@ -238,6 +247,7 @@ def __init__(
else "PACKAGES=('com.snowflake:snowpark:latest')"
)
self.secret_sql = f"SECRETS = ('cred' = {self.secret})"
self._dbms = get_jdbc_dbms(url)
self._temp_function_type = get_temp_type_for_object(
self.session._use_scoped_temp_objects, True
)
Expand All @@ -249,7 +259,7 @@ def schema(self) -> StructType:
infer_schema_udtf_name = random_name_for_temp_object(TempObjectType.FUNCTION)
infer_schema_udtf_registration = f"""
CREATE OR REPLACE {self._temp_function_type} FUNCTION {infer_schema_udtf_name}(query VARCHAR)
RETURNS TABLE (field_name VARCHAR, jdbc_type VARCHAR, java_type VARCHAR, precision INTEGER, scale INTEGER, nullable BOOLEAN)
RETURNS TABLE (field_name VARCHAR, jdbc_type VARCHAR, java_type VARCHAR, precision INTEGER, scale INTEGER, nullable BOOLEAN, type_name VARCHAR)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review provided by Security Bot (#ask-pr-scanner)


SQL injection

🟠 MEDIUM · imports_sql and packages_sql interpolate import/package strings into UDTF DDL without escaping single quotes · CWE-89

Fix: Escape each imports and packages entry before embedding it in SQL, mirroring the existing str_to_sql/single-quote-doubling approach used elsewhere in this module.

Full description

In JDBC.__init__, self.imports_sql and self.packages_sql build SQL fragments by wrapping each entry of imports and packages in single quotes via f-strings (e.g. f"'{imp}'") without escaping embedded single quotes. These fragments are injected verbatim into the CREATE OR REPLACE ... FUNCTION DDL in schema and read. Unlike url, properties and session_init_statement (which are escaped with to_java_string_literal) and the inference query (escaped with str_to_sql), the imports/packages list items reach the SQL sink unescaped, so a value containing a single quote or ') can alter the generated DDL.

Impact

A caller who controls udtf_configs["imports"] or udtf_configs["packages"] (for example a stage path or package coordinate assembled from another untrusted source) can inject arbitrary SQL into the UDTF registration statement executed by self.session.sql(...).collect(), running in the Snowpark session's authority. The demonstrated effect is malformed/attacker-influenced DDL execution; it does not by itself cross a tenant boundary because the statement runs with the caller's own session privileges.

Recommendation steps

  • Escape each imports and packages entry before embedding it in SQL, mirroring the existing str_to_sql/single-quote-doubling approach used elsewhere in this module.
  • Validate imports entries against an allowed stage-path grammar and packages entries against the expected group:artifact[:version] coordinate shape before building imports_sql/packages_sql.
  • Document the input contract for udtf_configs["imports"]/["packages"] so callers do not forward untrusted values.

LANGUAGE JAVA
RUNTIME_VERSION = '{self.java_version}'
EXTERNAL_ACCESS_INTEGRATIONS=({self.external_access_integration})
Expand Down Expand Up @@ -279,6 +289,7 @@ class OutputRow {{
public int precision;
public int scale;
public boolean nullable;
public String type_name;

}}

Expand Down Expand Up @@ -326,6 +337,7 @@ class OutputRow {{
row.precision = meta.getPrecision(currentColumnIndex);
row.scale = meta.getScale(currentColumnIndex);
row.nullable = meta.isNullable(currentColumnIndex) == ResultSetMetaData.columnNullable;
row.type_name = meta.getColumnTypeName(currentColumnIndex);
return row;
}} else {{
rs.close();
Expand Down Expand Up @@ -637,12 +649,24 @@ def infer_schema_sql(self):
)

def create_output_row_java_code(self):
return "".join(
[
f"row.{self._udtf_column_name(i)} = rs.getString({i+1});\n"
for i in range(len(self.schema.fields))
]
)
readers = getattr(self, "_column_readers", None) or []
code = []
for i in range(len(self.schema.fields)):
column = f"row.{self._udtf_column_name(i)}"
reader = readers[i] if i < len(readers) else STRING_READER
if reader == OFFSET_DATETIME_READER:
code.append(
f"{{ java.time.OffsetDateTime v = rs.getObject({i+1}, java.time.OffsetDateTime.class);"
f" {column} = v == null ? null : v.toString(); }}\n"
)
elif reader == INSTANT_READER:
code.append(
f"{{ Timestamp v = rs.getTimestamp({i+1});"
f" {column} = v == null ? null : v.toInstant().toString(); }}\n"
)
else:
code.append(f"{column} = rs.getString({i+1});\n")
return "".join(code)

def create_output_row_class(self):
return "".join(
Expand Down Expand Up @@ -685,14 +709,10 @@ def to_snow_type(self) -> StructType:
# Source names that differ only by case would both normalize to the same
# unquoted Snowflake name, so keep their exact case by quoting them.
lowered_names = [row[0].lower() for row in self.raw_schema]
for (
field_name,
jdbc_type,
java_type,
precision,
scale,
nullable,
) in self.raw_schema:
readers = []
for row in self.raw_schema:
field_name, jdbc_type, java_type, precision, scale, nullable = row[:6]
type_name = row[6] if len(row) > 6 else None
if lowered_names.count(field_name.lower()) > 1:
field_name = '"' + field_name.replace('"', '""') + '"'
try:
Expand All @@ -701,27 +721,48 @@ def to_snow_type(self) -> StructType:
jdbc_type = JDBCType.NOT_SUPPORTED

jdbc_to_snow_type = JDBC_TYPE_TO_SNOWFLAKE_TYPE.get(jdbc_type, None)
java_to_snow_type = JAVA_TYPE_TO_SNOWFLAKE_TYPE.get(java_type, VariantType)
# Types without a mapping are read as strings, as in DataFrameReader.dbapi.
java_to_snow_type = JAVA_TYPE_TO_SNOWFLAKE_TYPE.get(java_type, StringType)
snow_type = (
java_to_snow_type if jdbc_to_snow_type is None else jdbc_to_snow_type
)
if snow_type == DecimalType:
data_type = self._to_numeric_type(field_name, precision, scale)
elif snow_type == TimestampType and jdbc_type in (
JDBCType.TIMESTAMP,
None,
JDBCType.OTHER,
type_name = (type_name or "").lower()
reader = STRING_READER
if (
java_type in TIMESTAMP_TZ_JAVA_TYPES
or jdbc_type == JDBCType.TIMESTAMP_WITH_TIMEZONE
or type_name == "timestamptz"
):
data_type = TimestampType(TimestampTimeZone.TZ)
reader = OFFSET_DATETIME_READER
elif java_type in TIMESTAMP_LTZ_JAVA_TYPES:
data_type = TimestampType(TimestampTimeZone.LTZ)
reader = INSTANT_READER
elif jdbc_type == JDBCType.TIME_WITH_TIMEZONE or type_name == "timetz":
# Snowflake TIME has no time zone; keep the offset in a string.
data_type = StringType()
elif jdbc_type == JDBCType.BIT:
# JDBC BIT covers both booleans and bit strings, and MySQL also
# reports TINYINT(1) as BIT(1) although it can hold any tiny integer.
# Only map the types known to be booleans; keep the rest as strings.
data_type = (
BooleanType()
if type_name in ("bool", "boolean")
or (type_name == "bit" and self._dbms == "sqlserver")
else StringType()
)
elif snow_type == DecimalType:
data_type = self._to_numeric_type(field_name, precision, scale)
elif snow_type == TimestampType:
# Offset-aware types were handled above. Java Timestamp fallback
# is wall-clock time, even when JDBC reports an unknown type code.
data_type = snow_type(TimestampTimeZone.NTZ)
elif snow_type == TimestampType and jdbc_type in (
JDBCType.TIMESTAMP_WITH_TIMEZONE,
JDBCType.TIME_WITH_TIMEZONE,
):
data_type = snow_type(TimestampTimeZone.TZ)
else:
data_type = snow_type()

fields.append(StructField(field_name, data_type, nullable))
readers.append(reader)
self._column_readers = readers
return StructType(fields)

def generate_select_sql(self):
Expand Down
135 changes: 135 additions & 0 deletions tests/integ/datasource/test_jdbc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#
# Copyright (c) 2012-2025 Snowflake Computing Inc. All rights reserved.
#
import datetime
import re
from decimal import Decimal
from unittest.mock import patch
Expand All @@ -14,11 +15,15 @@
)
from snowflake.snowpark.exceptions import SnowparkSQLException
from snowflake.snowpark.types import (
BooleanType,
DecimalType,
DoubleType,
FloatType,
StringType,
StructField,
StructType,
TimestampTimeZone,
TimestampType,
)
from tests.resources.test_data_source_dir.test_jdbc_data import (
URL,
Expand Down Expand Up @@ -714,3 +719,133 @@ def temp_functions():
)
assert [r[0] for r in df.collect()] == [1]
assert len(temp_functions() - before) == 1


@pytest.mark.parametrize(
"url_fixture, init, query, expected_types, expected_rows",
[
(
"postgres",
[
"create temp table bool_bit (bo bool, b1 bit(1), b4 bit(4))",
"insert into bool_bit values (true, B'1', B'1010'), (false, B'0', B'0001'), (null, null, null)",
],
"select * from bool_bit",
[BooleanType(), StringType(), StringType()],
[(True, "1", "1010"), (False, "0", "0001"), (None, None, None)],
),
(
"mysql",
[
"create temporary table bool_bit (b1 bit(1), t1 tinyint(1), bo boolean)",
"insert into bool_bit values (b'1', 5, true), (b'0', 0, false), (null, null, null)",
],
"select * from bool_bit",
# MySQL reports TINYINT(1) like BIT(1), and it can hold 5: keep strings.
[StringType(), StringType(), StringType()],
[("1", "5", "1"), ("0", "0", "0"), (None, None, None)],
),
(
"sql_server",
[
"create table #bool_bit (b bit)",
"insert into #bool_bit values (1), (0), (null)",
],
"select * from #bool_bit",
[BooleanType()],
[(True,), (False,), (None,)],
),
],
)
def test_jdbc_boolean_and_bit_columns(
session,
postgres_udtf_configs,
mysql_udtf_configs,
sql_server_udtf_configs,
url_fixture,
init,
query,
expected_types,
expected_rows,
):
"""SNOW-4537767: Postgres bool and SQL Server bit used to be read as strings."""
url, configs = {
"postgres": (POSTGRES_URL, postgres_udtf_configs),
"mysql": (MYSQL_URL, mysql_udtf_configs),
"sql_server": (SQL_SERVER_URL, sql_server_udtf_configs),
}[url_fixture]
df = session.read.jdbc(
url=url, udtf_configs=configs, query=query, session_init_statement=init
)
assert [f.datatype for f in df.schema.fields] == expected_types
assert sorted((tuple(r) for r in df.collect()), key=str) == sorted(
expected_rows, key=str
)


UTC = datetime.timezone.utc


@pytest.mark.parametrize(
"url_fixture, init, query, expected_types, expected_row",
[
(
"oracle",
[],
"SELECT TIMESTAMP '2024-01-01 10:00:00.123 -08:00' AS TZ, "
"TIMESTAMP '2024-07-01 10:00:00 Asia/Kolkata' AS TZR, "
"CAST(TIMESTAMP '2024-01-01 10:00:00 +00:00' AS TIMESTAMP WITH LOCAL TIME ZONE) AS LTZ, "
"INTERVAL '2' DAY AS IV FROM DUAL",
[
TimestampType(TimestampTimeZone.TZ),
TimestampType(TimestampTimeZone.TZ),
TimestampType(TimestampTimeZone.LTZ),
StringType(),
],
(
datetime.datetime(2024, 1, 1, 18, 0, 0, 123000, tzinfo=UTC),
datetime.datetime(2024, 7, 1, 4, 30, tzinfo=UTC),
datetime.datetime(2024, 1, 1, 10, 0, tzinfo=UTC),
"2 0:0:0.0",
),
),
(
"postgres",
[],
"select '2024-07-01 10:00+05:30'::timestamptz as tz, '03:04:05-08'::timetz as ttz",
[TimestampType(TimestampTimeZone.TZ), StringType()],
(datetime.datetime(2024, 7, 1, 4, 30, tzinfo=UTC), "03:04:05-08"),
),
(
"sql_server",
[],
"select cast('2024-07-01 10:00:00 +05:30' as datetimeoffset) as dto",
[TimestampType(TimestampTimeZone.TZ)],
(datetime.datetime(2024, 7, 1, 4, 30, tzinfo=UTC),),
),
],
)
def test_jdbc_time_zone_columns(
session,
udtf_configs,
postgres_udtf_configs,
sql_server_udtf_configs,
url_fixture,
init,
query,
expected_types,
expected_row,
):
"""SNOW-4537767: time-zone aware values used to be VARIANT strings, and
Postgres timestamptz lost its time zone."""
url, configs = {
"oracle": (URL, udtf_configs),
"postgres": (POSTGRES_URL, postgres_udtf_configs),
"sql_server": (SQL_SERVER_URL, sql_server_udtf_configs),
}[url_fixture]
df = session.read.jdbc(
url=url, udtf_configs=configs, query=query, session_init_statement=init
)
assert [f.datatype for f in df.schema.fields] == expected_types
# Aware datetimes compare by instant, whatever offset they are returned in.
assert tuple(df.collect()[0]) == expected_row
Loading
Loading