Further hardening: Parameterizing queries
This commit is contained in:
@@ -101,10 +101,11 @@ namespace Swarmops.Database
|
||||
string sqlQuery =
|
||||
"SELECT Documents.DocumentId,Documents.ServerFileName,Documents.ClientFileName,Documents.Description,DocumentTypes.Name AS DocumentType,Documents.ForeignId,Documents.FileSize,Documents.UploadedByPersonId,Documents.UploadedDateTime From Documents,DocumentTypes " +
|
||||
"WHERE Documents.DocumentTypeId=DocumentTypes.DocumentTypeId AND " +
|
||||
"Documents.Description = '" + description.Replace ("'", "''") + "' AND " +
|
||||
"Documents.Description = @description AND " +
|
||||
"Documents.UploadedDateTime > '" + DateTime.UtcNow.AddDays (-1).ToString ("yyyy-MM-dd HH:mm") + "'";
|
||||
|
||||
DbCommand command = GetDbCommand (sqlQuery, connection);
|
||||
AddParameterWithName(command, "description", description);
|
||||
|
||||
using (DbDataReader reader = command.ExecuteReader())
|
||||
{
|
||||
|
||||
@@ -15,10 +15,11 @@ namespace Swarmops.Database
|
||||
|
||||
DbCommand command =
|
||||
GetDbCommand (
|
||||
"SELECT ExternalCredentialId,ServiceName,Login,Password From ExternalCredentials WHERE ServiceName='" +
|
||||
serviceName.Replace ("'", "''") + "'",
|
||||
"SELECT ExternalCredentialId,ServiceName,Login,Password From ExternalCredentials WHERE ServiceName=@serviceName",
|
||||
connection);
|
||||
|
||||
AddParameterWithName(command, "serviceName", serviceName);
|
||||
|
||||
using (DbDataReader reader = command.ExecuteReader())
|
||||
{
|
||||
if (reader.Read())
|
||||
|
||||
@@ -285,9 +285,11 @@ namespace Swarmops.Database
|
||||
|
||||
DbCommand command =
|
||||
GetDbCommand (
|
||||
"SELECT " + geographyFieldSequence + " WHERE Name ='" + geographyName.Replace ("'", "''") + "'",
|
||||
"SELECT " + geographyFieldSequence + " WHERE Name=@geographyName",
|
||||
connection);
|
||||
|
||||
AddParameterWithName(command, "geographyName", geographyName);
|
||||
|
||||
using (DbDataReader reader = command.ExecuteReader())
|
||||
{
|
||||
if (reader.Read())
|
||||
@@ -376,8 +378,9 @@ namespace Swarmops.Database
|
||||
DbCommand command =
|
||||
GetDbCommand (
|
||||
"select GeographyId FROM GeographyDesignations WHERE CountryId=" + countryId +
|
||||
" AND GeographyLevelId= " + ((int) level) + " AND Designation='" +
|
||||
designation.Replace ("'", "''") + "'", connection);
|
||||
" AND GeographyLevelId= " + ((int) level) + " AND Designation=@designation", connection);
|
||||
|
||||
AddParameterWithName(command, "designation", designation);
|
||||
|
||||
using (DbDataReader reader = command.ExecuteReader())
|
||||
{
|
||||
|
||||
@@ -74,8 +74,9 @@ namespace Swarmops.Database
|
||||
|
||||
DbCommand command =
|
||||
GetDbCommand (
|
||||
"SELECT" + internalPollVoteFieldSequence + "WHERE VerificationCode='" +
|
||||
verificationCode.Replace ("'", "''") + "';", connection);
|
||||
"SELECT" + internalPollVoteFieldSequence + "WHERE VerificationCode=@verificationCode", connection);
|
||||
|
||||
AddParameterWithName(command, "verificationCode", verificationCode);
|
||||
|
||||
using (DbDataReader reader = command.ExecuteReader())
|
||||
{
|
||||
|
||||
@@ -15,11 +15,9 @@ namespace Swarmops.Database
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
templatename = templatename.Replace ("'", "''");
|
||||
|
||||
string sql = string.Format ("SELECT * FROM MailTemplates WHERE (TemplateName = '{0}')", templatename);
|
||||
|
||||
string sql = "SELECT * FROM MailTemplates WHERE (TemplateName = @templateName)";
|
||||
DbCommand command = GetDbCommand (sql, connection);
|
||||
AddParameterWithName(command, "templateName", templatename);
|
||||
|
||||
using (DbDataReader reader = command.ExecuteReader())
|
||||
{
|
||||
|
||||
@@ -212,8 +212,8 @@ namespace Swarmops.Database
|
||||
|
||||
DbCommand command =
|
||||
GetDbCommand (
|
||||
"SELECT MediaKeywordId From MediaKeywords WHERE MediaKeyword='" + keyword.Replace ("'", "''") +
|
||||
"'", connection);
|
||||
"SELECT MediaKeywordId From MediaKeywords WHERE MediaKeyword=@keyword", connection);
|
||||
AddParameterWithName(command, "keyword", keyword);
|
||||
|
||||
using (DbDataReader reader = command.ExecuteReader())
|
||||
{
|
||||
@@ -281,8 +281,8 @@ namespace Swarmops.Database
|
||||
|
||||
DbCommand command =
|
||||
GetDbCommand (
|
||||
"SELECT MediaCategoryId, Name FROM MediaCategories WHERE Name='" +
|
||||
mediaCategoryName.Replace ("'", "''") + "'", connection);
|
||||
"SELECT MediaCategoryId, Name FROM MediaCategories WHERE Name=@mediaCategoryName", connection);
|
||||
AddParameterWithName(command, "mediaCategoryName", mediaCategoryName);
|
||||
|
||||
using (DbDataReader reader = command.ExecuteReader())
|
||||
{
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace Swarmops.Database
|
||||
|
||||
DbCommand command =
|
||||
GetDbCommand (
|
||||
"SELECT" + objectOptionalDataFieldSequence + "WHERE ObjectTypes.Name='" + objectType + "' " +
|
||||
"SELECT" + objectOptionalDataFieldSequence + "WHERE ObjectTypes.Name='" + objectType + "' " + // objectType is an enum
|
||||
"AND ObjectOptionalData.ObjectId=" + objectId + ";", connection);
|
||||
|
||||
using (DbDataReader reader = command.ExecuteReader())
|
||||
@@ -70,9 +70,11 @@ namespace Swarmops.Database
|
||||
|
||||
DbCommand command =
|
||||
GetDbCommand (
|
||||
"SELECT" + objectOptionalDataFieldSequence + "WHERE ObjectTypes.Name='" + objectType + "' " +
|
||||
"AND ObjectOptionalDataTypes.Name='" + dataType + "' " +
|
||||
"AND ObjectOptionalData.Data='" + data.Replace ("'", "''") + "';", connection);
|
||||
"SELECT" + objectOptionalDataFieldSequence + "WHERE ObjectTypes.Name='" + objectType + "' " + // objectType is an enum
|
||||
"AND ObjectOptionalDataTypes.Name='" + dataType + "' " + // dataType is an enum; not user input
|
||||
"AND ObjectOptionalData.Data=@dataString", connection);
|
||||
|
||||
AddParameterWithName(command, "dataString", data);
|
||||
|
||||
using (DbDataReader reader = command.ExecuteReader())
|
||||
{
|
||||
|
||||
@@ -16,8 +16,9 @@ namespace Swarmops.Database
|
||||
|
||||
DbCommand command =
|
||||
GetDbCommand (
|
||||
"select PaymentCodes.*, PhoneNumbers.PhoneNumber AS IssuedToPhoneNumber FROM PaymentCodes, PhoneNumbers where PaymentCodes.IssuedToPhoneId=PhoneNumbers.PhoneNumberId AND PaymentCodes.PaymentCode='" +
|
||||
paymentCode.Replace ("'", "''") + "'", connection);
|
||||
"select PaymentCodes.*, PhoneNumbers.PhoneNumber AS IssuedToPhoneNumber FROM PaymentCodes, PhoneNumbers where PaymentCodes.IssuedToPhoneId=PhoneNumbers.PhoneNumberId AND PaymentCodes.PaymentCode=@paymentCode", connection);
|
||||
|
||||
AddParameterWithName(command, "paymentCode", paymentCode);
|
||||
|
||||
using (DbDataReader reader = command.ExecuteReader())
|
||||
{
|
||||
|
||||
@@ -44,10 +44,11 @@ namespace Swarmops.Database
|
||||
|
||||
DbCommand command =
|
||||
GetDbCommand ("SELECT" + paymentGroupFieldSequence +
|
||||
"WHERE OrganizationId=" + organizationId + " AND Tag='" + tag.Replace ("'", "''") +
|
||||
"'",
|
||||
"WHERE OrganizationId=" + organizationId + " AND Tag=@paymentTag",
|
||||
connection);
|
||||
|
||||
AddParameterWithName(command, "paymentTag", tag);
|
||||
|
||||
using (DbDataReader reader = command.ExecuteReader())
|
||||
{
|
||||
if (reader.Read())
|
||||
|
||||
+17
-10
@@ -66,7 +66,7 @@ namespace Swarmops.Database
|
||||
|
||||
DbCommand command =
|
||||
GetDbCommand (
|
||||
"SELECT " + personFieldSequence + " WHERE Name like @namePattern",
|
||||
"SELECT " + personFieldSequence + " WHERE Name LIKE @namePattern",
|
||||
connection);
|
||||
AddParameterWithName(command, "namePattern", namePattern);
|
||||
command.CommandTimeout = 120;
|
||||
@@ -150,8 +150,10 @@ namespace Swarmops.Database
|
||||
|
||||
DbCommand command =
|
||||
GetDbCommand (
|
||||
"SELECT " + personFieldSequence + " WHERE Email LIKE '" + emailPattern.Replace ("'", "''") + "'",
|
||||
"SELECT " + personFieldSequence + " WHERE Email LIKE @emailPattern",
|
||||
connection);
|
||||
AddParameterWithName(command, "emailPattern", emailPattern);
|
||||
|
||||
command.CommandTimeout = 120;
|
||||
|
||||
using (DbDataReader reader = command.ExecuteReader())
|
||||
@@ -176,8 +178,10 @@ namespace Swarmops.Database
|
||||
|
||||
DbCommand command =
|
||||
GetDbCommand (
|
||||
"SELECT " + personFieldSequence + " WHERE City like '" + cityPattern.Replace ("'", "''") + "'",
|
||||
"SELECT " + personFieldSequence + " WHERE City LIKE @cityPattern",
|
||||
connection);
|
||||
AddParameterWithName(command, "cityPattern", cityPattern);
|
||||
|
||||
command.CommandTimeout = 120;
|
||||
|
||||
using (DbDataReader reader = command.ExecuteReader())
|
||||
@@ -192,7 +196,7 @@ namespace Swarmops.Database
|
||||
return result.ToArray();
|
||||
}
|
||||
|
||||
public BasicPerson[] GetPeopleFromPostalCodePattern (string pcPattern)
|
||||
public BasicPerson[] GetPeopleFromPostalCodePattern (string postalCodePattern)
|
||||
{
|
||||
List<BasicPerson> result = new List<BasicPerson>();
|
||||
|
||||
@@ -202,9 +206,9 @@ namespace Swarmops.Database
|
||||
|
||||
DbCommand command =
|
||||
GetDbCommand (
|
||||
"SELECT " + personFieldSequence + " WHERE PostalCode like '" + pcPattern.Replace ("'", "''") +
|
||||
"'",
|
||||
"SELECT " + personFieldSequence + " WHERE PostalCode LIKE @postalPattern",
|
||||
connection);
|
||||
AddParameterWithName(command, "postalPattern", postalCodePattern);
|
||||
command.CommandTimeout = 120;
|
||||
|
||||
using (DbDataReader reader = command.ExecuteReader())
|
||||
@@ -221,9 +225,11 @@ namespace Swarmops.Database
|
||||
|
||||
public BasicPerson[] GetPeopleFromPostalCodes (string[] postalCodes)
|
||||
{
|
||||
// Security consideration: This must NEVER be called with user input as parameter, only with postal codes from our own db.
|
||||
|
||||
List<BasicPerson> result = new List<BasicPerson>();
|
||||
for (int i = 0; i < postalCodes.Length; ++i)
|
||||
postalCodes[i] = postalCodes[i].Replace ("'", "''");
|
||||
postalCodes[i] = postalCodes[i].Replace ("'", "''"); // but we harden it anyway just for defense in depth
|
||||
|
||||
string postalCodesString = ("'" + string.Join ("','", postalCodes) + "'").Replace (" ", "");
|
||||
using (DbConnection connection = GetMySqlDbConnection())
|
||||
@@ -258,8 +264,9 @@ namespace Swarmops.Database
|
||||
|
||||
DbCommand command =
|
||||
GetDbCommand (
|
||||
"SELECT " + personFieldSequence + " WHERE Email='" + email.Replace ("'", "''").Trim() + "'",
|
||||
"SELECT " + personFieldSequence + " WHERE Email=@email",
|
||||
connection);
|
||||
AddParameterWithName(command, "email", email);
|
||||
command.CommandTimeout = 120;
|
||||
|
||||
using (DbDataReader reader = command.ExecuteReader())
|
||||
@@ -286,8 +293,8 @@ namespace Swarmops.Database
|
||||
DbCommand command =
|
||||
GetDbCommand (
|
||||
"SELECT " + personFieldSequence + " WHERE CountryId=" + countryId.ToString() +
|
||||
" AND PhoneNumber='" +
|
||||
phoneNumber.Replace ("'", "''") + "'", connection);
|
||||
" AND PhoneNumber=@phoneNumber", connection);
|
||||
AddParameterWithName(command, "phoneNumber", phoneNumber);
|
||||
command.CommandTimeout = 300;
|
||||
|
||||
using (DbDataReader reader = command.ExecuteReader())
|
||||
|
||||
@@ -35,8 +35,8 @@ namespace Swarmops.Database
|
||||
|
||||
DbCommand command =
|
||||
GetDbCommand (
|
||||
"SELECT CityId FROM PostalCodes WHERE PostalCode='" + postalCode.Replace ("'", "''") +
|
||||
"' AND CountryId=" + countryId, connection);
|
||||
"SELECT CityId FROM PostalCodes WHERE PostalCode=@postalCode AND CountryId=" + countryId, connection);
|
||||
AddParameterWithName(command, "postalCode", postalCode);
|
||||
|
||||
using (DbDataReader reader = command.ExecuteReader())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user