From 9aab8df0b4bb4119b0f834ad5a36fd865e3c04b1 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sat, 8 Dec 2018 19:59:43 +0100 Subject: [PATCH] More hardening --- Database/Database-ConnectionsCommands.cs | 2 +- Database/Database-ExchangeRates.cs | 2 ++ Database/Database-Financials.cs | 26 ++++++++++++------- .../Database-OrganizationFinancialAccounts.cs | 2 ++ Database/Database-Organizations.cs | 5 +++- Database/Database-People.cs | 6 ++--- 6 files changed, 28 insertions(+), 15 deletions(-) diff --git a/Database/Database-ConnectionsCommands.cs b/Database/Database-ConnectionsCommands.cs index 7b7af070..a85a0ab7 100644 --- a/Database/Database-ConnectionsCommands.cs +++ b/Database/Database-ConnectionsCommands.cs @@ -228,7 +228,7 @@ namespace Swarmops.Database return d.ToString ("yyyyMMddHHmmss"); } - + [Obsolete("This should be replaced with parameterized queries everywhere, so making a compile error.", true)] private string SqlSanitize (string input) // TODO: Replace with parameterized queries everywhere. { string[] forbiddenArray = diff --git a/Database/Database-ExchangeRates.cs b/Database/Database-ExchangeRates.cs index ad630721..db643879 100644 --- a/Database/Database-ExchangeRates.cs +++ b/Database/Database-ExchangeRates.cs @@ -77,6 +77,8 @@ namespace Swarmops.Database "SELECT" + currencyFieldSequence + "WHERE Code='" + currencyCode.Replace("'", "''").ToUpperInvariant() + "';", connection); + // TODO: NOTE THIS STRING QUERY, THOUGH IN COMMENTED OUT CODE, IS NOT PARAMETERIZED + using (DbDataReader reader = command.ExecuteReader()) { if (reader.Read()) diff --git a/Database/Database-Financials.cs b/Database/Database-Financials.cs index 936f1b96..03d750f1 100644 --- a/Database/Database-Financials.cs +++ b/Database/Database-Financials.cs @@ -221,7 +221,9 @@ namespace Swarmops.Database DbCommand command = GetDbCommand ( "SELECT " + financialTransactionFieldSequence + " WHERE OrganizationId=" + - organizationId + " AND ImportHash='" + SqlSanitize(importKey) + "';", connection); + organizationId + " AND ImportHash=@importKey';", connection); + + AddParameterWithName(command, "importKey", importKey); using (DbDataReader reader = command.ExecuteReader()) { @@ -1135,17 +1137,21 @@ namespace Swarmops.Database using (DbCommand command = GetDbCommand( - "Select FinancialTransactionId from FinancialTransactionForeignIds WHERE FinancialTransactionForeignIdType=" + (int)foreignIdType + - " AND ForeignId='" + SqlSanitize(foreignId) + "';", connection)) - - using (DbDataReader reader = command.ExecuteReader()) + "Select FinancialTransactionId from FinancialTransactionForeignIds WHERE FinancialTransactionForeignIdType=" + + (int) foreignIdType + " AND ForeignId=@foreignId;", connection)) { - if (reader.Read()) - { - return reader.GetInt32(0); - } - return 0; // none found. Todo: throw exception instead? This behavior is inconsistent + AddParameterWithName(command, "foreignId", foreignId); + + using (DbDataReader reader = command.ExecuteReader()) + { + if (reader.Read()) + { + return reader.GetInt32(0); + } + + return 0; // none found. Todo: throw exception instead? This behavior is inconsistent + } } } } diff --git a/Database/Database-OrganizationFinancialAccounts.cs b/Database/Database-OrganizationFinancialAccounts.cs index 28cd987a..982e6445 100644 --- a/Database/Database-OrganizationFinancialAccounts.cs +++ b/Database/Database-OrganizationFinancialAccounts.cs @@ -57,6 +57,8 @@ namespace Swarmops.Database "AND ObjectOptionalDataTypes.Name='" + dataType.ToString() + "' " + "AND ObjectOptionalData.Data='" + data.Replace("'", "''") + "';", connection); + // NOTE: THIS STRING QUERY IS NOT PARAMETERIZED, EVEN THOUGH IT'S IN COMMENTED OUT CODE + using (DbDataReader reader = command.ExecuteReader()) { while (reader.Read()) diff --git a/Database/Database-Organizations.cs b/Database/Database-Organizations.cs index a48f8fdd..6272b46b 100644 --- a/Database/Database-Organizations.cs +++ b/Database/Database-Organizations.cs @@ -66,6 +66,9 @@ namespace Swarmops.Database public void DeleteOrganization (int OrganizationId) { + throw new NotImplementedException(); + + /* int deletedOrganizationsID = 98; BasicOrganization org = GetOrganization (OrganizationId); //Delete is done by setting parent ID to deletedOrganizationsID @@ -80,7 +83,7 @@ namespace Swarmops.Database org.AcceptsMembers, org.AutoAssignNewMembers, org.DefaultCountryId, - OrganizationId); + OrganizationId);*/ } public void UpdateOrganization (int ParentOrganizationId, string NameInternational, string Name, diff --git a/Database/Database-People.cs b/Database/Database-People.cs index 0c38dc28..886e2a9d 100644 --- a/Database/Database-People.cs +++ b/Database/Database-People.cs @@ -66,9 +66,9 @@ namespace Swarmops.Database DbCommand command = GetDbCommand ( - "SELECT " + personFieldSequence + " WHERE Name like '" + - GetSqlPatternFromNamePattern (namePattern).Replace ("'", "''") + "'", + "SELECT " + personFieldSequence + " WHERE Name like @namePattern", connection); + AddParameterWithName(command, "namePattern", namePattern); command.CommandTimeout = 120; using (DbDataReader reader = command.ExecuteReader()) @@ -86,7 +86,7 @@ namespace Swarmops.Database private string GetSqlPatternFromNamePattern (string namePattern) { // Remove any injection codes - namePattern = SqlSanitize (namePattern).Replace ("%", String.Empty).Trim(); + namePattern = namePattern.Replace ("%", String.Empty).Trim(); while (namePattern.Contains (" ")) {