Repository navigation
Fix SQL injection in connector sample path and validate source queries #688
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -82,7 +82,7 @@ protected SchemaReader getSchemaReader(String sessionID) { | |||||||||||||||||
|
|
||||||||||||||||||
| @Override | ||||||||||||||||||
| protected String getTableName(String database, String schema, String table) { | ||||||||||||||||||
| return String.format("`%s`.`%s`", database, table); | ||||||||||||||||||
| return quoteIdentifier(database, '`') + "." + quoteIdentifier(table, '`'); | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
84
to
86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| @Override | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -86,7 +86,7 @@ protected SchemaReader getSchemaReader(String sessionID) { | |||||||||||||||||
|
|
||||||||||||||||||
| @Override | ||||||||||||||||||
| protected String getTableName(String database, String schema, String table) { | ||||||||||||||||||
| return String.format("\"%s\".\"%s\"", schema, table); | ||||||||||||||||||
| return quoteIdentifier(schema, '"') + "." + quoteIdentifier(table, '"'); | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
88
to
90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| @Override | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -150,8 +150,17 @@ protected String getConnectionString(String database) { | |
| } | ||
|
|
||
| protected String getTableName(String database, String schema, String table) { | ||
| return schema == null ? String.format("\"%s\".\"%s\"", database, table) | ||
| : String.format("\"%s\".\"%s\".\"%s\"", database, schema, table); | ||
| return schema == null ? quoteIdentifier(database, '"') + "." + quoteIdentifier(table, '"') | ||
| : quoteIdentifier(database, '"') + "." + quoteIdentifier(schema, '"') + "." + quoteIdentifier(table, '"'); | ||
| } | ||
|
Comment on lines
152
to
+155
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If protected String getTableName(String database, String schema, String table) {
StringBuilder sb = new StringBuilder();
if (database != null) {
sb.append(quoteIdentifier(database, '"')).append(".");
}
if (schema != null) {
sb.append(quoteIdentifier(schema, '"')).append(".");
}
sb.append(quoteIdentifier(table, '"'));
return sb.toString();
} |
||
|
|
||
| /** | ||
| * Wraps the given identifier in the given quote character, doubling any embedded quote characters so that a | ||
| * user supplied database, schema, table or column name can never break out of the quoted identifier. | ||
| */ | ||
| public static String quoteIdentifier(String identifier, char quote) { | ||
| String quoteString = String.valueOf(quote); | ||
| return quoteString + identifier.replace(quoteString, quoteString + quoteString) + quoteString; | ||
| } | ||
|
Comment on lines
+161
to
164
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If public static String quoteIdentifier(String identifier, char quote) {
if (identifier == null) {
return null;
}
String quoteString = String.valueOf(quote);
return quoteString + identifier.replace(quoteString, quoteString + quoteString) + quoteString;
} |
||
|
|
||
| protected String getTableQuery(String database, String schema, String table) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -46,6 +46,14 @@ protected String createConnectionString() { | |||||||||
| return databaseSourceConfig.connectionString; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Override | ||||||||||
| protected boolean useSquareBracketQuotes() { | ||||||||||
| // SQL Server quotes identifiers with square brackets | ||||||||||
| String connectionString = databaseSourceConfig.connectionString; | ||||||||||
| return connectionString != null && (connectionString.toLowerCase().startsWith("jdbc:sqlserver:") | ||||||||||
| || connectionString.toLowerCase().startsWith("jdbc:jtds:sqlserver:")); | ||||||||||
|
Comment on lines
+53
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Generic database source configuration. | ||||||||||
| */ | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -117,7 +117,8 @@ public StructuredRecord transform(LongWritable longWritable, SqlServerSourceDBRe | |||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||
| protected String getTableName(String database, String schema, String table) { | ||||||||||||||||||||||||||||||||||||
| return String.format("\"%s\".\"%s\".\"%s\"", database, schema, table); | ||||||||||||||||||||||||||||||||||||
| return quoteIdentifier(database, '"') + "." + quoteIdentifier(schema, '"') + "." | ||||||||||||||||||||||||||||||||||||
| + quoteIdentifier(table, '"'); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
119
to
122
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If
Suggested change
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||
|
|
@@ -137,6 +138,7 @@ protected String getRandomQuery(String tableName, int limit) { | |||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||
| protected String getStratifiedQuery(String tableName, int limit, String strata, String sessionID) { | ||||||||||||||||||||||||||||||||||||
| String strataColumn = quoteIdentifier(strata, '"'); | ||||||||||||||||||||||||||||||||||||
| return String.format("WITH t_%s AS (\n" + | ||||||||||||||||||||||||||||||||||||
| " SELECT *,\n" + | ||||||||||||||||||||||||||||||||||||
| " ROW_NUMBER() OVER (ORDER BY %s, RAND()) AS sqn_%s,\n" + | ||||||||||||||||||||||||||||||||||||
|
|
@@ -146,8 +148,8 @@ protected String getStratifiedQuery(String tableName, int limit, String strata, | |||||||||||||||||||||||||||||||||||
| "SELECT TOP %d * FROM t_%s\n" + | ||||||||||||||||||||||||||||||||||||
| "WHERE sqn_%s %% CAST(0.5 * ((c_%s / %d + 1) + ABS(c_%s / %d - 1)) AS bigint) = 1\n" + | ||||||||||||||||||||||||||||||||||||
| "ORDER BY %s", | ||||||||||||||||||||||||||||||||||||
| sessionID, strata, sessionID, sessionID, tableName, limit, sessionID, sessionID, sessionID, | ||||||||||||||||||||||||||||||||||||
| limit, sessionID, limit, strata); | ||||||||||||||||||||||||||||||||||||
| sessionID, strataColumn, sessionID, sessionID, tableName, limit, sessionID, sessionID, | ||||||||||||||||||||||||||||||||||||
| sessionID, limit, sessionID, limit, strataColumn); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
schemais null, callingquoteIdentifier(schema, '"')will throw aNullPointerExceptionbecausequoteIdentifiercallsreplaceon the passed string. Sinceschemacan be null when not specified, we should handle the null case gracefully to avoid runtime exceptions.