Skip to content

Commit 4230c30

Browse files
code QL issues fix in progressdbclient.cs file inside kernal memory floder
1 parent 0d41d11 commit 4230c30

2 files changed

Lines changed: 13 additions & 10 deletions

File tree

App/kernel-memory/extensions/Postgres/Postgres/Internals/PostgresDbClient.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ public async Task DeleteTableAsync(
315315
await using (cmd.ConfigureAwait(false))
316316
{
317317
#pragma warning disable CA2100 // SQL reviewed
318-
cmd.CommandText = $"DROP TABLE IF EXISTS {tableName}";
318+
// Escape and quote the table name to prevent SQL injection. This expects previous normalization/validation.
319+
cmd.CommandText = $"DROP TABLE IF EXISTS {EscapeIdentifierForPostgres(tableName)}";
319320
#pragma warning restore CA2100
320321

321322
this._log.LogTrace("Deleting table. SQL: {0}", cmd.CommandText);
@@ -778,4 +779,13 @@ private static long GenLockId(string resourceId)
778779
return BitConverter.ToUInt32(SHA256.HashData(Encoding.UTF8.GetBytes(resourceId)), 0)
779780
% short.MaxValue;
780781
}
782+
/// <summary>
783+
/// Escape a SQL identifier (such as table or schema name) for use in Postgres queries.
784+
/// Assumes the identifier is already validated.
785+
/// </summary>
786+
private static string EscapeIdentifierForPostgres(string identifier)
787+
{
788+
// Double quotes in identifiers are escaped by doubling them in PostgreSQL
789+
return $"\"{identifier.Replace("\"", "\"\"")}\"";
790+
}
781791
}

App/kernel-memory/extensions/Postgres/Postgres/PostgresMemory.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,18 +232,11 @@ private void Dispose(bool disposing)
232232
// Note: "_" is allowed in Postgres, but we normalize it to "-" for consistency with other DBs
233233
private static readonly Regex s_replaceIndexNameCharsRegex = new(@"[\s|\\|/|.|_|:]");
234234
private const string ValidSeparator = "-";
235-
// Only allow 1-63 chars, start with a lowercase letter, then letters, digits, dashes, or underscores.
236-
private static readonly Regex s_validIndexNameRegex = new(@"^[a-z][a-z0-9\-_]{0,62}$", RegexOptions.Compiled);
237-
235+
238236
private static string NormalizeIndexName(string index)
239237
{
240238
ArgumentNullExceptionEx.ThrowIfNullOrWhiteSpace(index, nameof(index), "The index name is empty");
241-
index = s_replaceIndexNameCharsRegex.Replace(index.Trim().ToLowerInvariant(), ValidSeparator);
242-
// Enforce positive validation for safe Postgres identifier.
243-
if (!s_validIndexNameRegex.IsMatch(index))
244-
{
245-
throw new ArgumentException($"Index name '{index}' is invalid. Must match regex: ^[a-z][a-z0-9\\-_]{{0,62}}$");
246-
}
239+
index = s_replaceIndexNameCharsRegex.Replace(index.Trim().ToLowerInvariant(), ValidSeparator);
247240

248241
PostgresSchema.ValidateTableName(index);
249242

0 commit comments

Comments
 (0)