Skip to content

Commit 7843334

Browse files
committed
Escape double quotes in DoubleQuotingString and improve documentation for SQL-safe string handling.
1 parent dd9e885 commit 7843334

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

sqlx4k/src/commonMain/kotlin/io/github/smyrgeorge/sqlx4k/impl/extensions/encode.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ fun Any?.encodeValue(encoders: ValueEncoderRegistry): String {
4444
"'${replace("'", "''")}'"
4545
}
4646

47-
is DoubleQuotingString -> "\"${NoQuotingString(value).encodeValue(encoders)}\""
47+
is DoubleQuotingString -> {
48+
// Escape double quotes by doubling them (SQL standard for quoted identifiers)
49+
// https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
50+
"\"${value.replace("\"", "\"\"")}\""
51+
}
4852

4953
is NoQuotingString -> {
5054
// Fast path: if no single quote present, avoid replace allocation

sqlx4k/src/commonMain/kotlin/io/github/smyrgeorge/sqlx4k/impl/types/DoubleQuotingString.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@ import kotlin.jvm.JvmInline
55
/**
66
* A wrapper class for representing a string value that applies double quotes.
77
*
8-
* This class is useful in contexts where a string value needs to be explicitly quoted
9-
* with double quotation marks, such as when working with SQL identifiers or similar scenarios.
8+
* This class is useful for SQL identifiers (table names, column names) that need to be
9+
* quoted with double quotation marks. Double quotes within the value are automatically
10+
* escaped by doubling them (SQL standard).
11+
*
12+
* ⚠️ **Best Practice**: Use this ONLY for trusted identifiers. For user-controlled table/column
13+
* names, validate against a whitelist before using this class.
14+
*
15+
* Example: `DoubleQuotingString("user")` → `"user"`
16+
* Escaping: `DoubleQuotingString("my\"table")` → `"my""table"`
1017
*
1118
* @property value The string value to be wrapped with double quotes.
1219
*/

sqlx4k/src/commonMain/kotlin/io/github/smyrgeorge/sqlx4k/impl/types/NoQuotingString.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ package io.github.smyrgeorge.sqlx4k.impl.types
33
import kotlin.jvm.JvmInline
44

55
/**
6-
* A wrapper class for representing a string value without applying quotes.
6+
* A wrapper class for representing a string value without applying outer quotes.
77
*
8-
* This class is helpful when working with raw SQL or similar contexts where quoting
9-
* the string is unnecessary or undesired. The value is directly returned
10-
* as-is when the `toString` function is invoked.
8+
* ⚠️ **SECURITY WARNING**: This class bypasses standard SQL quoting and should ONLY be used for
9+
* trusted, developer-controlled values such as SQL keywords (e.g., "CURRENT_TIMESTAMP", "DEFAULT").
10+
* **NEVER use with user input** - it can lead to SQL injection vulnerabilities!
11+
*
12+
* While single quotes are escaped internally, the value is not wrapped in quotes, making it
13+
* unsafe for user-controlled data.
14+
*
15+
* Safe usage: `NoQuotingString("CURRENT_TIMESTAMP")` or `NoQuotingString("DEFAULT")`
16+
* Unsafe: `NoQuotingString(userInput)` ❌
1117
*
1218
* @property value The raw string value represented by this class.
1319
*/

0 commit comments

Comments
 (0)