Skip to content

Commit b7f5e88

Browse files
authored
Suppressed CodeQL alerts related to tainted SQL command text. (dotnet#4028)
1 parent 2e87b50 commit b7f5e88

1 file changed

Lines changed: 29 additions & 24 deletions

File tree

  • src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.cs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public sealed partial class SqlCommand : DbCommand, ICloneable
184184
// @TODO: Make auto-property, also make nullable.
185185
private SqlCommandColumnEncryptionSetting _columnEncryptionSetting =
186186
SqlCommandColumnEncryptionSetting.UseConnectionSetting;
187-
187+
188188
/// <summary>
189189
/// Text to execute when executing the command.
190190
/// </summary>
@@ -194,7 +194,7 @@ public sealed partial class SqlCommand : DbCommand, ICloneable
194194
/// Maximum amount of time, in seconds, the command will execute before timing out.
195195
/// </summary>
196196
private int? _commandTimeout;
197-
197+
198198
/// <summary>
199199
/// Type of the command to execute.
200200
/// </summary>
@@ -250,7 +250,7 @@ public sealed partial class SqlCommand : DbCommand, ICloneable
250250
/// causes a hidden prepare.
251251
/// </summary>
252252
private bool _hiddenPrepare = false;
253-
253+
254254
/// <summary>
255255
/// _inPrepare will be set immediately before the actual prepare is done. The OnReturnValue
256256
/// function will test this flag to determine whether the returned value is a
@@ -265,12 +265,12 @@ public sealed partial class SqlCommand : DbCommand, ICloneable
265265
private volatile bool _internalEndExecuteInitiated;
266266

267267
private SqlNotificationRequest _notification;
268-
268+
269269
#if NETFRAMEWORK
270270
// @TODO: Make auto-property
271271
private bool _notificationAutoEnlist = true;
272272
#endif
273-
273+
274274
/// <summary>
275275
/// Parameters that have been added to the current instance.
276276
/// </summary>
@@ -369,7 +369,7 @@ public sealed partial class SqlCommand : DbCommand, ICloneable
369369
/// Current transaction the command is participating in.
370370
/// </summary>
371371
private SqlTransaction _transaction;
372-
372+
373373
/// <summary>
374374
/// How command results are applied to a DataRow when used by the update method of
375375
/// DbDataAdapter.
@@ -397,13 +397,15 @@ public SqlCommand()
397397
public SqlCommand(string cmdText)
398398
: this()
399399
{
400+
// CodeQL [SM03934] Executing user-supplied SQL commands is a core feature of a SQL driver.
400401
CommandText = cmdText;
401402
}
402403

403404
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlCommand.xml' path='docs/members[@name="SqlCommand"]/ctor[@name="cmdTextStringAndSqlConnection"]/*'/>
404405
public SqlCommand(string cmdText, SqlConnection connection)
405406
: this()
406407
{
408+
// CodeQL [SM03934] Executing user-supplied SQL commands is a core feature of a SQL driver.
407409
CommandText = cmdText;
408410
Connection = connection;
409411
}
@@ -412,6 +414,7 @@ public SqlCommand(string cmdText, SqlConnection connection)
412414
public SqlCommand(string cmdText, SqlConnection connection, SqlTransaction transaction)
413415
: this()
414416
{
417+
// CodeQL [SM03934] Executing user-supplied SQL commands is a core feature of a SQL driver.
415418
CommandText = cmdText;
416419
Connection = connection;
417420
Transaction = transaction;
@@ -425,6 +428,7 @@ public SqlCommand(
425428
SqlCommandColumnEncryptionSetting columnEncryptionSetting)
426429
: this()
427430
{
431+
// CodeQL [SM03934] Executing user-supplied SQL commands is a core feature of a SQL driver.
428432
CommandText = cmdText;
429433
Connection = connection;
430434
Transaction = transaction;
@@ -433,6 +437,7 @@ public SqlCommand(
433437

434438
private SqlCommand(SqlCommand from)
435439
{
440+
// CodeQL [SM03934] Executing user-supplied SQL commands is a core feature of a SQL driver.
436441
CommandText = from.CommandText;
437442
CommandTimeout = from.CommandTimeout;
438443
CommandType = from.CommandType;
@@ -455,7 +460,7 @@ private SqlCommand(SqlCommand from)
455460
#endregion
456461

457462
#region Events
458-
463+
459464
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlCommand.xml' path='docs/members[@name="SqlCommand"]/StatementCompleted/*'/>
460465
[ResCategory(nameof(Strings.DataCategory_StatementCompleted))]
461466
[ResDescription(nameof(Strings.DbCommand_StatementCompleted))]
@@ -470,28 +475,28 @@ public event StatementCompletedEventHandler StatementCompleted
470475
_statementCompletedEventHandler -= value;
471476
}
472477
}
473-
478+
474479
#endregion
475-
480+
476481
#region Enums
477-
482+
478483
// @TODO: Rename to match naming conventions
479484
private enum EXECTYPE
480485
{
481486
/// <summary>
482487
/// Execute unprepared commands, all server versions (results in sp_execsql call)
483488
/// </summary>
484-
UNPREPARED,
485-
489+
UNPREPARED,
490+
486491
/// <summary>
487492
/// Prepare and execute command, 8.0 and above only (results in sp_prepexec call)
488493
/// </summary>
489494
PREPAREPENDING,
490-
495+
491496
/// <summary>
492497
/// execute prepared commands, all server versions (results in sp_exec call)
493498
/// </summary>
494-
PREPARED,
499+
PREPARED,
495500
}
496501

497502
// Index into indirection arrays for columns of interest to DeriveParameters
@@ -524,7 +529,7 @@ private enum ProcParamsColIndex
524529
[ResCategory(nameof(Strings.DataCategory_Data))]
525530
[ResDescription(nameof(Strings.TCE_SqlCommand_ColumnEncryptionSetting))]
526531
public SqlCommandColumnEncryptionSetting ColumnEncryptionSetting => _columnEncryptionSetting;
527-
532+
528533
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlCommand.xml' path='docs/members[@name="SqlCommand"]/CommandTimeout/*'/>
529534
[ResCategory(nameof(Strings.DataCategory_Data))]
530535
[ResDescription(nameof(Strings.DbCommand_CommandTimeout))]
@@ -543,15 +548,15 @@ public override int CommandTimeout
543548
PropertyChanging();
544549
_commandTimeout = value;
545550
}
546-
551+
547552
SqlClientEventSource.Log.TryTraceEvent(
548553
"SqlCommand.Set_CommandTimeout | API | " +
549554
$"Object Id {ObjectID}, " +
550555
$"Command Timeout value {value}, " +
551556
$"Client Connection Id {Connection?.ClientConnectionId}");
552557
}
553558
}
554-
559+
555560
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlCommand.xml' path='docs/members[@name="SqlCommand"]/CommandText/*'/>
556561
[DefaultValue("")]
557562
[RefreshProperties(RefreshProperties.All)]
@@ -710,7 +715,7 @@ public SqlNotificationRequest Notification
710715
$"Object Id {ObjectID}");
711716
}
712717
}
713-
718+
714719
#if NETFRAMEWORK
715720
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlCommand.xml' path='docs/members[@name="SqlCommand"]/NotificationAutoEnlist/*'/>
716721
[DefaultValue(true)]
@@ -722,7 +727,7 @@ public bool NotificationAutoEnlist
722727
set => _notificationAutoEnlist = value;
723728
}
724729
#endif
725-
730+
726731
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlCommand.xml' path='docs/members[@name="SqlCommand"]/Parameters/*'/>
727732
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
728733
[ResCategory(nameof(Strings.DataCategory_Data))]
@@ -737,7 +742,7 @@ public bool NotificationAutoEnlist
737742
return _parameters;
738743
}
739744
}
740-
745+
741746
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlCommand.xml' path='docs/members[@name="SqlCommand"]/RetryLogicProvider/*' />
742747
[Browsable(false)]
743748
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
@@ -810,7 +815,7 @@ public override UpdateRowSource UpdatedRowSource
810815
default:
811816
throw ADP.InvalidUpdateRowSource(value);
812817
}
813-
818+
814819
SqlClientEventSource.Log.TryTraceEvent(
815820
"SqlCommand.UpdatedRowSource | API | " +
816821
$"Object Id {ObjectID}, " +
@@ -925,7 +930,7 @@ protected override DbParameterCollection DbParameterCollection
925930
{
926931
get => Parameters;
927932
}
928-
933+
929934
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlCommand.xml' path='docs/members[@name="SqlCommand"]/DbTransaction/*'/>
930935
protected override DbTransaction DbTransaction
931936
{
@@ -990,7 +995,7 @@ private bool IsDirty
990995
// @TODO: Factor out closeCount/reconnectCount checks to properties and clean up.
991996
// To wit: closeCount checks whether the connection has been closed after preparation,
992997
// reconnectCount, the same only with reconnections.
993-
998+
994999
// only dirty if prepared
9951000
// @TODO: we probably do not need to store this as a temp variable.
9961001
var activeConnection = _activeConnection;
@@ -1003,7 +1008,7 @@ private bool IsDirty
10031008
{
10041009
// @TODO: Consider reworking to do this in a helper method, since setting, sets to the
10051010
// _dirty, but that's not the only consideration when determining dirtiness.
1006-
1011+
10071012
// only mark the command as dirty if it is already prepared
10081013
// but always clear the value if we are clearing the dirty flag
10091014
_dirty = value ? IsPrepared : false;

0 commit comments

Comments
 (0)