Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,9 @@ public String convertJsonToHstoreLiteral(String jsonValue) {
isFirst = false;
}

// 5. Wrap the final result in single quotes to make it a PG string literal.
return "'" + hstoreBuilder.toString() + "'";
// 5. Escape SQL single quotes and null bytes, then wrap in single quotes to make a PG
// string literal.
return "'" + cleanSql(hstoreBuilder.toString()) + "'";

} catch (JsonProcessingException e) {
LOG.error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void testHstoreConversion_shouldHandleSpecialCharacters() {
// Arrange
DatastreamToPostgresDML dmlBuilder = DatastreamToPostgresDML.of(null);
String inputJsonWithQuotes = "{\"key\\\"\":\"val'ue\"}";
String expectedOutput = "'\"key\\\"\"=>\"val'ue\"'";
String expectedOutput = "'\"key\\\"\"=>\"val''ue\"'";

// Act
String actualOutput = dmlBuilder.convertJsonToHstoreLiteral(inputJsonWithQuotes);
Expand All @@ -150,6 +150,43 @@ public void testHstoreConversion_shouldHandleSpecialCharacters() {
assertEquals(expectedOutput, actualOutput);
}

/**
* Test whether {@link DatastreamToPostgresDML#convertJsonToHstoreLiteral(String)} prevents SQL
* injection by escaping single quotes and stripping null bytes in HSTORE keys and values.
*/
@Test
public void testHstoreConversion_preventsSqlInjection() {
DatastreamToPostgresDML dmlBuilder = DatastreamToPostgresDML.of(null);

assertEquals(
"'\"a'', 1); --\"=>\"b\"'",
dmlBuilder.convertJsonToHstoreLiteral("{\"a', 1); --\":\"b\"}"));
assertEquals(
"'\"key\"=>\"val'', 1); --\"'",
dmlBuilder.convertJsonToHstoreLiteral("{\"key\":\"val', 1); --\"}"));
assertEquals(
"'\"k\\\\''\"=>\"v\\\\''\"'",
dmlBuilder.convertJsonToHstoreLiteral("{\"k\\\\'\\u0000\":\"v\\\\'\\u0000\"}"));
}

/**
* Test whether {@link DatastreamToPostgresDML#getValueSql(JsonNode, String, Map)} escapes single
* quotes for HSTORE columns provided as both JSON objects and JSON strings.
*/
@Test
public void testHstoreTypeCoercion_escapesSingleQuotes() {
DatastreamToPostgresDML dmlBuilder = DatastreamToPostgresDML.of(null);
Map<String, String> tableSchema = new HashMap<>();
tableSchema.put("hstore_col", "HSTORE");
String expected = "'\"a'', 1); --\"=>\"b''c\"'::hstore";

JsonNode objectRowObj = getRowObj("{\"hstore_col\":{\"a', 1); --\":\"b'c\"}}");
assertEquals(expected, dmlBuilder.getValueSql(objectRowObj, "hstore_col", tableSchema));

JsonNode stringRowObj = getRowObj("{\"hstore_col\":\"{\\\"a', 1); --\\\":\\\"b'c\\\"}\"}");
assertEquals(expected, dmlBuilder.getValueSql(stringRowObj, "hstore_col", tableSchema));
}

/**
* Test whether {@link DatastreamToPostgresDML#getValueSql(JsonNode, String, Map)} correctly
* handles null and empty JSON for HSTORE types.
Expand Down
Loading